Skip to content

Commit

Permalink
refactor: use L2_BASE_TOKEN_ADDRESS as a address when working with ba…
Browse files Browse the repository at this point in the history
…se token
  • Loading branch information
petarTxFusion committed Jun 7, 2024
1 parent c0858a7 commit a14ceef
Show file tree
Hide file tree
Showing 3 changed files with 500 additions and 303 deletions.
33 changes: 7 additions & 26 deletions src/main/java/io/zksync/protocol/JsonRpc2_0ZkSync.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,30 +218,19 @@ public Request<?, EthEstimateGas> estimateL1ToL2Execute(String contractAddress,
}

public Transaction getWithdrawTransaction(WithdrawTransaction tx, ContractGasProvider gasProvider, TransactionManager transactionManager) throws Exception {
boolean isEthBasedChain = isEthBasedChain();

if (tx.tokenAddress != null &&
!tx.tokenAddress.isEmpty() &&
tx.tokenAddress.equalsIgnoreCase(ZkSyncAddresses.LEGACY_ETH_ADDRESS) &&
!isEthBasedChain){
tx.tokenAddress = tx.tokenAddress == null ? ZkSyncAddresses.L2_BASE_TOKEN_ADDRESS : tx.tokenAddress;
if (tx.tokenAddress.equalsIgnoreCase(ZkSyncAddresses.LEGACY_ETH_ADDRESS) || tx.tokenAddress.equalsIgnoreCase(ZkSyncAddresses.ETH_ADDRESS_IN_CONTRACTS)){
tx.tokenAddress = l2TokenAddress(ZkSyncAddresses.ETH_ADDRESS_IN_CONTRACTS);
} else if (tx.tokenAddress == null ||
tx.tokenAddress.isEmpty() ||
isBaseToken(tx.tokenAddress)){
tx.tokenAddress = ZkSyncAddresses.L2_BASE_TOKEN_ADDRESS;
}

if (tx.tokenAddress.equalsIgnoreCase(ZkSyncAddresses.LEGACY_ETH_ADDRESS)){
tx.tokenAddress = ZkSyncAddresses.ETH_ADDRESS_IN_CONTRACTS;
}
if (tx.from == null && tx.to == null){
throw new Error("Withdrawal target address is undefined!");
}

tx.to = tx.to == null ? tx.from : tx.to;
tx.options = tx.options == null ? new TransactionOptions() : tx.options;

if (ZkSyncAddresses.isEth(tx.tokenAddress)){
if (tx.tokenAddress.equalsIgnoreCase(ZkSyncAddresses.L2_BASE_TOKEN_ADDRESS)){
if (tx.options.getValue() == null){
tx.options.setValue(tx.amount);
}
Expand All @@ -253,7 +242,7 @@ public Transaction getWithdrawTransaction(WithdrawTransaction tx, ContractGasPro
}

IEthToken ethL2Token = IEthToken.load(ZkSyncAddresses.L2_ETH_TOKEN_ADDRESS, this, transactionManager, gasProvider);
String data = ethL2Token.encodeWithdraw(tx.to, passedValue);
String data = ethL2Token.encodeWithdraw(tx.to);
Eip712Meta meta = new Eip712Meta(BigInteger.valueOf(50000), null, null, tx.paymasterParams);

return new Transaction(tx.from, ZkSyncAddresses.L2_ETH_TOKEN_ADDRESS, BigInteger.ZERO, BigInteger.ZERO, tx.amount, data, meta);
Expand All @@ -275,17 +264,9 @@ public Transaction getWithdrawTransaction(WithdrawTransaction tx, ContractGasPro
}

public Transaction getTransferTransaction(TransferTransaction tx, TransactionManager transactionManager, ContractGasProvider gasProvider){
boolean isEthBasedChain = isEthBasedChain();

if (tx.tokenAddress != null &&
!tx.tokenAddress.isEmpty() &&
tx.tokenAddress.equalsIgnoreCase(ZkSyncAddresses.LEGACY_ETH_ADDRESS) &&
!isEthBasedChain){
tx.tokenAddress = tx.tokenAddress == null ? ZkSyncAddresses.L2_BASE_TOKEN_ADDRESS : tx.tokenAddress;
if (tx.tokenAddress.equalsIgnoreCase(ZkSyncAddresses.LEGACY_ETH_ADDRESS) || tx.tokenAddress.equalsIgnoreCase(ZkSyncAddresses.ETH_ADDRESS_IN_CONTRACTS)){
tx.tokenAddress = l2TokenAddress(ZkSyncAddresses.ETH_ADDRESS_IN_CONTRACTS);
} else if (tx.tokenAddress == null ||
tx.tokenAddress.isEmpty() ||
isBaseToken(tx.tokenAddress)){
tx.tokenAddress = ZkSyncAddresses.L2_BASE_TOKEN_ADDRESS;
}

tx.to = tx.to == null ? tx.from : tx.to;
Expand All @@ -299,7 +280,7 @@ public Transaction getTransferTransaction(TransferTransaction tx, TransactionMan
}
Eip712Meta meta = new Eip712Meta(tx.gasPerPubData, null, null, tx.paymasterParams);

if (tx.tokenAddress == null || tx.tokenAddress == ZkSyncAddresses.LEGACY_ETH_ADDRESS || isBaseToken(tx.tokenAddress)){
if (tx.tokenAddress.equalsIgnoreCase(ZkSyncAddresses.L2_BASE_TOKEN_ADDRESS)){
return new Transaction(tx.from, tx.to, BigInteger.ZERO, gasPrice, tx.amount, "0x", meta);
}
ERC20 token = ERC20.load(tx.tokenAddress, this, transactionManager, gasProvider);
Expand Down
238 changes: 238 additions & 0 deletions src/main/java/io/zksync/wrappers/IL2SharedBridge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
package io.zksync.wrappers;

import io.reactivex.Flowable;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.web3j.abi.EventEncoder;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Address;
import org.web3j.abi.datatypes.Event;
import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.Type;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameter;
import org.web3j.protocol.core.RemoteFunctionCall;
import org.web3j.protocol.core.methods.request.EthFilter;
import org.web3j.protocol.core.methods.response.BaseEventResponse;
import org.web3j.protocol.core.methods.response.Log;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.tx.Contract;
import org.web3j.tx.TransactionManager;
import org.web3j.tx.gas.ContractGasProvider;

/**
* <p>Auto generated code.
* <p><strong>Do not modify!</strong>
* <p>Please use the <a href="https://docs.web3j.io/command_line.html">web3j command line tools</a>,
* or the org.web3j.codegen.SolidityFunctionWrapperGenerator in the
* <a href="https://github.com/web3j/web3j/tree/master/codegen">codegen module</a> to update.
*
* <p>Generated with web3j version 1.5.0.
*/
@SuppressWarnings("rawtypes")
public class IL2SharedBridge extends Contract {
public static final String BINARY = "Bin file was not provided";

public static final String FUNC_FINALIZEDEPOSIT = "finalizeDeposit";

public static final String FUNC_L1BRIDGE = "l1Bridge";

public static final String FUNC_L1SHAREDBRIDGE = "l1SharedBridge";

public static final String FUNC_L1TOKENADDRESS = "l1TokenAddress";

public static final String FUNC_L2TOKENADDRESS = "l2TokenAddress";

public static final String FUNC_WITHDRAW = "withdraw";

public static final Event FINALIZEDEPOSIT_EVENT = new Event("FinalizeDeposit",
Arrays.<TypeReference<?>>asList(new TypeReference<Address>(true) {}, new TypeReference<Address>(true) {}, new TypeReference<Address>(true) {}, new TypeReference<Uint256>() {}));
;

public static final Event WITHDRAWALINITIATED_EVENT = new Event("WithdrawalInitiated",
Arrays.<TypeReference<?>>asList(new TypeReference<Address>(true) {}, new TypeReference<Address>(true) {}, new TypeReference<Address>(true) {}, new TypeReference<Uint256>() {}));
;

@Deprecated
protected IL2SharedBridge(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit);
}

protected IL2SharedBridge(String contractAddress, Web3j web3j, Credentials credentials, ContractGasProvider contractGasProvider) {
super(BINARY, contractAddress, web3j, credentials, contractGasProvider);
}

@Deprecated
protected IL2SharedBridge(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
super(BINARY, contractAddress, web3j, transactionManager, gasPrice, gasLimit);
}

protected IL2SharedBridge(String contractAddress, Web3j web3j, TransactionManager transactionManager, ContractGasProvider contractGasProvider) {
super(BINARY, contractAddress, web3j, transactionManager, contractGasProvider);
}

public static List<FinalizeDepositEventResponse> getFinalizeDepositEvents(TransactionReceipt transactionReceipt) {
List<EventValuesWithLog> valueList = staticExtractEventParametersWithLog(FINALIZEDEPOSIT_EVENT, transactionReceipt);
ArrayList<FinalizeDepositEventResponse> responses = new ArrayList<FinalizeDepositEventResponse>(valueList.size());
for (EventValuesWithLog eventValues : valueList) {
FinalizeDepositEventResponse typedResponse = new FinalizeDepositEventResponse();
typedResponse.log = eventValues.getLog();
typedResponse.l1Sender = (String) eventValues.getIndexedValues().get(0).getValue();
typedResponse.l2Receiver = (String) eventValues.getIndexedValues().get(1).getValue();
typedResponse.l2Token = (String) eventValues.getIndexedValues().get(2).getValue();
typedResponse.amount = (BigInteger) eventValues.getNonIndexedValues().get(0).getValue();
responses.add(typedResponse);
}
return responses;
}

public static FinalizeDepositEventResponse getFinalizeDepositEventFromLog(Log log) {
EventValuesWithLog eventValues = staticExtractEventParametersWithLog(FINALIZEDEPOSIT_EVENT, log);
FinalizeDepositEventResponse typedResponse = new FinalizeDepositEventResponse();
typedResponse.log = log;
typedResponse.l1Sender = (String) eventValues.getIndexedValues().get(0).getValue();
typedResponse.l2Receiver = (String) eventValues.getIndexedValues().get(1).getValue();
typedResponse.l2Token = (String) eventValues.getIndexedValues().get(2).getValue();
typedResponse.amount = (BigInteger) eventValues.getNonIndexedValues().get(0).getValue();
return typedResponse;
}

public Flowable<FinalizeDepositEventResponse> finalizeDepositEventFlowable(EthFilter filter) {
return web3j.ethLogFlowable(filter).map(log -> getFinalizeDepositEventFromLog(log));
}

public Flowable<FinalizeDepositEventResponse> finalizeDepositEventFlowable(DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) {
EthFilter filter = new EthFilter(startBlock, endBlock, getContractAddress());
filter.addSingleTopic(EventEncoder.encode(FINALIZEDEPOSIT_EVENT));
return finalizeDepositEventFlowable(filter);
}

public static List<WithdrawalInitiatedEventResponse> getWithdrawalInitiatedEvents(TransactionReceipt transactionReceipt) {
List<EventValuesWithLog> valueList = staticExtractEventParametersWithLog(WITHDRAWALINITIATED_EVENT, transactionReceipt);
ArrayList<WithdrawalInitiatedEventResponse> responses = new ArrayList<WithdrawalInitiatedEventResponse>(valueList.size());
for (EventValuesWithLog eventValues : valueList) {
WithdrawalInitiatedEventResponse typedResponse = new WithdrawalInitiatedEventResponse();
typedResponse.log = eventValues.getLog();
typedResponse.l2Sender = (String) eventValues.getIndexedValues().get(0).getValue();
typedResponse.l1Receiver = (String) eventValues.getIndexedValues().get(1).getValue();
typedResponse.l2Token = (String) eventValues.getIndexedValues().get(2).getValue();
typedResponse.amount = (BigInteger) eventValues.getNonIndexedValues().get(0).getValue();
responses.add(typedResponse);
}
return responses;
}

public static WithdrawalInitiatedEventResponse getWithdrawalInitiatedEventFromLog(Log log) {
EventValuesWithLog eventValues = staticExtractEventParametersWithLog(WITHDRAWALINITIATED_EVENT, log);
WithdrawalInitiatedEventResponse typedResponse = new WithdrawalInitiatedEventResponse();
typedResponse.log = log;
typedResponse.l2Sender = (String) eventValues.getIndexedValues().get(0).getValue();
typedResponse.l1Receiver = (String) eventValues.getIndexedValues().get(1).getValue();
typedResponse.l2Token = (String) eventValues.getIndexedValues().get(2).getValue();
typedResponse.amount = (BigInteger) eventValues.getNonIndexedValues().get(0).getValue();
return typedResponse;
}

public Flowable<WithdrawalInitiatedEventResponse> withdrawalInitiatedEventFlowable(EthFilter filter) {
return web3j.ethLogFlowable(filter).map(log -> getWithdrawalInitiatedEventFromLog(log));
}

public Flowable<WithdrawalInitiatedEventResponse> withdrawalInitiatedEventFlowable(DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) {
EthFilter filter = new EthFilter(startBlock, endBlock, getContractAddress());
filter.addSingleTopic(EventEncoder.encode(WITHDRAWALINITIATED_EVENT));
return withdrawalInitiatedEventFlowable(filter);
}

public RemoteFunctionCall<TransactionReceipt> finalizeDeposit(String _l1Sender, String _l2Receiver, String _l1Token, BigInteger _amount, byte[] _data) {
final Function function = new Function(
FUNC_FINALIZEDEPOSIT,
Arrays.<Type>asList(new Address(160, _l1Sender),
new Address(160, _l2Receiver),
new Address(160, _l1Token),
new Uint256(_amount),
new org.web3j.abi.datatypes.DynamicBytes(_data)),
Collections.<TypeReference<?>>emptyList());
return executeRemoteCallTransaction(function);
}

public RemoteFunctionCall<String> l1Bridge() {
final Function function = new Function(FUNC_L1BRIDGE,
Arrays.<Type>asList(),
Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {}));
return executeRemoteCallSingleValueReturn(function, String.class);
}

public RemoteFunctionCall<String> l1SharedBridge() {
final Function function = new Function(FUNC_L1SHAREDBRIDGE,
Arrays.<Type>asList(),
Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {}));
return executeRemoteCallSingleValueReturn(function, String.class);
}

public RemoteFunctionCall<String> l1TokenAddress(String _l2Token) {
final Function function = new Function(FUNC_L1TOKENADDRESS,
Arrays.<Type>asList(new Address(160, _l2Token)),
Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {}));
return executeRemoteCallSingleValueReturn(function, String.class);
}

public RemoteFunctionCall<String> l2TokenAddress(String _l1Token) {
final Function function = new Function(FUNC_L2TOKENADDRESS,
Arrays.<Type>asList(new Address(160, _l1Token)),
Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {}));
return executeRemoteCallSingleValueReturn(function, String.class);
}

public RemoteFunctionCall<TransactionReceipt> withdraw(String _l1Receiver, String _l2Token, BigInteger _amount) {
final Function function = new Function(
FUNC_WITHDRAW,
Arrays.<Type>asList(new Address(160, _l1Receiver),
new Address(160, _l2Token),
new Uint256(_amount)),
Collections.<TypeReference<?>>emptyList());
return executeRemoteCallTransaction(function);
}

@Deprecated
public static IL2SharedBridge load(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
return new IL2SharedBridge(contractAddress, web3j, credentials, gasPrice, gasLimit);
}

@Deprecated
public static IL2SharedBridge load(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
return new IL2SharedBridge(contractAddress, web3j, transactionManager, gasPrice, gasLimit);
}

public static IL2SharedBridge load(String contractAddress, Web3j web3j, Credentials credentials, ContractGasProvider contractGasProvider) {
return new IL2SharedBridge(contractAddress, web3j, credentials, contractGasProvider);
}

public static IL2SharedBridge load(String contractAddress, Web3j web3j, TransactionManager transactionManager, ContractGasProvider contractGasProvider) {
return new IL2SharedBridge(contractAddress, web3j, transactionManager, contractGasProvider);
}

public static class FinalizeDepositEventResponse extends BaseEventResponse {
public String l1Sender;

public String l2Receiver;

public String l2Token;

public BigInteger amount;
}

public static class WithdrawalInitiatedEventResponse extends BaseEventResponse {
public String l2Sender;

public String l1Receiver;

public String l2Token;

public BigInteger amount;
}
}
Loading

0 comments on commit a14ceef

Please sign in to comment.