SpendController contract

SpendController is the smart contract that enforces agent budgets. It lets a user give AI agents limited, revocable, hierarchical permission to spend USDC from the user’s own crypto wallet. The contract never holds tokens: a top-up moves money straight from the owner’s wallet to the agent’s address.

Network Base Sepolia (eip155:84532)
Address 0xa8307F9b607FB596828b4e7Efd28093bDA9FD3A9
Token Circle test USDC 0x036CbD53842c5426634e7929541eC2318f3dCF7e
Compiler Solidity 0.8.30, OpenZeppelin Contracts v5
EIP-712 domain name Allotr SpendController, version 1, chain id 84532, verifying contract = the address above
License Apache-2.0
Audit not yet; mainnet deployment follows an independent audit

Everyone uses this single shared address; users don’t deploy anything. The contract has no owner, no admin, no pause and no upgrade, so using the shared address gives us no power over your money. A new version means a new address. Anyone can deploy their own copy if they prefer.


Design in short

  • Limits enforced on-chain: a per-period limit (for example a day), a total limit, and a required expiry date. At least one of the two limits must be set; the Allotr panel always sets both (total limit = the budget).
  • Money stays with the owner: the owner gives the contract a USDC allowance (approve or EIP-2612 permit). A top-up calls transferFrom(owner, agent, amount); nothing is kept in the contract.
  • Hierarchy: an agent whose grant allows it (canDelegate) can create narrower sub-grants for other agents, up to 4 levels including the root. A top-up from a sub-grant counts against every grant above it.
  • No ETH needed: every action has a signed (EIP-712) version that anyone, such as a relay, can submit. Signatures use random, unordered 32-byte nonces (like EIP-3009) and a deadline.
  • Optional fee for the sender: signed actions can carry a fee and a fee recipient. Both are part of what’s signed, so whoever submits the signature can’t change them. The fee goes from the owner’s wallet straight to the recipient. Actions you send yourself have no fee.

The grant

struct GrantParams {
    address agent;       // the only recipient of top-ups, and the address that signs top-up requests
    uint96  periodLimit; // most per period (0 = no period limit)
    uint32  period;      // period length in seconds (required if periodLimit is set)
    uint96  totalLimit;  // most over the grant's life (0 = no total limit)
    uint40  expiry;      // the grant stops working after this time (required)
    bool    canDelegate; // may the agent create sub-grants
}

Stored state per grant also includes the owner, token, parent (0 for a root grant), depth, spent, spentInPeriod, periodStart and revoked. Grant ids start at 1.

Periods are fixed windows counted from the moment the grant was created, not a rolling 24 hours. When a new window starts, the period counter goes back to zero.

Sub-grant rules: a sub-grant can’t be wider than its parent for any limit the parent has, and can’t outlive it. It may leave a limit out (for example the parent has “1 per day” and the child only “0.10 in total”); the parent’s limit still applies because every top-up checks the whole chain.


Functions

Creating grants

Function Who calls it Notes
createGrant(token, p) the owner Root grant from the caller’s own wallet. A USDC allowance to the contract is needed separately.
createGrantWithSig(owner, token, p, fee, feeRecipient, nonce, deadline, sig) anyone, with the owner’s signature
createGrantWithPermit(owner, token, p, fee, feeRecipient, nonce, deadline, sig, permit) anyone USDC permit + grant + fee in one transaction. This is what the panel uses.
createSubGrant(parentId, p) the parent grant’s agent Parent must have canDelegate.
createSubGrantWithSig(parentId, p, fee, feeRecipient, nonce, deadline, sig) anyone, with the parent agent’s signature The fee counts against the parent chain’s limits.

A root grant’s fee doesn’t count against the grant’s limits (the owner signs it, and it comes from the allowance). A sub-grant’s fee does count against the parent’s limits, so an agent can’t spend the owner’s money through fees beyond its own limit.

Top-ups

Function Who calls it Notes
topUp(grantId, amount) the grant’s agent Moves amount from the owner to the agent.
topUpWithSig(grantId, amount, fee, feeRecipient, nonce, deadline, sig) anyone, with the agent’s signature The recipient is always the grant’s agent; whoever submits it can’t redirect the money. amount + fee counts against the limits.

Every top-up walks from the grant up to the root. Each link must be active (not revoked, not expired) and have room for the amount in both limits, otherwise the whole transaction reverts. State is updated before the transfer, and the function is protected against re-entry.

Revoking

Function Who calls it Notes
revoke(grantId) the owner, the grant’s agent, or the agent of any grant above it Permanent. Revoking twice does nothing.
revokeWithSig(grantId, signer, nonce, deadline, sig) anyone, with an authorized signer’s signature Revocation is never charged.
invalidateNonce(nonce) any signer Cancels a signature you handed out but that hasn’t been submitted yet.

Sub-grants of a revoked grant don’t need to be revoked one by one: every top-up checks the whole chain, so they stop working too.

Reading

Function Returns
getGrant(grantId) the stored grant
available(grantId) the most that can be topped up right now, across the whole chain (0 if revoked or expired). The owner’s balance and allowance aren’t included; read those from the token.
grantCount() the latest grant id
nonceUsed(signer, nonce) whether a nonce has been used
domainSeparator() the EIP-712 domain separator
MAX_DEPTH() 4

Signed messages (EIP-712)

CreateGrant(address owner,address token,address agent,uint96 periodLimit,uint32 period,uint96 totalLimit,uint40 expiry,bool canDelegate,uint256 fee,address feeRecipient,bytes32 nonce,uint256 deadline)
CreateSubGrant(uint256 parentId,address agent,uint96 periodLimit,uint32 period,uint96 totalLimit,uint40 expiry,bool canDelegate,uint256 fee,address feeRecipient,bytes32 nonce,uint256 deadline)
TopUp(uint256 grantId,uint256 amount,uint256 fee,address feeRecipient,bytes32 nonce,uint256 deadline)
Revoke(uint256 grantId,address signer,bytes32 nonce,uint256 deadline)

Signatures from normal accounts (ECDSA) and from smart accounts (ERC-1271, including EIP-7702 upgraded accounts) are both accepted. A signature fails if its deadline has passed or its nonce has been used.


Events

Event When
GrantCreated(grantId, parentId, owner, agent, token, periodLimit, period, totalLimit, expiry, canDelegate) a grant or sub-grant is created
ToppedUp(grantId, agent, amount) money moves to an agent
FeePaid(grantId, recipient, fee) a fee is paid
Revoked(grantId, by) a grant is revoked
NonceInvalidated(signer, nonce) a signature is cancelled

The panel builds your list of grants from GrantCreated events, so anyone can check the same data on a block explorer.

Errors

Error Meaning
InvalidParams(reason) bad parameters (agent, expiry, period, no limit, token, fee recipient)
UnknownGrant(id) no such grant
GrantInactive(id) a grant in the chain is revoked or expired
NotAuthorized() the caller or signer isn’t allowed to do this
NotDelegable(parentId) the parent grant doesn’t allow sub-grants
MaxDepthExceeded() more than 4 levels
ExceedsParent(field) a sub-grant is wider than its parent
PeriodLimitExceeded(id, available) / TotalLimitExceeded(id, available) the amount doesn’t fit; available says how much would
ZeroAmount() a top-up of 0
SignatureExpired() / NonceAlreadyUsed() / InvalidSignature() signature problems

Gas

Measured on Base Sepolia (live) and in tests (--isolate):

Action Gas
createGrantWithPermit (permit + grant + fee, what the panel sends) ~262k live
topUpWithSig with a fee ~122k live (~102k in tests)
topUpWithSig, first USDC the agent ever receives ~124k live
topUp sent by the agent itself ~59k in tests
top-up from the bottom of a 4-level chain ~137k in tests
revokeWithSig ~69k live

For comparison, the payment from an agent to a seller (plain EIP-3009) is about 86k gas.


Allowance and the panel

USDC’s permit sets the allowance to an exact value; it doesn’t add to it. When you create a grant, the panel therefore asks for an allowance equal to what’s left of all your active root grants (total limit minus spent) plus the new grant’s budget and fee, read from the chain at signing time. Revoked grants aren’t counted, so the allowance doesn’t grow over time. The allowance is a shared pool for all your grants; that’s why the panel always sets a total limit equal to the budget.

Testing

The contract has Foundry unit, fuzz and invariant tests, and a live demo on Base Sepolia runs permit → signed grant → signed top-up → limit reached → revocation with real test USDC. The source code will be published under Apache-2.0 together with the rest of the open-source libraries.