Skip to content

Instantly share code, notes, and snippets.

@atvanguard
Last active November 15, 2019 07:44
Show Gist options
  • Save atvanguard/867639b652ec3bad26a836ad91a9d647 to your computer and use it in GitHub Desktop.
Save atvanguard/867639b652ec3bad26a836ad91a9d647 to your computer and use it in GitHub Desktop.
/* Add maticjs Library as a dep in your project
This library is still WIP, so locking the lib on a commit id for now
npm i --save atvanguard/matic.js#ff738ac9addca5ead3474f238f42b921ebfe2ffd
*/
import Matic from 'maticjs'
async function initializeMaticClient(contracts) {
const options = {
parentProvider: new Web3('http://localhost:8545'),
maticProvider: new Web3('http://localhost:8546'),
registry: contracts.registry.address,
rootChain: contracts.rootChain.address,
depositManager: contracts.depositManager.address,
withdrawManager: contracts.withdrawManager.address
}
const maticClient = new Matic(options)
await maticClient.initialize()
return maticClient
}
async function execute() {
const bob = <address> // get from metamask or the likes
// Initialize Matic Client
// contracts are the matic contract addresses, will give them to you
const maticClient = await initializeMaticClient(contracts)
// Deposit a single token (Eth -> Matic)
await rootERC721.approve(contracts.depositManager.address, tokenId, { from: bob })
const deposit = await maticClient.depositManager.depositERC721(rootERC721.address, tokenId, { from: bob })
// Deposit Bulk (Eth -> Matic)
await rootERC721.setApprovalForAll(contracts.depositManager.address /* to */, true /* approved */, { from: bob })
const tokenIds = [] // array of tokenIds to deposit
const tokens = Array(tokenIds.length).fill(rootERC721.address) // array of token contract address
const deposit = await maticClient.depositManager.depositBulk(tokens, tokenIds, bob, { from: bob })
// Withdraw on matic chain
const withdraw = await maticClient.withdrawManager.burnERC721Token(
childErc721.address,
tokenId,
{ from: bob }
)
// start exit on mainchain for a single token
const startExit = await maticClient.withdrawManager.startExitForMintWithTokenURITokens(
withdraw.transactionHash,
{ from: bob }
)
// start exit on mainchain for several burnt tokens
const withdrawls = [withdraw.transactionHash] // array containing txHashes of withdraw txs on matic chain
const startExit = await maticClient.withdrawManager.startBulkExitForMintWithTokenURITokens(
withdrawls,
{ from: bob }
)
// processExits needs to be called after 7 days to actually transfer the token to owner
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment