Account Instance

The component of userop.js used to interface with any ERC-4337 compliant smart account.

The Account namespace contains modules for interfacing with a user's Smart Account. It can be configured to work seamlessly with any Smart Account implementation.

Instance

The Instance class constructor is used for initialising an Account with a given configuration.

const account = new V06.Account.Instance({
  // AccountOpts object...
})

getSender

Returns the address of the Smart Account.

const sender = await account.getSender();

encodeCallData

Sets the callData field on the User Operation. This method returns the instance and can be chained with other methods like buildUserOperation or sendUserOperation.

The method takes as input the name of the function to call on the Smart Account contract and an array of its inputs. This is strongly typed and will depend on the given account ABI.

const build = await account.encodeCallData("METHOD_NAME", [ /* method inputs... */ ])
  .buildUserOperation();

Interacting with other smart contracts

In a lot of cases you'll likely want your Smart Account to interact with other contracts. For instance, calling an ERC-20 token contract to initiate a transfer. In most cases a Smart Account implementation will have some variation of an execute function. This allows the Smart Account to make arbitrary calls to other third party contracts.

This is how it could look like using userop.js with SimpleAccount.

const funcData = encodeFunctionData({
  abi: erc20Abi,
  functionName: 'transfer',
  args: [toAddress, amount],
});

const build = await account.encodeCallData("execute", [tokenAddress, 0n, funcData])
  .buildUserOperation();

buildUserOperation

Runs through the process of building a User Operation based on the given account configuration.

const build = await account.buildUserOperation();

If successful, it returns a Promise that resolves into the following object:

interface BuildUserOperationResponse {
  userOperation: EntryPoint.UserOperation;
  userOpHash: Hex;
}

sendUserOperation

Runs through the process of building a User Operation and sending it to the Bundler.

const send = await account.sendUserOperation();

If successful, it returns a Promise that resolves into the following object:

export interface SendUserOperationResponse {
  userOpHash: Hex;
  wait: () => Promise<Bundler.UserOperationReceipt | null>;
}

The wait method can be used to poll for a User Operation receipt which is confirmation that the transaction has been included in a block.

const receipt = await send.wait();

get and set WaitTimeoutMs

Getter and setter methods for defining the total duration in milliseconds the wait method should run for. The setter returns the instance and can be chained with other methods.

const defaultTimeout = account.getWaitTimeoutMs(); // defaults to 60000

const newTimeout = account.setWaitTimeoutMs(30000).getWaitTimeoutMs(); // returns 30000

get and setWaitIntervalMs

Getter and setter methods for defining the polling intervals in milliseconds the wait method should use. The setter returns the instance and can be chained with other methods.

const defaultInterval = account.getWaitIntervalMs(); // defaults to 3000

const newInterval = account.setWaitTimeoutMs(2000).getWaitTimeoutMs(); // returns 2000

get and set Salt

Getter and setter methods for defining the salt value to use when setting the initCode. This allows a single verification method to control multiple Smart Accounts. The setter returns the instance and can be chained with other methods. The salt value will be passed to the setFactoryData hook.

const defaultSalt = account.getSalt();// defaults to 0n

const newSalt = account.setSalt(1n).getSalt(); // returns 1n

get and set NonceKey

Getter and setter methods for defining the nonce key to use for all User Operations. The setter returns the instance and can be chained with other methods.

const defaultNonceKey = account.getNonceKey();// defaults to 0n

const newSalt = account.setNonceKey(1n).getNonceKey(); // returns 1n

getNonce

Returns a big int of the current transaction sequence for the set nonce key.

const nonce = await account.getNonceKey();

set and clear StateOverrideSet

Methods for setting and clearing a state override set that is used for gas estimations. Both methods return the instance and can be chained with other methods.

const buildWithOverride = await account
  .setStateOverrideSetForEstimate({
    [await acc.getSender()]: {
      balance: "0x4337433743374337",
    },
  })
  .buildUserOperation();

const buildWithoutOverride = await account
  .clearStateOverrideSetForEstimate()
  .buildUserOperation();