How to approve an external spender

Authorize smart contracts to spend tokens.

Overview

When interacting with other smart contracts, approving an external spender is requirement. For example, if you wanted to provide liquidity to a liquidity pool on a DEX, your tokens are held in a smart contract. Approving the pool's contract as a spender allows it to manage your tokens as part of the liquidity pool.

In this guide, I’ll show you how to approve external spenders like a liquidity pool contract. This guide will assume some basic knowledge covered in the Quick Start but I’ll quickly walk through all the steps again.

Import required objects

import {
FunWallet,
Auth,
configureEnvironment,
generatePrivateKey,
} from "@funkit/core";

Configuring the FunWallet environment

To begin, we need to set up the environment for your FunWallet. We’ll be using the Goerli testnet and a gasless sponsor. Please visit our dashboard to receive your own production apiKey.

await configureEnvironment({
chain: "goerli",
gasSponsor: {
sponsorAddress: "0x175C5611402815Eba550Dad16abd2ac366a63329",
},
apiKey: "ZrhepzWGxm74D0sqstuhT6dGrJxhoy8SZIToX6I3",
});

Creating Auth with a private key

We’ll use a private key as the authentication method and I’ll provide us with one.

const privateKey = generatePrivateKey();
const auth = new Auth({ privateKey });

Creating FunWallet with Auth

With the Auth instance that we created in the step before, we are now able to initialize and create your FunWallet.

const funWallet = new FunWallet({
users: [{ userId: auth.getAddress() }],
uniqueId: auth.getWalletUniqueId(),
});
funWallet.create(auth, userId);

Approving a spender

Now that we’ve created a FunWallet, we can approve a spender for our FunWallet. First we’ll need to specify the parameters required for approval.

  1. spender - This is the address of the smart contract or external wallet that will be allowed to withdraw tokens from your account. I’m going to pick a random contract address.

  2. token - Token that we approving spend of. Here I’m going to use USDC.

  3. amount - The number of tokens we’re approving spend of. (10 USDC)

const approveParams = {
spender: "0x1F98431c8aD98523631AE4a59f267346ea31F984",
token: "usdc",
amount: 10,
};

Executing the approve operation

Now that we have created the approve parameters. All we need to do is to create the operation and execute it.

const operation = await funWallet.tokenApprove(
auth,
await auth.getAddress(),
approveParams
);
const receipt = await funWallet.executeOperation(auth, operation);

Summary

Putting together all the pieces we have approve() that you can use to approve spend of any asset in a FunWallet.

,
approve.js
import { FunWallet, Auth, configureEnvironment, generatePrivateKey } from "@funkit/core"
const approve = async () => {
await configureEnvironment({
chain: "goerli",
gasSponsor: {
sponsorAddress: "0xCB5D0b4569A39C217c243a436AC3feEe5dFeb9Ad"
},
apiKey: "ZrhepzWGxm74D0sqstuhT6dGrJxhoy8SZIToX6I3"
})
const privateKey = generatePrivateKey()
const auth = new Auth({ privateKey })
const funWallet = new FunWallet({
users: [{ userId: await auth.getAddress() }],
uniqueId: await auth.getWalletUniqueId()
})
const approveParams = {
spender: "0x1F98431c8aD98523631AE4a59f267346ea31F984",
token: "usdc",
amount: 10
}
const operation = await funWallet.tokenApprove(auth, await auth.getAddress(), approveParams)
const receipt = await funWallet.executeOperation(auth, operation)
console.log(receipt)
}
approve()