Providers


Providers

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

The zksync2open in new window library supports provider methods from the web3.pyopen in new window library and supplies additional functionality.

Provider

Info

init

Returns a zkSync Era Provider object.

Inputs

ParameterTypeDescription
web3string or ConnectionInfoopen in new windowNetwork RPC URL (optional)
def __init__(self, web3: "Web3"):
    super(ZkSync, self).__init__(web3)
    self.main_contract_address = None
    self.bridge_addresses = None

Example

from zksync2.module.module_builder import ZkSyncBuilder

zksync = ZkSyncBuilder.build("https://sepolia.era.zksync.dev")

estimate_fee

Returns an estimated Fee for requested transaction.

Inputs

ParameterTypeDescription
transactionTransactionTransaction request.
nonce = self.web3.zksync.get_transaction_count(self.account.address, ZkBlockParams.COMMITTED.value)
        gas_price = self.web3.zksync.gas_price

        func_call = TxFunctionCall(chain_id=self.chain_id,
                                   nonce=nonce,
                                   from_=self.account.address,
                                   to=self.account.address,
                                   gas_limit=0,
                                   gas_price=gas_price)
        estimated_fee = self.web3.zksync.zks_estimate_fee(func_call.tx)

eth_estimate_gas

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

ParameterTypeDescription
transactionTransactionTransaction request.
web3.py implementation.open in new window

zks_estimate_gas_l1_to_l2

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

Calls the zks_estimateL1ToL2 JSON-RPC method.

estimateGasTransfer

Returns the gas estimation for a transfer transaction.

Calls internal method getTransferTxopen in new window to get the transfer transaction and sends it to the eth_estimate_gas method.

Inputs

ParameterTypeDescription
transactionTransactionTransaction.
tokenAddress stringToken address (optional).
def zks_estimate_gas_transfer(self, transaction: Transaction, token_address: HexStr = ADDRESS_DEFAULT) -> int:

    if token_address is not None and not is_eth(token_address):
        transfer_params = (transaction["to"], transaction["value"])
        transaction["value"] = 0
        contract = self.contract(Web3.to_checksum_address(token_address), abi=get_erc20_abi())
        transaction["data"] = contract.encodeABI("transfer", args=transfer_params)
        transaction["nonce"] = self.get_transaction_count(transaction["from_"], ZkBlockParams.COMMITTED.value)

        return self.eth_estimate_gas(transaction)

estimateL1ToL2Execute

Returns gas estimation for an L1 to L2 execute operation.

Inputs

ParameterType
TransactionTransaction
def zks_estimate_l1_to_l2_execute(self, transaction: Transaction) -> int:
    if transaction["from"] is None:
        transaction["from"] = self.account.create().address

    return self.zks_estimate_gas_l1_to_l2(transaction)

zks_get_all_account_balances

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

Calls the zks_getAllAccountBalances JSON-RPC method.

zks_get_balance

Returns the user's balance as a int object for an (optional) block tag and (optional) token.

When block and token are not supplied, committed and ETH are the default values.

Inputs

NameDescription
addressUser's address.
block_tagBlock tag for getting the balance on. Latest committed block is default.
token_addressThe address of the token. ETH is default.
    def zks_get_balance(self, address: HexStr, block_tag = ZkBlockParams.COMMITTED.value, token_address: HexStr = None) -> int:
        if token_address is None or is_eth(token_address):
            return self.get_balance(to_checksum_address(address), block_tag)

        try:
            token = self.contract(Web3.to_checksum_address(token_address), abi=get_erc20_abi())
            return token.functions.balanceOf(address).call()
        except:
            return 0

Example

zksync_web3 = ZkSyncBuilder.build("https://sepolia.era.zksync.dev")

#Find the USDC ADDRESS in https://zksync2-testnet.zkscan.io/address/0x0faF6df7054946141266420b43783387A78d82A9/transactions
USDC_L2_ADDRESS = "<USDC_L2_ADDRESS>"
# Get the USDC balance from your account using your address. Get your address from https://zksync2-testnet.zkscan.io/txs
usdc_balance = zksync_web3.zksync.zks_get_balance("<YOUR_ADDRESS>", "latest", USDC_L2_ADDRESS)

// Getting ETH balance
eth_balance = zksync_web3.zksync.zks_get_balance("<YOUR_ADDRESS>")

get_block

Returns block from the network.Throws BlockNotFound error if the block is not found

web3.py implementation.open in new window

zks_get_block_details

Returns additional zkSync-specific information about the L2 block.

Calls the zks_getBlockDetails JSON-RPC method.

getContractAccountInfo

Returns the version of the supported account abstraction and nonce ordering from a given contract address.

Inputs

NameDescription
addressContract address
def get_contract_account_info(self, address: HexStr) -> ContractAccountInfo:
    deployer = self.contract(address=Web3.to_checksum_address(ZkSyncAddresses.CONTRACT_DEPLOYER_ADDRESS.value), abi=icontract_deployer_abi_default())

    data = deployer.functions.getAccountInfo(Web3.to_checksum_address(address)).call()

    return ContractAccountInfo(account_abstraction_version=data[0],account_nonce_ordering=data[1])

zks_get_bridge_contracts

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

def zks_get_bridge_contracts(self) -> BridgeAddresses:
    if self.bridge_addresses is None:
        self.bridge_addresses = self._zks_get_bridge_contracts()
    return self.bridge_addresses

getL1BatchBlockRange

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

Calls the zks_getL1BatchBlockRange JSON-RPC method.

def zks_get_l1_batch_block_range(self, l1_batch_number: int) -> BlockRange:
    return self._zks_get_l1_batch_block_range(l1_batch_number)

zks_get_l1_batch_details

Returns data pertaining to a given batch.

Calls the zks_getL1BatchDetails JSON-RPC method.

zks_l1_batch_number

Returns the latest L1 batch number.

Calls the zks_getL1BatchNumber JSON-RPC method.

getL2TransactionFromPriorityOp

Returns a transaction object from a given Ethers TransactionResponseopen in new window object.

Inputs

NameDescription
tx_receiptTransaction receipt.
main_contractZkSync main contract
def get_l2_transaction_from_priority_op(self, tx_receipt, main_contract: Contract):
    l2_hash = self.get_l2_hash_from_priority_op(tx_receipt, main_contract)
    self.wait_for_transaction_receipt(l2_hash)
    return self.get_transaction(l2_hash)

zks_get_log_proof

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

Calls the zks_getL2ToL1LogProof JSON-RPC method.

zks_main_contract

Returns the main zkSync Era smart contract address.

Calls the zks_getMainContract JSON-RPC method.

def zks_main_contract(self) -> HexStr:
    if self.main_contract_address is None:
        self.main_contract_address = self._zks_main_contract()
    return self.main_contract_address

zks_get_l2_to_l1_msg_proof

Returns the proof for a message sent via the L1Messenger system contract.

Calls the zks_getL2ToL1MsgProof JSON-RPC method.

def zks_get_l2_to_l1_msg_proof(self,
                               block: int,
                               sender: HexStr,
                               message: str,
                               l2log_pos: Optional[int]) -> ZksMessageProof:
    return self._zks_get_l2_to_l1_msg_proof(block, sender, message, l2log_pos)

zks_get_testnet_paymaster_address

Returns the testnet paymaster address if available, or null.

def zks_get_testnet_paymaster_address(self) -> HexStr:
    return Web3.to_checksum_address(self._zks_get_testnet_paymaster_address())

zks_get_transaction_details

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

Calls the getTransactionDetails JSON-RPC method.

eth_get_transaction_receipt

Returns the transaction receipt from a given hash number.

zks_l1_chain_id

Returns the chain id of the underlying L1.

Calls the zks_L1ChainId JSON-RPC method.

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.

Note

Only works for tokens bridged on default zkSync Era bridges.

Inputs

NameDescription
tokenThe address of the token on L2.
def l1_token_address(self, token: HexStr) -> HexStr:
    if is_eth(token):
        return ADDRESS_DEFAULT
    bridge_address = self.zks_get_bridge_contracts()
    l2_weth_bridge = self.contract(Web3.to_checksum_address(bridge_address.weth_bridge_l2),
                                   abi=l2_bridge_abi_default())
    try:
        l1_weth_token = l2_weth_bridge.functions.l1TokenAddress(token).call()
        if not is_eth(l1_weth_token):
            return l1_weth_token
        except:
            pass

    erc20_bridge = self.contract(Web3.to_checksum_address(bridge_address.erc20_l2_default_bridge),
                                     abi=l2_bridge_abi_default())

    return erc20_bridge.functions.l1TokenAddress(token).call()

l2TokenAddress

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

Note

Only works for tokens bridged on default zkSync Era bridges.

Inputs

NameDescription
tokenThe address of the token on L1.
def l2_token_address(self, token: HexStr) -> HexStr:
    if is_eth(token):
        return ADDRESS_DEFAULT
    bridge_address = self.zks_get_bridge_contracts()
    l2_weth_bridge = self.contract(Web3.to_checksum_address(bridge_address.weth_bridge_l2),
                                   abi=l2_bridge_abi_default())
    try:
        l1_weth_token = l2_weth_bridge.functions.l2TokenAddress(token).call()
        if not is_eth(l1_weth_token):
            return l1_weth_token
    except:
        pass

    erc20_bridge = self.contract(Web3.to_checksum_address(bridge_address.erc20_l2_default_bridge),
    abi=l2_bridge_abi_default())

    return erc20_bridge.functions.l2TokenAddress(token).call()