move depositDataByHash to DepositManager
MODIFY
src/index.ts
src/index.ts
@@ -14,7 +14,6 @@ export default class Matic extends ContractsBase {
public rootChain: RootChain
public withdrawManager: WithdrawManager
public registry: Registry
- public childChainAddress: address
constructor(options: any = {}) {
const web3Client = new Web3Client(
@@ -27,12 +26,12 @@ export default class Matic extends ContractsBase {
this.web3Client = web3Client
this.registry = new Registry(options.registry, this.web3Client)
this.rootChain = new RootChain(options.rootChain, this.web3Client)
- this.depositManager = new DepositManager(options.depositManager, this.web3Client)
+ this.depositManager = new DepositManager(options.depositManager, this.web3Client, this.registry)
this.withdrawManager = new WithdrawManager(options.withdrawManager, this.rootChain, this.web3Client, this.registry)
}
initialize() {
- return Promise.all([this.withdrawManager.initialize()])
+ return Promise.all([this.withdrawManager.initialize(), this.depositManager.initialize()])
}
setWallet(_wallet) {
@@ -123,24 +122,11 @@ export default class Matic extends ContractsBase {
return this.depositManager.depositEther(amount, options)
}
- async depositDataByHash(txHash: string) {
- this.childChainAddress = await this.registry.registry.methods.getChildChainAndStateSender().call()
-
- const depositReceipt = await this.web3Client.parentWeb3.eth.getTransactionReceipt(txHash)
- if (!depositReceipt) {
- throw new Error('Transaction hash is not Found')
- }
- const newDepositEvent = depositReceipt.logs.find(
- l => l.topics[0].toLowerCase() === DepositManager.NEW_DEPOSIT_EVENT_SIG
- )
-
- const data = newDepositEvent.data
- const depositId = new BN(data.substring(data.length - 64), 16)
- const depositExists = await this.depositManager.isDepositExistById(depositId, this.childChainAddress[0])
- if (!depositExists) {
- throw new Error('Deposit is not processed on Matic chain')
+ depositDataByHash(txHash: string) {
+ if (!txHash) {
+ throw new Error('txHash is missing')
}
- return depositReceipt
+ return this.depositManager.depositDataByHash(txHash)
}
approveERC20TokensForDeposit(token: address, amount: BN | string, options?: SendOptions) {

MODIFY
src/root/DepositManager.ts
src/root/DepositManager.ts
@@ -6,23 +6,56 @@ import ChildChainArtifact from 'matic-protocol/contracts-core/artifacts/ChildCha
import ContractsBase from '../common/ContractsBase'
import { address, SendOptions } from '../types/Common'
import Web3Client from '../common/Web3Client'
+import Registry from './Registry'
export default class DepositManager extends ContractsBase {
static NEW_DEPOSIT_EVENT_SIG = '0x1dadc8d0683c6f9824e885935c1bec6f76816730dcec148dda8cf25a7b9f797b'.toLowerCase()
public depositManagerContract: Contract
+ public childChainContract: Contract
- constructor(depositManager: address, web3Client: Web3Client) {
+ private registry: Registry
+
+ constructor(depositManager: address, web3Client: Web3Client, registry: Registry) {
super(web3Client)
this.depositManagerContract = new this.web3Client.parentWeb3.eth.Contract(
DepositManagerArtifact.abi,
depositManager
)
+ this.registry = registry
+ }
+
+ async initialize() {
+ const childChainAddress = await this.registry.registry.methods.getChildChainAndStateSender().call()
+ this.childChainContract = new this.web3Client.web3.eth.Contract(ChildChainArtifact.abi, childChainAddress[0])
}
- async isDepositExistById(depositId: BN, childChainAddress: address) {
- const childChainContract = new this.web3Client.web3.eth.Contract(ChildChainArtifact.abi, childChainAddress)
- return childChainContract.methods.deposits(this.encode(depositId)).call()
+ async depositDataByHash(txHash: string) {
+ const depositExistList = []
+ const depositReceipt = await this.web3Client.parentWeb3.eth.getTransactionReceipt(txHash)
+ if (!depositReceipt) {
+ throw new Error('Transaction hash is not Found')
+ }
+ const newDepositEventList = 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))
+ })
+ 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
}
async approveERC20(token: address, amount: BN | string, options?: SendOptions) {

move depositDataByHash to DepositManager