add withdrawExit faster test
MODIFY
src/pos/exit_util.ts
src/pos/exit_util.ts
@@ -6,7 +6,7 @@ import { ITransactionReceipt } from "../interfaces";
import { service } from "../services";
import { BaseWeb3Client } from "../abstracts";
import { ErrorHelper } from "../utils/error_helper";
-import { ERROR_TYPE } from "..";
+import { ERROR_TYPE, IBaseClientConfig } from "..";
interface IChainBlockInfo {
lastChildBlock: string;
@@ -25,11 +25,13 @@ export class ExitUtil {
rootChain: RootChain;
requestConcurrency: number;
+ config: IBaseClientConfig;
- constructor(maticClient: BaseWeb3Client, rootChain: RootChain, requestConcurrency: number) {
+ constructor(config: IBaseClientConfig, maticClient: BaseWeb3Client, rootChain: RootChain, requestConcurrency: number) {
this.maticClient_ = maticClient;
this.rootChain = rootChain;
this.requestConcurrency = requestConcurrency;
+ this.config = config;
}
private getLogIndex_(logEventSig: string, receipt: ITransactionReceipt) {
@@ -106,7 +108,7 @@ export class ExitUtil {
private async getRootBlockInfoFromAPI(txBlockNumber: number) {
try {
this.maticClient_.logger.log("block info from API 1");
- const headerBlock = await service.network.getBlockIncluded(txBlockNumber);
+ const headerBlock = await service.network.getBlockIncluded(this.config.network, txBlockNumber);
this.maticClient_.logger.log("block info from API 2", headerBlock);
if (!headerBlock || !headerBlock.start || !headerBlock.end || !headerBlock.headerBlockNumber) {
throw Error('Network API Error');
@@ -131,7 +133,9 @@ export class ExitUtil {
try {
const blockProof = await service.network.getProof(
- rootBlockInfo.start, rootBlockInfo.end,
+ this.config.network,
+ rootBlockInfo.start,
+ rootBlockInfo.end,
txBlockNumber
);
if (!blockProof) {

MODIFY
src/pos/index.ts
src/pos/index.ts
@@ -45,6 +45,7 @@ export class POSClient extends BridgeClient<IPOSClientConfig> {
);
this.exitUtil = new ExitUtil(
+ config,
this.client.child,
rootChain,
config.requestConcurrency

MODIFY
src/services/network_service.ts
src/services/network_service.ts
@@ -8,8 +8,13 @@ export class NetworkService {
this.httpRequest = new HttpRequest(baseUrl);
}
- getBlockIncluded(blockNumber: number) {
- const url = `/block-included/${blockNumber}`;
+ private createUrl(network: string, url: string) {
+ return `${network === 'mainnet' ? 'matic' : network}${url}`;
+ }
+
+ getBlockIncluded(network: string, blockNumber: number) {
+
+ const url = this.createUrl(network, `/block-included/${blockNumber}`);
return this.httpRequest.get<{
start: string;
end: string;
@@ -21,8 +26,8 @@ export class NetworkService {
});
}
- getProof(start, end, blockNumber) {
- const url = `/fast-merkle-proof?start=${start}&end=${end}&number=${blockNumber}`;
+ getProof(network: string, start, end, blockNumber) {
+ const url = this.createUrl(network, `/fast-merkle-proof?start=${start}&end=${end}&number=${blockNumber}`);
return this.httpRequest.get<any>(url).then(result => {
return result.proof;
});

MODIFY
src/utils/set_proof_api_url.ts
src/utils/set_proof_api_url.ts
import { service, NetworkService } from "../services";
export const setProofApi = (url: string) => {
- if (url[url.length - 1] !== '/') {
-
+ const urlLength = url.length;
+ if (url[urlLength - 1] !== '/') {
+ url += '/';
}
+ url += 'api/v1/';
service.network = new NetworkService(url) as any;
};
\ No newline at end of file

MODIFY
test/specs/erc20.spec.ts
test/specs/erc20.spec.ts
import { erc20, from, posClient, posClientForTo, to } from "./client";
import { expect } from 'chai'
-import { ABIManager } from '@maticnetwork/maticjs'
+import { ABIManager, setProofApi } from '@maticnetwork/maticjs'
import BN from "bn.js";
@@ -134,12 +134,36 @@ describe('ERC20', () => {
const result = await erc20Parent.withdrawExit('0x1c20c41b9d97d1026aa456a21f13725df63edec1b1f43aacb180ebcc6340a2d3', {
returnTransaction: true
});
- console.log('result', result);
const rootChainManager = await abiManager.getConfig("Main.POSContracts.RootChainManagerProxy")
expect(result['to'].toLowerCase()).equal(rootChainManager.toLowerCase());
});
+ it('withdrawExitFaster return tx without setProofAPI', async () => {
+ try {
+ const result = await erc20Parent.withdrawExitFaster('0x1c20c41b9d97d1026aa456a21f13725df63edec1b1f43aacb180ebcc6340a2d3', {
+ returnTransaction: true
+ });
+ throw new Error("there should be exception");
+ } catch (error) {
+ expect(error).deep.equal({
+ message: `Proof api is not set, please set it using "setProofApi"`,
+ type: 'proof_api_not_set'
+ })
+ }
+ });
+
+ it('withdrawExitFaster return tx', async () => {
+ setProofApi("https://apis.matic.network");
+ const result = await erc20Parent.withdrawExitFaster('0x1c20c41b9d97d1026aa456a21f13725df63edec1b1f43aacb180ebcc6340a2d3', {
+ returnTransaction: true
+ });
+
+ const rootChainManager = await abiManager.getConfig("Main.POSContracts.RootChainManagerProxy")
+ expect(result['to'].toLowerCase()).equal(rootChainManager.toLowerCase());
+ });
+
+
it('child transfer', async () => {
const oldBalance = await erc20Child.getBalance(to);

add withdrawExit faster test