Java API

SmartAccount

A flexible signer for various payloads using different secrets.

A SmartAccount is a signer which can be configured to sign various payloads using a provided secret. The secret can be in any form, allowing for flexibility when working with different account implementations. The SmartAccount is bound to a specific address and provides the ability to define custom method for populating transactions and custom signing method used for signing.

constructor

Creates a SmartAccount instance with provided signer and provider. By default, uses sign_payload_with_ecdsa and sign_payload_with_multiple_ecdsa.

Inputs

ParameterTypeDescription
providerZkSyncThe provider to connect to.
addressStringAddress of the smart account.
secretListSecrets to be used for signing transactions.
transactionBuilderIPopulateTransactionImplementation of transaction builder (default is populate_transaction_ecdsa).
payloadSignerISignPayloadImplementation of signing (default is sign_payload_with_ecdsa).

getAddress

Returns the address of the account.

Example

String result = smartAccount.getAddress()

getAllBalances

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

Inputs and outputs

NameDescription
returnsZksAccountBalances with all account balances.

Example

ZksAccountBalances allBalances = wallet.getAddress().join();

getBalance

Returns the amount of the token the Wallet has.

BigInteger ethBalance = wallet.getBalance().sendAsync().join();

Inputs

ParameterTypeDescription
tokenStringThe address of the token. ETH by default (optional).

Example

String L1_DAI = "0x56E69Fa1BB0d1402c89E3A4E3417882DeA6B14Be"
String l2DAI = wallet.l2TokenAddress(L1_DAI);
 
BigInteger tokenBalance = wallet.getBalance(l2DAI).sendAsync().join();

Inputs

ParameterTypeDescription
addressStringThe address of the wallet.
tokenStringThe address of the token. ETH by default.
blockNumberBlockNumberIn which block a balance should be checked on. committed, i.e. the latest processed one is the default option.

Example

String L1_DAI = "0x56E69Fa1BB0d1402c89E3A4E3417882DeA6B14Be"
String l2DAI = wallet.l2TokenAddress(L1_DAI);
 
BigInteger tokenBalance = wallet.getBalance(signer.getAddress(), l2DAI, ZkBlockParameterName.COMMITTED).sendAsync().join();

getDeploymentNonce

Returns account's deployment nonce number.

Inputs and outputs

NameDescription
returnsThe deployment nonce number.

Example

BigInteger deploymentNonce = wallet.getDeploymentNonce().join()

populateTransaction

Populates the transaction tx using the provided transaction_builder function. If tx.from is not set, it sets the value from the getAddress method which can be utilized in the transaction builder function.

Inputs

ParameterTypeDescription
txTransactionThe transaction that needs to be populated.

sendTransaction

Sends tx to the Network. The signTransaction is called first to ensure transaction is properly signed.

Inputs

ParameterTypeDescription
txTransactionThe transaction that needs to be sent.

signTransaction

Signs the transaction tx using the provided payload_signer function, returning the fully signed transaction. The populateTransaction method is called first to ensure that all necessary properties for the transaction to be valid have been populated.

Inputs

ParameterTypeDescription
txTransactionThe transaction that needs to be signed.

transfer

For convenience, the Wallet class has transfer method, which can transfer ETH or any ERC20 token within the same interface.

Inputs and outputs

NameDescription
txTransferTransaction.
returnsA TransactionReceipt of transaction.

Example

Transfer ETH.

TransferTransaction transaction = new TransferTransaction("<RECIPIENT_ADDRESS>", BigInteger(7_000_000_000L), signer.getAddress());
 
TransactionReceipt receipt = smartAccount.transfer(transaction).sendAsync().join();
 
smartAccount.getTransactionReceiptProcessor().waitForTransactionReceipt(receipt.getTransactionHash());

Transfer ETH using paymaster to facilitate fee payment with an ERC20 token.

PaymasterParams paymasterParams = new PaymasterParams(PAYMASTER, Numeric.hexStringToByteArray(FunctionEncoder.encode(Paymaster.encodeApprovalBased(TOKEN, BigInteger.ONE, new byte[] {}))));
TransferTransaction transaction = new TransferTransaction("<RECIPIENT_ADDRESS>", BigInteger(7_000_000_000L), signer.getAddress(), paymasterParams);
 
TransactionReceipt receipt = smartAccount.transfer(transaction).sendAsync().join();
 
smartAccount.getTransactionReceiptProcessor().waitForTransactionReceipt(receipt.getTransactionHash());

withdraw

Initiates the withdrawal process which withdraws ETH or any ERC20 token from the associated account on L2 network to the target account on L1 network.

Inputs

NameDescription
transactionWithdrawTransaction

Example

BigInteger amount = new BigInteger("7000000000");
WithdrawTransaction transaction = new WithdrawTransaction(ZkSyncAddresses.ETH_ADDRESS, amount, testWallet.getAddress());
 
TransactionReceipt result = smartAccount.withdraw(transaction).sendAsync().join();
 
TransactionReceipt receipt = smartAccount.getTransactionReceiptProcessor().waitForTransactionReceipt(result.getTransactionHash());

Withdraw ETH using paymaster to facilitate fee payment with an ERC20 token.

BigInteger amount = new BigInteger("7000000000");
String token = "0x927488F48ffbc32112F1fF721759649A89721F8F"; // Crown token which can be minted for free
String paymaster = "0x13D0D8550769f59aa241a41897D4859c87f7Dd46"; // Paymaster for Crown token
 
PaymasterParams paymasterParams = new PaymasterParams(paymaster, Numeric.hexStringToByteArray(FunctionEncoder.encode(Paymaster.encodeApprovalBased(token, BigInteger.ONE, new byte[]
 {}))));
WithdrawTransaction transaction = new WithdrawTransaction(ZkSyncAddresses.ETH_ADDRESS, amount, paymasterParams);
 
TransactionReceipt result = smartAccount.withdraw(transaction).sendAsync().join();
 
TransactionReceipt receipt = smartAccount.getTransactionReceiptProcessor().waitForTransactionReceipt(result.getTransactionHash());

Made with ❤️ by the ZKsync Community