Skip to main content
When using the StableFX API to deliver the funds onchain for an FX trade, you must grant an allowance to the Permit2 contract. This allows the Permit2 contract to transfer the USDC from your wallet to the FxEscrow contract. This guide shows an example of how to grant a USDC token allowance to the Permit2 contract using a Circle Wallets developer-controlled wallet. The Permit2 documentation provides additional examples of how to grant this allowance.

Prerequisites

Before you begin, ensure you have:
  • Installed Node.js and npm on your development machine
    • A project initialized with npm init -y
    • @circle-fin/developer-controlled-wallets and dotenv installed in your project
  • Created a Circle Developer Account
  • Created a .env file in your project root with the following variables defined:
    • CIRCLE_API_KEY
    • CIRCLE_ENTITY_SECRET
    • CIRCLE_WALLET_ID

Steps

The following steps show how to grant a USDC token allowance to the Permit2 contract using an EIP-1193 Ethereum wallet.

Step 1. Grant a USDC token allowance to the Permit2 contract

The following example code shows the process for granting a USDC token allowance to the Permit2 contract using an EIP-1193 Ethereum wallet.
JavaScript
const {
  initiateDeveloperControlledWalletsClient,
} = require("@circle-fin/developer-controlled-wallets");
const dotenv = require("dotenv");
dotenv.config();

async function executePermit2Approval() {
  const client = await initiateDeveloperControlledWalletsClient({
    apiKey: process.env.CIRCLE_API_KEY,
    entitySecret: process.env.CIRCLE_ENTITY_SECRET,
  });

  const tokenAddress = "0x3600000000000000000000000000000000000000"; // Token to approve (e.g., USDC)
  const spenderAddress = "0x1f91886C7028986aD885ffCee0e40b75C9cd5aC1"; // FxEscrow contract address
  const amount = "100.00"; // Enter the amount you want to approve
  const expiration = Math.floor(Date.now() / 1000) + 365 * 24 * 60 * 60; // Expiration for the approval

  const response = await client.createContractExecutionTransaction({
    walletId: process.env.CIRCLE_WALLET_ID,
    contractAddress: "0x000000000022D473030F116dDEE9F6B43aC78BA3", // Permit2 contract address
    abiFunctionSignature: "approve(address,address,uint160,uint48)", // Permit2 Approve ABI function signature
    abiParameters: [
      tokenAddress,
      spenderAddress,
      amount,
      expiration.toString(),
    ],
    fee: {
      type: "level",
      config: {
        feeLevel: "MEDIUM",
      },
    },
  });

  return response.data;
}