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 messages, typed data, and transactions.

Hierarchy

  • AdapterL2
    • SmartAccount

Constructors

  • Creates a SmartAccount instance with provided signer and provider. By default, uses signPayloadWithECDSA and populateTransactionECDSA.

    Parameters

    • signer: SmartAccountSigner

      Contains necessary properties for signing payloads.

    • Optionalprovider: Web3ZKsyncL2

      The provider to connect to. Can be null for offline usage.

    Returns SmartAccount

    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
    );

Properties

payloadSigner: PayloadSigner

Custom method for signing different payloads.

transactionBuilder: TransactionBuilder

Custom method for populating transaction requests.

Accessors

Methods

  • Creates a new instance of SmartAccount connected to a provider or detached from any provider if null is provided.

    Parameters

    • Optionalprovider: null | Web3ZKsyncL2

      The provider to connect the SmartAccount to. If null, the SmartAccount will be detached from any provider.

    Returns SmartAccount

    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.

    Returns Promise<WalletBalances>

    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.

    Parameters

    • Optionaltoken: string

      The token address to query balance for. Defaults to the native token.

    • OptionalblockTag: BlockTag = 'latest'

      The block tag to get the balance at.

    Returns Promise<bigint>

    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.

    Returns Promise<bigint>

    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();
  • Parameters

    • blockNumber: BlockNumberOrTag = 'latest'

    Returns Promise<bigint>

  • 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.

    Parameters

    Returns Promise<DeepWriteable<Transaction & {
        customData?: null | Eip712Meta;
        type?: Numbers;
    }>>

    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,
    });
  • Parameters

    • signedTx: string

    Returns Promise<string>

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

    Parameters

    Returns Promise<PriorityOpResponse>

    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.

    Parameters

    • message: string | Uint8Array

      The message that needs to be signed.

    Returns string

    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.

    Parameters

    Returns Promise<string>

    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.

    Parameters

    • domain: TypedDataDomain

      The domain data.

    • types: Record<string, TypedDataField[]>

      A map of records pointing from field name to field type.

    • value: Record<string, any>

      A single record value.

    Returns Promise<string>

    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.

    Parameters

    Returns Promise<PriorityOpResponse>

    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}`);
  • 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.

    Parameters

    Returns Promise<PriorityOpResponse>

    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(),
    }),
    });