Add erc20 token transfer functionality
MODIFY
package-lock.json
package-lock.json
"ansi-regex": {
"version": "2.1.1",
"bundled": true,
- "dev": true
+ "dev": true,
+ "optional": true
},
"aproba": {
"version": "1.2.0",
"balanced-match": {
"version": "1.0.0",
"bundled": true,
- "dev": true
+ "dev": true,
+ "optional": true
},
"brace-expansion": {
"version": "1.1.11",
"bundled": true,
"dev": true,
+ "optional": true,
"requires": {
"balanced-match": "^1.0.0",
"concat-map": "0.0.1"
"code-point-at": {
"version": "1.1.0",
"bundled": true,
- "dev": true
+ "dev": true,
+ "optional": true
},
"concat-map": {
"version": "0.0.1",
"bundled": true,
- "dev": true
+ "dev": true,
+ "optional": true
},
"console-control-strings": {
"version": "1.1.0",
"bundled": true,
- "dev": true
+ "dev": true,
+ "optional": true
},
"core-util-is": {
"version": "1.0.2",
"inherits": {
"version": "2.0.3",
"bundled": true,
- "dev": true
+ "dev": true,
+ "optional": true
},
"ini": {
"version": "1.3.5",
"version": "1.0.0",
"bundled": true,
"dev": true,
+ "optional": true,
"requires": {
"number-is-nan": "^1.0.0"
}
"version": "3.0.4",
"bundled": true,
"dev": true,
+ "optional": true,
"requires": {
"brace-expansion": "^1.1.7"
}
"minimist": {
"version": "0.0.8",
"bundled": true,
- "dev": true
+ "dev": true,
+ "optional": true
},
"minipass": {
"version": "2.3.5",
"bundled": true,
"dev": true,
+ "optional": true,
"requires": {
"safe-buffer": "^5.1.2",
"yallist": "^3.0.0"
"version": "0.5.1",
"bundled": true,
"dev": true,
+ "optional": true,
"requires": {
"minimist": "0.0.8"
}
"number-is-nan": {
"version": "1.0.1",
"bundled": true,
- "dev": true
+ "dev": true,
+ "optional": true
},
"object-assign": {
"version": "4.1.1",
"version": "1.4.0",
"bundled": true,
"dev": true,
+ "optional": true,
"requires": {
"wrappy": "1"
}
"safe-buffer": {
"version": "5.1.2",
"bundled": true,
- "dev": true
+ "dev": true,
+ "optional": true
},
"safer-buffer": {
"version": "2.1.2",
"version": "1.0.2",
"bundled": true,
"dev": true,
+ "optional": true,
"requires": {
"code-point-at": "^1.0.0",
"is-fullwidth-code-point": "^1.0.0",
"version": "3.0.1",
"bundled": true,
"dev": true,
+ "optional": true,
"requires": {
"ansi-regex": "^2.0.0"
}
"wrappy": {
"version": "1.0.2",
"bundled": true,
- "dev": true
+ "dev": true,
+ "optional": true
},
"yallist": {
"version": "3.0.3",
"bundled": true,
- "dev": true
+ "dev": true,
+ "optional": true
}
}
},
"requires": {
"underscore": "1.9.1",
"web3-core-helpers": "1.2.1",
- "websocket": "github:web3-js/WebSocket-Node#905deb4812572b344f5801f8c9ce8bb02799d82e"
+ "websocket": "websocket@github:web3-js/WebSocket-Node#905deb4812572b344f5801f8c9ce8bb02799d82e"
}
},
"web3-shh": {

MODIFY
src/common/ContractsBase.ts
src/common/ContractsBase.ts
import Web3Client from './Web3Client'
import BN from 'bn.js'
import assert from 'assert';
+import ChildERC20Artifact from 'matic-protocol/contracts-core/artifacts/ChildERC20.json'
+
+import { address, SendOptions } from '../types/Common'
export default class ContractsBase {
web3Client: Web3Client
@@ -17,4 +20,8 @@ export default class ContractsBase {
return number
}
}
+
+ public getERC20TokenContract(token: address) {
+ return new this.web3Client.web3.eth.Contract(ChildERC20Artifact.abi, token).methods
+ }
}

MODIFY
src/index.ts
src/index.ts
+import BN from 'bn.js'
+
import DepositManager from './root/DepositManager'
import RootChain from './root/RootChain'
import Registry from './root/Registry'
import WithdrawManager from './root/WithdrawManager'
import Web3Client from './common/Web3Client'
+import { address, SendOptions } from './types/Common'
+import ContractsBase from './common/ContractsBase'
-export default class Matic {
+export default class Matic extends ContractsBase {
public web3Client: Web3Client
public depositManager: DepositManager
public rootChain: RootChain
@@ -12,7 +16,9 @@ export default class Matic {
public registry: Registry
constructor(options: any = {}) {
- this.web3Client = new Web3Client(options.parentProvider, options.maticProvider, options.parentDefaultOptions || {}, options.maticDefaultOptions || {})
+ const web3Client = new Web3Client(options.parentProvider, options.maticProvider, options.parentDefaultOptions || {}, options.maticDefaultOptions || {})
+ super(web3Client)
+ this.web3Client = web3Client
this.registry = new Registry(options.registry, this.web3Client)
this.rootChain = new RootChain(options.rootChain, this.web3Client)
this.depositManager = new DepositManager(options.depositManager, this.web3Client)
@@ -24,4 +30,11 @@ export default class Matic {
this.withdrawManager.initialize()
])
}
+
+ transferTokens(token: address, to: address, amount: BN | string, options?: SendOptions) {
+ return this.web3Client.sendOnMatic(
+ this.getERC20TokenContract(token).transfer(to, this.encode(amount)),
+ options
+ )
+ }
}

MODIFY
src/root/Registry.ts
src/root/Registry.ts
@@ -6,7 +6,7 @@ import ContractsBase from '../common/ContractsBase'
import { address, SendOptions } from '../types/Common'
import Web3Client from '../common/Web3Client'
-export default class DepositManager extends ContractsBase {
+export default class Registry extends ContractsBase {
public registry: Contract
constructor(registry: address, web3Client: Web3Client) {

MODIFY
src/root/WithdrawManager.ts
src/root/WithdrawManager.ts
-import Web3 from 'web3'
+import assert from 'assert'
+import ethUtils from 'ethereumjs-util'
import Contract from 'web3/eth/contract'
-import { address, SendOptions } from '../types/Common'
+
+import BN from 'bn.js'
import WithdrawManagerArtifact from 'matic-protocol/contracts-core/artifacts/WithdrawManager.json'
-import Proofs from 'matic-protocol/contracts-core/helpers/proofs.js'
-import ChildERC20Artifact from 'matic-protocol/contracts-core/artifacts/ChildERC20.json'
import ERC20PredicateArtifact from 'matic-protocol/contracts-core/artifacts/ERC20Predicate.json'
-import BN from 'bn.js'
+import Proofs from 'matic-protocol/contracts-core/helpers/proofs.js'
+
+import { address, SendOptions } from '../types/Common'
import Web3Client from '../common/Web3Client'
import ContractsBase from '../common/ContractsBase'
import RootChain from './RootChain'
-import assert from 'assert'
-import ethUtils from 'ethereumjs-util'
import Registry from './Registry'
export default class WithdrawManager extends ContractsBase {
@@ -33,7 +33,7 @@ export default class WithdrawManager extends ContractsBase {
burnERC20Tokens(token: address, amount: BN | string, options?: SendOptions) {
return this.web3Client.sendOnMatic(
- this._getERC20TokenContract(token).methods.withdraw(this.encode(amount)),
+ this.getERC20TokenContract(token).withdraw(this.encode(amount)),
options
)
}
@@ -109,8 +109,4 @@ export default class WithdrawManager extends ContractsBase {
logIndex
]))
}
-
- private _getERC20TokenContract(token: address) {
- return new this.web3Client.web3.eth.Contract(ChildERC20Artifact.abi, token)
- }
}

Add erc20 token transfer functionality