Handle unprocessed deposits gracefully
MODIFY
examples/node/config.js
examples/node/config.js
+// Testnet V3 config
module.exports = {
MATIC_PROVIDER: 'https://testnetv3.matic.network', // This is the MATIC testnet RPC
- PARENT_PROVIDER:
- 'https://ropsten.infura.io/v3/70645f042c3a409599c60f96f6dd9fbc', // This is the Ropsten testnet RPC
+ PARENT_PROVIDER: 'https://ropsten.infura.io/v3/70645f042c3a409599c60f96f6dd9fbc', // This is the Ropsten testnet RPC
REGISTRY_ADDRESS: '0x56B082d0a590A7ce5d170402D6f7f88B58F71988',
ROOTCHAIN_ADDRESS: '0x82a72315E16cE224f28E1F1fB97856d3bF83f010', // The address for the main Plasma contract in Ropsten testnet
WITHDRAWMANAGER_ADDRESS: '0x3cf9aD3395028a42EAfc949e2EC4588396b8A7D4', // An address for the WithdrawManager contract on Ropsten testnet

MODIFY
examples/node/deposit-data.js
examples/node/deposit-data.js
-const bn = require('bn.js')
-
-const Matic = require('../../dist/index').default
const config = require('./config')
const utils = require('./utils')
-const token = config.PARENT_TEST_TOKEN
-const amount = new bn(1).mul(utils.SCALING_FACTOR)
-const from = config.FROM_ADDRESS
-
// Create Matic object
-const matic = new Matic({
- maticProvider: config.MATIC_PROVIDER,
- parentProvider: config.PARENT_PROVIDER,
- rootChain: config.ROOTCHAIN_ADDRESS,
- registry: config.REGISTRY_ADDRESS,
- depositManager: config.DEPOSITMANAGER_ADDRESS,
- withdrawManager: config.WITHDRAWMANAGER_ADDRESS,
- childChain: config.CHILDCHAIN_ADDRESS,
-})
+const matic = utils.getMaticClient()
async function execute() {
await matic.initialize()
- matic.setWallet(config.PRIVATE_KEY)
- return matic.depositDataByHash('0x8cff1ef2b198273fe7f9fb41918cd7e3e8d04c58650614af39b3086e1de1a7e6')
- // return matic.depositManager.isDepositExistById(101230019, '0xa2EF03edfA084ac9e5Bf110e409Ed5483BAe4101')
-}
-
-async function executeRaw() {
- // eslint-disable-line
- await matic.initialize()
- let txParams = await matic.approveERC20TokensForDeposit(token, amount, { from, encodeAbi: true })
- let serializedTx = utils.buildRawTransaction(txParams, config.PRIVATE_KEY)
- await matic.web3Client.parentWeb3.eth.sendSignedTransaction(serializedTx)
-
- txParams = await matic.depositERC20ForUser(token, from, amount, { from, encodeAbi: true })
- serializedTx = utils.buildRawTransaction(txParams, config.PRIVATE_KEY)
- return matic.web3Client.parentWeb3.eth.sendSignedTransaction(serializedTx)
+ return matic.depositStatusFromTxHash('0x8cff1ef2b198273fe7f9fb41918cd7e3e8d04c58650614af39b3086e1de1a7e6')
}
execute().then(console.log) // eslint-disable-line

MODIFY
examples/node/utils.js
examples/node/utils.js
const bn = require('bn.js')
const EthereumTx = require('ethereumjs-tx')
+const config = require('./config')
+const Matic = require('../../dist/index').default
+
const SCALING_FACTOR = new bn(10).pow(new bn(18))
function buildRawTransaction(txParams, privateKey) {
@@ -12,7 +15,20 @@ function buildRawTransaction(txParams, privateKey) {
return serializedTx
}
+function getMaticClient() {
+ return new Matic({
+ maticProvider: config.MATIC_PROVIDER,
+ parentProvider: config.PARENT_PROVIDER,
+ rootChain: config.ROOTCHAIN_ADDRESS,
+ registry: config.REGISTRY_ADDRESS,
+ depositManager: config.DEPOSITMANAGER_ADDRESS,
+ withdrawManager: config.WITHDRAWMANAGER_ADDRESS,
+ childChain: config.CHILDCHAIN_ADDRESS,
+ })
+}
+
module.exports = {
SCALING_FACTOR,
buildRawTransaction,
+ getMaticClient,
}

MODIFY
src/index.ts
src/index.ts
@@ -122,11 +122,11 @@ export default class Matic extends ContractsBase {
return this.depositManager.depositEther(amount, options)
}
- depositDataByHash(txHash: string) {
+ depositStatusFromTxHash(txHash: string): Promise<{ receipt: any; deposits: any[] }> {
if (!txHash) {
throw new Error('txHash is missing')
}
- return this.depositManager.depositDataByHash(txHash)
+ return this.depositManager.depositStatusFromTxHash(txHash)
}
approveERC20TokensForDeposit(token: address, amount: BN | string, options?: SendOptions) {

MODIFY
src/root/DepositManager.ts
src/root/DepositManager.ts
import BN from 'bn.js'
+import bluebird from 'bluebird'
import Contract from 'web3/eth/contract'
import DepositManagerArtifact from 'matic-protocol/contracts-core/artifacts/DepositManager.json'
import ChildChainArtifact from 'matic-protocol/contracts-core/artifacts/ChildChain.json'
@@ -9,7 +10,7 @@ import Web3Client from '../common/Web3Client'
import Registry from './Registry'
export default class DepositManager extends ContractsBase {
- static NEW_DEPOSIT_EVENT_SIG = '0x1dadc8d0683c6f9824e885935c1bec6f76816730dcec148dda8cf25a7b9f797b'.toLowerCase()
+ static NEW_DEPOSIT_EVENT_SIG = '0x1dadc8d0683c6f9824e885935c1bec6f76816730dcec148dda8cf25a7b9f797b'
public depositManagerContract: Contract
public childChainContract: Contract
@@ -26,36 +27,30 @@ export default class DepositManager extends ContractsBase {
}
async initialize() {
- const childChainAddress = await this.registry.registry.methods.getChildChainAndStateSender().call()
- this.childChainContract = new this.web3Client.web3.eth.Contract(ChildChainArtifact.abi, childChainAddress[0])
+ const childChainAddress = (await this.registry.registry.methods.getChildChainAndStateSender().call())[0]
+ this.childChainContract = new this.web3Client.web3.eth.Contract(ChildChainArtifact.abi, childChainAddress)
}
- async depositDataByHash(txHash: string) {
- const depositExistList = []
+ async depositStatusFromTxHash(txHash: string) {
+ const deposits = []
const depositReceipt = await this.web3Client.parentWeb3.eth.getTransactionReceipt(txHash)
if (!depositReceipt) {
- throw new Error('Transaction hash is not Found')
+ throw new Error('Transaction hash not found')
}
- const newDepositEventList = depositReceipt.logs.filter(
+ const depositEvents = depositReceipt.logs.filter(
l => l.topics[0].toLowerCase() === DepositManager.NEW_DEPOSIT_EVENT_SIG
)
- if (newDepositEventList.length > 0) {
- newDepositEventList.forEach(newDepositEvent => {
- const data = newDepositEvent.data
- const depositId = new BN(data.substring(data.length - 64), 16)
- depositExistList.push(this.isDepositExistById(depositId))
+ if (depositEvents.length > 0) {
+ await bluebird.map(depositEvents, async event => {
+ const data = event.data
+ const depositId = '0x' + data.substring(data.length - 64)
+ deposits.push({
+ depositId,
+ isProcessed: await this.childChainContract.methods.deposits(depositId).call(),
+ })
})
- Promise.all(depositExistList)
- }
- return depositReceipt
- }
-
- async isDepositExistById(depositId: BN) {
- const depositExists = this.childChainContract.methods.deposits(this.encode(depositId)).call()
- if (!depositExists) {
- throw new Error('Deposit is not processed on Matic chain')
}
- return depositExists
+ return { receipt: depositReceipt, deposits }
}
async approveERC20(token: address, amount: BN | string, options?: SendOptions) {

Handle unprocessed deposits gracefully