Get Remaining Access Key Limit
accessKey.getRemainingLimit
Gets the remaining spending limit for a key-token pair.
Usage
import { } from './viem.config'
const = await ..({
: '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEbb',
: '0x8ba1f109551bD432803012645Ac136ddd64DBA72',
: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
})
.('Remaining limit:', )
Remaining limit: { periodEnd: 1735689600n, remaining: 1000000n }import { , } from 'viem/tempo'
export const = .({
: .('0x...'),
})Recipes
Gate a Subscription Charge on Remaining Budget
Compare remaining with the charge amount before submitting a token.transfer, and use periodEnd to schedule the retry when the cap resets.
import { } from 'viem/utils'
import { } from './viem.config'
const = .('49.99', 6)
const { , } = await ..({
: '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEbb', // subscriber
: '0x8ba1f109551bD432803012645Ac136ddd64DBA72', // billing key
: '0x20c0000000000000000000000000000000000000',
})
if ( < )
.('Budget exhausted, retry at:', )
Budget exhausted, retry at: 1735689600nimport { , } from 'viem/tempo'
export const = .({
: .('0x...'),
})Monitor an Agent's Budgets Across Tokens
Query each token an agent key can spend to build a treasury dashboard of remaining budgets.
import { } from './viem.config'
const = [
'0x20c0000000000000000000000000000000000000', // pathUSD
'0x20c0000000000000000000000000000000000001', // alphaUsd
] as
const = await .(
.(async () => ({
,
...(await ..({
: '0x8ba1f109551bD432803012645Ac136ddd64DBA72',
,
})),
})),
)import { , } from 'viem/tempo'
export const = .({
: .('0x...'),
})Return Value
type ReturnType = {
/** End of the current period, or `undefined` for non-periodic limits. */
periodEnd: bigint | undefined
/** Remaining spending amount. */
remaining: bigint
}The remaining spending amount and period end timestamp.
Parameters
accessKey
- Type:
Address | AccessKeyAccount
Access key to read the limit of. Pass an access key address or an access key account.
account
- Type:
Account | Address - Default:
client.account
Account (or address) that owns the key.
token
- Type:
Address
Token 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.