Providers


Providers

Providers are objects that wrap interactions with the zkSync Era node. If you are new to the concept of providers in web3, check out the Web3j docsopen in new window.

zkSync Era fully supports the Ethereum Web3 JSON-RPC API, so you can use the provider objects from web3j. However, zkSync API provides some additional JSON-RPC methods, which allow:

  • Easy tracking of L1<->L2 transactions.
  • Different stages of finality for transactions. By default, the RPC returns information about the last state processed by the server, but some use cases may require tracking "finalized" transactions only.

Initialize

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://testnet.era.zksync.dev"));
    }
}

Inputs and outputs

NameDescription
web3jServiceURL of the zkSync Era operator node.
returnsProvider object.

zksGetAllAccountBalances

Get all known balances for a given account.

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://testnet.era.zksync.dev"));
        ZksAccountBalances response = zksync.zksGetAllAccountBalances("ADDRESS").send();
    }
}

Inputs and outputs

NameDescription
addressThe user address with balances to check.
returns'ZksAccounteBalances': List of all balances where account has non-zero value.

zksGetConfirmedTokens

Get list of the tokens supported by ZkSync Era. The tokens are returned in alphabetical order by their symbol. This means that the token id is its position in an alphabetically sorted array of tokens.

Example:

import io.zksync.protocol.ZkSync;
import org.web3j.protocol.http.HttpService;

public class Main {
    public static void main(String ...args) {
      ZksTokens response = zksync.zksGetConfirmedTokens(offset, limit).send();    }
}

Inputs and outputs

NameDescription
fromOffset of tokens.
listLimit of amount of tokens to return.
returnsPrepared get confirmed tokens request.

zksGetBridgeContracts

Get default bridges addresses deployed on L1 and L2.

Example:

import io.zksync.protocol.ZkSync;
import org.web3j.protocol.http.HttpService;
import io.zksync.protocol.core.BridgeAddresses;

public class Main {
    public static void main(String ...args) {
      BridgeAddresses bridgeAddresses = zksync.zksGetBridgeContracts().sendAsync().join().getResult();
}

Inputs and outputs

NameDescription
fromOffset of tokens.
listLimit of amount of tokens to return.
returnsPrepared get bridge contract request.

zksMainContract

Get address of main contract for current network chain.

Example:

import io.zksync.protocol.ZkSync;
import org.web3j.protocol.http.HttpService;

public class Main {
    public static void main(String ...args) {
      String mainContract = zksync.zksMainContract().sendAsync().join().getResult();
}

Inputs and outputs

NameDescription
returnsPrepared main contract request.

zksGetTestnetPaymaster

Get the address of the testnet paymaster; i.e. the paymaster that is available on testnets and enables paying fees in ERC-20 compatible tokens.

Example:

import io.zksync.protocol.ZkSync;
import org.web3j.protocol.http.HttpService;
import io.zksync.methods.response.ZksTestnetPaymasterAddress;

public class Main {
    public static void main(String ...args) {
      ZksTestnetPaymasterAddress response = zksync.zksGetTestnetPaymaster().send();
}

Inputs and outputs

NameDescription
returnsPrepared get paymaster request.

zksGetTokenPrice

Get price of the token in USD.

Example:

import io.zksync.protocol.ZkSync;
import org.web3j.protocol.http.HttpService;

public class Main {
    public static void main(String ...args) {
      ZksTokenPrice response = zksync.zksGetTokenPrice(ETH.getL2Address()).send();
}

Inputs and outputs

NameDescription
tokenAddressAddress of the token in hex.
returnsPrepared get token price request.

zksGetTransactionReceipt

Get transaction receipt. The same as eth_getTransactionReceipt but with additional fields belong to L2toL1Log

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) {
      ZkTransactionReceipt zkReceipt = wallet.getZksync().zksGetTransactionReceipt("TRANSACTION_HASH").send().getTransactionReceipt().get();
}

Inputs and outputs

NameDescription
transactionHashHash of the sent message's executed transaction hash.
returnsPrepared get transaction receipt request.

zksGetTransactionDetails

Get transaction details.

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) {
      ZksGetTransactionDetails response = zksync.zksGetTransactionDetails("TRANSACTION_HASH").send();
}

Inputs and outputs

NameDescription
transactionHashHash of the sent message's executed transaction hash.
returnsPrepared get transaction details request.

zksGetTransactionByHash

Get transaction details.

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) {
      ZksGetTransactionDetails response = zksync.zksGetTransactionDetails("TRANSACTION_HASH").send();
}

Inputs and outputs

NameDescription
transactionHashHash of the sent message's executed transaction hash.
returnsPrepared get transaction details request.

zksEstimateFee

Estimate fee for the given transaction at the latest committed block time.

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) {
      Transaction forEstimate;

      Fee fee = zksync.zksEstimateFee(forEstimate).send().getResult();
    }

Inputs and outputs

NameDescription
transactionTransaction data for estimation.
returnsPrepared estimate fee request.

zksL1ChainId

Get chain identifier of the L1 chain.

Example:

import io.zksync.protocol.ZkSync;
import org.web3j.protocol.http.HttpService;

public class Main {
    public static void main(String ...args) {
      ZksL1ChainId response = zksync.zksL1ChainId().send();
    }

Inputs and outputs

NameDescription
returnsPrepared L1 chain id request.

zksGetL2ToL1MsgProof

Get the proof for the message sent via the L1Messenger system contract.

Example:

import io.zksync.protocol.ZkSync;
import org.web3j.protocol.http.HttpService;

public class Main {
    public static void main(String ...args) {
      ZksL1ChainId response = zksync.zksL1ChainId().send();
    }

Inputs and outputs

NameDescription
blockThe number of the block where the message was emitted.
senderThe sender of the message (i.e. the account that called the L1Messenger system contract).
messageThe keccak256 hash of the message that was sent.
l2LogPositionThe index in the block of the event that was emitted by the L1Messenger when submitting this message. If it is omitted, the proof for the first message with such content will be returned.
returnsPrepared L1 chain id request.

ethEstimateGas

Generates and returns an estimate of how much gas is required to allow the transaction to complete. The transaction will not be added to the blockchain.

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) {
      Transaction forEstimate;

      BigInteger gasUsed = zksync.ethEstimateGas(forEstimate).send().getAmountUsed();
    }

Inputs and outputs

NameDescription
transactionTransaction data for estimation.
returnsPrepared estimate gas request.

zksGetBlockByHash

Get block by hash.

Example:

import io.zksync.protocol.ZkSync;
import org.web3j.protocol.http.HttpService;

public class Main {
    public static void main(String... args) {
      ZksBlock response = zksync.zksGetBlockByHash("BLOCK_HASH", true).send();
    }
}

Inputs and outputs

NameDescription
blockHashHash of a block.
returnFullTransactionObjectsIf true it returns the full transaction objects. If false only the hashes of the transactions.
returnsPrepared get transaction receipt request.