Build with Spend Permissions
Overview
Spend Permissions enable third-party signers to spend assets (native and ERC-20 tokens) from a user’s Smart Wallet. Once granted, Spend Permissions allow developers to move users’ assets without any further signatures, unlocking use cases like subscriptions & trading bots.
The following is a technical guide to build with Spend Permissions. If you want to learn more about high level details of this feature, check out the Spend Permissions feature page.
Let’s build!
This guide will walk you through a simple example of building an app that leverages Spend Permissions using OnchainKit, Viem and Wagmi.
The repository for the complete example of this demo can be found here.
A hosted version of the demo can be found here.
Smart contract deployment addresses for SpendPermissionManager.sol
can be found here.
Set up a basic app template using OnchainKit
Set up a boilerplate React/Next app by running the following command and following the instructions. Don’t worry about getting a Coinbase Developer Platform API Key, you don’t need one for this example. When prompted to use Coinbase Smart Wallet select “yes”.
This will generate an app that is ready to run and contains a wallet connection button that users can use to connect their smart wallet to the application.
From here, we’ll modify the app to assemble, sign, approve and use a spend permission to spend our users’ funds!
Set up your spender wallet
Add the following variables to your .env
:
Always secure your private keys appropriately! We insecurely use an environment variable in this demo for simplicity.
Our spender will need to sign transactions from our app, so we’ll create a wallet (private key and address) for our spender.
If you have Foundry installed, you can generate a new wallet via cast wallet new
. If you already have a
development keypair you can use that too. Assign the private key and address to
SPENDER_PRIVATE_KEY
and NEXT_PUBLIC_SPENDER_ADDRESS
, respectively.
Create a spender client
Our client is what our app will use to communicate with the blockchain.
Create a sibling directory to app
called lib
and add the following spender.ts
file to create your spender client.
Configure the Smart Wallet URL
In app/providers.tsx
, update your configuration based on your environment:
- For testnets:
- Set
keysUrl: "https://keys-dev.coinbase.com/connect"
- Replace all instances of
base
withbaseSepolia
(including the import)
- Set
- For mainnets:
- Leave
keysUrl
undefined (defaults to keys.coinbase.com) - Keep the default
base
chain from the template
- Leave
Your config in app/providers.tsx
should look like this for testnet:
Set up our interface to the SpendPermissionManager smart contract
Spend permissions are managed by a singleton contract called the SpendPermissionManager
. We’ll add some
configuration so our client knows how to interact with this contract.
Inside your /lib
directory, create a new subdirectory called /abi
. This is where we’ll store information about
smart contract interfaces and addresses.
Add a new file called SpendPermissionManager.ts
and copy and paste the code from this file.
Here’s an example of finding the ABI for any verified contract on Basescan.
Add a Subscribe button
Let’s create a button that will prompt a user to subscribe to our services by authorizing a spend permission for our app to spend their assets.
Create a subdirectory inside /app
called /components
and paste the following code into a new file called Subscribe.tsx
.
We’ll walk through what’s happening here in subsequent steps.
Also be sure to add this new Subscribe
button component to the top level component in page.tsx
.
You can delete lines 77-127 and put your button there.
Examine the SpendPermission object our user will sign
A SpendPermission
is the struct that defines the parameters of the permission.
See the solidity struct here.
You can see the spend permission object being defined in lines 49-59 of our Subscribe
component:
Observe the signTypedData hook
As part of our button handler handleSubmit
, in lines 61-91 of our subscribe component we call signTypedDataAsync
,
a Wagmi hook that will prompt our user to create a signature from their wallet across the details of the spend permission.
SpendPermissions use ERC-712 signatures.
Approve the spend permission onchain
Now that we have a signature from the user, we can approve the permission onchain by submitting the
signature and the permission details to approveWithSignature
on the SpendPermissionManager
contract.
Our handleCollectSubscription
function that’s defined in our Subscribe
will pass this signature and data to our
backend, so the spender client we created earlier can handle our onchain calls.
But wait, we don’t have any backend routes set up yet! Let’s define what should happen when we want to approve and use our spend permission.
Create a new subdirectory under /app
called collect
. Create a new file there called route.tsx
and paste the following
code:
This code is using our spender client to do two things:
- calls
approveWithSignature
to approve the spend permission - calls
spend
to make use of our allowance and spend our user’s funds
Try out your app
Run your app locally with npm run dev
and visit localhost:3000
.
When you click the “Subscribe” button you should be prompted to create or connect your Smart Wallet.
You can create a new Smart Wallet via the popup. Note that you’ll need a little ETH in this wallet to fund the deployment of your account. If you don’t have any testnet ETH, try this Coinbase faucet.
Note that we’ll need a little bit of base sepolia ETH in both wallet addresses (the “user” wallet and the “app” wallet). In a more involved implementation you would use a paymaster to eliminate this requirement. For now, If you don’t have any base sepolia ETH, try this Coinbase faucet.
Once your wallet is created and both wallets are funded, return to the app and click “Subscribe”, then sign the prompt to allow the spend permission.
Once you’ve subscribed, you should see a spend transaction hash show up on screen after a few seconds. You can prompt subsequent spends by clicking the “Collect Subscription” button. Click the transactions to check them out on Etherscan!
We’ve made it! 🎉
Our app successfully
- prompts the user to connect their Coinbase Smart Wallet to our app
- assembles a spend permission representing our recurring spending needs as an app
- retrieves a signature from the user authorizing this spend permission
- approves the spend permission onchain
- uses this permission to retrieve user assets within our allowance