chg: use new pattern for erc20 deposit
MODIFY
package.json
package.json
"ethereumjs-block": "2.2.0",
"ethereumjs-tx": "1.3.7",
"ethereumjs-util": "^5.2.0",
+ "ethers": "^5.0.2",
"isomorphic-fetch": "2.2.1",
"matic-pos-portal": "github:maticnetwork/pos-portal#93b4791aadf3a4faf9971e1587f421b4fe0b73c3",
"merkle-patricia-tree": "2.3.2",

MODIFY
src/root/POSRootChainManager.ts
src/root/POSRootChainManager.ts
import BN from 'bn.js'
import Contract from 'web3/eth/contract'
+import ethers from 'ethers'
import RootChainManagerArtifact from 'matic-pos-portal/artifacts/RootChainManager.json'
-import ChildTokenArtifact from 'matic-pos-portal/artifacts/ChildToken.json'
import ContractsBase from '../common/ContractsBase'
import { address, SendOptions, MaticClientInitializationOptions } from '../types/Common'
@@ -10,7 +10,9 @@ import Web3Client from '../common/Web3Client'
import RootChain from './RootChain'
import ExitManager from '../common/ExitManager'
-const TRANSFER_EVENT_SIG = '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'
+const ERC20_TRANSFER_EVENT_SIG = '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'
+
+const abiCoder: ethers.utils.AbiCoder = ethers.utils.defaultAbiCoder
export default class POSRootChainManager extends ContractsBase {
public posRootChainManager: Contract
@@ -19,6 +21,8 @@ export default class POSRootChainManager extends ContractsBase {
private erc721Predicate: address | null
private erc1155Predicate: address | null
+ private formatUint256 = this.encode
+
constructor(options: MaticClientInitializationOptions, rootChain: RootChain, web3Client: Web3Client) {
super(web3Client, options.network)
this.posRootChainManager = new this.web3Client.parentWeb3.eth.Contract(
@@ -31,34 +35,38 @@ export default class POSRootChainManager extends ContractsBase {
this.erc1155Predicate = options.posERC1155Predicate
}
- async approveERC20(rootToken: address, amount: BN | string, options?: SendOptions) {
- const txObject = this.getERC20TokenContract(rootToken, true).methods.approve(
- this.posRootChainManager.options.address,
- this.encode(amount)
+ async depositEtherForUser(amount: BN | string, user: address, options: SendOptions = {}) {
+ const txObject = this.posRootChainManager.methods.depositEtherFor(user)
+ const _options = await this.web3Client.fillOptions(
+ txObject,
+ true /* onRootChain */,
+ Object.assign(options, { value: this.formatUint256(amount) })
)
- const _options = await this.web3Client.fillOptions(txObject, true /* onRootChain */, options)
if (_options.encodeAbi) {
- return Object.assign(_options, { data: txObject.encodeABI(), to: rootToken })
+ return Object.assign(_options, { data: txObject.encodeABI(), to: this.posRootChainManager.options.address })
}
return this.web3Client.send(txObject, _options)
}
- async depositERC20ForUser(rootToken: address, amount: BN | string, user: address, options?: SendOptions) {
- const txObject = this.posRootChainManager.methods.depositFor(user, rootToken, this.encode(amount))
+ async approveERC20(rootToken: address, amount: BN | string, options?: SendOptions) {
+ if (!this.erc20Predicate) {
+ throw new Error('Set posERC20Predicate while constructing client')
+ }
+ const txObject = this.getPOSERC20TokenContract(rootToken, true).methods.approve(
+ this.erc20Predicate,
+ this.formatUint256(amount)
+ )
const _options = await this.web3Client.fillOptions(txObject, true /* onRootChain */, options)
if (_options.encodeAbi) {
- return Object.assign(_options, { data: txObject.encodeABI(), to: this.posRootChainManager.options.address })
+ return Object.assign(_options, { data: txObject.encodeABI(), to: rootToken })
}
return this.web3Client.send(txObject, _options)
}
- async depositEtherForUser(amount: BN | string, user: address, options: SendOptions = {}) {
- const txObject = this.posRootChainManager.methods.depositEtherFor(user)
- const _options = await this.web3Client.fillOptions(
- txObject,
- true /* onRootChain */,
- Object.assign(options, { value: this.encode(amount) })
- )
+ async depositERC20ForUser(rootToken: address, amount: BN | string, user: address, options?: SendOptions) {
+ const depositData = abiCoder.encode(['uint256'], [this.formatUint256(amount)])
+ const txObject = this.posRootChainManager.methods.depositFor(user, rootToken, depositData)
+ const _options = await this.web3Client.fillOptions(txObject, true /* onRootChain */, options)
if (_options.encodeAbi) {
return Object.assign(_options, { data: txObject.encodeABI(), to: this.posRootChainManager.options.address })
}
@@ -66,8 +74,8 @@ export default class POSRootChainManager extends ContractsBase {
}
async burnERC20(childToken: address, amount: BN | string, options?: SendOptions) {
- const childTokenContract = new this.web3Client.web3.eth.Contract(ChildTokenArtifact.abi, childToken)
- const txObject = childTokenContract.methods.withdraw(this.encode(amount))
+ const childTokenContract = this.getPOSERC20TokenContract(childToken)
+ const txObject = childTokenContract.methods.withdraw(this.formatUint256(amount))
const _options = await this.web3Client.fillOptions(txObject, false /* onRootChain */, options)
if (_options.encodeAbi) {
return Object.assign(_options, { data: txObject.encodeABI(), to: childToken })
@@ -76,7 +84,7 @@ export default class POSRootChainManager extends ContractsBase {
}
async exitERC20(burnTxHash, options?) {
- const payload = await this.exitManager.buildPayloadForExit(burnTxHash, TRANSFER_EVENT_SIG)
+ const payload = await this.exitManager.buildPayloadForExit(burnTxHash, ERC20_TRANSFER_EVENT_SIG)
const txObject = this.posRootChainManager.methods.exit(payload)
const _options = await this.web3Client.fillOptions(txObject, true /* onRootChain */, options)
if (_options.encodeAbi) {

chg: use new pattern for erc20 deposit