Get Token Allowance
token.getAllowance
Gets the TIP-20 allowance a spender has over an account's tokens.
Usage
import { } from './viem.config'
const = await ..({
: '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEbb',
: '0x70997970C51812dc3A010C7d01b50e0d17dc79C8',
: '0x20c0000000000000000000000000000000000000',
})
.('Allowance:', )
Allowance: { amount: 10500000n, decimals: 6, formatted: '10.5' }import { , } from 'viem/tempo'
export const = .({
: .('0x...'),
})Recipes
Verify an Allowance Covers the Next Charge
Compare the base-unit amount with the upcoming charge before pulling funds with token.transferSync, so a shortfall becomes a renewal prompt instead of a reverted transaction.
import { } from 'viem/utils'
import { } from './viem.config'
const = '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEbb'
const = '0x20c0000000000000000000000000000000000000'
const = .('19.99', 6)
const = await ..({
: ,
: ..,
,
})
if (. >= )
await ..({
: ,
: ,
: ..,
,
})import { , } from 'viem/tempo'
export const = .({
: .('0x...'),
})Return Value
type ReturnType = {
/** Allowance in the token's base units. */
amount: bigint
/** Token decimals used to derive `formatted`. */
decimals: number
/** Allowance formatted as a human-readable decimal string. */
formatted: string
}The remaining allowance, both in base units (amount) and as a human-readable
decimal string (formatted, derived from the token's decimals).
Parameters
account
- Type:
Address
Account that owns the tokens.
decimals
- Type:
number
Decimals used to convert between base units and the human-readable amount.
Inferred from the Client's tokens set when token matches a declared token;
otherwise fetched from the token contract when needed.
spender
- Type:
Address
Spender of the tokens.
token
- Type:
Address | bigint
Token to operate on: a TIP-20 token id or a contract address.
blockNumber (optional)
- Type:
bigint
Block number to read the state from.
blockOverrides (optional)
- Type:
BlockOverrides
Block overrides to apply to the state.
blockTag (optional)
- Type:
BlockTag
Block tag to read the state from.
stateOverride (optional)
- Type:
StateOverride
State override to apply.