Quickstart CLI Example

Explore a basic implementation of account abstraction using ERC-4337. This example will create an account and initiate a transaction on the Ethereum testnet.

What you'll learn

In this tutorial series, you'll create a Smart Account and send tokens using ERC-4337. The example app addresses how to create ERC-4337 accounts and build simple User Operation. In the example app, users can send tokens from the command line.

If you are new to account abstraction, we recommend the introduction to account abstraction and an overview of ERC-4337.


1. Download

Clone the ERC-4337 Examples repository to download the scripts. This example is a basic command-line wallet application.

git clone https://github.com/stackup-wallet/erc-4337-examples.git

2. Install

This example uses the userop.js library for building user operations. You can think of it like ethers.js but for ERC-4337. Install it, and all other dependencies.

yarn install

3. Initialize your configuration

You can now initialize your local configuration.

yarn run init

A config.json file will be created. The file will look like this:

{
  "rpcUrl": "https://api.stackup.sh/v1/node/API_KEY",
  "signingKey": "0x876d6e83487dc265a65066449b6fce5a1edfddfb0d67b71df8a9306c5324f192",
  "entryPoint": "0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789",
  "simpleAccountFactory": "0x9406Cc6185a346906296840746125a0E44976454",
  "paymaster": {
    "rpcUrl": "https://api.stackup.sh/v1/paymaster/API_KEY",
    "context": {}
  }
}

Anatomy of the config.json file

KeyValue
rpcUrlURL of an ERC-4337 bundler and node
signingKeyPrivate key that can sign transactions for the Smart Account
entryPointAddress of the EntryPoint contract
simpleAccountFactoryAddress of the factory that will create the account
paymasterURL of the Paymaster service you are using and context (optional)

In this quick start you will only need to set the rpcUrl.

Set the rpcUrl

You can create a free bundler instance at app.stackup.sh.

Create an account and select the Polygon Mumbai network for the instance and ensure the version is set to 0.6.x. Once the instance is created, click the copy icon to view the instance URL. Copy it and replace the default rpcUrl in config.json with the generated address.

Copy the RPC URL from the Stackup user portal.

You can leave the paymaster blank. If you want to try a paymaster, see the Paymaster Example.

4. Create an account

Create an account using the factory simpleAccountFactory defined in the configuration file.

yarn run simpleAccount address

An address will be returned. At this point, the Smart Account has not been deployed. ERC-4337 account addresses are deterministic, so you don't need to deploy the contract to know its address.

5. Fund the account

You will now need to deposit the native token of the blockchain you are using into your new Smart Account. Since this tutorial uses the Polygon Mumbai testnet, you will deposit MATIC into the account on Mumbai.

Navigate to a faucet, such as https://faucet.polygon.technology/. Enter the account address from step 4 and claim the testnet token.

πŸ“˜

Faucets do not send directly to smart contracts. You must deposit tokens from the faucet before your first transaction.

6. Initiate the transfer

The simpleAccount transfer command allows you to transfer the native token from the smart contract account to any address. It will create a User Operation, sign it, and send it to the Bundler:

yarn run simpleAccount transfer --to <address> --amount <eth>

You can then find the transaction using a block explorer like polygonscan!

Next steps

Stackup's infrastructure is compatible with any ERC-4337 SDK, Smart Account, and Paymaster. You can find some of the leading ERC-4337 developer SDKs here.

Additionally Stackup maintains userop.js, an MIT-licensed library for creating User Operations that is the backbone of many of these SDKs. If you can't find an SDK that meets all of your needs, we recommend building directly from this library or extending an open source SDK using userop.js.

Wondering what account abstraction means for your company?

You may be interested in a Discovery package.

Troubleshooting

During the quickstart you may encounter these common error messages.

AA21 didn't pay prefund is an error from the bundler that indicates that the account does not have enough funds. This usually means that you did not fund your account in step 5.

400 bad request: invalid tracer value or This method is not whitelisted are responses that you may get if you are running your own bundler locally and using an external node provider. Most node providers do not support the custom tracing that bundlers need. Either run your own local node or use Stackup's bundler service.

entryPoint: Implementation not supported means that the bundler being used for the transaction does not support the selected Entry Point. If you are using a Stackup bundler, we recommend setting your bundler instance to the latest version and the canonical Entry Point contract.


What’s Next

You've successfully sent a transaction using a smart contract account and ERC-4337! Now that you've done a simple transfer with your account, you can begin working with ERC-4337.