pass config in init
MODIFY
src/pos/index.ts
src/pos/index.ts
@@ -15,21 +15,16 @@ export class POSClient extends BridgeClient<IPOSClientConfig> {
rootChainManager: RootChainManager;
- constructor(config: IPOSClientConfig) {
- super(config);
- }
-
- init() {
+ init(config: IPOSClientConfig) {
const client = this.client;
- let config: IPOSClientConfig = client.config;
- return client.init().then(_ => {
- const mainPOSContracts = this.client.mainPOSContracts;
+ return client.init(config).then(_ => {
+ const mainPOSContracts = client.mainPOSContracts;
client.config = config = Object.assign(
{
rootChainManager: mainPOSContracts.RootChainManagerProxy,
- rootChain: this.client.mainPlasmaContracts.RootChainProxy
+ rootChain: client.mainPlasmaContracts.RootChainProxy
} as IPOSClientConfig,
config
);

MODIFY
src/utils/bridge_client.ts
src/utils/bridge_client.ts
@@ -4,11 +4,7 @@ import { BaseToken, utils } from "..";
export class BridgeClient<T> {
- protected client: Web3SideChainClient<T>;
-
- constructor(config: T) {
- this.client = new Web3SideChainClient(config as any);
- }
+ client: Web3SideChainClient<T> = new Web3SideChainClient();
exitUtil: ExitUtil;

MODIFY
src/utils/web3_side_chain_client.ts
src/utils/web3_side_chain_client.ts
@@ -14,7 +14,7 @@ export class Web3SideChainClient<T_CONFIG> {
logger = new Logger();
- constructor(config: IBaseClientConfig) {
+ init(config: IBaseClientConfig) {
config = config || {} as any;
config.parent.defaultConfig = config.parent.defaultConfig || {} as any;
config.child.defaultConfig = config.child.defaultConfig || {} as any;
@@ -31,15 +31,13 @@ export class Web3SideChainClient<T_CONFIG> {
this.child = new (Web3Client as any)(config.child.provider, this.logger);
this.logger.enableLog(config.log);
- }
- init() {
- const config = this.config as any as IBaseClientConfig;
const network = config.network;
const version = config.version;
- const abiHelper = this.abiManager = new ABIManager(network, version);
- this.logger.log("init called", abiHelper);
- return abiHelper.init().catch(err => {
+ const abiManager = this.abiManager =
+ new ABIManager(network, version);
+ this.logger.log("init called", abiManager);
+ return abiManager.init().catch(err => {
throw new Error(`network ${network} - ${version} is not supported`);
});
}

MODIFY
test/debug.js
test/debug.js
@@ -12,7 +12,10 @@ const execute = async () => {
const mumbaiERC20 = pos.child.erc20;
const goerliERC20 = pos.parent.erc20;
- const client = new POSClient({
+ const client = new POSClient();
+
+
+ await client.init({
log: true,
network: 'testnet',
version: 'mumbai',
@@ -29,9 +32,6 @@ const execute = async () => {
}
}
});
-
-
- await client.init();
console.log("init called");
const mumbaiERC20Token = client.erc20(mumbaiERC20);
@@ -44,10 +44,10 @@ const execute = async () => {
// return console.log(await client.isDeposited('0x05b6d0d2280557c04de48d395f1f4ea9deb498fabb9bb09b9aec929db5ce62fa'));
- // const balance = await mumbaiERC20Token.getBalance(
- // from
- // );
- // return console.log("balance", balance);
+ const balance = await mumbaiERC20Token.getBalance(
+ from
+ );
+ return console.log("balance", balance);
// const tokens = await goerliERC721Token.getAllTokens(
// from

MODIFY
test/package-lock.json
test/package-lock.json
}
},
"@maticnetwork/maticjs-ethers": {
- "version": "0.2.0",
- "resolved": "https://registry.npmjs.org/@maticnetwork/maticjs-ethers/-/maticjs-ethers-0.2.0.tgz",
- "integrity": "sha512-V3Diz8UtVt0UlOkY2f5y72lDyVzM1QMZoyZEwL/Dtx7nX3RmL5e34LmiEdS0i97agyklZ1cWKSDJPGXoOau0/g==",
+ "version": "0.3.0",
+ "resolved": "https://registry.npmjs.org/@maticnetwork/maticjs-ethers/-/maticjs-ethers-0.3.0.tgz",
+ "integrity": "sha512-Rwmmyg8O99v6wNpg79oWbmfo9dePLOobVOnz8WASHSq9SCNn0ctS7gd+6iBJooCTKaVd19+6EJmsnBWizcKoZA==",
"requires": {
"ethers": "^5.5.1"
}

MODIFY
test/package.json
test/package.json
"webpack-cli": "^4.2.0"
},
"dependencies": {
- "@maticnetwork/maticjs-ethers": "^0.2.0",
+ "@maticnetwork/maticjs-ethers": "^0.3.0",
"@maticnetwork/maticjs-web3": "^0.5.0",
"@truffle/hdwallet-provider": "^1.5.1-alpha.1",
"mochapack": "^2.1.2"

MODIFY
test/specs/client.ts
test/specs/client.ts
import { POSClient } from "@maticnetwork/maticjs";
-import HDWalletProvider from "@truffle/hdwallet-provider";
import { user1, rpc, pos, user2 } from "../config";
-import { providers, Wallet } from "ethers";
export const privateKey = user1.privateKey;
export const from = user1.address;
@@ -19,42 +17,8 @@ export const erc721 = {
child: pos.child.erc721
}
-const parentPrivder = new providers.JsonRpcProvider(rpc.parent);
-const childProvider = new providers.JsonRpcProvider(rpc.child);
-export const posClient = new POSClient({
- // log: true,
- network: 'testnet',
- version: 'mumbai',
- parent: {
- provider: new Wallet(privateKey, parentPrivder),
- defaultConfig: {
- from
- }
- },
- child: {
- provider: new Wallet(privateKey, childProvider),
- defaultConfig: {
- from
- }
- }
-});
+export const posClient = new POSClient();
-export const posClientForTo = new POSClient({
- // log: true,
- network: 'testnet',
- version: 'mumbai',
- parent: {
- provider: new Wallet(toPrivateKey, parentPrivder),
- defaultConfig: {
- from: to
- }
- },
- child: {
- provider: new Wallet(toPrivateKey, childProvider),
- defaultConfig: {
- from: to
- }
- }
-});
\ No newline at end of file
+export const posClientForTo = new POSClient();
\ No newline at end of file

MODIFY
test/specs/erc20.spec.ts
test/specs/erc20.spec.ts
@@ -12,13 +12,12 @@ describe('ERC20', () => {
const abiManager = new ABIManager("testnet", "mumbai");
before(() => {
return Promise.all([
- posClient.init(),
abiManager.init()
]);
});
it('get balance child', async () => {
- console.log('process.env.NODE_ENV', process.env.NODE_ENV);
+ console.log('process.env.NODE_ENV', process.env.NODE_ENV, posClient.client);
const balance = await erc20Child.getBalance(from);
console.log('balance', balance);
@@ -231,7 +230,6 @@ describe('ERC20', () => {
//transfer money back to user
- await posClientForTo.init();
const erc20ChildToken = posClientForTo.erc20(erc20.child);
result = await erc20ChildToken.transfer(amount, to);

MODIFY
test/specs/erc721.spec.ts
test/specs/erc721.spec.ts
@@ -11,7 +11,6 @@ describe('ERC721', () => {
const abiManager = new ABIManager("testnet", "mumbai");
before(() => {
return Promise.all([
- posClient.init(),
abiManager.init()
]);
});
@@ -133,8 +132,6 @@ describe('ERC721', () => {
// console.log('newAllTokensTo', newAllTokensTo);
expect(newAllTokensTo.length).equal(allTokensTo.length + 1);
- await posClientForTo.init();
-
const erc721ChildToken = posClientForTo.erc721(erc721.child);

MODIFY
test/specs/index.ts
test/specs/index.ts
@@ -7,7 +7,6 @@ console.log('process.env.NODE_ENV', process.env.NODE_ENV);
import './map_promise.spec'
+import './pos_client.spec'
import './erc20.spec'
-
import './erc721.spec'
-import './pos_client.spec'

MODIFY
test/specs/pos_client.spec.ts
test/specs/pos_client.spec.ts
-import { erc721, from, posClient } from "./client";
+import { erc721, from, posClient, posClientForTo, privateKey, RPC, to, toPrivateKey } from "./client";
import { expect } from 'chai'
import { ABIManager } from '@maticnetwork/maticjs'
+import { providers, Wallet } from "ethers";
+
describe('POS Client', () => {
const abiManager = new ABIManager("testnet", "mumbai");
+ const parentPrivder = new providers.JsonRpcProvider(RPC.parent);
+ const childProvider = new providers.JsonRpcProvider(RPC.child);
+
before(() => {
return Promise.all([
- posClient.init(),
+ posClient.init({
+ // log: true,
+ network: 'testnet',
+ version: 'mumbai',
+ parent: {
+ provider: new Wallet(privateKey, parentPrivder),
+ defaultConfig: {
+ from
+ }
+ },
+ child: {
+ provider: new Wallet(privateKey, childProvider),
+ defaultConfig: {
+ from
+ }
+ }
+ }),
+ posClientForTo.init({
+ // log: true,
+ network: 'testnet',
+ version: 'mumbai',
+ parent: {
+ provider: new Wallet(toPrivateKey, parentPrivder),
+ defaultConfig: {
+ from: to
+ }
+ },
+ child: {
+ provider: new Wallet(toPrivateKey, childProvider),
+ defaultConfig: {
+ from: to
+ }
+ }
+ }),
abiManager.init()
]);
});

pass config in init