Skip to content

How to use an ERC-7579 compatible smart account with permissionless.js

ERC-7579 defines a standard for modular smart account interfaces. It also defines behavior for interoperability with minimal restrictions for accounts and modules.

Currently Safe and Kernel are the only smart accounts that implements ERC-7579.

For this guide, we will use the Safe smart accounts as an example. If you would like to find out more about the Safe smart account, you can check out the Safe-specific guide.

This guide will show you how to create and use a ERC-7579 compatible smart account with permissionless.js.

Steps

Import the required packages

import { owner } from "../owner"
import { createSmartAccountClient } from "permissionless"
import { sepolia } from "viem/chains"
import { encodePacked, http } from "viem"
import { erc7579Actions } from "permissionless/actions/erc7579"
import { createPublicClient } from "viem"
import { createPimlicoClient } from "permissionless/clients/pimlico"
import { entryPoint07Address } from "viem/account-abstraction"
import { toSafeSmartAccount } from "permissionless/accounts"

Create the clients

First we must create the public, (optionally) pimlico paymaster clients that will be used to interact with the SafeAccount.

const apiKey = "YOUR_PIMLICO_API_KEY"
const bundlerUrl = `https://api.pimlico.io/v2/sepolia/rpc?apikey=${apiKey}`
 
const publicClient = createPublicClient({
	transport: http("https://rpc.ankr.com/eth_sepolia"),
})
 
const pimlicoClient = createPimlicoClient({
	transport: http(bundlerUrl),
	entryPoint: {
		address: entryPoint07Address,
		version: "0.7",
	},
})

Create the SafeAccount

You can also pass an address to use an already created SafeAccount.

const safeAccount = await toSafeSmartAccount({
	client: publicClient,
	owners: [owner],
	version: "1.4.1",
	entryPoint: {
		address: entryPoint07Address,
		version: "0.7",
	},
	safe4337ModuleAddress: "0x3Fdb5BC686e861480ef99A6E3FaAe03c0b9F32e2", // These are not meant to be used in production as of now.
	erc7579LaunchpadAddress: "0xEBe001b3D534B9B6E2500FB78E67a1A137f561CE", // These are not meant to be used in production as of now.
})

Create the smart account client and extend it with the ERC7579 actions

The smart account client is a permissionless.js client that is meant to serve as an almost drop-in replacement for viem's walletClient.

const smartAccountClient = createSmartAccountClient({
	account: safeAccount,
	chain: sepolia,
	bundlerTransport: http(bundlerUrl),
	paymaster: pimlicoClient,
	userOperation: {
		estimateFeesPerGas: async () => {
			return (await pimlicoClient.getUserOperationGasPrice()).fast
		},
	},
}).extend(erc7579Actions())

Interact with the 7579 actions

You can install a module on the Safe account using the installModule action.

const ownableExecutorModule = "0xc98B026383885F41d9a995f85FC480E9bb8bB891"
const moduleData = encodePacked(["address"], ["0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"])
const userOpHash = await smartAccountClient.installModule({
	type: "executor",
	address: ownableExecutorModule,
	context: moduleData,
})
 
const receipt = await pimlicoClient.waitForUserOperationReceipt({ hash: userOpHash })

You can also call all other ERC7579 actions, example supportsExecutionMode.

const isExecutionModeSupported = await smartAccountClient.supportsExecutionMode({
	type: "delegatecall",
	revertOnError: true,
	selector: "0x",
	context: "0x",
})