feat(transaction): add useTokenAllowance and useRevokeAllowance hooks - #2635
Open
lau90eth wants to merge 1 commit into
Open
feat(transaction): add useTokenAllowance and useRevokeAllowance hooks#2635lau90eth wants to merge 1 commit into
lau90eth wants to merge 1 commit into
Conversation
Adds ERC-20 token allowance security hooks for OnchainKit. - useTokenAllowance: reads existing token spending allowances - useRevokeAllowance: revokes allowance by approving 0 - getTokenAllowance utility: reads onchain allowance via viem - Risk detection: flags infinite (max uint256) allowances - 5 tests covering: empty state, normal allowance, infinite allowance, API error, revoke action, revoking state Refs: coinbase#2572
|
@lau90eth is attempting to deploy a commit to the Coinbase Team on Vercel. A member of the Team first needs to authorize it. |
🟡 Heimdall Review Status
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Infinite token allowances are one of the most common exploit vectors on Base.
Users approve spending and forget — leaving funds permanently exposed to
contracts that may later be compromised or malicious.
OnchainKit has no way to read or revoke existing allowances.
This PR adds
useTokenAllowanceanduseRevokeAllowance— two security hooksthat let users audit and revoke ERC-20 approvals in one click.
What changed
useTokenAllowancehook — reads ERC-20 allowances for a connectedwallet across multiple tokens and spenders. Detects and flags infinite
(
type(uint256).max) approvals explicitly.useRevokeAllowancehook — revokes an allowance by callingapprove(spender, 0)viauseWriteContract. ExposesisRevokingstatefor UI feedback during the transaction.
getTokenAllowanceutility — onchain read using viemerc20AbiandreadContract. No external API dependency — reads directly from chain.TokenAllowancetype — standardized shape for allowance data includinginfinite detection flag.
src/transaction/index.tsUsage
API
Notes to reviewers
useTokenAllowancereads directly onchainvia viem
readContract+erc20Abi. Works on any EVM chain configuredin
OnchainKitProvider.amount === MaxUint256explicitlyas
isInfinite: true. This is the most common dangerous pattern.approve(spender, 0)which is the ERC-20standard for revoking. Compatible with all standard ERC-20 tokens.
useRevokeAllowanceusesuseWriteContractfrom wagmi — same pattern asexisting OnchainKit transaction hooks.
APIErrorpattern used across existing utilities.addresschanges.Testing
useTokenAllowance.test.ts— 4 testsuseRevokeAllowance.test.ts— 2 testsTest cases covered:
revoke()callsapprove(spender, 0)correctlyisRevokingstate exposed during transactionRisk
Low.
useTokenAllowanceis read-only — zero side effects, cannot modify state.useRevokeAllowancewrites onchain only when explicitly called by the user.The only state change is
approve(spender, 0)— reducing an allowance,never increasing it. This is strictly safer than the current state.
No existing hooks, components, or utilities are modified.