Java API

Provider

Web3Provider for zksync2-java

A Web3 Provider object provides application-layer access to underlying blockchain networks.

The zksync2-java library supports provider methods from the web3j library and supplies additional functionality.

ZkSync: Supplies the same functionality as Web3 and extends it with ZKsync-specific methods.

ZkSync

  • This doc details ZKsync Era specific methods.
  • Web3Swift implementations link to their docs.

allAccountBalances

Returns all balances for confirmed tokens given by an account address.

Calls the zks_getAllAccountBalances JSON-RPC method.

Inputs

ParameterTypeDescription
addressStringAccount address.

Example

import io.zksync.protocol.ZkSync;
import org.web3j.protocol.http.HttpService;
import io.zksync.methods.response.ZkTransactionReceipt;
 
public class Main {
    public static void main(String ...args) {
      ZksAccountBalances allBalances = zkSync.zksGetAllAccountBalances("0x36615Cf349d7F6344891B1e7CA7C72883F5dc049").send();
    }
}

constructor

Returns a ZkSyncClient object.

Inputs

ParameterTypeDescription
providerURLURLNetwork RPC URL (optional).

Example

import io.zksync.protocol.ZkSync;
import org.web3j.protocol.http.HttpService;
 
public class Main {
    public static void main(String ...args) {
        ZkSync zksync = ZkSync.build(new HttpService("https://sepolia.era.zksync.dev"));
    }
}

estimateFee

Returns an estimated ZksEstimateFee for requested transaction.

Inputs

ParameterTypeDescription
transactionTransactionTransaction request.
import io.zksync.protocol.ZkSync;
import org.web3j.protocol.http.HttpService;
import io.zksync.methods.response.ZkTransactionReceipt;
 
public class Main {
    public static void main(String ...args) {
      io.zksync.methods.request.Transaction estimate = io.zksync.methods.request.Transaction.createFunctionCallTransaction(
                "0x7e5f4552091a69125d5dfcb7b8c2659029395bdf",
                "0x7e5f4552091a69125d5dfcb7b8c2659029395bdf",
                BigInteger.ZERO,
                BigInteger.ZERO,
                "0x"
        );
 
      Fee fee = zksync.zksEstimateFee(estimate).send().getResult();
    }

estimateGas

Returns an estimate of the amount of gas required to submit a transaction to the network.

Inputs

ParameterTypeDescription
transactionTransactionTransaction class.

Example

import io.zksync.protocol.ZkSync;
import org.web3j.protocol.http.HttpService;
import io.zksync.methods.response.ZkTransactionReceipt;
 
public class Main {
    public static void main(String ...args) {
      io.zksync.methods.request.Transaction estimate = io.zksync.methods.request.Transaction.createFunctionCallTransaction(
                "0x7e5f4552091a69125d5dfcb7b8c2659029395bdf",
                "0x7e5f4552091a69125d5dfcb7b8c2659029395bdf",
                BigInteger.ZERO,
                BigInteger.ZERO,
                "0x"
        );
 
      BigInteger gasUsed = zksync.ethEstimateGas(estimate).send().getAmountUsed();
    }
}

estimateGasL1

Returns an estimate of the amount of gas required to submit a transaction from L1 to L2 as a EthEstimateGas object.

Calls the zks_estimateL1ToL2 JSON-RPC method.

Inputs

ParameterTypeDescription
transactionTransactionTransaction class.

Example

import io.zksync.protocol.ZkSync;
import org.web3j.protocol.http.HttpService;
import io.zksync.methods.response.ZkTransactionReceipt;
 
public class Main {
    public static void main(String ...args) {
      io.zksync.methods.request.Transaction estimate = io.zksync.methods.request.Transaction.createFunctionCallTransaction(
                "0x7e5f4552091a69125d5dfcb7b8c2659029395bdf",
                "0x7e5f4552091a69125d5dfcb7b8c2659029395bdf",
                BigInteger.ZERO,
                BigInteger.ZERO,
                "0x"
        );
 
      BigInteger estimatedGas = zksync.estimateGasL1(estimate).send().getAmountUsed();
    }
}

estimateL1ToL2Execute

Returns gas estimation for an L1 to L2 execute operation.

Inputs

ParameterTypeDescription
contractAddressStringAddress of contract.
calldatabyte[]The transaction call data.
callerStringCaller address.
l2GasLimitBigIntegerCurrent L2 gas value (optional).
l2ValueBigIntegerL2 amount (optional).
factoryDepsbyte[][]Factory deps (optional).
operatorTipBigIntegerOperator tip (optional).
gasPerPubdataByte?BigIntegerConstant representing current amount of gas per byte (optional).
refoundRecipientStringRefound address.

Example

import io.zksync.protocol.ZkSync;
import org.web3j.protocol.http.HttpService;
import io.zksync.methods.response.ZkTransactionReceipt;
 
public class Main {
    public static void main(String ...args) {
      String mainContractAddress = zksync.zksMainContract().sendAsync().join()
 
      BigInteger estimatedGas = zksync.estimateGasL1(mainContractAddress, Numeric.hexStringToByteArray("0x"), "0x36615Cf349d7F6344891B1e7CA7C72883F5dc049", null, BigInteger.
      valueOf(7_000_000_000L), null, null, null, null).
      send().
      getAmountUsed();
    }
}

getBlockDetails

Returns additional ZKsync-specific information about the L2 block.

Calls the zks_getBlockDetails JSON-RPC method.

Inputs

ParameterTypeDescription
blockNumberBigIntegerBlock number.

Example

import io.zksync.protocol.ZkSync;
import org.web3j.protocol.http.HttpService;
import io.zksync.methods.response.ZkTransactionReceipt;
 
public class Main {
    public static void main(String ...args) {
      ZksBlockDetails blockDetails = zkSync.getBlockDetails(BigInteger.valueOf(90_000)).send();
    }
}

getL1BatchBlockRange

Returns the range of blocks contained within a batch given by batch number.

Calls the zks_getL1BatchBlockRange JSON-RPC method.

Inputs

ParameterTypeDescription
l1BatchNumberBigIntegerL1 batch number.

Example

import io.zksync.protocol.ZkSync;
import org.web3j.protocol.http.HttpService;
import io.zksync.methods.response.ZkTransactionReceipt;
 
public class Main {
    public static void main(String ...args) {
        BigInteger l1BatchNumber = zkSync.getL1BatchNumber().send().getL1BatchNumber();
        BlockRange blockRange = zkSync.getL1BatchBlockRange(l1BatchNumber).send().getResult();
    }
}

getL1BatchDetails

Returns data pertaining to a given batch.

Calls the zks_getL1BatchDetails JSON-RPC method.

Inputs

ParameterTypeDescription
numberBigIntegerL1 batch number.

Example

import io.zksync.protocol.ZkSync;
import org.web3j.protocol.http.HttpService;
import io.zksync.methods.response.ZkTransactionReceipt;
 
public class Main {
    public static void main(String ...args) {
        BigInteger l1BatchNumber = zkSync.getL1BatchNumber().send().getL1BatchNumber();
        BatchDetails blockRange = zkSync.getL1BatchBlockRange(l1BatchNumber).send().getResult();
    }
}

getL1BatchNumber

Returns the latest L1 batch number.

Calls the zks_getL1BatchNumber JSON-RPC method.

Example

import io.zksync.protocol.ZkSync;
import org.web3j.protocol.http.HttpService;
import io.zksync.methods.response.ZkTransactionReceipt;
 
public class Main {
    public static void main(String ...args) {
        BigInteger l1BatchNumber = zkSync.getL1BatchNumber().send().getL1BatchNumber();
    }
}

getL2ToL1LogProof

Returns the proof for a transaction's L2 to L1 log sent via the L1Messenger system contract.

Calls the zks_getL2ToL1LogProof JSON-RPC method.

Inputs

ParameterTypeDescription
txHashStringHash of the L2 transaction the L2 to L1 log was produced within.
indexintThe index of the L2 to L1 log in the transaction (optional).

Example

import io.zksync.protocol.ZkSync;
import org.web3j.protocol.http.HttpService;
import io.zksync.methods.response.ZkTransactionReceipt;
 
public class Main {
    public static void main(String ...args) {
        // Any L2 -> L1 transaction can be used.
        // In this case, withdrawal transaction is used.
        String tx = "0x2a1c6c74b184965c0cb015aae9ea134fd96215d2e4f4979cfec12563295f610e";
        L2ToL1MessageProof result = zkSync.zksGetL2ToL1LogProof(tx, 0).sendAsync().join().getResult();
    }
}

getTransferTransaction

Returns the populated transfer transaction.

Inputs

ParameterTypeDescription
txTransferTransactionTransferTransaction class.
transactionManagerTransactionManagerL2 transaction manager.
gasProviderContractGasProviderL2 gas provider.

Example

import io.zksync.protocol.ZkSync;
import org.web3j.protocol.http.HttpService;
import io.zksync.methods.response.ZkTransactionReceipt;
 
public class Main {
    public static void main(String ...args) {
        TransferTransaction transaction = new TransferTransaction(RECEIVER, amount, signer.getAddress());
        Transaction result = zkSync.getTransferTransaction(transaction, transactionManager, gasProvider).sendAsync().join().getResult();
    }
}

Retrieve populated ETH transfer transaction using paymaster to facilitate fee payment with an ERC20 token.

import io.zksync.protocol.ZkSync;
import org.web3j.protocol.http.HttpService;
import io.zksync.methods.response.ZkTransactionReceipt;
 
public class Main {
    public static void main(String ...args) {
        BigInteger amount = BigInteger.valueOf(7_000_000_000L);
        PaymasterParams paymasterParams = new PaymasterParams(PAYMASTER, Numeric.hexStringToByteArray(FunctionEncoder.encode(Paymaster.encodeApprovalBased(TOKEN, BigInteger.ONE, new byte[] {}))));
 
        TransferTransaction transaction = new TransferTransaction(RECEIVER, amount, signer.getAddress(), paymasterParams);
        Transaction result = zkSync.getTransferTransaction(transaction, transactionManager, gasProvider).sendAsync().join().getResult();
    }
}

getWithdrawTx

Returns the populated withdrawal transaction.

Inputs

ParameterTypeDescription
txTransferTransactionTransferTransaction class.
transactionManagerTransactionManagerL2 transaction manager.
gasProviderContractGasProviderL2 gas provider.

Examples

Retrieve populated ETH withdrawal transactions.

import io.zksync.protocol.ZkSync;
import org.web3j.protocol.http.HttpService;
import io.zksync.methods.response.ZkTransactionReceipt;
 
public class Main {
    public static void main(String ...args) {
        WithdrawTransaction transaction = new WithdrawTransaction(RECEIVER, amount, signer.getAddress());
        Transaction result = zkSync.getWithdrawTx(transaction, transactionManager, gasProvider).sendAsync().join().getResult();
    }
}

Retrieve populated ETH withdrawal transaction using paymaster to facilitate fee payment with an ERC20 token.

import io.zksync.protocol.ZkSync;
import org.web3j.protocol.http.HttpService;
import io.zksync.methods.response.ZkTransactionReceipt;
 
public class Main {
    public static void main(String ...args) {
        BigInteger amount = BigInteger.valueOf(7_000_000_000L);
        PaymasterParams paymasterParams = new PaymasterParams(PAYMASTER, Numeric.hexStringToByteArray(FunctionEncoder.encode(Paymaster.encodeApprovalBased(TOKEN, BigInteger.ONE, new byte[] {}))));
 
        WithdrawTransaction transaction = new WithdrawTransaction(RECEIVER, amount, signer.getAddress(), paymasterParams);
        Transaction result = zkSync.getWithdrawTx(transaction, transactionManager, gasProvider).sendAsync().join().getResult();
    }
}

l1ChainId

Returns the chain id of the underlying L1.

Calls the zks_L1ChainId JSON-RPC method.

Example

import io.zksync.protocol.ZkSync;
import org.web3j.protocol.http.HttpService;
import io.zksync.methods.response.ZkTransactionReceipt;
 
public class Main {
    public static void main(String ...args) {
        BigInteger result = zkSync.l1ChainId().sendAsync().join().getResult();
    }
}

l1TokenAddress

Returns the L1 token address equivalent for a L2 token address as they are not equal. ETH's address is set to zero address.

Only works for tokens bridged on default ZKsync Era bridges.

Inputs

ParameterTypeDescription
tokenStringThe address of the token on L2.

Example

import io.zksync.protocol.ZkSync;
import org.web3j.protocol.http.HttpService;
import io.zksync.methods.response.ZkTransactionReceipt;
 
public class Main {
    public static void main(String ...args) {
        BigInteger result = zkSync.l1ChainId().sendAsync().join().getResult();
    }
}

mainContract

Returns the main ZKsync Era smart contract address.

Calls the zks_getMainContract JSON-RPC method.

Example

import io.zksync.protocol.ZkSync;
import org.web3j.protocol.http.HttpService;
import io.zksync.methods.response.ZkTransactionReceipt;
 
public class Main {
    public static void main(String ...args) {
        String result = zkSync.zksMainContract().sendAsync().join().getResult();
    }
}

zksGetBridgeContracts

Returns the addresses of the default ZKsync Era bridge contracts on both L1 and L2.

Example

import io.zksync.protocol.ZkSync;
import org.web3j.protocol.http.HttpService;
import io.zksync.methods.response.ZkTransactionReceipt;
 
public class Main {
    public static void main(String ...args) {
      ZksBlockDetails blockDetails = zkSync.zksGetBridgeContracts().send();
    }
}

zksGetTestnetPaymaster

Returns the testnet paymaster address if available, or null.

Example

import io.zksync.protocol.ZkSync;
import org.web3j.protocol.http.HttpService;
import io.zksync.methods.response.ZkTransactionReceipt;
 
public class Main {
    public static void main(String ...args) {
        String result = zkSync.zksGetTestnetPaymaster().sendAsync().join().getResult();
    }
}

zksGetTransactionDetails

Returns data from a specific transaction given by the transaction hash.

Calls the getTransactionDetails JSON-RPC method.

Inputs

ParameterTypeDescription
txHashBytesLikeTransaction hash.

Example

import io.zksync.protocol.ZkSync;
import org.web3j.protocol.http.HttpService;
import io.zksync.methods.response.ZkTransactionReceipt;
 
public class Main {
    public static void main(String ...args) {
        TransactionDetails result = zkSync.zksGetTransactionDetails("<TX_HASH>").sendAsync().join().getResult();
    }
}

Made with ❤️ by the ZKsync Community