Go API

Common Utilities

Utilities for building on ZKsync Era

Provides essential utilities for building on ZKsync Era. These utilities include constants, useful addresses, and functions that facilitate the development and interaction with the ZKsync blockchain.

Constants

L1 to L2 alias offset

Used for applying and undoing aliases on contract addresses during bridging from L1 to L2.

L1ToL2AliasOffset := common.HexToAddress("0x1111000000000000000000000000000000001111")

Useful addresses

ETH token on L1

EthAddress := common.HexToAddress("0x0000000000000000000000000000000000000000")

ETH token alias on ZkSync Era

L2EthTokenAddress := common.HexToAddress("0x000000000000000000000000000000000000800a")

Bootloader

BootloaderFormalAddress := common.HexToAddress("0x0000000000000000000000000000000000008001")

Contract deployer

ContractDeployerAddress := common.HexToAddress("0x0000000000000000000000000000000000008006")

L1 messenger

L1MessengerAddress := common.HexToAddress("0x0000000000000000000000000000000000008008")

Nonce holder

NonceHolderAddress := common.HexToAddress("0x0000000000000000000000000000000000008003")

Gas

DefaultGasPerPubdataLimit

  • Use a large amount of gas per pubdata for signing on layer 2.
  • The amount ensures any reasonable limit is accepted.
The operator is NOT required to use the actual value and can use any value up to that signed by the user.
DefaultGasPerPubdataLimit := big.NewInt(50_000)

RequiredL1ToL2GasPerPubdataLimit

The current required gas per pubdata for L1->L2 transactions.

RequiredL1ToL2GasPerPubdataLimit := big.NewInt(800)

Functions

ApplyL1ToL2Alias

Converts the address that submitted a transaction to the inbox on L1 to the msg.sender viewed on L2.

Inputs

ParameterTypeDescription
addresscommon.AddressContract address.

Outputs

Returns the msg.sender of the L1->L2 transaction as the common.Address of the contract that initiated the transaction.

More info
  1. During a normal transaction, if contract A calls contract B, the msg.sender is A.
  2. During L1->L2 communication, if an EOA X calls contract B, the msg.sender is X.
  3. During L1->L2 communication, if a contract A calls contract B, the msg.sender is ApplyL1ToL2Alias(A).
func ApplyL1ToL2Alias(address common.Address) common.Address

See also UndoL1ToL2Alias.

Create2Address

Generates a future-proof contract address using salt plus bytecode which allows determination of an address before deployment.

The ZKsync Era implementation is slightly different from Ethereum.

Inputs

ParameterTypeDescription
sendercommon.AddressSender address.
bytecode[]byteOutput from zkSolc.
constructor[]byteABI encoded constructor arguments.
salt[]byteRandomization element.

Outputs

Returns a common.Address of the future contract.

func Create2Address(sender common.Address, bytecode, constructor, salt []byte) (common.Address, error)
The prefix is equal to keccak256("zksyncCreate2").

CreateAddress

Generates a contract address from deployer's account and nonce.

Inputs

ParameterTypeDescription
sendercommon.AddressSender address.
nonce*big.Int objectSender nonce.

Outputs

Returns a common.Address of the future contract.

func CreateAddress(sender common.Address, nonce *big.Int) (common.Address, error)
The prefix is equal to keccak256("zksyncCreate").

CreateETH

Creates ETH token with appropriate Name, Symbol and Decimals values.

func CreateETH() *types.Token

EncodeCreate

Returns the encoded constructor data for CREATE method used for smart contract deployment.

Inputs

ParameterTypeDescription
bytecode[]byteOutput from zkSolc.
calldata[]byteABI encoded constructor arguments.
func EncodeCreate(bytecode, calldata []byte) ([]byte, error)

EncodeCreate2

Returns the encoded constructor data for CREATE2 method used for smart contract deployment.

Inputs

ParameterTypeDescription
bytecode[]byteOutput from zkSolc.
calldata[]byteABI encoded constructor arguments.
salt[]byteRandomization element.
func EncodeCreate2(bytecode, calldata, salt []byte) ([]byte, error)

EncodeCreateAccount

Returns the encoded constructor data for CREATE method used for smart account deployment.

Inputs

ParameterTypeDescription
bytecode[]byteOutput from zkSolc.
calldata[]byteABI encoded constructor arguments.
versiontypes.AccountAbstractionVersionAccount abstraction version.
 EncodeCreate2Account(bytecode, calldata, version types.AccountAbstractionVersion) ([]byte, error)

EncodeCreate2Account

Returns the encoded constructor data for CREATE2 method used for smart account deployment.

Inputs

ParameterTypeDescription
bytecode[]byteOutput from zkSolc.
calldata[]byteABI encoded constructor arguments.
salt[]byteRandomization element.
versiontypes.AccountAbstractionVersionAccount abstraction version.
 EncodeCreate2Account(bytecode, calldata, salt []byte, version types.AccountAbstractionVersion) ([]byte, error)

Erc20BridgeCalldata

Returns the calldata sent by an L1 ERC20 bridge to its L2 counterpart during token-bridging.

Inputs

ParameterTypeDescription
l1TokenAddresscommon.AddressToken address on L1.
l1Sendercommon.AddressSender address on L1.
l2Receivercommon.AddressRecipient address on L2.
amount*big.IntGas fee for the number of tokens to bridge.
bridgeData*big.IntData
func Erc20BridgeCalldata(l1TokenAddress, l1Sender, l2Receiver common.Address, amount *big.Int, bridgeData []byte)

Erc20DefaultBridgeData

Returns the data needed for correct initialization of an L1 token counterpart on L2.

Inputs

ParameterTypeDescription
l1TokenAddresscommon.AddressToken address on L1.
backendbind.ContractBackendClient that is able to work with contracts on a read-write basis.

Outputs

An ABI-encoded []byte which contains token name, symbol and decimals.

func Erc20DefaultBridgeData(l1TokenAddress common.Address, backend bind.ContractBackend) ([]byte, error)

HashBytecode

Returns the hash of given bytecode.

Inputs

ParameterTypeDescription
bytecode[]byteBytecode.
func HashBytecode(bytecode []byte) ([]byte, error)

ReadStandardJson

Reads standard-json file generated as output from zksolc. Returns standard json configuration and extracted contracts abi and bytecode from config file.

Inputs

ParameterTypeDescription
pathstringPath to the standard-json file.
func ReadStandardJson(path string) (*types.StandardConfiguration, abi.ABI, []byte, error)

UndoL1ToL2Alias

Converts and returns the msg.sender viewed on L2 to the address that submitted a transaction to the inbox on L1.

Inputs

ParameterTypeDescription
addresscommon.AddressSender address.

Outputs

Returns the msg.sender of the L2->L1 transaction as the common.Address of the contract that initiated the transaction.

func UndoL1ToL2Alias(address common.Address) common.Address

See also ApplyL1ToL2Alias.


Made with ❤️ by the ZKsync Community