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 ethers
Imports
import { createSmartAccountClient } from "@biconomy/account";
import { Wallet, providers, ethers } from "ethers";
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!
Create the Biconomy Smart Account
async function createAccount() {
const biconomySmartAccount = await createSmartAccountClient({
signer,
bundlerUrl: "", // <-- Read about this at https://docs.biconomy.io/dashboard#bundler-url
biconomyPaymasterApiKey: "", // <-- Read about at https://docs.biconomy.io/dashboard/paymaster
});
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 ethers
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 biconomySmartAccount = await createSmartAccountClient({
signer,
bundlerUrl: "", // <-- Read about this at https://docs.biconomy.io/dashboard#bundler-url
biconomyPaymasterApiKey: "", // <-- Read about at https://docs.biconomy.io/dashboard/paymaster
});
const address = await biconomySmartAccount.getAccountAddress();
console.log(address);
} catch (error) {
console.error(error);
}
};