Matic SDK
This repository contains the maticjs
client library. maticjs
makes it easy for developers, who may not be deeply familiar with smart contract development, to interact with the various components of Matic Network.
This library will help developers to move assets from Ethereum chain to Matic chain, and withdraw from Matic to Ethereum using fraud proofs.
We will be improving this library to make all features available like Plasma Faster Exit, challenge exit, finalize exit and more.
Installation
NPM
$ npm install --save @maticnetwork/maticjs
Getting started
import Matic from '@maticnetwork/maticjs'
const matic = new Matic({
maticProvider: <web3-provider>,
parentProvider: <web3-provider>,
rootChain: <root-contract-address>,
registry: <registry-contract-address>,
withdrawManager: <withdraw-manager-address>,
depositManager: <deposit-manager-address>,
})
matic.initialize()
await matic.approveERC20TokensForDeposit(
token,
amount,
options
)
await matic.depositERC20ForUser(
token,
user,
amount,
options
)
await matic.safeDepositERC721Tokens(
token,
tokenId,
options
)
await matic.transferERC20Tokens(
token,
user,
amount,
options
)
await matic.transferERC721Tokens(
token,
user,
tokenId,
options
)
await matic.startWithdraw(
token,
amount,
options
)
await matic.startWithdrawERC721(
token,
tokenId,
options
)
await matic.withdraw(
txId,
options
)
await matic.confirmWithdrawERC721(
txId,
options
)
await matic.processExits(
tokenAddress,
options
)
How it works?
The flow for asset transfers on the Matic Network is as follows:
- User deposits crypto assets in Matic contract on mainchain
- Once deposited tokens get confirmed on the main chain, the corresponding tokens will get reflected on the Matic chain.
- The user can now transfer tokens to anyone they want instantly with negligible fees. Matic chain has faster blocks (approximately ~ 1 second). That way, the transfer will be done almost instantly.
- Once a user is ready, they can withdraw remaining tokens from the mainchain by establishing proof of remaining tokens on Root contract (contract deployed on Ethereum chain)
Contracts and addresses
Matic Testnet
Ropsten testnet addresses
- TEST mainchain ERC20 token: 0x6b0b0e265321e788af11b6f1235012ae7b5a6808
- Root Contract: 0x60e2b19b9a87a3f37827f2c8c8306be718a5f9b4
- DepositManager Contract: 0x4072fab2a132bf98207cbfcd2c341adb904a67e9
- WithdrawManager Contract: 0x4ef2b60cdd4611fa0bc815792acc14de4c158d22
Faucet
Please write to info@matic.network to request TEST tokens for development purposes. We will soon have a faucet in place for automatic distribution of tokens for testing.
API
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.approveERC20TokensForDeposit(token, amount, options)
Approves given amount
of token
to rootChainContract
.
token
must be valid ERC20 token addressamount
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 addressuser
must be value account addressamount
must be token amount in wei (string, not in Number)options
see more infomation here
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 addresstokenId
must be valid token IDoptions
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 addressuser
must be value account addressamount
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
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...',
})
matic.transferERC721Tokens(token, user, tokenId, options)
Transfer given tokenId
of token
to user
.
token
must be valid ERC721 token addressuser
must be value account addresstokenId
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
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...',
})
matic.startWithdraw(token, amount, options)
Start withdraw process with given amount
for token
.
token
must be valid ERC20 token addressamount
must be token amount in wei (string, not in Number)options
see more infomation here
This returns Promise
object, which will be fulfilled when transaction gets confirmed (when receipt is generated).
Example:
matic
.startWithdraw("0x718Ca123...", "1000000000000000000", {
from: "0xABc578455..."
})
matic.startWithdrawERC721(token, tokenId, options)
Start withdraw process with given tokenId
for token
.
token
must be valid ERC721 token addresstokenId
must be token tokenId (string, not in Number)options
see more infomation here
This returns Promise
object, which will be fulfilled when transaction gets confirmed (when receipt is generated).
Example:
matic
.startWithdrawERC721("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.confirmWithdrawERC721(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.confirmWithdrawERC721("0xabcd...789", {
from: "0xABc578455..."
})
matic.processExits(rootTokenAddress, options)
Call processExits after completion of challenge period, after that withdrawn funds get transfered to your account on mainchain
rootTokenAddress
RootToken addressoptions
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..."
})
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