SmartAccount
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
Parameter | Type | Description |
---|---|---|
provider | ZkSync | The provider to connect to. |
address | String | Address of the smart account. |
secret | List | Secrets to be used for signing transactions. |
transactionBuilder | IPopulateTransaction | Implementation of transaction builder (default is populate_transaction_ecdsa). |
payloadSigner | ISignPayload | Implementation 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
Name | Description |
---|---|
returns | ZksAccountBalances 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
Parameter | Type | Description |
---|---|---|
token | String | The 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
Parameter | Type | Description |
---|---|---|
address | String | The address of the wallet. |
token | String | The address of the token. ETH by default. |
blockNumber | BlockNumber | In 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
Name | Description |
---|---|
returns | The 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
Parameter | Type | Description |
---|---|---|
tx | Transaction | The transaction that needs to be populated. |
sendTransaction
Sends tx
to the Network. The signTransaction
is called first to ensure transaction is properly signed.
Inputs
Parameter | Type | Description |
---|---|---|
tx | Transaction | The 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
Parameter | Type | Description |
---|---|---|
tx | Transaction | The 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
Name | Description |
---|---|
tx | TransferTransaction . |
returns | A 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
Name | Description |
---|---|
transaction | WithdrawTransaction |
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());