API

WithdrawManager

new Matic(options)

Creates Matic SDK instance with give options. It returns a MaticSDK object.

import Matic from 'maticjs'
const matic = new Matic(options)
matic.initialize()
  • options is simple Javascript object which can have following fields:
    • maticProvider can be string or Web3.providers instance. This provider must connect to Matic chain. Value can be anyone of following:
    • parentProvider can be string or Web3.providers instance. This provider must connect to Ethereum chain (testnet or mainchain). Value can be anyone of following:
    • rootChain must be valid Ethereum contract address.
    • registry must be valid Ethereum contract address.
    • withdrawManager must be valid Ethereum contract address.
    • depositManager must be valid Ethereum contract address.

matic.balanceOfERC20(userAddress, token, options)

get balance of ERC20 token for address.

  • token must be valid token address
  • userAddress must be valid user address
  • options see more infomation here
    • parent must be boolean value. For balance on Main chain, use parent: true

This returns balance.

Example:

matic
.balanceOfERC20('0xABc578455...', '0x5E9c4ccB05...', {
from: '0xABc578455...',
})
.then(balance => {
console.log('balance', balance)
})

matic.balanceOfERC721(userAddress, token, options)

get balance of ERC721 token for address.

  • token must be valid token address
  • userAddress must be valid user address
  • options see more infomation here
    • parent must be boolean value. For balance on Main chain, use parent: true

This returns balance.

Example:

matic
.balanceOfERC721('0xABc578455...', '0x5E9c4ccB05...', {
from: '0xABc578455...',
})
.then(balance => {
console.log('balance', balance)
})

matic.tokenOfOwnerByIndexERC721(userAddress, token, index, options)

get ERC721 tokenId at index for token and for address.

  • token must be valid token address
  • userAddress must be valid user address
  • index index of tokenId

This returns matic tokenId.

Example:

matic
.tokenOfOwnerByIndexERC721('0xfeb14b...', '21', 0, {
from: '0xABc578455...',
})
.then(tokenID => {
console.log('Token ID', tokenID)
})

matic.depositEthers(amount, options)

Deposit options.value

  • amount must be token amount in wei (string, not in Number)
  • options see more infomation here.
    • from must be valid account address(required)
    • encodeAbi must be boolean value. For Byte code of transaction, use encodeAbi: true

This returns Promise object, which will be fulfilled when transaction gets confirmed (when receipt is generated).

Example:

matic.depositEthers(amount, {
from: '0xABc578455...',
})

matic.approveERC20TokensForDeposit(token, amount, options)

Approves given amount of token to rootChainContract.

  • token must be valid ERC20 token address
  • amount must be token amount in wei (string, not in Number)
  • options (optional) must be valid javascript object containing from, gasPrice, gasLimit, nonce, value, onTransactionHash, onReceipt or onError
    • from must be valid account address(required)
    • gasPrice same as Ethereum sendTransaction
    • gasLimit same as Ethereum sendTransaction
    • nonce same as Ethereum sendTransaction
    • value contains ETH value. Same as Ethereum sendTransaction. This returns Promise object, which will be fulfilled when transaction gets confirmed (when receipt is generated).

Example:

matic.approveERC20TokensForDeposit('0x718Ca123...', '1000000000000000000', {
from: '0xABc578455...',
})

matic.depositERC20ForUser(token, user, amount, options)

Deposit given amount of token with user user.

  • token must be valid ERC20 token address
  • user must be value account address
  • amount must be token amount in wei (string, not in Number)
  • options see more infomation here
    • encodeAbi must be boolean value. For Byte code of transaction, use encodeAbi: true

This returns Promise object, which will be fulfilled when transaction gets confirmed (when receipt is generated).

Example:

const user = <your-address> or <any-account-address>
matic.depositToken('0x718Ca123...', user, '1000000000000000000', {
from: '0xABc578455...'
})

matic.safeDepositERC721Tokens(token, tokenId, options)

Deposit given TokenID of token with user user.

  • token must be valid ERC20 token address
  • tokenId must be valid token ID
  • options see more infomation here

This returns Promise object, which will be fulfilled when transaction gets confirmed (when receipt is generated).

Example:

matic.safeDepositERC721Tokens('0x718Ca123...', '70000000000', {
from: '0xABc578455...',
})

matic.transferERC20Tokens(token, user, amount, options)

Transfer given amount of token to user.

  • token must be valid ERC20 token address
  • user must be value account address
  • amount must be token amount in wei (string, not in Number)
  • options see more infomation here
    • parent must be boolean value. For token transfer on Main chain, use parent: true
    • encodeAbi must be boolean value. For Byte code of transaction, use encodeAbi: true

This returns Promise object, which will be fulfilled when transaction gets confirmed (when receipt is generated).

Example:

const user = <your-address> or <any-account-address>
matic.transferERC20Tokens('0x718Ca123...', user, '1000000000000000000', {
from: '0xABc578455...',
// For token transfer on Main network
// parent: true
})

matic.transferERC721Tokens(token, user, tokenId, options)

Transfer given tokenId of token to user.

  • token must be valid ERC721 token address
  • user must be value account address
  • tokenId must be token amount in wei (string, not in Number)
  • options see more infomation here
    • parent must be boolean value. For token transfer on Main chain, use parent: true
    • encodeAbi must be boolean value. For Byte code of transaction, use encodeAbi: true

This returns Promise object, which will be fulfilled when transaction gets confirmed (when receipt is generated).

Example:

const user = <your-address> or <any-account-address>
matic.transferERC721Tokens('0x718Ca123...', user, '100006500000000000000', {
from: '0xABc578455...',
// For token transfer on Main network
// parent: true
})

matic.startWithdraw(token, amount, options)

Start withdraw process with given amount for token.

  • token must be valid ERC20 token address
  • amount must be token amount in wei (string, not in Number)
  • options see more infomation here
    • encodeAbi must be boolean value. For Byte code of transaction, use encodeAbi: true

This returns Promise object, which will be fulfilled when transaction gets confirmed (when receipt is generated).

Example:

matic.startWithdraw('0x718Ca123...', '1000000000000000000', {
from: '0xABc578455...',
})

matic.startWithdrawForNFT(token, tokenId, options)

Start withdraw process with given tokenId for token.

  • token must be valid ERC721 token address
  • tokenId must be token tokenId (string, not in Number)
  • options see more infomation here
    • encodeAbi must be boolean value. For Byte code of transaction, use encodeAbi: true

This returns Promise object, which will be fulfilled when transaction gets confirmed (when receipt is generated).

Example:

matic.startWithdrawForNFT('0x718Ca123...', '1000000000000000000', {
from: '0xABc578455...',
})

matic.withdraw(txId, options)

Withdraw tokens on mainchain using txId from startWithdraw method after header has been submitted to mainchain.

This returns Promise object, which will be fulfilled when transaction gets confirmed (when receipt is generated).

Example:

matic.withdraw('0xabcd...789', {
from: '0xABc578455...',
})

matic.withdrawNFT(txId, options)

Withdraw tokens on mainchain using txId from startWithdraw method after header has been submitted to mainchain.

  • txId must be valid tx hash
  • options see more infomation here
    • encodeAbi must be boolean value. For Byte code of transaction, use encodeAbi: true

This returns Promise object, which will be fulfilled when transaction gets confirmed (when receipt is generated).

Example:

matic.withdrawNFT('0xabcd...789', {
from: '0xABc578455...',
})

matic.getTransferSignature

Off-chain signature generation for transferWithSig function call

  • toSell object

    • token: address of token owned,
    • amount: amount/tokenId of the token to sell,
    • expiry: expiry (block number after which the signature should be invalid),
    • orderId: a random 32 byte hex string,
    • spender: the address approved to execute this transaction
  • toBuy object

    • token: address of token to buy
    • amount: amount/tokenId of token to buy
  • options see more infomation here

    • from: owner of the token (toSell)
    // sell order
    let toSell = {
    token: token2,
    amount: value2,
    expiry: expire,
    orderId: orderId,
    spender: spender,
    }
    // buy order
    let toBuy = {
    token: token1,
    amount: value1,
    }
    const sig = await matic.getTransferSignature(toSell, toBuy, {
    from: tokenOwner,
    })

matic.transferWithSignature

Executes transferWithSig on child token (erc20/721). Takes input as signature generated from matic.getTransferSignature

  • sig: signature generated with matic.getTransferSignature
  • toSell: object
    • token: address of token owned,
    • amount: amount/tokenId of the token to sell,
    • expiry: expiry (block number after which the signature should be invalid),
    • orderId: a random 32 byte hex string,
    • spender: the address approved to execute this transaction
  • toBuy: object
    • token: address of token to buy
    • amount: amount/tokenId of token to buy
  • orderFiller: address of user to transfer the tokens to
  • options see more infomation here
    • from: the approved spender in the toSell object by the token owner

transfers toSell.token from tokenOwner to orderFiller

// sell order
let toSell = {
token: token2,
amount: value2,
expiry: expire,
orderId: orderId,
spender: spender,
}
// buy order
let toBuy = {
token: token1,
amount: value1,
}
const tx = await matic.transferWithSignature(
sig, // signature with the intent to buy tokens
toSell, // sell order
toBuy, // buy order
orderFiller, // order fulfiller
{
from: spender, // approved spender
}
)

matic.processExits(rootTokenAddress, options)

Call processExits after completion of challenge period, after that withdrawn funds get transfered to your account on mainchain

  • rootTokenAddress RootToken address
  • options see more infomation here
    • encodeAbi must be boolean value. For Byte code of transaction, use encodeAbi: true

This returns Promise object, which will be fulfilled when transaction gets confirmed (when receipt is generated).

Example:

matic.processExits('0xabcd...789', {
from: '0xABc578455...',
})

WithdrawManager

matic.withdrawManager.startExitForMintableBurntToken(burnTxHash, predicate: address, options)

/**
* Start an exit for a token that was minted and burnt on the side chain
* Wrapper over contract call: MintableERC721Predicate.startExitForMintableBurntToken
* @param burnTxHash Hash of the burn transaction on Matic
* @param predicate address of MintableERC721Predicate
*/

See MintableERC721Predicate.startExitForMintableBurntToken

const burn = await this.maticClient.startWithdrawForNFT(childErc721.address, tokenId)
await this.maticClient.withdrawManager.startExitForMintableBurntToken(burn.transactionHash, predicate.address)

matic.withdrawManager.startExitForMintableBurntToken(burnTxHash, predicate: address, options)

/**
* Start an exit for a token with metadata (token uri) that was minted and burnt on the side chain
* Wrapper over contract call: MintableERC721Predicate.startExitForMetadataMintableBurntToken
* @param burnTxHash Hash of the burn transaction on Matic
* @param predicate address of MintableERC721Predicate
*/

See MintableERC721Predicate.startExitForMetadataMintableBurntToken

const burn = await this.maticClient.startWithdrawForNFT(childErc721.address, tokenId)
await this.maticClient.withdrawManager.startExitForMetadataMintableBurntToken(burn.transactionHash, predicate.address)

Support

Please write to info@matic.network for integration support. If you have any queries, feedback or feature requests, feel free to reach out to us on telegram: t.me/maticnetwork

License

MIT