add test for allowance
MODIFY
package.json
package.json
"clean": "rimraf lib dist",
"build": "npm run build:dev",
"build:link": "npm run build && npm link",
- "build:webpack": "npm run lint && webpack --config webpack/webpack.all.config.js",
+ "build:webpack": "npm run lint && npm run clean && webpack --config webpack/webpack.all.config.js",
"build:pack": "npm run build:dev && npm pack",
"build:dev": "cross-env NODE_ENV=development npm run build:webpack",
"build:prod": "cross-env NODE_ENV=production npm run build:webpack",

MODIFY
src/interfaces/index.ts
src/interfaces/index.ts
@@ -15,3 +15,4 @@ export * from "./rpc_response_payload";
export * from "./map_promise_option";
export * from "./base_client_config";
export * from "./error";
+export * from "./pos_contracts";
\ No newline at end of file

ADD
src/interfaces/pos_contracts.ts
+import { ExitUtil, RootChainManager } from "../pos";
+
+export interface IPOSContracts {
+ rootChainManager: RootChainManager;
+ exitUtil: ExitUtil;
+}
\ No newline at end of file

MODIFY
src/pos/erc20.ts
src/pos/erc20.ts
@@ -6,6 +6,7 @@ import { TYPE_AMOUNT } from "../types";
import { ExitUtil } from "./exit_util";
import { Log_Event_Signature } from "../enums";
import { IPOSClientConfig, MAX_AMOUNT } from "..";
+import { IPOSContracts } from "../interfaces/pos_contracts";
export class ERC20 extends POSToken {
@@ -14,15 +15,14 @@ export class ERC20 extends POSToken {
tokenAddress: string,
isParent: boolean,
client: Web3SideChainClient<IPOSClientConfig>,
- rootChainManager: RootChainManager,
- exitUtil: ExitUtil
+ getContracts: () => IPOSContracts
) {
super({
isParent,
address: tokenAddress,
name: 'ChildERC20',
bridgeType: 'pos'
- }, client, rootChainManager, exitUtil);
+ }, client, getContracts);
}
getBalance(userAddress: string, option?: ITransactionOption) {
@@ -103,7 +103,7 @@ export class ERC20 extends POSToken {
);
}
- private depositEther__(amount: TYPE_AMOUNT, option: ITransactionOption = {}) {
+ private depositEther_(amount: TYPE_AMOUNT, option: ITransactionOption = {}) {
this.checkForRoot_("depositEther");

MODIFY
src/pos/erc721.ts
src/pos/erc721.ts
-import { IPOSClientConfig, ITransactionOption } from "../interfaces";
+import { IPOSClientConfig, IPOSContracts, ITransactionOption } from "../interfaces";
import { RootChainManager } from "./root_chain_manager";
import { Converter, Web3SideChainClient } from "../utils";
import { POSToken } from "./pos_token";
@@ -13,15 +13,14 @@ export class ERC721 extends POSToken {
tokenAddress: string,
isParent: boolean,
client: Web3SideChainClient<IPOSClientConfig>,
- rootChainManager: RootChainManager,
- exitUtil: ExitUtil
+ getContracts: () => IPOSContracts
) {
super({
isParent,
address: tokenAddress,
name: 'ChildERC721',
bridgeType: 'pos'
- }, client, rootChainManager, exitUtil);
+ }, client, getContracts);
}
private validateMany__(tokenIds) {

MODIFY
src/pos/index.ts
src/pos/index.ts
import { ERC20 } from "./erc20";
import { RootChainManager } from "./root_chain_manager";
import { BaseToken, BridgeClient, Web3SideChainClient } from "../utils";
-import { IPOSClientConfig, ITransactionOption } from "../interfaces";
+import { IPOSClientConfig, IPOSContracts, ITransactionOption } from "../interfaces";
import { ExitUtil } from "./exit_util";
import { RootChain } from "./root_chain";
import { ERC721 } from "./erc721";
@@ -64,8 +64,7 @@ export class POSClient extends BridgeClient {
tokenAddress,
isParent,
this.client_,
- this.rootChainManager,
- this.exitUtil
+ this.getContracts_.bind(this)
);
}
@@ -74,16 +73,21 @@ export class POSClient extends BridgeClient {
tokenAddress,
isParent,
this.client_,
- this.rootChainManager,
- this.exitUtil
+ this.getContracts_.bind(this)
);
}
depositEther(amount: TYPE_AMOUNT, option: ITransactionOption) {
return new ERC20(
'', true, this.client_,
- this.rootChainManager,
- this.exitUtil
- )['depositEther__'](amount, option);
+ this.getContracts_,
+ )['depositEther_'](amount, option);
+ }
+
+ private getContracts_() {
+ return {
+ exitUtil: this.exitUtil,
+ rootChainManager: this.rootChainManager
+ } as IPOSContracts;
}
}
\ No newline at end of file

MODIFY
src/pos/pos_token.ts
src/pos/pos_token.ts
@@ -2,6 +2,7 @@ import { BaseToken, Web3SideChainClient, promiseResolve } from "../utils";
import { IBaseClientConfig, IContractInitParam, IPOSClientConfig } from "../interfaces";
import { RootChainManager } from "./root_chain_manager";
import { ExitUtil } from "./exit_util";
+import { IPOSContracts } from "../interfaces";
export class POSToken extends BaseToken<IPOSClientConfig> {
@@ -10,12 +11,24 @@ export class POSToken extends BaseToken<IPOSClientConfig> {
constructor(
contractParam: IContractInitParam,
client: Web3SideChainClient<IPOSClientConfig>,
- protected rootChainManager: RootChainManager,
- protected exitUtil: ExitUtil
+ protected getPOSContracts: () => IPOSContracts
) {
super(contractParam, client);
}
+ get rootChainManager() {
+ const contracts = this.getPOSContracts();
+ console.log("contracts", contracts);
+ return contracts.rootChainManager;
+ }
+
+ get exitUtil() {
+ const contracts = this.getPOSContracts();
+ console.log("contracts", contracts);
+ return contracts.exitUtil;
+ }
+
+
async getPredicateAddress() {
if (this.predicateAddress) {
return promiseResolve(this.predicateAddress);

MODIFY
test/debug.js
test/debug.js
@@ -36,7 +36,7 @@ const execute = async () => {
// const mumbaiERC720Token = client.erc721('0x33fc58f12a56280503b04ac7911d1eceebce179c');
const goerliERC20Token = client.erc20(pos.parent.erc20, true);
- return console.log(await goerliERC20Token.getBalance(from));
+ return console.log(await goerliERC20Token.getAllowance(from));
// const balance = await mumbaiERC720Token.getBalance(

MODIFY
test/package.json
test/package.json
"main": "index.spec.js",
"scripts": {
"link:lib": "npm link @maticnetwork/maticjs",
- "debug": "cross-env-file -p ./config.json node debug.js",
+ "debug": "dotenv node debug.js",
"install:lib": "node install_lib.js",
"install:lib:test": "npm run install:lib && npm run test",
"install:lib:debug": "npm run install:lib && npm run debug",

MODIFY
test/specs/erc20.spec.ts
test/specs/erc20.spec.ts
@@ -15,13 +15,19 @@ describe('ERC20', () => {
const balance = await erc20Child.getBalance(from);
console.log('balance', balance);
expect(balance).to.be.an('string');
- expect(Number(balance)).gt(0);
+ expect(Number(balance)).gte(0);
})
it('get balance parent', async () => {
- const balance = await erc20Child.getBalance(from);
+ const balance = await erc20Parent.getBalance(from);
console.log('balance', balance);
expect(balance).to.be.an('string');
- expect(Number(balance)).gt(0);
+ expect(Number(balance)).gte(0);
+ })
+
+ it('get allowance', async () => {
+ const allowance = await erc20Parent.getAllowance(from);
+ expect(allowance).to.be.an('string');
+ expect(Number(allowance)).gte(0);
})
});
\ No newline at end of file

add test for allowance