transfer erc20
MODIFY
examples/node/transfer-ERC20.js
examples/node/transfer-ERC20.js
-const Matic = require('maticjs').default
+const bn = require('bn.js')
+
+const Matic = require('../../dist/index').default
+
const config = require('./config')
const from = config.FROM_ADDRESS // from address
const recipient = 'Paste Your receipent address here ...' // receipent address
const token = config.MATIC_TEST_TOKEN // test token address
-const amount = '1000000000000000000' // amount in wei
+const amount = new bn(100000000) // amount in wei
+
// Create object of Matic
const matic = new Matic({
maticProvider: config.MATIC_PROVIDER,
parentProvider: config.PARENT_PROVIDER,
- rootChainAddress: config.ROOTCHAIN_ADDRESS,
- syncerUrl: config.SYNCER_URL,
- watcherUrl: config.WATCHER_URL,
+ rootChain: config.ROOTCHAIN_ADDRESS,
+ registry: config.REGISTRY_ADDRESS,
+ depositManager: config.DEPOSITMANAGER_ADDRESS,
+ withdrawManager: config.WITHDRAWMANAGER_ADDRESS,
})
-matic.wallet = config.PRIVATE_KEY // prefix with `0x`
-
+matic.initialize().then(() => {
+ matic.wallet(config.PRIVATE_KEY)
// Send Tokens
-matic.transferTokens(token, recipient, amount, {
+ matic.transferERC20Tokens(token, recipient, amount, {
from,
// parent: true, // For token transfer on Main network (false for Matic Network)
- onTransactionHash: (hash) => {
- // action on Transaction success
- console.log(hash) // eslint-disable-line
- },
+ }).then((res)=>{
+ console.log("hash", res) // eslint-disable-line
+ })
})
+

MODIFY
src/common/ContractsBase.ts
src/common/ContractsBase.ts
@@ -45,7 +45,6 @@ export default class ContractsBase {
!(options.gasLimit || options.gas)
? txObject.estimateGas({ from, value: options.value })
: options.gasLimit || options.gas,
- // NOTE: Gas Price is set to '0', take care of type of gasPrice on web3^1.0.0-beta.36
!options.gasPrice ? web3.eth.getGasPrice() : options.gasPrice,
!options.nonce
? web3.eth.getTransactionCount(from, 'pending')

MODIFY
src/common/Web3Client.ts
src/common/Web3Client.ts
@@ -78,13 +78,14 @@ export default class Web3Client {
}
async send(txObject, options?) {
- const _options = options || this.parentDefaultOptions
- _options.gas = _options.gas + 1000000
+ const _options = options || {}
+
// since we use the delegated proxy patterns, the following should be a good way to provide enough gas
// apparently even when provided with a buffer of 20k, the call reverts. This shouldn't be happening because the actual gas used is less than what the estimation returns
// providing higher buffer for now
// @todo handle hex values of gas
- console.log('sending tx on matic with', _options)
+ _options.gas = options.parent ? _options.gas + 1000000 : _options.gas
+ _options.gasPrice = options.parent ? _options.gasPrice : 0
return this.wrapWeb3Promise(txObject.send(_options), _options)
}

MODIFY
src/index.ts
src/index.ts
@@ -46,7 +46,7 @@ export default class Matic extends ContractsBase {
this.web3Client.wallet = _wallet
}
- transferERC20Tokens(
+ async transferERC20Tokens(
token: address,
to: address,
amount: BN | string,
@@ -55,12 +55,27 @@ export default class Matic extends ContractsBase {
if (options && (!options.from || !amount || !token || !to)) {
throw new Error('options.from, to, token or amount is missing')
}
- return this.wrapWeb3Promise(
- this.getERC20TokenContract(token)
- .methods.transfer(to, this.encode(amount))
- .send(options),
+
+ const txObject = this.getERC20TokenContract(
+ token,
+ options.parent,
+ ).methods.transfer(to, this.encode(amount))
+
+ const _options = await this._fillOptions(
options,
+ txObject,
+ options.parent
+ ? this.web3Client.getParentWeb3()
+ : this.web3Client.getMaticWeb3(),
)
+
+ if (options.encodeAbi) {
+ _options.data = txObject.encodeABI()
+ _options.to = token
+ return _options
+ }
+
+ return this.web3Client.send(txObject, _options)
}
async approveERC20TokensForDeposit(

MODIFY
src/root/DepositManager.ts
src/root/DepositManager.ts
@@ -96,7 +96,6 @@ export default class DepositManager extends ContractsBase {
user: address,
options?: SendOptions,
) {
- console.log('-->', token, user, this.encode(amount))
const txObject = this.depositManagerContract.methods.depositERC20ForUser(
token,
user,

MODIFY
src/types/Common.ts
src/types/Common.ts
-export type address = string;
+export type address = string
export declare interface SendOptions {
from?: string
@@ -6,4 +6,5 @@ export declare interface SendOptions {
gas?: string | number
gasPrice?: string | number
encodeAbi?: boolean
+ parent?: boolean
}

transfer erc20