Skip to content

Commit

Permalink
Merge pull request maticnetwork#19 from maticnetwork/feat-mapped-token
Browse files Browse the repository at this point in the history
Feat mapped token
  • Loading branch information
shahbhavir authored Oct 31, 2018
2 parents 66d3275 + 8d532e3 commit 33d0cc6
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -116,6 +121,7 @@ Please write to [email protected] 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>
Expand Down Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions examples/node/getMaticTokenAddress.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const Matic = require('maticjs').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
})
11 changes: 11 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export default class Matic {

// internal cache
this._tokenCache = {}
this._tokenMappedCache = {}
}

//
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 33d0cc6

Please sign in to comment.