Skip to content

Token API

Functions for token allowance management and token details.

Approve a spender to spend tokens on behalf of the owner.

approveAllowance(params: ApproveAllowanceParams): Promise<`0x${string}`>
ParameterTypeRequiredDescription
chainIdnumberYesChain ID (1, 137, or 8453)
tokenAddressAddressYesToken contract address
spenderAddressAddressYesAddress to approve
amountbigintYesAmount to approve in wei
ownerAddress?AddressNoOwner address (default: connected wallet)

Promise<0x${string}> - Transaction hash.

import { approveAllowance } from "cooperative";
import { parseUnits } from "viem";
const txHash = await approveAllowance({
chainId: 1,
tokenAddress: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
spenderAddress: "0x...", // Contract that needs approval
amount: parseUnits("1000", 6), // 1000 USDC (6 decimals)
});
console.log(`Approval transaction: ${txHash}`);
  • If ownerAddress is not provided, uses the currently connected wallet
  • Never approves unlimited amounts for security
  • Waits for transaction confirmation before returning

Check how much a spender is allowed to spend.

retrieveAllowance(params: RetrieveAllowanceParams): Promise<bigint>
ParameterTypeRequiredDescription
chainIdnumberYesChain ID
tokenAddressAddressYesToken contract address
ownerAddressAddressYesToken owner address
spenderAddressAddressYesApproved spender address

Promise<bigint> - Allowance amount in wei.

import { retrieveAllowance } from "cooperative";
const allowance = await retrieveAllowance({
chainId: 1,
tokenAddress: USDC_ADDRESS,
ownerAddress: userAddress,
spenderAddress: contractAddress,
});
console.log(`Allowance: ${allowance} wei`);

Get token details including metadata and current price.

retrieveTokenWithDetails(params: RetrieveTokenWithDetailsParams): Promise<TokenWithDetails>
ParameterTypeRequiredDescription
chainIdnumberYesChain ID
addressAddressYesToken contract address

Promise<TokenWithDetails> - Token details.

{
chainId: number;
address: Address;
name: string;
symbol: string;
decimals: number;
logoUrl?: string;
priceUsd?: number;
priceChange24h?: number;
}
import { retrieveTokenWithDetails } from "cooperative";
const token = await retrieveTokenWithDetails({
chainId: 1,
address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
});
console.log(`Token: ${token.name} (${token.symbol})`);
console.log(`Price: $${token.priceUsd}`);
console.log(`24h change: ${token.priceChange24h}%`);
import { retrieveAllowance, approveAllowance } from "cooperative";
import { parseUnits } from "viem";
async function ensureAllowance(
chainId: number,
tokenAddress: Address,
spenderAddress: Address,
requiredAmount: bigint,
): Promise<void> {
// Check current allowance
const currentAllowance = await retrieveAllowance({
chainId,
tokenAddress,
ownerAddress: userAddress,
spenderAddress,
});
// If insufficient, approve more
if (currentAllowance < requiredAmount) {
const approvalAmount = requiredAmount * 2n; // Approve 2x required
console.log(`Approving ${approvalAmount} tokens...`);
const txHash = await approveAllowance({
chainId,
tokenAddress,
spenderAddress,
amount: approvalAmount,
});
console.log(`Approval transaction: ${txHash}`);
// Wait for confirmation (optional)
import { waitReceipt } from "cooperative";
await waitReceipt({ hash: txHash, chainId });
console.log("Approval confirmed");
} else {
console.log("Sufficient allowance already exists");
}
}

For native tokens (ETH, MATIC), use the special address:

import { NATIVE_TOKEN_ADDRESS } from "cooperative";
// ETH on Ethereum
const ethAddress = NATIVE_TOKEN_ADDRESS; // '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE'
// No approval needed for native tokens in swaps
// The SDK handles this automatically
import { approveAllowance } from "cooperative";
try {
const txHash = await approveAllowance(params);
} catch (error) {
if (error.message.includes("user rejected")) {
console.log("User cancelled approval");
} else if (error.message.includes("insufficient funds")) {
console.log("Not enough ETH for gas");
} else if (error.message.includes("execution reverted")) {
console.log("Token contract rejected approval");
} else {
console.error("Approval failed:", error);
}
}