Skip to content

Commit

Permalink
refactor: update finalizeWithdrawal to use l1Sharedbridge
Browse files Browse the repository at this point in the history
  • Loading branch information
petarTxFusion committed Jun 7, 2024
1 parent 1912e05 commit a4be7a0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
4 changes: 1 addition & 3 deletions src/main/java/io/zksync/abi/ZkTypeEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,17 +188,15 @@ static String encodeBytes(BytesType bytesType) {
byte[] value = bytesType.getValue();
int length = value.length;
int mod = length % MAX_BYTE_LENGTH;
String val = Numeric.toHexString(value);
byte[] dest;
if (mod != 0) {
int padding = MAX_BYTE_LENGTH - mod;
dest = new byte[padding];
return Numeric.toHexStringNoPrefix(dest) + Numeric.toHexStringNoPrefix(value);
return Numeric.toHexStringNoPrefix(value) + Numeric.toHexStringNoPrefix(dest);
} else {
dest = value;
}

String x = Numeric.toHexString(value);
return Numeric.toHexStringNoPrefix(dest);
}

Expand Down
18 changes: 15 additions & 3 deletions src/main/java/io/zksync/protocol/account/WalletL1.java
Original file line number Diff line number Diff line change
Expand Up @@ -943,7 +943,7 @@ public CompletableFuture<Boolean> isWithdrawalFinalized(String txHash, int index
return sharedBridge.isWithdrawalFinalized(chainId.join().getChainId(), receipt.getL1BatchNumber(), BigInteger.valueOf(proof.join().getId())).sendAsync();
}

public RemoteFunctionCall<TransactionReceipt> finalizeWithdraw(String txHash, int index) throws Exception {
public CompletableFuture<EthSendTransaction> finalizeWithdraw(String txHash, int index) throws Exception {
ZkTransactionReceipt receipt = providerL2.zksGetTransactionReceipt(txHash).sendAsync().join().getResult();

int logIndex = getWithdrawalLogIndex(receipt.getLogs(), index);
Expand All @@ -962,9 +962,21 @@ public RemoteFunctionCall<TransactionReceipt> finalizeWithdraw(String txHash, in
merkle_proof.add(Numeric.hexStringToByteArray(l2ToL1MessageProof.getProof().get(i)));
}

IL1Bridge il1Bridge = IL1Bridge.load(getL1BridgeContracts().sharedL1Bridge.getContractAddress(), providerL1, transactionManager, gasProvider);
String il1BridgeAddress = getL1BridgeContracts().sharedL1Bridge.getContractAddress();

return il1Bridge.finalizeWithdrawal(providerL2.ethChainId().sendAsync().join().getChainId(), l1BatchNumber, BigInteger.valueOf(l2ToL1MessageProof.getId()), receipt.getL1BatchTxIndex(), bytes_data, merkle_proof);
String calldata = IL1SharedBridge.encodeFinalizeWithdrawal(
providerL2.ethChainId().sendAsync().join().getChainId(),
l1BatchNumber,
BigInteger.valueOf(l2ToL1MessageProof.getId()),
receipt.getL1BatchTxIndex(),
bytes_data,
merkle_proof
);

Transaction tx = Transaction.createEthCallTransaction(credentials.getAddress(), il1BridgeAddress, calldata);
BigInteger gas = providerL1.ethEstimateGas(tx).sendAsync().join().getAmountUsed();

return providerL1.ethSendRawTransaction(Numeric.toHexStringNoPrefix(TransactionEncoder.signMessage(RawTransaction.createTransaction(providerL1.ethGetTransactionCount(signer.getAddress(), DefaultBlockParameterName.LATEST).sendAsync().join().getTransactionCount(), providerL1.ethGasPrice().sendAsync().join().getGasPrice(), gas, il1BridgeAddress, calldata), credentials))).sendAsync();
}

public int getWithdrawalLogIndex(List<Log> logs, int index){
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/io/zksync/wrappers/IL1SharedBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import io.zksync.abi.ZkFunctionEncoder;
import org.web3j.abi.EventEncoder;
import org.web3j.abi.FunctionEncoder;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Address;
import org.web3j.abi.datatypes.Bool;
Expand Down Expand Up @@ -447,6 +450,21 @@ public RemoteFunctionCall<TransactionReceipt> depositLegacyErc20Bridge(String _m
return executeRemoteCallTransaction(function, weiValue);
}

public static String encodeFinalizeWithdrawal(BigInteger _chainId, BigInteger _l2BatchNumber, BigInteger _l2MessageIndex, BigInteger _l2TxNumberInBatch, byte[] _message, List<byte[]> _merkleProof) {
final Function function = new Function(
FUNC_FINALIZEWITHDRAWAL,
Arrays.<Type>asList(new Uint256(_chainId),
new Uint256(_l2BatchNumber),
new Uint256(_l2MessageIndex),
new org.web3j.abi.datatypes.generated.Uint16(_l2TxNumberInBatch),
new DynamicBytes(_message),
new DynamicArray<Bytes32>(
Bytes32.class,
org.web3j.abi.Utils.typeMap(_merkleProof, Bytes32.class))),
Collections.<TypeReference<?>>emptyList());
return ZkFunctionEncoder.encode(function);
}

public RemoteFunctionCall<TransactionReceipt> finalizeWithdrawal(BigInteger _chainId, BigInteger _l2BatchNumber, BigInteger _l2MessageIndex, BigInteger _l2TxNumberInBatch, byte[] _message, List<byte[]> _merkleProof) {
final Function function = new Function(
FUNC_FINALIZEWITHDRAWAL,
Expand Down
8 changes: 5 additions & 3 deletions src/test/java/io/zksync/integration/account/WalletTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,9 @@ public void testWithdrawEth() throws Exception {
TransactionReceipt result = testWallet.withdraw(transaction).sendAsync().join();
TransactionReceipt receipt = testWallet.getTransactionReceiptProcessor().waitFinalized(result.getTransactionHash());

TransactionReceipt finalizeWithdraw = testWallet.finalizeWithdraw(receipt.getTransactionHash(), 0).sendAsync().join();

assertFalse(testWallet.isWithdrawalFinalized(receipt.getTransactionHash(), 0).join());
EthSendTransaction finalizeWithdraw = testWallet.finalizeWithdraw(receipt.getTransactionHash(), 0).join();
assertNotNull(finalizeWithdraw.getResult());
BigInteger senderAfter = testWallet.getBalance().sendAsync().join();

assertNotNull(receipt);
Expand Down Expand Up @@ -476,7 +477,8 @@ public void testWithdrawErc20() throws Exception {
TransactionReceipt result = testWallet.withdraw(transaction).sendAsync().join();
TransactionReceipt receipt = testWallet.getTransactionReceiptProcessor().waitFinalized(result.getTransactionHash());

TransactionReceipt finalizeWithdraw = testWallet.finalizeWithdraw(receipt.getTransactionHash(), 0).sendAsync().join();
EthSendTransaction finalizeWithdraw = testWallet.finalizeWithdraw(receipt.getTransactionHash(), 0).join();
assertNotNull(finalizeWithdraw.getResult());
BigInteger senderAfter = testWallet.getBalance(l2DAI).sendAsync().join();

assertNotNull(receipt);
Expand Down

0 comments on commit a4be7a0

Please sign in to comment.