send network as an initialization option
MODIFY
src/common/ContractsBase.ts
src/common/ContractsBase.ts
import BN from 'bn.js'
import Web3Client from './Web3Client'
-import network from './matic-protocol'
import { address } from '../types/Common'
@@ -9,9 +8,11 @@ export default class ContractsBase {
static MATIC_CHILD_TOKEN: address = '0x0000000000000000000000000000000000001010'
web3Client: Web3Client
+ network: any
- constructor(web3Client: Web3Client) {
+ constructor(web3Client: Web3Client, network: any) {
this.web3Client = web3Client
+ this.network = network
}
public encode(number: BN | string | number) {
@@ -28,15 +29,15 @@ export default class ContractsBase {
public getERC20TokenContract(token: address, parent: boolean = false) {
const web3 = parent ? this.web3Client.parentWeb3 : this.web3Client.web3
- return new web3.eth.Contract(network.abi('ChildERC20'), token)
+ return new web3.eth.Contract(this.network.abi('ChildERC20'), token)
}
public getERC721TokenContract(token: address, parent: boolean = false) {
const web3 = parent ? this.web3Client.parentWeb3 : this.web3Client.web3
- return new web3.eth.Contract(network.abi('ChildERC721'), token)
+ return new web3.eth.Contract(this.network.abi('ChildERC721'), token)
}
public getChildMaticContract() {
- return new this.web3Client.web3.eth.Contract(network.abi('MRC20'), ContractsBase.MATIC_CHILD_TOKEN)
+ return new this.web3Client.web3.eth.Contract(this.network.abi('MRC20'), ContractsBase.MATIC_CHILD_TOKEN)
}
}

MODIFY
src/common/ExitManager.ts
src/common/ExitManager.ts
@@ -5,7 +5,7 @@ import ethUtils from 'ethereumjs-util'
import Web3Client from './Web3Client'
import ContractsBase from './ContractsBase'
import RootChain from '../root/RootChain'
-
+import { MaticClientInitializationOptions } from '../types/Common'
import Proofs from '../libs/ProofsUtil.js'
const logger = {
@@ -16,8 +16,8 @@ const logger = {
export default class ExitManager extends ContractsBase {
private rootChain: RootChain
- constructor(rootChain: RootChain, web3Client: Web3Client) {
- super(web3Client)
+ constructor(rootChain: RootChain, options: MaticClientInitializationOptions, web3Client: Web3Client) {
+ super(web3Client, options.network)
this.rootChain = rootChain
}

MODIFY
src/common/SDKClient.ts
src/common/SDKClient.ts
@@ -4,17 +4,15 @@ import Web3Client from '../common/Web3Client'
import { address, SendOptions } from '../types/Common'
import ContractsBase from '../common/ContractsBase'
-export default class Matic extends ContractsBase {
- public web3Client: Web3Client
+export default class SDKClient extends ContractsBase {
constructor(options: any = {}) {
const web3Client = new Web3Client(
- options.parentProvider,
- options.maticProvider,
+ options.parentProvider || options.network.Main.RPC,
+ options.maticProvider || options.network.Matic.RPC,
options.parentDefaultOptions || {},
options.maticDefaultOptions || {}
)
- super(web3Client)
- this.web3Client = web3Client
+ super(web3Client, options.network)
}
setWallet(_wallet) {

DELETE
src/common/matic-protocol.ts
-const Network = require('@maticnetwork/meta/network')
-
-const network = new Network('testnet', 'cs-2007')
-export default network

MODIFY
src/index.ts
src/index.ts
import BN from 'bn.js'
+import Network from '@maticnetwork/meta/network'
import DepositManager from './root/DepositManager'
import RootChain from './root/RootChain'
import Registry from './root/Registry'
import WithdrawManager from './root/WithdrawManager'
import POSRootChainManager from './root/POSRootChainManager'
-import { address, SendOptions, order } from './types/Common'
+import { address, SendOptions, order, MaticClientInitializationOptions } from './types/Common'
import SDKClient from './common/SDKClient'
import { Utils } from './common/Utils'
@@ -73,12 +74,32 @@ export default class Matic extends SDKClient {
public utils: Utils
public static MaticPOSClient = MaticPOSClient // workaround for web compatibility
- constructor(options: any = {}) {
+ constructor(options: MaticClientInitializationOptions = {}) {
+ let network
+ if (options.network && options.version) {
+ network = new Network(options.network, options.version)
+ if (!network) throw new Error(`network ${options.network} - ${options.version} is not supported`)
+ } else {
+ // User provided the contract addresses in options object
+ // We'll use latest network that this version of maticjs supports to pickup abis.
+ network = new Network('testnet', 'cs-2007')
+ }
+ // override contract addresses if they were provided during initialization
+ options = Object.assign(
+ {
+ registry: network.Main.Contracts.Registry,
+ rootChain: network.Main.Contracts.Registry,
+ depositManager: network.Main.Contracts.DepositManagerProxy,
+ withdrawManager: network.Main.Contracts.WithdrawManagerProxy,
+ },
+ options
+ )
+ options.network = network
super(options)
- this.registry = new Registry(options.registry, this.web3Client)
- this.rootChain = new RootChain(options.rootChain, this.web3Client)
- this.depositManager = new DepositManager(options.depositManager, this.web3Client, this.registry)
- this.withdrawManager = new WithdrawManager(options.withdrawManager, this.rootChain, this.web3Client, this.registry)
+ this.registry = new Registry(options, this.web3Client)
+ this.rootChain = new RootChain(options, this.web3Client)
+ this.depositManager = new DepositManager(options, this.web3Client, this.registry)
+ this.withdrawManager = new WithdrawManager(options, this.rootChain, this.web3Client, this.registry)
this.utils = new Utils()
}

MODIFY
src/root/DepositManager.ts
src/root/DepositManager.ts
@@ -2,10 +2,8 @@ import BN from 'bn.js'
import bluebird from 'bluebird'
import Contract from 'web3/eth/contract'
-import network from '../common/matic-protocol'
-
import ContractsBase from '../common/ContractsBase'
-import { address, SendOptions } from '../types/Common'
+import { address, MaticClientInitializationOptions, SendOptions } from '../types/Common'
import Web3Client from '../common/Web3Client'
import Registry from './Registry'
@@ -17,18 +15,18 @@ export default class DepositManager extends ContractsBase {
private registry: Registry
- constructor(depositManager: address, web3Client: Web3Client, registry: Registry) {
- super(web3Client)
+ constructor(options: MaticClientInitializationOptions, web3Client: Web3Client, registry: Registry) {
+ super(web3Client, options.network)
this.depositManagerContract = new this.web3Client.parentWeb3.eth.Contract(
- network.abi('DepositManager'),
- depositManager
+ options.network.abi('DepositManager'),
+ options.depositManager
)
this.registry = registry
}
async initialize() {
const childChainAddress = (await this.registry.registry.methods.getChildChainAndStateSender().call())[0]
- this.childChainContract = new this.web3Client.web3.eth.Contract(network.abi('ChildChain'), childChainAddress)
+ this.childChainContract = new this.web3Client.web3.eth.Contract(this.network.abi('ChildChain'), childChainAddress)
}
async depositStatusFromTxHash(txHash: string) {

MODIFY
src/root/POSRootChainManager.ts
src/root/POSRootChainManager.ts
@@ -5,7 +5,7 @@ import RootChainManagerArtifact from 'matic-pos-portal/artifacts/RootChainManage
import ChildTokenArtifact from 'matic-pos-portal/artifacts/ChildToken.json'
import ContractsBase from '../common/ContractsBase'
-import { address, SendOptions } from '../types/Common'
+import { address, SendOptions, MaticClientInitializationOptions } from '../types/Common'
import Web3Client from '../common/Web3Client'
import RootChain from './RootChain'
import ExitManager from '../common/ExitManager'
@@ -16,13 +16,13 @@ export default class POSRootChainManager extends ContractsBase {
public posRootChainManager: Contract
private exitManager: ExitManager
- constructor(posRootChainManager: address, rootChain: RootChain, web3Client: Web3Client) {
- super(web3Client)
+ constructor(options: MaticClientInitializationOptions, rootChain: RootChain, web3Client: Web3Client) {
+ super(web3Client, options.network)
this.posRootChainManager = new this.web3Client.parentWeb3.eth.Contract(
RootChainManagerArtifact.abi,
- posRootChainManager
+ options.posRootChainManager
)
- this.exitManager = new ExitManager(rootChain, web3Client)
+ this.exitManager = new ExitManager(rootChain, options, web3Client)
}
async approveERC20(rootToken: address, amount: BN | string, options?: SendOptions) {

MODIFY
src/root/Registry.ts
src/root/Registry.ts
import Contract from 'web3/eth/contract'
-import network from '../common/matic-protocol'
import ContractsBase from '../common/ContractsBase'
-import { address } from '../types/Common'
+import { MaticClientInitializationOptions } from '../types/Common'
import Web3Client from '../common/Web3Client'
export default class Registry extends ContractsBase {
public registry: Contract
- constructor(registry: address, web3Client: Web3Client) {
- super(web3Client)
- this.registry = new this.web3Client.parentWeb3.eth.Contract(network.abi('Registry'), registry)
+ constructor(options: MaticClientInitializationOptions, web3Client: Web3Client) {
+ super(web3Client, options.network)
+ this.registry = new this.web3Client.parentWeb3.eth.Contract(options.network.abi('Registry'), options.registry)
}
}

MODIFY
src/root/RootChain.ts
src/root/RootChain.ts
import Contract from 'web3/eth/contract'
-import network from '../common/matic-protocol'
import ContractsBase from '../common/ContractsBase'
-import { address } from '../types/Common'
+import { MaticClientInitializationOptions } from '../types/Common'
import Web3Client from '../common/Web3Client'
import BN from 'bn.js'
export default class RootChain extends ContractsBase {
public rootChain: Contract
- constructor(rootChain: address, web3Client: Web3Client) {
- super(web3Client)
- this.rootChain = new this.web3Client.parentWeb3.eth.Contract(network.abi('RootChain'), rootChain)
+ constructor(options: MaticClientInitializationOptions, web3Client: Web3Client) {
+ super(web3Client, options.network)
+ this.rootChain = new this.web3Client.parentWeb3.eth.Contract(options.network.abi('RootChain'), options.rootChain)
}
getLastChildBlock() {

MODIFY
src/root/WithdrawManager.ts
src/root/WithdrawManager.ts
@@ -2,10 +2,9 @@ import ethUtils from 'ethereumjs-util'
import Contract from 'web3/eth/contract'
import BN from 'bn.js'
-import network from '../common/matic-protocol'
import Proofs from '../libs/ProofsUtil.js'
-import { address, SendOptions } from '../types/Common'
+import { address, MaticClientInitializationOptions, SendOptions } from '../types/Common'
import Web3Client from '../common/Web3Client'
import ContractsBase from '../common/ContractsBase'
import RootChain from './RootChain'
@@ -28,23 +27,31 @@ export default class WithdrawManager extends ContractsBase {
private registry: Registry
private exitManager: ExitManager
- constructor(withdrawManager: address, rootChain: RootChain, web3Client: Web3Client, registry: Registry) {
- super(web3Client)
- this.withdrawManager = new this.web3Client.parentWeb3.eth.Contract(network.abi('WithdrawManager'), withdrawManager)
+ constructor(
+ options: MaticClientInitializationOptions,
+ rootChain: RootChain,
+ web3Client: Web3Client,
+ registry: Registry
+ ) {
+ super(web3Client, options.network)
+ this.withdrawManager = new this.web3Client.parentWeb3.eth.Contract(
+ this.network.abi('WithdrawManager'),
+ options.withdrawManager
+ )
this.rootChain = rootChain
this.registry = registry
- this.exitManager = new ExitManager(rootChain, web3Client)
+ this.exitManager = new ExitManager(rootChain, options, web3Client)
}
async initialize() {
const erc20PredicateAddress = await this.registry.registry.methods.erc20Predicate().call()
const erc721PredicateAddress = await this.registry.registry.methods.erc721Predicate().call()
this.erc20Predicate = new this.web3Client.parentWeb3.eth.Contract(
- network.abi('ERC20Predicate'),
+ this.network.abi('ERC20Predicate'),
erc20PredicateAddress
)
this.erc721Predicate = new this.web3Client.parentWeb3.eth.Contract(
- network.abi('ERC721Predicate'),
+ this.network.abi('ERC721Predicate'),
erc721PredicateAddress
)
}
@@ -115,7 +122,10 @@ export default class WithdrawManager extends ContractsBase {
*/
async startExitForMintableBurntToken(burnTxHash, predicate: address, options?) {
const { payload, mint } = await this._buildPayloadAndFindMintTransaction(burnTxHash)
- const _predicate = new this.web3Client.parentWeb3.eth.Contract(network.abi('MintableERC721Predicate'), predicate)
+ const _predicate = new this.web3Client.parentWeb3.eth.Contract(
+ this.network.abi('MintableERC721Predicate'),
+ predicate
+ )
const txObject = _predicate.methods.startExitForMintableBurntToken(payload, mint)
const _options = await this.web3Client.fillOptions(txObject, true /* onRootChain */, options)
return this.web3Client.send(txObject, _options)
@@ -129,7 +139,10 @@ export default class WithdrawManager extends ContractsBase {
*/
async startExitForMetadataMintableBurntToken(burnTxHash, predicate: address, options?) {
const { payload, mint } = await this._buildPayloadAndFindMintTransaction(burnTxHash)
- const _predicate = new this.web3Client.parentWeb3.eth.Contract(network.abi('MintableERC721Predicate'), predicate)
+ const _predicate = new this.web3Client.parentWeb3.eth.Contract(
+ this.network.abi('MintableERC721Predicate'),
+ predicate
+ )
const txObject = _predicate.methods.startExitForMetadataMintableBurntToken(payload, mint)
const _options = await this.web3Client.fillOptions(txObject, true /* onRootChain */, options)
return this.web3Client.send(txObject, _options)
@@ -143,7 +156,7 @@ export default class WithdrawManager extends ContractsBase {
)
const tokenId = withdrawEvent.data
logger.debug({ burnTxHash, burnReceipt, withdrawEvent, tokenId })
- const contract = new this.web3Client.web3.eth.Contract(network.abi('ChildERC721Mintable'), burnReceipt.to)
+ const contract = new this.web3Client.web3.eth.Contract(this.network.abi('ChildERC721Mintable'), burnReceipt.to)
const mintEvents = await contract.getPastEvents('Transfer', {
filter: { tokenId },
fromBlock: 0,

MODIFY
src/types/Common.ts
src/types/Common.ts
@@ -17,3 +17,17 @@ export declare interface order {
orderId?: string
spender?: string
}
+
+export declare interface MaticClientInitializationOptions {
+ network?: any
+ version?: string
+ parentProvider?: any
+ maticProvider?: any
+ parentDefaultOptions?: any
+ maticDefaultOptions?: any
+ registry?: string
+ rootChain?: string
+ depositManager?: string
+ withdrawManager?: string
+ posRootChainManager?: string
+}

send network as an initialization option