Skip to content
LogoLogo

token.approve

Approves a spender to transfer TIP-20 tokens on behalf of the caller.

Usage

import { client } from './viem.config'
 
const { receipt } = await client.token.approveSync({
  amount: { formatted: '10.5' },
  spender: '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEbb',
  token: 'pathusd',
})
 
console.log('Transaction hash:', receipt.transactionHash)
Transaction hash: 0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef

By Token Address

When passing a token address, include amount.decimals so the formatted amount can be parsed.

import { client } from './viem.config'
 
const { receipt } = await client.token.approveSync({
  amount: { decimals: 6, formatted: '10.5' },
  spender: '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEbb',
  token: '0x20c0000000000000000000000000000000000000',
})

Base Amounts

Pass a bigint to use the token's base unit amount directly.

import { client } from './viem.config'
 
const { receipt } = await client.token.approveSync({
  amount: 10500000n,
  spender: '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEbb',
  token: 'pathusd',
})

Asynchronous Usage

The examples above use a *Sync variant of the action, that will wait for the transaction to be included before returning.

If you are optimizing for performance, you should use the non-sync token.approve action and wait for inclusion manually:

import { Actions } from 'viem/tempo'
import { client } from './viem.config'
 
const hash = await client.token.approve({
  amount: { formatted: '10.5' },
  spender: '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEbb',
  token: 'pathusd',
})
const receipt = await client.waitForTransactionReceipt({ hash })
 
const { args } 
  = Actions.token.approve.extractEvent(receipt.logs)

Return Type

type ReturnType = {
  /** Amount of tokens approved, in base units. */
  amount: bigint
  /** Token decimals used to derive `formatted`, if known. */
  decimals?: number | undefined
  /** Approved amount formatted with the token's `decimals`, if known. */
  formatted?: string | undefined
  /** Address of the token owner. */
  owner: Address
  /** Transaction receipt. */
  receipt: TransactionReceipt
  /** Address of the spender. */
  spender: Address
}

Parameters

amount

  • Type: bigint | { decimals?: number, formatted: string }

Amount to approve in base units. Use { formatted: '10.5' } to parse a human-readable decimal string for a chain-declared token, or include amount.decimals when using a token address.

spender

  • Type: Address

Address of the spender.

token

  • Type: string

Token to operate on: a chain-declared token name (resolving its address and decimals), a TIP-20 token id, or a token address.

account (optional)

  • Type: Account | Address

Account that will be used to send the transaction.

feeToken (optional)

  • Type: Address | bigint

Fee token for the transaction.

Can be an unpaused USD-denominated TIP-20 token address or ID. Use client.fee.validateToken({ token }) to validate a token before submitting a transaction or setting it as a fee preference.

feePayer (optional)

  • Type: Account | true

Fee payer for the transaction.

Can be a Viem Account, or true if a Fee Payer Service will be used.

gas (optional)

  • Type: bigint

Gas limit for the transaction.

maxFeePerGas (optional)

  • Type: bigint

Max fee per gas for the transaction.

maxPriorityFeePerGas (optional)

  • Type: bigint

Max priority fee per gas for the transaction.

nonce (optional)

  • Type: number

Nonce for the transaction.

nonceKey (optional)

  • Type: 'expiring' | bigint

Nonce key for the transaction. Use 'expiring' to use expiring nonces (TIP-1009), which enables concurrent transaction submission without nonce ordering.

validBefore (optional)

  • Type: number

Unix timestamp before which the transaction must be included.

validAfter (optional)

  • Type: number

Unix timestamp after which the transaction can be included.

throwOnReceiptRevert (optional)

  • Type: boolean
  • Default: true

Whether to throw an error if the transaction receipt indicates a revert. Only applicable to *Sync actions.