Create Smart Accounts with Ethers JS
This section showcases two ways you can create Smart Accounts using Ethers JS. Using Private keys or using the Ethers Signer Object from a browser injected EOA. Note this is not a full implementation but a recipe to use and add to your existing projects. See our tutorials for full context on how this can be used.
Using Private Keys
Dependencies
You will need the following dependencies to create a Smart Account this way:
yarn add @biconomy/account @biconomy/bundler @biconomy/common @biconomy/modules @biconomy/paymaster ethers@5.7.2
Imports
import { IPaymaster, BiconomyPaymaster } from "@biconomy/paymaster";
import { IBundler, Bundler } from "@biconomy/bundler";
import {
BiconomySmartAccountV2,
DEFAULT_ENTRYPOINT_ADDRESS,
} from "@biconomy/account";
import { Wallet, providers, ethers } from "ethers";
import {
ECDSAOwnershipValidationModule,
DEFAULT_ECDSA_OWNERSHIP_MODULE,
} from "@biconomy/modules";
Create a Signer using a Private Key:
const provider = new providers.JsonRpcProvider(
"https://rpc-amoy.polygon.technology/",
); // or any other rpc provider link
const signer = new Wallet("<your_private_key>" || "", provider);
// we recommend using environment variables for your private keys!
Configuration Values
Set up instances of Bundler and Paymaster.
const bundler: IBundler = new Bundler({
// get from biconomy dashboard https://dashboard.biconomy.io/
bundlerUrl: "",
chainId: 80002, // or any supported chain of your choice
entryPointAddress: DEFAULT_ENTRYPOINT_ADDRESS,
});
const paymaster: IPaymaster = new BiconomyPaymaster({
// get from biconomy dashboard https://dashboard.biconomy.io/
paymasterUrl: "",
});
Create the Biconomy Smart Account
async function createAccount() {
const module = await ECDSAOwnershipValidationModule.create({
signer,
moduleAddress: DEFAULT_ECDSA_OWNERSHIP_MODULE,
});
let biconomySmartAccount = await BiconomySmartAccountV2.create({
chainId: 80002, // or any supported chain of your choice
bundler: bundler,
paymaster: paymaster,
entryPointAddress: DEFAULT_ENTRYPOINT_ADDRESS,
defaultValidationModule: module,
activeValidationModule: module,
});
console.log("address: ", await biconomySmartAccount.getAccountAddress());
return biconomySmartAccount;
}
createAccount();
Using Ethers Signer from Browser EOA
Dependencies
You will need the following dependencies to create a Smart Account this way:
yarn add @biconomy/account @biconomy/bundler @biconomy/common @biconomy/modules @biconomy/paymaster ethers@5.7.2
Imports
import { IPaymaster, BiconomyPaymaster } from "@biconomy/paymaster";
import { IBundler, Bundler } from "@biconomy/bundler";
import {
BiconomySmartAccountV2,
DEFAULT_ENTRYPOINT_ADDRESS,
} from "@biconomy/account";
import { Wallet, providers, ethers } from "ethers";
Configuration Values
Set up instances of Bundler and Paymaster.
const bundler: IBundler = new Bundler({
// get from biconomy dashboard https://dashboard.biconomy.io/
bundlerUrl: "",
chainId: 80002, // or any supported chain of your choice
entryPointAddress: DEFAULT_ENTRYPOINT_ADDRESS,
});
const paymaster: IPaymaster = new BiconomyPaymaster({
// get from biconomy dashboard https://dashboard.biconomy.io/
paymasterUrl: "",
});
Create the Biconomy Smart Account
const connect = async () => {
const { ethereum } = window;
try {
const provider = new ethers.providers.Web3Provider(ethereum);
await provider.send("eth_requestAccounts", []);
const signer = provider.getSigner();
const ownerShipModule = await ECDSAOwnershipValidationModule.create({
signer: signer,
moduleAddress: DEFAULT_ECDSA_OWNERSHIP_MODULE,
});
let biconomySmartAccount = await BiconomySmartAccountV2.create({
chainId: 80002,
bundler: bundler,
paymaster: paymaster,
entryPointAddress: DEFAULT_ENTRYPOINT_ADDRESS,
defaultValidationModule: ownerShipModule,
activeValidationModule: ownerShipModule,
});
const address = await biconomySmartAccount.getAccountAddress();
console.log(address);
} catch (error) {
console.error(error);
}
};