diff --git a/src/constant.ts b/src/constant.ts index d124ebe..617a2f9 100644 --- a/src/constant.ts +++ b/src/constant.ts @@ -4,6 +4,7 @@ export const DAI_PERMIT_TYPEHASH = "0xea2aa0a1be11a07ed86d755c93467f4f82362b4523 export const EIP_2612_PERMIT_TYPEHASH = "0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9"; export const EIP_2612_DOMAIN_TYPEHASH = "0x8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400f"; export const UNISWAP_DOMAIN_TYPEHASH = "0x8cad95687ba82c2ce50e74f7b754645e5117c3a5bec8151c0726d5857980a866"; +export const _GLOBAL_INDEX_MAINNET_FLAG = BigInt(2 ** 64); export enum Permit { DAI = "DAI", EIP_2612 = "EIP_2612", diff --git a/src/lxly/bridge.ts b/src/lxly/bridge.ts index a9ccf24..1a0f229 100644 --- a/src/lxly/bridge.ts +++ b/src/lxly/bridge.ts @@ -55,62 +55,12 @@ export class Bridge extends BaseToken { }); } - /** - * Claim function to be called on the destination network - * - * @param {string[]} smtProof Merkle Proof - * @param {number} index Deposit Index - * @param {string} mainnetExitRoot Mainnet Exit Root - * @param {string} rollupExitRoot RollUP Exit Root - * @param {number} originNetwork Network at which token was initially deployed - * @param {string} originTokenAddress Address of token at network where token was initially deployed - * @param {string} destinationAddress Address to which tokens will be bridged - * @param {TYPE_AMOUNT} amount amount of tokens - * @param {string} [metadata] Metadata of token - * @param {ITransactionOption} [option] - * - * @returns - * @memberof Bridge - */ - claimAsset( - smtProof: string[], - index: number, - mainnetExitRoot: string, - rollupExitRoot: string, - originNetwork: number, - originTokenAddress: string, - destinationNetwork: number, - destinationAddress: string, - amount: TYPE_AMOUNT, - metadata: string, - option: ITransactionOption - ) { - return this.method( - "claimAsset", - smtProof, - index, - mainnetExitRoot, - rollupExitRoot, - originNetwork, - originTokenAddress, - destinationNetwork, - destinationAddress, - amount, - metadata - ).then(method => { - return this.processWrite( - method, - option - ); - }); - } - /** * Claim function to be called on the destination network * * @param {string[]} smtProof Merkle Proof * @param {string[]} smtProofRollup Roll up Merkle Proof - * @param {number} index Deposit Index + * @param {string} globalIndex Global Index * @param {string} mainnetExitRoot Mainnet Exit Root * @param {string} rollupExitRoot RollUP Exit Root * @param {number} originNetwork Network at which token was initially deployed @@ -123,10 +73,10 @@ export class Bridge extends BaseToken { * @returns * @memberof Bridge */ - claimAssetNew( + claimAsset( smtProof: string[], smtProofRollup: string[], - index: number, + globalIndex: string, mainnetExitRoot: string, rollupExitRoot: string, originNetwork: number, @@ -141,7 +91,7 @@ export class Bridge extends BaseToken { "claimAsset", smtProof, smtProofRollup, - index, + globalIndex, mainnetExitRoot, rollupExitRoot, originNetwork, @@ -187,58 +137,6 @@ export class Bridge extends BaseToken { }); } - - /** - * Claim Message function to be called on the destination network - * If the receiving address is an EOA, the call will result as a success - * Which means that the amount of ether will be transferred correctly, but the message - * will not trigger any execution - * @param {string[]} smtProof Merkle Proof - * @param {number} index Deposit Index - * @param {string} mainnetExitRoot Mainnet Exit Root - * @param {string} rollupExitRoot RollUP Exit Root - * @param {number} originNetwork Network at which token was initially deployed - * @param {string} originTokenAddress Address of token at network where token was initially deployed - * @param {string} destinationAddress Address to which tokens will be bridged - * @param {TYPE_AMOUNT} amount amount of tokens - * @param {string} [metadata] Metadata of token - * @param {ITransactionOption} [option] - * - * @returns - * @memberof Bridge - */ - claimMessage( - smtProof: string[], - index: number, - mainnetExitRoot: string, - rollupExitRoot: string, - originNetwork: number, - originTokenAddress: string, - destinationNetwork: number, - destinationAddress: string, - amount: TYPE_AMOUNT, - metadata: string, - option: ITransactionOption) { - return this.method( - "claimMessage", - smtProof, - index, - mainnetExitRoot, - rollupExitRoot, - originNetwork, - originTokenAddress, - destinationNetwork, - destinationAddress, - amount, - metadata - ).then(method => { - return this.processWrite( - method, - option - ); - }); - } - /** * Claim Message new function to be called on the destination network * If the receiving address is an EOA, the call will result as a success @@ -246,7 +144,7 @@ export class Bridge extends BaseToken { * will not trigger any execution. this will work after Etrog changes * @param {string[]} smtProof Merkle Proof * @param {string[]} smtProofRollup Roll up Merkle Proof - * @param {number} index Deposit Index + * @param {string} globalIndex Global Index * @param {string} mainnetExitRoot Mainnet Exit Root * @param {string} rollupExitRoot RollUP Exit Root * @param {number} originNetwork Network at which token was initially deployed @@ -259,10 +157,10 @@ export class Bridge extends BaseToken { * @returns * @memberof Bridge */ - claimMessageNew( + claimMessage( smtProof: string[], smtProofRollup: string[], - index: number, + globalIndex: string, mainnetExitRoot: string, rollupExitRoot: string, originNetwork: number, @@ -276,7 +174,7 @@ export class Bridge extends BaseToken { "claimMessage", smtProof, smtProofRollup, - index, + globalIndex, mainnetExitRoot, rollupExitRoot, originNetwork, diff --git a/src/lxly/bridge_util.ts b/src/lxly/bridge_util.ts index e2bf2b0..b3f8edf 100644 --- a/src/lxly/bridge_util.ts +++ b/src/lxly/bridge_util.ts @@ -1,9 +1,9 @@ import { Web3SideChainClient } from "../utils"; import { service } from "../services"; -import { IBaseClientConfig } from ".."; +import { IBaseClientConfig, _GLOBAL_INDEX_MAINNET_FLAG } from ".."; import { TYPE_AMOUNT } from '../types'; -interface IBridgeEventInfo { +export interface IBridgeEventInfo { originNetwork: number; originTokenAddress: string; destinationNetwork: number; @@ -13,7 +13,7 @@ interface IBridgeEventInfo { depositCount: number; } -interface IMerkleProof { +export interface IMerkleProof { merkle_proof: string[]; rollup_merkle_proof?: string[]; exit_root_num: string; @@ -22,10 +22,10 @@ interface IMerkleProof { rollup_exit_root: string; } -interface IClaimPayload { +export interface IClaimPayload { smtProof: string[]; smtProofRollup?: string[]; - index: number; + globalIndex: string; mainnetExitRoot: string; rollupExitRoot: string; originNetwork: number; @@ -95,8 +95,16 @@ export class BridgeUtil { return this.getBridgeLogData_(transactionHash, networkId, isRefuel); } + computeGlobalIndex(indexLocal: number, indexRollup: number, sourceNetworkId: number) { + if (sourceNetworkId === 0) { + return BigInt(indexLocal) + _GLOBAL_INDEX_MAINNET_FLAG; + } else { + return BigInt(indexLocal) + BigInt(indexRollup) * BigInt(2 ** 32); + } + } + buildPayloadForClaim(transactionHash: string, networkId: number, isRefuel = false) { - return this.getBridgeLogData_(transactionHash, networkId, isRefuel).then(data => { + return this.getBridgeLogData_(transactionHash, networkId, isRefuel).then((data: IBridgeEventInfo) => { const { originNetwork, originTokenAddress, @@ -105,11 +113,11 @@ export class BridgeUtil { amount, metadata, depositCount } = data; - return this.getProof_(networkId, depositCount).then(proof => { + return this.getProof_(networkId, depositCount).then((proof: IMerkleProof) => { const payload = {} as IClaimPayload; payload.smtProof = proof.merkle_proof; payload.smtProofRollup = proof.rollup_merkle_proof; - payload.index = depositCount; + payload.globalIndex = this.computeGlobalIndex(depositCount, destinationNetwork, networkId).toString(); payload.mainnetExitRoot = proof.main_exit_root; payload.rollupExitRoot = proof.rollup_exit_root; payload.originNetwork = originNetwork; diff --git a/src/lxly/erc20.ts b/src/lxly/erc20.ts index 5048568..0a2bd3f 100644 --- a/src/lxly/erc20.ts +++ b/src/lxly/erc20.ts @@ -14,6 +14,7 @@ import { Permit, BaseContract, BaseWeb3Client, + IClaimPayload, } from '..'; import { IAllowanceTransactionOption, @@ -359,37 +360,21 @@ export class ERC20 extends Token { claimCustomERC20(transactionHash: string, sourceNetworkId: number, option?: ITransactionOption) { return this.bridgeUtil.buildPayloadForClaim( transactionHash, sourceNetworkId, option.isRefuel || false - ).then(payload => { - if (payload.smtProofRollup) { - return this.bridge.claimMessageNew( - payload.smtProof, - payload.smtProofRollup, - payload.index, - payload.mainnetExitRoot, - payload.rollupExitRoot, - payload.originNetwork, - payload.originTokenAddress, - payload.destinationNetwork, - payload.destinationAddress, - payload.amount, - payload.metadata, - option - ); - } else { - return this.bridge.claimMessage( - payload.smtProof, - payload.index, - payload.mainnetExitRoot, - payload.rollupExitRoot, - payload.originNetwork, - payload.originTokenAddress, - payload.destinationNetwork, - payload.destinationAddress, - payload.amount, - payload.metadata, - option - ); - } + ).then((payload: IClaimPayload) => { + return this.bridge.claimMessage( + payload.smtProof, + payload.smtProofRollup, + payload.globalIndex, + payload.mainnetExitRoot, + payload.rollupExitRoot, + payload.originNetwork, + payload.originTokenAddress, + payload.destinationNetwork, + payload.destinationAddress, + payload.amount, + payload.metadata, + option + ); }); } @@ -406,45 +391,37 @@ export class ERC20 extends Token { claimAsset(transactionHash: string, sourceNetworkId: number, option?: ITransactionOption) { return this.bridgeUtil.buildPayloadForClaim( transactionHash, sourceNetworkId, option.isRefuel || false - ).then(payload => { - if (payload.smtProofRollup) { - return this.bridge.claimAssetNew( - payload.smtProof, - payload.smtProofRollup, - payload.index, - payload.mainnetExitRoot, - payload.rollupExitRoot, - payload.originNetwork, - payload.originTokenAddress, - payload.destinationNetwork, - payload.destinationAddress, - payload.amount, - payload.metadata, - option - ); - } else { - return this.bridge.claimAsset( - payload.smtProof, - payload.index, - payload.mainnetExitRoot, - payload.rollupExitRoot, - payload.originNetwork, - payload.originTokenAddress, - payload.destinationNetwork, - payload.destinationAddress, - payload.amount, - payload.metadata, - option - ); - } + ).then((payload: IClaimPayload) => { + return this.bridge.claimAsset( + payload.smtProof, + payload.smtProofRollup, + payload.globalIndex, + payload.mainnetExitRoot, + payload.rollupExitRoot, + payload.originNetwork, + payload.originTokenAddress, + payload.destinationNetwork, + payload.destinationAddress, + payload.amount, + payload.metadata, + option + ); }); } /** * Claim Assets after GlobalExitRootManager is synced from source to destination * - * @param {string} transactionHash - * @param {number} sourceNetworkId + * @param {string[]} smtProof Merkle Proof + * @param {string[]} smtProofRollup Roll up Merkle Proof + * @param {string} globalIndex Global Index + * @param {string} mainnetExitRoot Mainnet Exit Root + * @param {string} rollupExitRoot RollUP Exit Root + * @param {number} originNetwork Network at which token was initially deployed + * @param {string} originTokenAddress Address of token at network where token was initially deployed + * @param {string} destinationAddress Address to which tokens will be bridged + * @param {TYPE_AMOUNT} amount amount of tokens + * @param {string} metadata Metadata of token * @param {ITransactionOption} [option] * @returns * @memberof ERC20 @@ -452,7 +429,7 @@ export class ERC20 extends Token { claimAssetRaw( smtProof: string[], smtProofRollup: string[], - index: number, + globalIndex: string, mainnetExitRoot: string, rollupExitRoot: string, originNetwork: number, @@ -463,10 +440,10 @@ export class ERC20 extends Token { metadata: string, option?: ITransactionOption ) { - return this.bridge.claimAssetNew( + return this.bridge.claimAsset( smtProof, smtProofRollup, - index, + globalIndex, mainnetExitRoot, rollupExitRoot, originNetwork, diff --git a/test/package-lock.json b/test/package-lock.json index 4f2bf7c..2cdeae3 100644 --- a/test/package-lock.json +++ b/test/package-lock.json @@ -1871,7 +1871,7 @@ "node_modules/@maticnetwork/lxlyjs": { "version": "0.0.1", "resolved": "file:../maticnetwork-lxlyjs-0.0.1.tgz", - "integrity": "sha512-kwDpNCI+KFQ4DmXl37diKLRQRW1GoKyaiVa6r2r2YZIBOgGbmhjmCXRa80E3RFPMFcqiDqJ1fxAaAivE5C/DeQ==", + "integrity": "sha512-37dtbvvGyISuJXIENoghVftKQ1S9ayrgslBxa9wa/NHG14gozk/XE1YzCDYmvhvheNS+BJACJ+UYObnqRi7Oug==", "license": "MIT", "dependencies": { "@ethereumjs/block": "^3.6.2", @@ -16186,7 +16186,7 @@ }, "@maticnetwork/lxlyjs": { "version": "file:../maticnetwork-lxlyjs-0.0.1.tgz", - "integrity": "sha512-kwDpNCI+KFQ4DmXl37diKLRQRW1GoKyaiVa6r2r2YZIBOgGbmhjmCXRa80E3RFPMFcqiDqJ1fxAaAivE5C/DeQ==", + "integrity": "sha512-37dtbvvGyISuJXIENoghVftKQ1S9ayrgslBxa9wa/NHG14gozk/XE1YzCDYmvhvheNS+BJACJ+UYObnqRi7Oug==", "requires": { "@ethereumjs/block": "^3.6.2", "ethereumjs-util": "^7.1.4",