transfer custom eth on matic
MODIFY
README.md
README.md
@@ -80,6 +80,13 @@ const tokenAddressOnMatic = await matic.getMappedTokenAddress(
tokenAddress // token address on mainchain
)
+// get ERC20 token balance
+await matic.balanceOfERC20(
+ user, //User address
+ tokenAddress, // Token address
+ options // transaction fields
+)
+
// get ERC721 token balance
await matic.balanceOfERC721(
user, // User address
@@ -229,6 +236,7 @@ Please write to info@matic.network to request TEST tokens for development purpos
- <a href="#initialize"><code>new Matic()</code></a>
- <a href="#getMappedTokenAddress"><code>matic.<b>getMappedTokenAddress()</b></code></a>
+- <a href="#balanceOfERC20"><code>matic.<b>balanceOfERC20()</b></code></a>
- <a href="#balanceOfERC721"><code>matic.<b>balanceOfERC721()</b></code></a>
- <a href="#tokenOfOwnerByIndexERC721"><code>matic.<b>tokenOfOwnerByIndexERC721()</b></code></a>
- <a href="#depositEthers"><code>matic.<b>depositEther()</b></code></a>
@@ -299,6 +307,29 @@ matic
})
```
+---
+<a name="balanceOfERC20"></a>
+
+
+#### matic.balanceOfERC20(address, token, options)
+
+get balance of ERC20 `token` for `address`.
+
+- `token` must be valid token address
+- `address` must be valid user address
+
+This returns matic `balance`.
+
+Example:
+
+```js
+matic
+ .balanceOfERC20("0xfeb14bc6aaf5d39fa43ff51ed94e6c260539e296")
+ .then(address => {
+ console.log("matic address", address)
+ })
+```
+
---
<a name="balanceOfERC721"></a>
@@ -584,6 +615,7 @@ Transfer given `amount` of ethers to `user`.
- `amount` must be ethers amount in wei (string, not in Number)
- `options` see [more infomation here](#approveERC20TokensForDeposit)
- `parent` must be boolean value. For ether transfer on Main chain, use `parent: true`
+ - `isCustomEth` must be boolean value. For custom ether transfer on Matic Chain, use `isCustomEth: true`
This returns `Promise` object, which will be fulfilled when transaction gets confirmed (when receipt is generated).

MODIFY
src/index.js
src/index.js
@@ -124,6 +124,17 @@ export default class Matic {
return balance
}
+ async balanceOfERC20(address, token, options = {}) {
+ let web3Object = this._web3
+ if (options.parent) {
+ web3Object = this._parentWeb3
+ }
+ const balance = await this._getERC20TokenContract(token, web3Object)
+ .methods.balanceOf(address)
+ .call()
+ return balance
+ }
+
async tokenOfOwnerByIndexERC721(address, token, index, options = {}) {
let web3Object = this._web3
if (options.parent) {
@@ -279,6 +290,8 @@ export default class Matic {
}
async transferEthers(to, amount, options = {}) {
+ let web3Object = this._parentWeb3
+
if (options && (!options.from || !amount || !to)) {
throw new Error('Missing Parameters')
}
@@ -288,18 +301,21 @@ export default class Matic {
if (!options.parent) {
return this.transferTokens(this._maticWethAddress, to, amount, options)
}
+ if (!options.parent && options.isCustomEth) {
+ web3Object = this._web3
+ }
- const gasLimit = await this._parentWeb3.eth.estimateGas({
+ const gasLimit = await web3Object.eth.estimateGas({
from,
value: amount,
})
options.gasLimit = gasLimit
options.value = amount
options.to = to
- const _options = await this._fillOptions(options, {}, this._parentWeb3)
+ const _options = await this._fillOptions(options, {}, web3Object)
return this._wrapWeb3Promise(
- this._parentWeb3.eth.sendTransaction(_options),
+ web3Object.eth.sendTransaction(_options),
options
)
}

transfer custom eth on matic