Skip to content

connectToAccount

connectToAccount is a method on EthersProviderAdapter that you can optionally call to connect the provider to an account and returns a AccountSigner. This enables the returned AccountSigner to leverage the provider when signing messages, UserOperations, and transactions for a smart account using the connected EOA signer account.

Usage

ts
import { createLightAccount } from "@alchemy/aa-accounts";
import {
  LocalAccountSigner,
  SmartAccountSigner,
  polygonMumbai,
} from "@alchemy/aa-core";
import { http } from "viem";
import { provider } from "./ethers-provider.js";

const eoaSigner: SmartAccountSigner =
  LocalAccountSigner.mnemonicToAccountSigner(process.env.YOUR_OWNER_MNEMONIC!);

const chain = polygonMumbai;

// 2. Connect the provider to the smart account signer
export const accountSigner = provider.connectToAccount(
  await createLightAccount({
    chain,
    transport: http("RPC_URL"),
    signer: eoaSigner,
  })
);
ts
import {
  createSimpleSmartAccount,
  getChain,
  getDefaultSimpleAccountFactoryAddress,
} from "@alchemy/aa-core";
import { EthersProviderAdapter } from "@alchemy/aa-ethers";
import { Alchemy, Network } from "alchemy-sdk";
import { signer } from "snippets/aa-core/lightAccountClient";
import { http } from "viem";

// 1. Create alchemy instance
const alchemy = new Alchemy({
  apiKey: process.env.API_KEY!,
  network: Network.MATIC_MUMBAI,
});
const ethersProvider = await alchemy.config.getProvider();

const chain = getChain(ethersProvider.network.chainId);

// 2. smart account client from alchemy's ethers provider and connect with simple smart account
export const provider = EthersProviderAdapter.fromEthersProvider(
  ethersProvider
).connectToAccount(
  await createSimpleSmartAccount({
    chain,
    signer,
    factoryAddress: getDefaultSimpleAccountFactoryAddress(chain),
    transport: http(
      `${chain.rpcUrls.alchemy.http[0]}/${ethersProvider.apiKey}`
    ),
  })
);

Returns

AccountSigner<TAccount extends SmartContractAccount>

A new instance of a connected AccountSignerfor any implementation class of SmartContractAccount

Parameters

account: TAccount extends SmartContractAccount

A function that takes in the provider's rpcClient and returns an AccountSigner