Skip to content

Commit

Permalink
refactor: update contract wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
petarTxFusion committed Jun 7, 2024
1 parent a14ceef commit d61558c
Show file tree
Hide file tree
Showing 12 changed files with 294 additions and 378 deletions.
10 changes: 5 additions & 5 deletions src/main/java/io/zksync/protocol/account/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public String getAddress(){
*/
public L2BridgeContracts getL2BridgeContracts(){
BridgeAddresses bridgeAddresses = providerL2.zksGetBridgeContracts().sendAsync().join().getResult();
return new L2BridgeContracts(bridgeAddresses.getL2Erc20DefaultBridge(), bridgeAddresses.getL2WethBridge(), providerL2, transactionManager, feeProviderL2);
return new L2BridgeContracts(bridgeAddresses.l2Erc20DefaultBridge, bridgeAddresses.l2WethBridge, bridgeAddresses.l2SharedDefaultBridge, providerL2, transactionManager, feeProviderL2);
}

public CompletableFuture<BigInteger> getDeploymentNonce(){
Expand Down Expand Up @@ -295,7 +295,7 @@ public RemoteCall<TransactionReceipt> execute(String contractAddress, Function f
* @return Prepared get balance call
*/
public RemoteCall<BigInteger> getBalance() {
return getBalance(getAddress(), getBaseToken().sendAsync().join(), ZkBlockParameterName.COMMITTED);
return getBalance(getAddress(), ZkSyncAddresses.L2_BASE_TOKEN_ADDRESS, ZkBlockParameterName.COMMITTED);
}

/**
Expand Down Expand Up @@ -329,10 +329,10 @@ public RemoteCall<BigInteger> getBalance(String address, String token) {
* @return Prepared get balance call
*/
public RemoteCall<BigInteger> getBalance(String address, String token, DefaultBlockParameter at) {
if (token.equalsIgnoreCase(ZkSyncAddresses.LEGACY_ETH_ADDRESS)){
token = ZkSyncAddresses.ETH_ADDRESS_IN_CONTRACTS;
if (token.equalsIgnoreCase(ZkSyncAddresses.LEGACY_ETH_ADDRESS) || token.equalsIgnoreCase(ZkSyncAddresses.ETH_ADDRESS_IN_CONTRACTS)){
token = l2TokenAddress(ZkSyncAddresses.ETH_ADDRESS_IN_CONTRACTS);
}
if (providerL2.isBaseToken(token)) {
if (token.equalsIgnoreCase(ZkSyncAddresses.L2_BASE_TOKEN_ADDRESS)) {
return new RemoteCall<>(() ->
this.providerL2.ethGetBalance(address, at).sendAsync().join().getBalance());
} else {
Expand Down
13 changes: 5 additions & 8 deletions src/main/java/io/zksync/protocol/account/WalletL1.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
import io.zksync.transaction.type.*;
import io.zksync.utils.WalletUtils;
import io.zksync.utils.ZkSyncAddresses;
import io.zksync.wrappers.ERC20;
import io.zksync.wrappers.IBridgehub;
import io.zksync.wrappers.IL1Bridge;
import io.zksync.wrappers.IZkSync;
import io.zksync.wrappers.*;
import lombok.Getter;
import org.jetbrains.annotations.Nullable;
import org.web3j.abi.EventValues;
Expand Down Expand Up @@ -71,7 +68,7 @@ public class WalletL1 {
protected final TransactionReceiptProcessor transactionReceiptProcessorL1;
protected final ContractGasProvider gasProvider;
protected final String mainContractAddress;
protected final IZkSync contract;
protected final IZkSyncHyperchain contract;
protected final EthSigner signer;
protected final Credentials credentials;
public WalletL1(Web3j providerL1, ZkSync providerL2, TransactionManager transactionManager, ContractGasProvider gasProvider, Credentials credentials) {
Expand All @@ -80,7 +77,7 @@ public WalletL1(Web3j providerL1, ZkSync providerL2, TransactionManager transact
this.transactionManager = transactionManager;
this.gasProvider = gasProvider;
this.mainContractAddress = providerL2.zksMainContract().sendAsync().join().getResult();
this.contract = IZkSync.load(providerL2.zksMainContract().sendAsync().join().getResult(), providerL1, transactionManager, gasProvider);
this.contract = IZkSyncHyperchain.load(providerL2.zksMainContract().sendAsync().join().getResult(), providerL1, transactionManager, gasProvider);
this.credentials = credentials;
this.signer = new PrivateKeyEthSigner(credentials, providerL1.ethChainId().sendAsync().join().getChainId().longValue());
this.transactionReceiptProcessorL1 = new PollingTransactionReceiptProcessor(providerL1, DEFAULT_POLLING_FREQUENCY, DEFAULT_POLLING_ATTEMPTS_PER_TX_HASH);
Expand All @@ -100,9 +97,9 @@ public WalletL1(Web3j providerL1, ZkSync providerL2, Credentials credentials){
*
* @return IZkSync
*/
public IZkSync getMainContract(){
public IZkSyncHyperchain getMainContract(){
if (contract == null){
return IZkSync.load(providerL2.zksMainContract().sendAsync().join().getResult(), providerL1, transactionManager, gasProvider);
return IZkSyncHyperchain.load(providerL2.zksMainContract().sendAsync().join().getResult(), providerL1, transactionManager, gasProvider);
}
return contract;
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/io/zksync/protocol/core/BridgeAddresses.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
@NoArgsConstructor
@Data
public class BridgeAddresses {
private String l1Erc20DefaultBridge;
private String l2Erc20DefaultBridge;
private String l1WethBridge;
private String l2WethBridge;
private String l1SharedDefaultBridge;
private String l2SharedDefaultBridge;
public String l1Erc20DefaultBridge;
public String l2Erc20DefaultBridge;
public String l1WethBridge;
public String l2WethBridge;
public String l1SharedDefaultBridge;
public String l2SharedDefaultBridge;
}
16 changes: 0 additions & 16 deletions src/main/java/io/zksync/transaction/type/DepositTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,6 @@ public Transaction toTx(String from, String calldata){
return new Transaction(from, options.getNonce(), options.getGasPrice(), BigInteger.ZERO, bridgeAddress, options.getValue(), calldata, options.getChainId().longValue(), options.getMaxPriorityFeePerGas(), options.getMaxFeePerGas());
}

public Transaction toFunctionCallTx(String from, IL1Bridge bridge){
String data = bridge.encodeDeposit(from, tokenAddress, amount, l2GasLimit, gasPerPubdataByte, refoundRecepient, options.getValue());
return new Transaction(
from,
options.getNonce(),
options.getGasPrice(),
options.getGasLimit(),
bridge.getContractAddress(),
options.getValue(),
data,
options.getChainId().longValue(),
options.getMaxPriorityFeePerGas(),
options.getMaxFeePerGas());
}


@Override
public boolean equals(final Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@
import io.zksync.protocol.ZkSync;
import io.zksync.wrappers.IL1Bridge;
import io.zksync.wrappers.IL2Bridge;
import io.zksync.wrappers.IL2SharedBridge;
import org.web3j.protocol.Web3j;
import org.web3j.tx.TransactionManager;
import org.web3j.tx.gas.ContractGasProvider;

public class L2BridgeContracts {
public IL2Bridge erc20L2Bridge;
public IL2Bridge wethL2Bridge;
public IL2SharedBridge sharedL2Bridge;

public L2BridgeContracts(IL2Bridge erc20L1Bridge, IL2Bridge wethL1Bridge) {
public L2BridgeContracts(IL2Bridge erc20L1Bridge, IL2Bridge wethL1Bridge, IL2SharedBridge sharedL2Bridge) {
this.erc20L2Bridge = erc20L1Bridge;
this.wethL2Bridge = wethL1Bridge;
this.sharedL2Bridge = sharedL2Bridge;
}

public L2BridgeContracts(String erc20L2Bridge, String wethL2Bridge, ZkSync providerL2, TransactionManager manager, ContractGasProvider gasProvider) {
this(IL2Bridge.load(erc20L2Bridge, providerL2, manager, gasProvider), IL2Bridge.load(wethL2Bridge, providerL2, manager, gasProvider));
public L2BridgeContracts(String erc20L2Bridge, String wethL2Bridge, String sharedL2Bridge, ZkSync providerL2, TransactionManager manager, ContractGasProvider gasProvider) {
this(IL2Bridge.load(erc20L2Bridge, providerL2, manager, gasProvider), IL2Bridge.load(wethL2Bridge, providerL2, manager, gasProvider), IL2SharedBridge.load(sharedL2Bridge, providerL2, manager, gasProvider));
}
}
6 changes: 3 additions & 3 deletions src/main/java/io/zksync/wrappers/IBridgehub.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class IBridgehub extends Contract {

public static final String FUNC_CREATENEWCHAIN = "createNewChain";

public static final String FUNC_GETSTATETRANSITION = "getStateTransition";
public static final String FUNC_GETHYPERCHAIN = "getHyperchain";

public static final String FUNC_L2TRANSACTIONBASECOST = "l2TransactionBaseCost";

Expand Down Expand Up @@ -258,8 +258,8 @@ public RemoteFunctionCall<TransactionReceipt> createNewChain(BigInteger _chainId
return executeRemoteCallTransaction(function);
}

public RemoteFunctionCall<String> getStateTransition(BigInteger _chainId) {
final Function function = new Function(FUNC_GETSTATETRANSITION,
public RemoteFunctionCall<String> getHyperchain(BigInteger _chainId) {
final Function function = new Function(FUNC_GETHYPERCHAIN,
Arrays.<Type>asList(new Uint256(_chainId)),
Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {}));
return executeRemoteCallSingleValueReturn(function, String.class);
Expand Down
84 changes: 8 additions & 76 deletions src/main/java/io/zksync/wrappers/IContractDeployer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@
import org.web3j.abi.EventEncoder;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Address;
import org.web3j.abi.datatypes.Bool;
import org.web3j.abi.datatypes.DynamicBytes;
import org.web3j.abi.datatypes.DynamicStruct;
import org.web3j.abi.datatypes.Event;
import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.StaticStruct;
import org.web3j.abi.datatypes.Type;
import org.web3j.abi.datatypes.generated.Bytes32;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.abi.datatypes.generated.Uint8;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
Expand Down Expand Up @@ -52,12 +48,6 @@ public class IContractDeployer extends Contract {

public static final String FUNC_CREATEACCOUNT = "createAccount";

public static final String FUNC_EXTENDEDACCOUNTVERSION = "extendedAccountVersion";

public static final String FUNC_FORCEDEPLOYONADDRESS = "forceDeployOnAddress";

public static final String FUNC_FORCEDEPLOYONADDRESSES = "forceDeployOnAddresses";

public static final String FUNC_GETACCOUNTINFO = "getAccountInfo";

public static final String FUNC_GETNEWADDRESSCREATE = "getNewAddressCreate";
Expand Down Expand Up @@ -201,7 +191,7 @@ public RemoteFunctionCall<TransactionReceipt> create(byte[] _salt, byte[] _bytec
FUNC_CREATE,
Arrays.<Type>asList(new Bytes32(_salt),
new Bytes32(_bytecodeHash),
new DynamicBytes(_input)),
new org.web3j.abi.datatypes.DynamicBytes(_input)),
Collections.<TypeReference<?>>emptyList());
return executeRemoteCallTransaction(function, weiValue);
}
Expand All @@ -211,7 +201,7 @@ public RemoteFunctionCall<TransactionReceipt> create2(byte[] _salt, byte[] _byte
FUNC_CREATE2,
Arrays.<Type>asList(new Bytes32(_salt),
new Bytes32(_bytecodeHash),
new DynamicBytes(_input)),
new org.web3j.abi.datatypes.DynamicBytes(_input)),
Collections.<TypeReference<?>>emptyList());
return executeRemoteCallTransaction(function, weiValue);
}
Expand All @@ -221,47 +211,23 @@ public RemoteFunctionCall<TransactionReceipt> create2Account(byte[] _salt, byte[
FUNC_CREATE2ACCOUNT,
Arrays.<Type>asList(new Bytes32(_salt),
new Bytes32(_bytecodeHash),
new DynamicBytes(_input),
new org.web3j.abi.datatypes.DynamicBytes(_input),
new Uint8(_aaVersion)),
Collections.<TypeReference<?>>emptyList());
return executeRemoteCallTransaction(function, weiValue);
}

public RemoteFunctionCall<TransactionReceipt> createAccount(byte[] param0, byte[] _bytecodeHash, byte[] _input, BigInteger _aaVersion, BigInteger weiValue) {
public RemoteFunctionCall<TransactionReceipt> createAccount(byte[] _salt, byte[] _bytecodeHash, byte[] _input, BigInteger _aaVersion, BigInteger weiValue) {
final Function function = new Function(
FUNC_CREATEACCOUNT,
Arrays.<Type>asList(new Bytes32(param0),
Arrays.<Type>asList(new Bytes32(_salt),
new Bytes32(_bytecodeHash),
new DynamicBytes(_input),
new org.web3j.abi.datatypes.DynamicBytes(_input),
new Uint8(_aaVersion)),
Collections.<TypeReference<?>>emptyList());
return executeRemoteCallTransaction(function, weiValue);
}

public RemoteFunctionCall<BigInteger> extendedAccountVersion(String _address) {
final Function function = new Function(FUNC_EXTENDEDACCOUNTVERSION,
Arrays.<Type>asList(new Address(160, _address)),
Arrays.<TypeReference<?>>asList(new TypeReference<Uint8>() {}));
return executeRemoteCallSingleValueReturn(function, BigInteger.class);
}

public RemoteFunctionCall<TransactionReceipt> forceDeployOnAddress(ForceDeployment _deployment, String _sender, BigInteger weiValue) {
final Function function = new Function(
FUNC_FORCEDEPLOYONADDRESS,
Arrays.<Type>asList(_deployment,
new Address(160, _sender)),
Collections.<TypeReference<?>>emptyList());
return executeRemoteCallTransaction(function, weiValue);
}

public RemoteFunctionCall<TransactionReceipt> forceDeployOnAddresses(List<ForceDeployment> _deployments, BigInteger weiValue) {
final Function function = new Function(
FUNC_FORCEDEPLOYONADDRESSES,
Arrays.<Type>asList(new org.web3j.abi.datatypes.DynamicArray<ForceDeployment>(ForceDeployment.class, _deployments)),
Collections.<TypeReference<?>>emptyList());
return executeRemoteCallTransaction(function, weiValue);
}

public RemoteFunctionCall<AccountInfo> getAccountInfo(String _address) {
final Function function = new Function(FUNC_GETACCOUNTINFO,
Arrays.<Type>asList(new Address(160, _address)),
Expand All @@ -272,7 +238,7 @@ public RemoteFunctionCall<AccountInfo> getAccountInfo(String _address) {
public RemoteFunctionCall<String> getNewAddressCreate(String _sender, BigInteger _senderNonce) {
final Function function = new Function(FUNC_GETNEWADDRESSCREATE,
Arrays.<Type>asList(new Address(160, _sender),
new Uint256(_senderNonce)),
new org.web3j.abi.datatypes.generated.Uint256(_senderNonce)),
Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {}));
return executeRemoteCallSingleValueReturn(function, String.class);
}
Expand All @@ -282,7 +248,7 @@ public RemoteFunctionCall<String> getNewAddressCreate2(String _sender, byte[] _b
Arrays.<Type>asList(new Address(160, _sender),
new Bytes32(_bytecodeHash),
new Bytes32(_salt),
new DynamicBytes(_input)),
new org.web3j.abi.datatypes.DynamicBytes(_input)),
Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {}));
return executeRemoteCallSingleValueReturn(function, String.class);
}
Expand Down Expand Up @@ -321,40 +287,6 @@ public static IContractDeployer load(String contractAddress, Web3j web3j, Transa
return new IContractDeployer(contractAddress, web3j, transactionManager, contractGasProvider);
}

public static class ForceDeployment extends DynamicStruct {
public byte[] bytecodeHash;

public String newAddress;

public Boolean callConstructor;

public BigInteger value;

public byte[] input;

public ForceDeployment(byte[] bytecodeHash, String newAddress, Boolean callConstructor, BigInteger value, byte[] input) {
super(new Bytes32(bytecodeHash),
new Address(160, newAddress),
new Bool(callConstructor),
new Uint256(value),
new DynamicBytes(input));
this.bytecodeHash = bytecodeHash;
this.newAddress = newAddress;
this.callConstructor = callConstructor;
this.value = value;
this.input = input;
}

public ForceDeployment(Bytes32 bytecodeHash, Address newAddress, Bool callConstructor, Uint256 value, DynamicBytes input) {
super(bytecodeHash, newAddress, callConstructor, value, input);
this.bytecodeHash = bytecodeHash.getValue();
this.newAddress = newAddress.getValue();
this.callConstructor = callConstructor.getValue();
this.value = value.getValue();
this.input = input.getValue();
}
}

public static class AccountInfo extends StaticStruct {
public BigInteger supportedAAVersion;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/zksync/wrappers/IEthToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public RemoteFunctionCall<TransactionReceipt> transferFromTo(String _from, Strin
return executeRemoteCallTransaction(function);
}

public String encodeWithdraw(String _l1Receiver, BigInteger weiValue) {
public String encodeWithdraw(String _l1Receiver) {
final Function function = new Function(
FUNC_WITHDRAW,
Arrays.<Type>asList(new Address(160, _l1Receiver)),
Expand Down
Loading

0 comments on commit d61558c

Please sign in to comment.