From 216893a169bec86894caf62be38d841a3b9025b5 Mon Sep 17 00:00:00 2001 From: Nitin Mittal Date: Thu, 25 Jan 2024 15:24:26 +0400 Subject: [PATCH 1/2] new: etrog changes --- src/utils/zkevm_bridge_client.ts | 4 +- src/zkevm/bridge_util.ts | 3 + src/zkevm/erc20.ts | 201 +++++++++++++++++++++---------- src/zkevm/zkevm_bridge.ts | 156 ++++++++++++++++++++---- 4 files changed, 272 insertions(+), 92 deletions(-) diff --git a/src/utils/zkevm_bridge_client.ts b/src/utils/zkevm_bridge_client.ts index 85cb8352c..69de41a0a 100644 --- a/src/utils/zkevm_bridge_client.ts +++ b/src/utils/zkevm_bridge_client.ts @@ -63,7 +63,7 @@ export class ZkEvmBridgeClient { return this.bridgeUtil.getBridgeLogData( txHash, true ).then(result => { - return this.childChainBridge.isClaimed(result.depositCount); + return this.childChainBridge.isClaimed(result.depositCount, 0); }); } @@ -78,7 +78,7 @@ export class ZkEvmBridgeClient { return this.bridgeUtil.getBridgeLogData( txHash, false ).then(result => { - return this.rootChainBridge.isClaimed(result.depositCount); + return this.rootChainBridge.isClaimed(result.depositCount, 1); }); } diff --git a/src/zkevm/bridge_util.ts b/src/zkevm/bridge_util.ts index 5e02ffe7a..fcd68cb28 100644 --- a/src/zkevm/bridge_util.ts +++ b/src/zkevm/bridge_util.ts @@ -15,6 +15,7 @@ interface IBridgeEventInfo { interface IMerkleProof { merkle_proof: string[]; + rollup_merkle_proof?: string[]; exit_root_num: string; l2_exit_root_num: string; main_exit_root: string; @@ -23,6 +24,7 @@ interface IMerkleProof { interface IClaimPayload { smtProof: string[]; + smtProofRollup?: string[]; index: number; mainnetExitRoot: string; rollupExitRoot: string; @@ -107,6 +109,7 @@ export class BridgeUtil { return this.getProof_(networkId, depositCount).then(proof => { const payload = {} as IClaimPayload; payload.smtProof = proof.merkle_proof; + payload.smtProofRollup = proof.rollup_merkle_proof; payload.index = depositCount; payload.mainnetExitRoot = proof.main_exit_root; payload.rollupExitRoot = proof.rollup_exit_root; diff --git a/src/zkevm/erc20.ts b/src/zkevm/erc20.ts index 9f610fcca..d9a040b80 100644 --- a/src/zkevm/erc20.ts +++ b/src/zkevm/erc20.ts @@ -24,11 +24,11 @@ export class ERC20 extends ZkEvmToken { name: 'ERC20', bridgeType: 'zkevm' }, client, getContracts); - if(bridgeAdapterAddress) { - this.bridgeAdapter = new ZkEVMBridgeAdapter( - this.client, - bridgeAdapterAddress, - isParent + if (bridgeAdapterAddress) { + this.bridgeAdapter = new ZkEVMBridgeAdapter( + this.client, + bridgeAdapterAddress, + isParent ); } } @@ -204,12 +204,12 @@ export class ERC20 extends ZkEvmToken { ); option.value = Converter.toHex(ethGasAmount); - if (option.v && option.r && option.s){ + if (option.v && option.r && option.s) { return this.zkEVMWrapper.depositPermitWithGas( this.contractParam.address, amountInABI, userAddress, - Math.floor((Date.now() + 3600000)/1000).toString(), + Math.floor((Date.now() + 3600000) / 1000).toString(), option.v, option.r, option.s, @@ -250,7 +250,7 @@ export class ERC20 extends ZkEvmToken { this.contractParam.address, amountInABI, userAddress, - Math.floor((Date.now() + 3600000)/1000).toString(), + Math.floor((Date.now() + 3600000) / 1000).toString(), signatureParams.v, signatureParams.r, signatureParams.s, @@ -323,22 +323,39 @@ export class ERC20 extends ZkEvmToken { this.checkForChild("customERC20DepositClaim"); return this.parentBridge.networkID().then(networkId => { return this.bridgeUtil.buildPayloadForClaim( - transactionHash, true, networkId + transactionHash, true, networkId ); }).then(payload => { - return this.childBridge.claimMessage( - payload.smtProof, - payload.index, - payload.mainnetExitRoot, - payload.rollupExitRoot, - payload.originNetwork, - payload.originTokenAddress, - payload.destinationNetwork, - payload.destinationAddress, - payload.amount, - payload.metadata, - option - ); + if (payload.smtProofRollup) { + return this.childBridge.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.childBridge.claimMessage( + payload.smtProof, + payload.index, + payload.mainnetExitRoot, + payload.rollupExitRoot, + payload.originNetwork, + payload.originTokenAddress, + payload.destinationNetwork, + payload.destinationAddress, + payload.amount, + payload.metadata, + option + ); + } }); } @@ -358,19 +375,36 @@ export class ERC20 extends ZkEvmToken { transactionHash, true, networkId ); }).then(payload => { - return this.childBridge.claimAsset( - payload.smtProof, - payload.index, - payload.mainnetExitRoot, - payload.rollupExitRoot, - payload.originNetwork, - payload.originTokenAddress, - payload.destinationNetwork, - payload.destinationAddress, - payload.amount, - payload.metadata, - option - ); + if (payload.smtProofRollup) { + return this.childBridge.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.childBridge.claimAsset( + payload.smtProof, + payload.index, + payload.mainnetExitRoot, + payload.rollupExitRoot, + payload.originNetwork, + payload.originTokenAddress, + payload.destinationNetwork, + payload.destinationAddress, + payload.amount, + payload.metadata, + option + ); + } }); } @@ -438,22 +472,40 @@ export class ERC20 extends ZkEvmToken { this.checkForRoot("customERC20WithdrawExit"); return this.childBridge.networkID().then(networkId => { return this.bridgeUtil.buildPayloadForClaim( - burnTransactionHash, false, networkId + burnTransactionHash, false, networkId ); }).then(payload => { - return this.parentBridge.claimMessage( - payload.smtProof, - payload.index, - payload.mainnetExitRoot, - payload.rollupExitRoot, - payload.originNetwork, - payload.originTokenAddress, - payload.destinationNetwork, - payload.destinationAddress, - payload.amount, - payload.metadata, - option - ); + if (payload.smtProofRollup) { + return this.parentBridge.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.parentBridge.claimMessage( + payload.smtProof, + payload.index, + payload.mainnetExitRoot, + payload.rollupExitRoot, + payload.originNetwork, + payload.originTokenAddress, + payload.destinationNetwork, + payload.destinationAddress, + payload.amount, + payload.metadata, + option + ); + } + }); } @@ -506,19 +558,36 @@ export class ERC20 extends ZkEvmToken { burnTransactionHash, false, networkId ); }).then(payload => { - return this.parentBridge.claimAsset( - payload.smtProof, - payload.index, - payload.mainnetExitRoot, - payload.rollupExitRoot, - payload.originNetwork, - payload.originTokenAddress, - payload.destinationNetwork, - payload.destinationAddress, - payload.amount, - payload.metadata, - option - ); + if (payload.smtProofRollup) { + return this.parentBridge.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.parentBridge.claimAsset( + payload.smtProof, + payload.index, + payload.mainnetExitRoot, + payload.rollupExitRoot, + payload.originNetwork, + payload.originTokenAddress, + payload.destinationNetwork, + payload.destinationAddress, + payload.amount, + payload.metadata, + option + ); + } }); } @@ -631,7 +700,7 @@ export class ERC20 extends ZkEvmToken { holder: account, spender: spenderAddress, nonce, - expiry: Math.floor((Date.now() + 3600000)/1000), + expiry: Math.floor((Date.now() + 3600000) / 1000), allowed: true, }; case Permit.EIP_2612: @@ -657,7 +726,7 @@ export class ERC20 extends ZkEvmToken { spender: spenderAddress, value: amount, nonce: nonce, - deadline: Math.floor((Date.now() + 3600000)/1000), + deadline: Math.floor((Date.now() + 3600000) / 1000), }; } return typedData; @@ -718,7 +787,7 @@ export class ERC20 extends ZkEvmToken { account, spenderAddress, nonce, - Math.floor((Date.now() + 3600000)/1000), + Math.floor((Date.now() + 3600000) / 1000), true, v, r, @@ -733,7 +802,7 @@ export class ERC20 extends ZkEvmToken { account, spenderAddress, amount, - Math.floor((Date.now() + 3600000)/1000), + Math.floor((Date.now() + 3600000) / 1000), v, r, s, diff --git a/src/zkevm/zkevm_bridge.ts b/src/zkevm/zkevm_bridge.ts index e0b0e819b..d05d9c185 100644 --- a/src/zkevm/zkevm_bridge.ts +++ b/src/zkevm/zkevm_bridge.ts @@ -106,6 +106,59 @@ export class ZkEvmBridge extends BaseToken { }); } + /** + * 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} 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 ZkEvmBridge + */ + claimAssetNew( + smtProof: string[], + smtProofRollup: 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, + smtProofRollup, + index, + mainnetExitRoot, + rollupExitRoot, + originNetwork, + originTokenAddress, + destinationNetwork, + destinationAddress, + amount, + metadata + ).then(method => { + return this.processWrite( + method, + option + ); + }); + } + /** * bridge function to be called on that network from where message is to be transferred to a different network * @param {number} destinationNetwork Network at which tokens will be bridged @@ -118,18 +171,18 @@ export class ZkEvmBridge extends BaseToken { * @memberof ZkEvmBridge */ bridgeMessage( - destinationNetwork: number, - destinationAddress: string, - forceUpdateGlobalExitRoot: boolean, - permitData = '0x', - option?: ITransactionOption + destinationNetwork: number, + destinationAddress: string, + forceUpdateGlobalExitRoot: boolean, + permitData = '0x', + option?: ITransactionOption ) { return this.method( - "bridgeMessage", - destinationNetwork, - destinationAddress, - forceUpdateGlobalExitRoot, - permitData + "bridgeMessage", + destinationNetwork, + destinationAddress, + forceUpdateGlobalExitRoot, + permitData ).then(method => { return this.processWrite(method, option); }); @@ -168,21 +221,75 @@ export class ZkEvmBridge extends BaseToken { metadata: string, option: ITransactionOption) { return this.method( - "claimMessage", - smtProof, - index, - mainnetExitRoot, - rollupExitRoot, - originNetwork, - originTokenAddress, - destinationNetwork, - destinationAddress, - amount, - metadata + "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 + * Which means that the amount of ether will be transferred correctly, but the message + * 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} 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 ZkEvmBridge + */ + claimMessageNew( + smtProof: string[], + smtProofRollup: 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, + smtProofRollup, + index, + mainnetExitRoot, + rollupExitRoot, + originNetwork, + originTokenAddress, + destinationNetwork, + destinationAddress, + amount, + metadata ).then(method => { return this.processWrite( - method, - option + method, + option ); }); } @@ -215,9 +322,10 @@ export class ZkEvmBridge extends BaseToken { */ isClaimed( index: number, + sourceBridgeNetwork: number ) { return this.method( - "isClaimed", index + "isClaimed", index, sourceBridgeNetwork ).then(method => { return this.processRead(method); }); From f65d7874f5c1034f15c061df41a8d044b485b667 Mon Sep 17 00:00:00 2001 From: Nitin Mittal Date: Thu, 25 Jan 2024 15:25:15 +0400 Subject: [PATCH 2/2] upd: patch version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f14f635a9..89ee53abc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@maticnetwork/maticjs", - "version": "3.7.7", + "version": "3.7.8", "description": "Javascript developer library for interacting with Matic Network", "main": "dist/npm.export.js", "types": "dist/ts/index.d.ts",