Skip to content

@alchemy/aa-ethers

This package contains EthersProviderAdapter and AccountSigner, respective extensions of the JsonRpcProvider and Signer classes defined in ethers.js external library.

If you currently rely ethers.js for web3 development, you can use these ethers.js-compatible JsonRpcProvider and Signer to integrate Account Abstraction into your dApp. You may also find the util methods helpful.

This repo is community maintained and we welcome contributions!

Getting started

If you are already using the @alchemy/aa-core package, you can simply install this package and start using the EthersProviderAdapter and AccountSigner. If you are not using @alchemy/aa-core, you can install it and follow the instructions in the "Getting started" docs to get started.

bash
yarn add @alchemy/aa-ethers
bash
npm i -s @alchemy/aa-ethers
bash
pnpm i @alchemy/aa-ethers

You can create a provider and connect it to a Signer account like so:

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