allowance erc20
ADD
examples/node/allowance-ERC20.js
+const Matic = require('maticjs').default
+const config = require('./config')
+
+const token = config.ROPSTEN_TEST_TOKEN // test token address
+const amount = '1000000000000000000' // amount in wei
+const from = config.FROM_ADDRESS // from address
+
+// 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,
+})
+
+matic.wallet = config.PRIVATE_KEY // prefix with `0x`
+
+// Approve token
+matic
+ .allowanceERC20TokensForDeposit(from, token)
+ .then(result => {
+ console.log(result)
+ })
+ .catch(e => {
+ console.log(e)
+ })

MODIFY
src/index.js
src/index.js
@@ -169,10 +169,8 @@ export default class Matic {
if (options && (!options.from || !amount || !token)) {
throw new Error('Missing Parameters')
}
- const _tokenContract = new this._parentWeb3.eth.Contract(
- StandardTokenArtifacts.abi,
- token
- )
+
+ const _tokenContract = this._getERC20TokenContract(token, this._parentWeb3)
const approveTx = await _tokenContract.methods.approve(
this._rootChainAddress,
amount
@@ -186,6 +184,17 @@ export default class Matic {
return this._wrapWeb3Promise(approveTx.send(_options), options)
}
+ async allowanceERC20TokensForDeposit(address, token) {
+ if (!address || !token) {
+ throw new Error('Missing Parameters')
+ }
+
+ const _tokenContract = this._getERC20TokenContract(token, this._parentWeb3)
+ return _tokenContract.methods
+ .allowance(address, this._rootChainAddress)
+ .call()
+ }
+
async depositERC20Tokens(token, user, amount, options = {}) {
if (options && (!options.from || !token || !user || !amount)) {
throw new Error('Missing Parameters')
@@ -222,7 +231,10 @@ export default class Matic {
this._parentWeb3
)
- return this._wrapWeb3Promise(safeDepositERC721Tokens.send(_options), options)
+ return this._wrapWeb3Promise(
+ safeDepositERC721Tokens.send(_options),
+ options
+ )
}
async approveERC721TokenForDeposit(token, tokenId, options = {}) {
@@ -230,10 +242,7 @@ export default class Matic {
throw new Error('Missing Parameters')
}
- const _tokenContract = this._getERC721TokenContract(
- token,
- this._parentWeb3
- )
+ const _tokenContract = this._getERC721TokenContract(token, this._parentWeb3)
const approveTx = await _tokenContract.methods.approve(
this._rootChainAddress,
tokenId
@@ -601,9 +610,7 @@ export default class Matic {
? 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.gasPrice ? web3.eth.getGasPrice() : options.gasPrice,
!options.nonce
? web3.eth.getTransactionCount(from, 'pending')
: options.nonce,

allowance erc20