From 8657cabc2d931e456c9b1af6ae16f6edb7c81795 Mon Sep 17 00:00:00 2001 From: petarTxFusion Date: Fri, 7 Jun 2024 13:50:45 +0200 Subject: [PATCH] refactor: `isWithdrawalFinalized` to use shared bridge --- .../io/zksync/protocol/account/WalletL1.java | 41 +++++++++++++++---- .../transaction/type/L1BridgeContracts.java | 7 ++-- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src/main/java/io/zksync/protocol/account/WalletL1.java b/src/main/java/io/zksync/protocol/account/WalletL1.java index 90fee95..f90eb5e 100644 --- a/src/main/java/io/zksync/protocol/account/WalletL1.java +++ b/src/main/java/io/zksync/protocol/account/WalletL1.java @@ -5,6 +5,7 @@ import io.zksync.methods.response.FullDepositFee; import io.zksync.methods.response.L2toL1Log; import io.zksync.methods.response.ZkTransactionReceipt; +import io.zksync.methods.response.ZksMessageProof; import io.zksync.protocol.ZkSync; import io.zksync.protocol.core.BridgeAddresses; import io.zksync.protocol.core.L2ToL1MessageProof; @@ -30,10 +31,7 @@ import org.web3j.protocol.Web3j; import org.web3j.protocol.core.*; import org.web3j.protocol.core.methods.request.Transaction; -import org.web3j.protocol.core.methods.response.EthEstimateGas; -import org.web3j.protocol.core.methods.response.EthSendTransaction; -import org.web3j.protocol.core.methods.response.Log; -import org.web3j.protocol.core.methods.response.TransactionReceipt; +import org.web3j.protocol.core.methods.response.*; import org.web3j.tx.Contract; import org.web3j.tx.RawTransactionManager; import org.web3j.tx.TransactionManager; @@ -451,7 +449,7 @@ private Request _depositBaseTokenToNonETHBasedChain(Depos IBridgehub bridgehub = getBridgehubContract(); BigInteger chainId = providerL2.ethChainId().sendAsync().join().getChainId(); String baseTokenAddress = getBaseToken().sendAsync().join(); - IL1Bridge sharedL1Bridge = getL1BridgeContracts().sharedL1Bridge; + IL1SharedBridge sharedL1Bridge = getL1BridgeContracts().sharedL1Bridge; GetDepositTransaction depositTransaction = _getDepositBaseTokenOnNonETHBasedChainTx(transaction); RequestExecuteTransaction tx = depositTransaction.requestExecuteTransaction; @@ -476,7 +474,7 @@ private Request _depositBaseTokenToNonETHBasedChain(Depos private Request _depositETHToNonETHBasedChain(DepositTransaction transaction) throws Exception { String baseTokenAddress = getBaseToken().sendAsync().join(); - IL1Bridge sharedL1Bridge = getL1BridgeContracts().sharedL1Bridge; + IL1SharedBridge sharedL1Bridge = getL1BridgeContracts().sharedL1Bridge; GetDepositTransaction depositTransaction = _getDepositETHOnNonETHBasedChainTx(transaction); Transaction tx = depositTransaction.tx; @@ -644,7 +642,7 @@ private GetDepositTransaction _getDepositBaseTokenOnNonETHBasedChainTx(DepositTr private GetDepositTransaction _getDepositETHOnNonETHBasedChainTx(DepositTransaction transaction) throws Exception { IBridgehub bridgehub = getBridgehubContract(); BigInteger chainId = providerL2.ethChainId().sendAsync().join().getChainId(); - IL1Bridge sharedBridge = getL1BridgeContracts().sharedL1Bridge; + IL1SharedBridge sharedBridge = getL1BridgeContracts().sharedL1Bridge; _getDepositTxWithDefaults(transaction); @@ -913,9 +911,36 @@ public RemoteCall getFullRequiredDepositFee(DepositTransaction t return fullFee; }); } + public CompletableFuture isWithdrawalFinalized(String txHash){ + return isWithdrawalFinalized(txHash, 0); + } + + public CompletableFuture isWithdrawalFinalized(String txHash, int index){ + ZkTransactionReceipt receipt = providerL2.zksGetTransactionReceipt(txHash).sendAsync().join().getResult(); + int logIndex = getWithdrawalLogIndex(receipt.getLogs(), index); + Log log = receipt.getLogs().get(logIndex); + + int l2ToL1LogIndex = getWithdrawalL2ToL1LogIndex(receipt.getl2ToL1Logs(), index); + String sender = Numeric.prependHexPrefix(log.getTopics().get(1).substring(26)); - public void getPriorityOpConfirmation(String txHash, int index){ + CompletableFuture proof = providerL2.zksGetL2ToL1LogProof(txHash, l2ToL1LogIndex).sendAsync(); + CompletableFuture chainId = providerL2.ethChainId().sendAsync(); + + IL1SharedBridge sharedBridge; + if (providerL2.isBaseToken(sender)){ + sharedBridge = getL1BridgeContracts().sharedL1Bridge; + }else{ + IL2SharedBridge sharedBridgeL2 = IL2SharedBridge.load(sender, providerL2, credentials, gasProvider); + String l1BridgeAddress = sharedBridgeL2.l1SharedBridge().sendAsync().join(); + + sharedBridge = IL1SharedBridge.load(l1BridgeAddress, providerL1, credentials, gasProvider); + } + + if (proof.join() == null){ + throw new Error("Log proof not found!"); + } + return sharedBridge.isWithdrawalFinalized(chainId.join().getChainId(), receipt.getL1BatchNumber(), BigInteger.valueOf(proof.join().getId())).sendAsync(); } public RemoteFunctionCall finalizeWithdraw(String txHash, int index) throws Exception { diff --git a/src/main/java/io/zksync/transaction/type/L1BridgeContracts.java b/src/main/java/io/zksync/transaction/type/L1BridgeContracts.java index 2fe11d1..c261f05 100644 --- a/src/main/java/io/zksync/transaction/type/L1BridgeContracts.java +++ b/src/main/java/io/zksync/transaction/type/L1BridgeContracts.java @@ -1,6 +1,7 @@ package io.zksync.transaction.type; import io.zksync.wrappers.IL1Bridge; +import io.zksync.wrappers.IL1SharedBridge; import org.web3j.crypto.Credentials; import org.web3j.protocol.Web3j; import org.web3j.tx.TransactionManager; @@ -9,15 +10,15 @@ public class L1BridgeContracts { public IL1Bridge erc20L1Bridge; public IL1Bridge wethL1Bridge; - public IL1Bridge sharedL1Bridge; + public IL1SharedBridge sharedL1Bridge; - public L1BridgeContracts(IL1Bridge erc20L1Bridge, IL1Bridge wethL1Bridge, IL1Bridge sharedL1Bridge) { + public L1BridgeContracts(IL1Bridge erc20L1Bridge, IL1Bridge wethL1Bridge, IL1SharedBridge sharedL1Bridge) { this.erc20L1Bridge = erc20L1Bridge; this.wethL1Bridge = wethL1Bridge; this.sharedL1Bridge = sharedL1Bridge; } public L1BridgeContracts(String erc20L1Bridge, String wethL1Bridge, String sharedL1Bridge, Web3j providerL1, TransactionManager manager, ContractGasProvider gasProvider) { - this(IL1Bridge.load(erc20L1Bridge, providerL1, manager, gasProvider), IL1Bridge.load(wethL1Bridge, providerL1, manager, gasProvider), IL1Bridge.load(sharedL1Bridge, providerL1, manager, gasProvider)); + this(IL1Bridge.load(erc20L1Bridge, providerL1, manager, gasProvider), IL1Bridge.load(wethL1Bridge, providerL1, manager, gasProvider), IL1SharedBridge.load(sharedL1Bridge, providerL1, manager, gasProvider)); } }