Skip to content

toSmartContractAccount

Converts an account to a smart contract account and sets up various account-related methods using the provided parameters like transport, chain, entry point, and other utilities.

Import

import { toSmartContractAccount } from "@aa-sdk/core";

Usage

import { http, type SignableMessage } from "viem";
import { sepolia } from "viem/chains";
 
const myAccount = await toSmartContractAccount({
  /// REQUIRED PARAMS ///
  source: "MyAccount",
  transport: http("RPC_URL"),
  chain: sepolia,
  // The EntryPointDef that your account is com"patible with
  entryPoint: getEntryPoint(sepolia, { version: "0.6.0" }),
  // This should return a concatenation of your `factoryAddress` and the `callData` for your factory's create account method
  getAccountInitCode: async () => "0x{factoryAddress}{callData}",
  // an invalid signature that doesn't cause your account to revert during validation
  getDummySignature: () => "0x1234...",
  // given a UO in the form of {target, data, value} should output the calldata for calling your contract's execution method
  encodeExecute: async (uo) => "0xcalldata",
  signMessage: async ({ message }: { message: SignableMessage }) => "0x...",
  signTypedData: async (typedData) => "0x000",
 
  /// OPTIONAL PARAMS ///
  // if you already know your account's address, pass that in here to avoid generating a new counterfactual
  accountAddress: "0xaddressoverride",
  // if your account supports batching, this should take an array of UOs and return the calldata for calling your contract's batchExecute method
  encodeBatchExecute: async (uos) => "0x...",
  // if your contract expects a different signing scheme than the default signMessage scheme, you can override that here
  signUserOperationHash: async (hash) => "0x...",
  // allows you to define the calldata for upgrading your account
  encodeUpgradeToAndCall: async (params) => "0x...",
});

Parameters

params

ToSmartContractAccountParams the parameters required for converting to a smart contract account

params.transport

Transport the transport mechanism used for communication

params.chain

Chain the blockchain chain used in the account

params.entryPoint

EntryPoint the entry point of the smart contract

params.source

string the source identifier for the account

params.accountAddress

Address the address of the account

params.getAccountInitCode

() => Promise<Hex> a function to get the initial state code of the account

params.signMessage

(message: { message: SignableMessage }) => Promise<Hex> a function to sign a message

params.signTypedData

(typedDataDefinition: TypedDataDefinition<typedData, primaryType>) => Promise<Hex> a function to sign typed data

params.encodeBatchExecute

(transactions: Transaction[]) => Hex a function to encode batch transactions

params.encodeExecute

(tx: Transaction) => Hex a function to encode a single transaction

params.getDummySignature

() => Promise<Hex> a function to get a dummy signature

params.signUserOperationHash

(uoHash: Hex) => Promise<Hex> a function to sign user operations

params.encodeUpgradeToAndCall

(implementationAddress: Address, implementationCallData: Hex) => Hex a function to encode upgrade call

Returns

Promise<SmartContractAccount> a promise that resolves to a SmartContractAccount object with methods and properties for interacting with the smart contract account