Java API

Contract Interfaces

System contracts for interacting with the ZKsync Era network

zksync2-java implements contract wrappers. Created using web3j CLI

Generated wrappers

  • ERC20
  • IEthToken
  • IL1Bridge
  • IL1Messenger
  • IL2Bridge
  • INonceHolder
  • IZkSync
  • ZkSyncContract

ContractFactory

The ContractDeployer class is a utility class for deploying smart contracts on ZKsync, using create and create2 methods. It provides methods to compute contract addresses, based on EIP-1014 (create2) and extract contract addresses from transaction receipts.

computeL2Create2Address

Compute contract address according EIP-1014

Inputs

ParameterTypeDescription
senderAddressThe address of the transaction sender.
bytecodebyteThe compiled contract bytecode.
constructorbyteThe encoded constructor parameters.
saltbyteA 32-byte value used for the address computation.

Example

String result = ContractDeployer.computeL2Create2Address(new Address(credentials.getAddress()), Numeric.hexStringToByteArray(CounterContract.BINARY), new byte[]{}, salt).getValue();

computeL2CreateAddress

Computes the contract address using the create method.

Inputs

ParameterTypeDescription
senderAddressAddress of a source of a transaction.
nonceBigIntegerDeployment nonce.

Example

INonceHolder nonceHolder = INonceHolder.load(ZkSyncAddresses.NONCE_HOLDER_ADDRESS, zksync, new ReadonlyTransactionManager(zksync, credentials.getAddress()), new DefaultGasProvider());
BigInteger deploymentNonce = nonceHolder.getDeploymentNonce(credentials.getAddress()).send();
String precomputedAddress = ContractDeployer.computeL2CreateAddress(new Address(credentials.getAddress()), deploymentNonce).getValue();

encodeCreate

Encode create deployment function of default factory contract

Inputs

ParameterTypeDescription
bytecodebyteThe compiled contract bytecode.
calldatabyteEncoded constructor parameters.

Example

ContractDeployer.encodeCreate(bytecodeBytes, calldataBytes)

encodeCreate2

Encodes the create2 deployment function with empty constructor parameters.

Inputs

ParameterTypeDescription
bytecodebyteThe compiled contract bytecode.
calldatabyteEncoded constructor parameters.
saltbyte32 bytes salt.

Example

byte[] salt = SecureRandom.getSeed(32);
ContractDeployer.encodeCreate2(bytecodeBytes, calldataBytes, salt)

encodeCreate2Account

Encodes the create2 deployment function for a custom account with empty constructor parameters EIP-4337.

Inputs

ParameterTypeDescription
bytecodebyteThe compiled contract bytecode.
calldatabyteEncoded constructor parameters.
saltbyte32 bytes salt.
accountAbstractionVersionAccountAbstractionVersionVersion of the account abstraction.

Example

ContractDeployer.encodeCreate2Account(bytecodeBytes, calldataBytes, salt, AccountAbstractionVersion.Version1)

encodeCreateAccount

Encode create deployment custom account function of default factory contract EIP-4337

Inputs

ParameterTypeDescription
bytecodebyteThe compiled contract bytecode.
calldatabyteEncoded constructor parameters.

Example

ContractDeployer.encodeCreate2Account(bytecodeBytes, calldataBytes, AccountAbstractionVersion.Version1)

extractContractAddress

Extracts the deployed contract address from a transaction receipt.

Inputs

ParameterTypeDescription
receiptTransactionReceiptThe transaction receipt.

Example

String correctContractAddress = ContractDeployer.extractContractAddress(response).getValue();

hashBytecode

Generates the SHA-256 hash of the contract bytecode.

Inputs

ParameterTypeDescription
bytecodebyteThe compiled contract bytecode.

Example

byte[] result = ContractDeployer.hashBytecode(Numeric.hexStringToByteArray(CounterContract.BINARY));

Made with ❤️ by the ZKsync Community