Save all three files in a directory and install dependencies:
$ npm init -y
$ npm i -s @maticnetwork/maticjs @truffle/hdwallet-provider web3| module.exports = { | |
| root: { | |
| RPC: 'https://goerli.infura.io/v3/<API_KEY>', | |
| POSRootChainManager: '0xBbD7cBFA79faee899Eaf900F13C9065bF03B1A74', | |
| DERC20: '0x655F2166b0709cd575202630952D71E2bB0d61Af', | |
| DERC721: '0x084297B12F204Adb74c689be08302FA3f12dB8A7', | |
| DERC1155: '0x2e3Ef7931F2d0e4a7da3dea950FF3F19269d9063', | |
| posERC20Predicate: '0xdD6596F2029e6233DEFfaCa316e6A95217d4Dc34', | |
| posERC721Predicate: '0x74D83801586E9D3C4dc45FfCD30B54eA9C88cf9b', | |
| posERC1155Predicate: '0xB19a86ba1b50f0A395BfdC3557608789ee184dC8', | |
| posEtherPredicate: '0xe2B01f3978c03D6DdA5aE36b2f3Ac0d66C54a6D5', | |
| }, | |
| child: { | |
| RPC: 'https://rpc-mumbai.matic.today', | |
| DERC20: '0xfe4F5145f6e09952a5ba9e956ED0C25e3Fa4c7F1', | |
| DERC721: '0x757b1BD7C12B81b52650463e7753d7f5D0565C0e', | |
| DERC1155: '0xA07e45A987F19E25176c877d98388878622623FA', | |
| MaticWETH: '0x714550C2C1Ea08688607D86ed8EeF4f5E4F22323', | |
| }, | |
| user: { | |
| privateKey: '<PRIVATE_KEY>', | |
| address: '<ADDRESS>' | |
| }, | |
| } |
| const Web3 = require('web3') | |
| const rootRPC = 'https://goerli.infura.io/v3/982855ae82c9477e91981103b7e26ea7' | |
| const childRPC = 'https://rpc-mumbai.matic.today' | |
| const web3 = new Web3() | |
| const config = require('./config') | |
| const utils = require('./utils') | |
| const rootErc721ABI = require('./abi/pos/DummyERC721.json') | |
| const rootErc721Address = config.root.DERC721 | |
| const rootErc721 = new web3.eth.Contract(rootErc721ABI.abi, rootErc721Address) | |
| const childErc721Address = config.child.DERC721 | |
| const childErc721 = new web3.eth.Contract(rootErc721ABI.abi, childErc721Address) | |
| function getRandomInt(min, max) { | |
| min = Math.ceil(min); | |
| max = Math.floor(max); | |
| return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive | |
| } | |
| web3.eth.accounts.wallet.add(config.user.privateKey) | |
| async function mint (id) { | |
| web3.setProvider (rootRPC) | |
| let r = await rootErc721.methods.mint( | |
| id | |
| ).send({ | |
| from: web3.eth.accounts.wallet[0].address, | |
| gas: 800000 | |
| }).then ((r) => { | |
| console.log(r.transactionHash) | |
| }) | |
| return id | |
| } | |
| const maticPOSClient = utils.getMaticPOSClient() | |
| async function deposit(tokenId) { | |
| try { | |
| const approve = await maticPOSClient.approveERC721ForDeposit(rootErc721Address, tokenId, { | |
| from: web3.eth.accounts.wallet[0].address | |
| }) | |
| console.log ('approved') | |
| const tx = await maticPOSClient.depositERC721ForUser(rootErc721Address, web3.eth.accounts.wallet[0].address, tokenId) | |
| console.log('deposited', tx.transactionHash) | |
| } catch (e) { | |
| console.error(e) | |
| } | |
| // process.exit() | |
| } | |
| function wait (delayms) { | |
| return new Promise(function(resolve, reject) { | |
| setTimeout(resolve, delayms) | |
| }) | |
| } | |
| async function main () { | |
| for (let i = 0; i < 10; i++) { | |
| let id = getRandomInt(1000000, 10000000000) | |
| console.log ('minting:',id) | |
| let token = await mint (id) | |
| let token='4792115200' | |
| console.log ('depositing', token) | |
| await deposit(token) | |
| // console.log('waiting 30s') | |
| // await wait(30000) | |
| } | |
| } | |
| main() |
| const MaticPOSClient = require('@maticnetwork/maticjs').MaticPOSClient | |
| const config = require('./config') | |
| const HDWalletProvider = require('@truffle/hdwallet-provider') | |
| const getMaticPOSClient = () => { | |
| return new MaticPOSClient({ | |
| network: 'testnet', // optional, default is testnet | |
| version: 'mumbai', // optional, default is mumbai | |
| parentProvider: new HDWalletProvider(config.user.privateKey, config.root.RPC), | |
| maticProvider: new HDWalletProvider(config.user.privateKey, config.child.RPC), | |
| posRootChainManager: config.root.POSRootChainManager, | |
| posERC20Predicate: config.root.posERC20Predicate, // optional, required only if working with ERC20 tokens | |
| posERC721Predicate: config.root.posERC721Predicate, // optional, required only if working with ERC721 tokens | |
| posERC1155Predicate: config.root.posERC1155Predicate, // optional, required only if working with ERC71155 tokens | |
| parentDefaultOptions: { from: config.user.address }, // optional, can also be sent as last param while sending tx | |
| maticDefaultOptions: { from: config.user.address }, // optional, can also be sent as last param while sending tx | |
| }) | |
| } | |
| module.exports = { | |
| getMaticPOSClient, | |
| } |