Creates a SmartAccount
instance with provided signer
and provider
.
By default, uses signPayloadWithECDSA and populateTransactionECDSA.
Contains necessary properties for signing payloads.
Optional
provider: Web3ZKsyncL2The provider to connect to. Can be null
for offline usage.
Protected
payloadCustom method for signing different payloads.
Protected
transactionCustom method for populating transaction requests.
Returns a context (provider + Signer) instance for connecting to an L2 network.
Creates a new instance of SmartAccount
connected to a provider or detached
from any provider if null
is provided.
Optional
provider: null | Web3ZKsyncL2The provider to connect the SmartAccount
to.
If null
, the SmartAccount
will be detached from any provider.
import { SmartAccount, types, Web3ZKsyncL2 } from "web3-plugin-zksync";
const ADDRESS = "<ADDRESS>";
const PRIVATE_KEY = "<PRIVATE_KEY>";
const sepoliaProvider = Web3ZKsyncL2.initWithDefaultProvider(types.Network.Sepolia);
const sepoliaAccount = new SmartAccount(
{address: ADDRESS, secret: PRIVATE_KEY},
sepoliaProvider
);
const mainnetProvider = Web3ZKsyncL2.initWithDefaultProvider(types.Network.Mainnet);
const mainnetAccount = sepoliaAccount.connect(mainnetProvider);
Returns all token balances of the account.
import { SmartAccount, types, Web3ZKsyncL2 } from "web3-plugin-zksync";
const ADDRESS = "<ADDRESS>";
const PRIVATE_KEY = "<PRIVATE_KEY>";
const l2 = Web3ZKsyncL2.initWithDefaultProvider(types.Network.Sepolia);
const account = new SmartAccount(
{address: ADDRESS, secret: PRIVATE_KEY},
l2
);
const balances = await account.getAllBalances();
Returns the balance of the account.
Optional
token: stringThe token address to query balance for. Defaults to the native token.
Optional
blockTag: BlockTag = 'latest'The block tag to get the balance at.
import { SmartAccount, types, Web3ZKsyncL2 } from "web3-plugin-zksync";
const ADDRESS = "<ADDRESS>";
const PRIVATE_KEY = "<PRIVATE_KEY>";
const l2 = Web3ZKsyncL2.initWithDefaultProvider(types.Network.Sepolia);
const account = new SmartAccount(
{address: ADDRESS, secret: PRIVATE_KEY},
l2
);
const balance = await account.getBalance();
Returns the deployment nonce of the account.
import { SmartAccount, types, Web3ZKsyncL2 } from "web3-plugin-zksync";
const ADDRESS = "<ADDRESS>";
const PRIVATE_KEY = "<PRIVATE_KEY>";
const l2 = Web3ZKsyncL2.initWithDefaultProvider(types.Network.Sepolia);
const account = new SmartAccount(
{address: ADDRESS, secret: PRIVATE_KEY},
l2
);
const nonce = await account.getDeploymentNonce();
Returns L2 bridge contracts.
Populates the transaction tx
using the provided TransactionBuilder function.
If tx.from
is not set, it sets the value from the getAddress method which can
be utilized in the TransactionBuilder function.
The transaction that needs to be populated.
import { constants, SmartAccount, types, Web3ZKsyncL2 } from "web3-plugin-zksync";
const ADDRESS = "<ADDRESS>";
const PRIVATE_KEY = "<PRIVATE_KEY>";
const l2 = Web3ZKsyncL2.initWithDefaultProvider(types.Network.Sepolia);
const account = new SmartAccount(
{address: ADDRESS, secret: PRIVATE_KEY},
l2
);
const populatedTx = await account.populateTransaction({
type: constants.EIP712_TX_TYPE,
to: "<RECEIVER>",
value: 7_000_000_000,
});
Sends tx
to the Network. The signTransaction
is called first to ensure transaction is properly signed.
The transaction that needs to be sent.
import { SmartAccount, types, Web3ZKsyncL2 } from "web3-plugin-zksync";
const ADDRESS = "<ADDRESS>";
const PRIVATE_KEY = "<PRIVATE_KEY>";
const l2 = Web3ZKsyncL2.initWithDefaultProvider(types.Network.Sepolia);
const account = new SmartAccount(
{address: ADDRESS, secret: PRIVATE_KEY},
l2
);
const signedTx = await account.sendTransaction({
to: "<RECEIVER>",
value: 7_000_000_000,
});
Signs a message
using the provided PayloadSigner function.
The message that needs to be signed.
import { SmartAccount, types, Web3ZKsyncL2 } from "web3-plugin-zksync";
const ADDRESS = "<ADDRESS>";
const PRIVATE_KEY = "<PRIVATE_KEY>";
const l2 = Web3ZKsyncL2.initWithDefaultProvider(types.Network.Sepolia);
const account = new SmartAccount(
{address: ADDRESS, secret: PRIVATE_KEY},
l2
);
const signedMessage = await account.signMessage('Hello World!');
Signs the transaction tx
using the provided PayloadSigner 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.
The transaction that needs to be signed.
import { SmartAccount, types, Web3ZKsyncL2 } from "web3-plugin-zksync";
const ADDRESS = "<ADDRESS>";
const PRIVATE_KEY = "<PRIVATE_KEY>";
const l2 = Web3ZKsyncL2.initWithDefaultProvider(types.Network.Sepolia);
const account = new SmartAccount(
{address: ADDRESS, secret: PRIVATE_KEY},
l2
);
const signedTx = await account.signTransaction({
to: "<RECEIVER>",
value: 7_000_000_000,
});
Signs a typed data using the provided PayloadSigner function.
The domain data.
A map of records pointing from field name to field type.
A single record value.
import { SmartAccount, types, Web3ZKsyncL2 } from "web3-plugin-zksync";
const ADDRESS = "<ADDRESS>";
const PRIVATE_KEY = "<PRIVATE_KEY>";
const l2 = Web3ZKsyncL2.initWithDefaultProvider(types.Network.Sepolia);
const account = new SmartAccount(
{address: ADDRESS, secret: PRIVATE_KEY},
l2
);
const signedTypedData = await account.signTypedData(
{name: 'Example', version: '1', chainId: 270},
{
Person: [
{name: 'name', type: 'string'},
{name: 'age', type: 'uint8'},
],
},
{name: 'John', age: 30}
);
Transfer ETH or any ERC20 token within the same interface.
Transfer transaction request.
A Promise resolving to a transfer transaction response.
import { constants, SmartAccount, types, Web3ZKsyncL2 } from "web3-plugin-zksync";
const ADDRESS = "<ADDRESS>";
const PRIVATE_KEY = "<PRIVATE_KEY>";
const l2 = Web3ZKsyncL2.initWithDefaultProvider(types.Network.Sepolia);
const account = new SmartAccount(
{address: ADDRESS, secret: PRIVATE_KEY},
l2
);
const transferTx = await account.transfer({
token: constants.ETH_ADDRESS,
to: "<RECEIVER>",
amount: 10_000_000n,
});
const receipt = await transferTx.wait();
console.log(`The sum of ${receipt.value} ETH was transferred to ${receipt.to}`);
import { constants, paymasterUtils, SmartAccount, types, Web3ZKsyncL2 } from "web3-plugin-zksync";
const ADDRESS = "<ADDRESS>";
const PRIVATE_KEY = "<PRIVATE_KEY>";
const token = "0x927488F48ffbc32112F1fF721759649A89721F8F"; // Crown token which can be minted for free
const paymaster = "0x13D0D8550769f59aa241a41897D4859c87f7Dd46"; // Paymaster for Crown token
const l2 = Web3ZKsyncL2.initWithDefaultProvider(types.Network.Sepolia);
const account = new SmartAccount(
{address: ADDRESS, secret: PRIVATE_KEY},
l2
);
const transferTx = await account.transfer({
to: Wallet.createRandom().address,
amount: 10_000_000n,
paymasterParams: paymasterUtils.getPaymasterParams(paymaster, {
type: "ApprovalBased",
token: token,
minimalAllowance: 1,
innerInput: new Uint8Array(),
}),
});
const receipt = await transferTx.wait();
console.log(`The sum of ${receipt.value} ETH was transferred to ${receipt.to}`);
Transfer ETH or any ERC20 token within the same interface.
Transfer transaction request.
A Promise resolving to a transfer transaction response.
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.
Withdrawal transaction request.
A Promise resolving to a withdrawal transaction response.
import { constants, SmartAccount, types, Web3ZKsyncL2 } from "web3-plugin-zksync";
const ADDRESS = "<ADDRESS>";
const PRIVATE_KEY = "<PRIVATE_KEY>";
const l2 = Web3ZKsyncL2.initWithDefaultProvider(types.Network.Sepolia);
const account = new SmartAccount(
{address: ADDRESS, secret: PRIVATE_KEY},
l2
);
const withdrawTx = await account.withdraw({
token: constants.ETH_ADDRESS,
amount: 10_000_000n,
});
import { constants, paymasterUtils, SmartAccount, types, Web3ZKsyncL2 } from "web3-plugin-zksync";
const ADDRESS = "<ADDRESS>";
const PRIVATE_KEY = "<PRIVATE_KEY>";
const token = "0x927488F48ffbc32112F1fF721759649A89721F8F"; // Crown token which can be minted for free
const paymaster = "0x13D0D8550769f59aa241a41897D4859c87f7Dd46"; // Paymaster for Crown token
const l2 = Web3ZKsyncL2.initWithDefaultProvider(types.Network.Sepolia);
const account = new SmartAccount(
{address: ADDRESS, secret: PRIVATE_KEY},
l2
);
const withdrawTx = await account.withdraw({
token: constants.ETH_ADDRESS,
amount: 10_000_000n,
paymasterParams: paymasterUtils.getPaymasterParams(paymaster, {
type: "ApprovalBased",
token: token,
minimalAllowance: 1,
innerInput: new Uint8Array(),
}),
});
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. TheSmartAccount
is bound to a specific address and provides the ability to define custom method for populating transactions and custom signing method used for signing messages, typed data, and transactions.