Skip to content

Commit

Permalink
Merge pull request #16 from zksync-sdk/feat/finalize-withdraw
Browse files Browse the repository at this point in the history
Feat/finalize withdraw
  • Loading branch information
petarTxFusion authored Jun 1, 2023
2 parents 877ca72 + 964230d commit e9a54ca
Show file tree
Hide file tree
Showing 10 changed files with 390 additions and 32 deletions.
12 changes: 9 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
id 'java-library'
id 'maven-publish'
id 'signing'
id 'io.codearte.nexus-staging' version '0.30.0'
}

group = 'io.zksync'
Expand Down Expand Up @@ -52,9 +53,9 @@ publishing {
}
developers {
developer {
id = 'mfischuk'
name = 'Maxim Fischuk'
email = '[email protected]'
id = 'matterLabs'
name = 'Matter Labs'
email = '[email protected]'
}
}
scm {
Expand All @@ -77,6 +78,11 @@ publishing {
}
}

nexusStaging {
packageGroup = "io.zksync.zksync2" //optional if packageGroup == project.getGroup()
stagingProfileId findProperty("OSSRH_STAGING_PROFILE_ID")
}


signing {
def signingKeyId = findProperty("signingKeyId")
Expand Down
51 changes: 34 additions & 17 deletions src/main/java/io/zksync/ZkSyncWallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.Hash;
import org.web3j.protocol.core.DefaultBlockParameter;
import org.web3j.protocol.core.RemoteCall;
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.exceptions.TransactionException;
import org.web3j.tx.ReadonlyTransactionManager;
Expand Down Expand Up @@ -183,28 +185,43 @@ public RemoteCall<TransactionReceipt> withdraw(String to, BigInteger amount, @Nu
String l2Bridge;
ArrayList parameters = new ArrayList();
parameters.add(new Address(to));

Transaction estimate;
if (tokenToUse.isETH()) {
Function function = new Function(
IL2Bridge.FUNC_WITHDRAW,
Arrays.asList(new Address(to)),
Collections.emptyList());

String calldata = FunctionEncoder.encode(function);
l2Bridge = L2_ETH_TOKEN_ADDRESS;

estimate = Transaction.createFunctionCallTransaction(
signer.getAddress(),
l2Bridge,
BigInteger.ZERO,
BigInteger.ZERO,
amount,
calldata
);
} else {
parameters.add(new Address(tokenToUse.getL2Address()));
parameters.add(new Uint256(amount));
Function function = new Function(
IL2Bridge.FUNC_WITHDRAW,
Arrays.asList(new Address(to),
new Address(tokenToUse.getL2Address()),
new Uint256(amount)),
Collections.emptyList());

String calldata = FunctionEncoder.encode(function);
l2Bridge = zksync.zksGetBridgeContracts().send().getResult().getL2Erc20DefaultBridge();
}

Function function = new Function(
IL2Bridge.FUNC_WITHDRAW,
parameters,
Collections.emptyList());
String calldata = FunctionEncoder.encode(function);

Transaction estimate = Transaction.createFunctionCallTransaction(
signer.getAddress(),
l2Bridge,
BigInteger.ZERO,
BigInteger.ZERO,
calldata
);
estimate = Transaction.createFunctionCallTransaction(
signer.getAddress(),
l2Bridge,
BigInteger.ZERO,
BigInteger.ZERO,
calldata
);
}

EthSendTransaction sent = estimateAndSend(estimate, nonceToUse).join();

Expand Down
107 changes: 107 additions & 0 deletions src/main/java/io/zksync/methods/response/L2toL1Log.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package io.zksync.methods.response;

import org.web3j.abi.datatypes.Address;

import java.math.BigInteger;

public class L2toL1Log {
public String BlockNumber;
public String BlockHash;
public String L1BatchNumber;
public String TransactionIndex;
public String ShardId;
public boolean IsService;
public Address Sender;
public String Key;
public String Value;
public String TxHash;
public int Index;

public String getBlockNumber() {
return BlockNumber;
}

public void setBlockNumber(String blockNumber) {
BlockNumber = blockNumber;
}

public String getBlockHash() {
return BlockHash;
}

public void setBlockHash(String blockHash) {
BlockHash = blockHash;
}

public String getL1BatchNumber() {
return L1BatchNumber;
}

public void setL1BatchNumber(String l1BatchNumber) {
L1BatchNumber = l1BatchNumber;
}

public String getTransactionIndex() {
return TransactionIndex;
}

public void setTransactionIndex(String transactionIndex) {
TransactionIndex = transactionIndex;
}

public String getShardId() {
return ShardId;
}

public void setShardId(String shardId) {
ShardId = shardId;
}

public boolean isService() {
return IsService;
}

public void setService(boolean service) {
IsService = service;
}

public Address getSender() {
return Sender;
}

public void setSender(Address sender) {
Sender = sender;
}

public String getKey() {
return Key;
}

public void setKey(String key) {
Key = key;
}

public String getValue() {
return Value;
}

public void setValue(String value) {
Value = value;
}

public String getTxHash() {
return TxHash;
}

public void setTxHash(String txHash) {
TxHash = txHash;
}

public int getIndex() {
return Index;
}

public void setIndex(int index) {
Index = index;
}
}
16 changes: 14 additions & 2 deletions src/main/java/io/zksync/methods/response/ZkTransactionReceipt.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
package io.zksync.methods.response;

import kotlin.OverloadResolutionByLambdaReturnType;
import kotlin.jvm.JvmOverloads;
import org.web3j.protocol.core.methods.response.Log;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.utils.Numeric;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class ZkTransactionReceipt extends TransactionReceipt {

private String l1BatchNumber;
private String l1BatchTxIndex;
private List<L2toL1Log> l2ToL1Logs;

public ZkTransactionReceipt() {
}
public ZkTransactionReceipt(String transactionHash, String transactionIndex, String blockHash, String blockNumber, String cumulativeGasUsed, String gasUsed, String contractAddress, String root, String status, String from, String to, List<Log> logs, String logsBloom, String revertReason, String type, String effectiveGasPrice, String l1BatchNumber, String l1BatchTxIndex) {
public ZkTransactionReceipt(String transactionHash, String transactionIndex, String blockHash, String blockNumber, String cumulativeGasUsed, String gasUsed, String contractAddress, String root, String status, String from, String to, List<Log> logs, String logsBloom, String revertReason, String type, String effectiveGasPrice, String l1BatchNumber, String l1BatchTxIndex, List<L2toL1Log> l2ToL1Logs) {
super(transactionHash, transactionIndex, blockHash, blockNumber, cumulativeGasUsed, gasUsed, contractAddress, root, status, from, to, logs, logsBloom, revertReason, type, effectiveGasPrice);
this.l1BatchNumber = l1BatchNumber;
this.l1BatchTxIndex = l1BatchTxIndex;
this.l2ToL1Logs = l2ToL1Logs;
}

public String getL1BatchNumberRaw() {
Expand All @@ -29,7 +34,6 @@ public BigInteger getL1BatchNumber() {
return Numeric.decodeQuantity(l1BatchNumber);
}


public void setL1BatchNumber(String l1BatchNumber) {
this.l1BatchNumber = l1BatchNumber;
}
Expand All @@ -46,6 +50,14 @@ public void setL1BatchTxIndex(String l1BatchTxIndex) {
this.l1BatchTxIndex = l1BatchTxIndex;
}

public void setL2ToL1Logs(List<L2toL1Log> l2ToL1Logs) {
this.l2ToL1Logs = l2ToL1Logs;
}

public List<L2toL1Log> getl2ToL1Logs() {
return l2ToL1Logs;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/io/zksync/protocol/JsonRpc2_0ZkSync.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ public Request<?, ZksMessageProof> zksGetL2ToL1MsgProof(Integer block, String se
return new Request<>("zks_getL2ToL1MsgProof", Arrays.asList(block, sender, message), web3jService, ZksMessageProof.class);
}

@Override
public Request<?, ZksMessageProof> zksGetL2ToL1LogProof(String txHash, int index) {
return new Request<>("zks_getL2ToL1LogProof", Arrays.asList(txHash, index), web3jService, ZksMessageProof.class);
}

@Override
public Request<?, EthEstimateGas> ethEstimateGas(Transaction transaction) {
return new Request<>(
Expand Down
1 change: 1 addition & 0 deletions src/main/java/io/zksync/protocol/ZkSync.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ static ZkSync build(Web3jService web3jService) {
*/
Request<?, ZksMessageProof> zksGetL2ToL1MsgProof(Integer block, String sender, String message, @Nullable Long l2LogPosition);

Request<?, ZksMessageProof> zksGetL2ToL1LogProof(String txHash, int index);
/**
* Generates and returns an estimate of how much gas is necessary to allow the transaction to complete. The transaction will not be added to the blockchain.
*
Expand Down
Loading

0 comments on commit e9a54ca

Please sign in to comment.