get mapped token address
MODIFY
README.md
README.md
@@ -50,6 +50,11 @@ const matic = new Matic({
// Warning: Not-safe
// matic.wallet = <private-key> // Use metamask provider or use WalletConnect provider instead.
+// get token address mapped with mainchain token address
+await matic.getMappedTokenAddress(
+ tokenAddress // token address on mainchain
+)
+
// Approve token for deposit
await matic.approveTokensForDeposit(
token, // Token address,
@@ -116,6 +121,7 @@ Please write to info@matic.network to request TEST tokens for development purpos
### API
- <a href="#initialize"><code>new Matic()</code></a>
+- <a href="#getMappedTokenAddress"><code>matic.<b>getMappedTokenAddress()</b></code></a>
- <a href="#approveTokensForDeposit"><code>matic.<b>approveTokensForDeposit()</b></code></a>
- <a href="#depositTokens"><code>matic.<b>depositTokens()</b></code></a>
- <a href="#depositEthers"><code>matic.<b>depositEthers()</b></code></a>
@@ -156,6 +162,28 @@ const matic = new Matic(options)
---
+<a name="getMappedTokenAddress"></a>
+
+#### matic.getMappedTokenAddress(tokenAddress)
+
+get matic token `address` mapped with mainchain `tokenAddress`.
+
+- `tokenAddress` must be valid token address
+
+This returns matic `address`.
+
+Example:
+
+```js
+matic
+ .getMappedTokenAddress("0x670568761764f53E6C10cd63b71024c31551c9EC")
+ .then(address => {
+ console.log("matic address", address)
+ })
+```
+
+---
+
<a name="approveTokensForDeposit"></a>
#### matic.approveTokensForDeposit(token, amount, options)

ADD
examples/node/getMaticTokenAddress.js
+const Matic = require('../../lib/index').default
+const config = require('./config')
+
+// 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,
+ maticWethAddress: config.MATICWETH_ADDRESS,
+})
+
+const tokenAddress = '0x670568761764f53E6C10cd63b71024c31551c9EC' // token address on mainchain
+
+matic.wallet = '<private-key>' // prefix with `0x`
+
+// get token address mapped with mainchain token address
+matic.getMappedTokenAddress(tokenAddress).then(() => {
+ // action on Transaction success
+})

MODIFY
src/index.js
src/index.js
@@ -43,6 +43,7 @@ export default class Matic {
// internal cache
this._tokenCache = {}
+ this._tokenMappedCache = {}
}
//
@@ -84,6 +85,16 @@ export default class Matic {
return this._parentWeb3.eth.accounts.wallet.create(1)
}
+ async getMappedTokenAddress(address) {
+ const _a = address.toLowerCase()
+ if (!this._tokenMappedCache[_a]) {
+ this._tokenMappedCache[_a] = await this._rootChainContract.methods
+ .tokens(_a)
+ .call()
+ }
+ return this._tokenMappedCache[_a]
+ }
+
async approveTokensForDeposit(token, amount, options = {}) {
const _tokenContract = new this._parentWeb3.eth.Contract(
StandardTokenArtifacts.abi,

get mapped token address