Skip to content

Commit

Permalink
refactor: isWithdrawalFinalized to use shared bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
petarTxFusion committed Jun 7, 2024
1 parent d61558c commit 8657cab
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
41 changes: 33 additions & 8 deletions src/main/java/io/zksync/protocol/account/WalletL1.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -451,7 +449,7 @@ private Request<?, EthSendTransaction> _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;
Expand All @@ -476,7 +474,7 @@ private Request<?, EthSendTransaction> _depositBaseTokenToNonETHBasedChain(Depos

private Request<?, EthSendTransaction> _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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -913,9 +911,36 @@ public RemoteCall<FullDepositFee> getFullRequiredDepositFee(DepositTransaction t
return fullFee;
});
}
public CompletableFuture<Boolean> isWithdrawalFinalized(String txHash){
return isWithdrawalFinalized(txHash, 0);
}

public CompletableFuture<Boolean> 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<ZksMessageProof> proof = providerL2.zksGetL2ToL1LogProof(txHash, l2ToL1LogIndex).sendAsync();
CompletableFuture<EthChainId> 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<TransactionReceipt> finalizeWithdraw(String txHash, int index) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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));
}
}

0 comments on commit 8657cab

Please sign in to comment.