Automated summary
Provides a complete TypeScript function that chains an ERC20 approval to Permit2 followed by a Permit2 approval to a spender, including the reset-to-zero pattern for tokens like USDT.
Strengths
- Handles non-standard ERC20s such as USDT through a reset-to-zero fallback when the direct approve fails
- Reads existing allowances before each write to avoid redundant transactions
- Sets a three-day expiration on the Permit2 approval using the PERMIT2_ADDRESS constant and a typed ABI
- Waits for each transaction receipt so the two-step sequence stays deterministic
Limitations
- The reset-to-zero path keys off a generic catch block, so any unrelated revert also triggers the two-transaction sequence
- The three-day expiration is hardcoded, with no refresh or renewal logic
- Assumes writeContractAsync, publicClient and the owner address are supplied by the caller, with no error handling beyond the approval itself
Use it when you need an end-to-end implementation of the two-layer ERC20-then-Permit2 approval flow in a viem-based TypeScript dApp.
Avoid it if you only support standard tokens, want infinite approvals, or prefer off-chain Permit2 signatures over an on-chain approve.
Security analysis
SafeThis is a pure instructional TypeScript code snippet for an on-chain ERC20/Permit2 approval flow. It contains no shell commands, obfuscation, credential handling, or exfiltration. All actions are standard smart-contract reads/writes performed through an injected wallet client, which is normal and expected for the blockchain domain. No destructive or misleading behavior is present.
No concerns found
How this analysis is madeExamples
Write a TypeScript function using viem that makes sure my swap contract has enough allowance: first approve the ERC20 to Permit2, then approve Permit2 to spend for my spender, handling USDT's reset-to-zero requirement.My approve call to Permit2 reverts for USDT. Can you implement the reset-to-zero pattern with a fallback so the approval still succeeds?Create an ensureSpendingLimits helper using PERMIT2_ADDRESS that checks both the ERC20 allowance and the Permit2 allowance before writing, and sets a 3-day expiration.name: permit2-allowance-flow description: Complete approval flow with reset-to-zero pattern. Use when implementing full ERC20 and Permit2 approval workflow.
Permit2 Allowance Transfer - Complete Flow
Full implementation with reset-to-zero pattern for tokens that require it.
Constants
const PERMIT2_ADDRESS = '0x000000000022D473030F116dDEE9F6B43aC78BA3'
const PERMIT2_APPROVE_ABI = [
{
inputs: [
{ name: 'token', type: 'address' },
{ name: 'spender', type: 'address' },
{ name: 'amount', type: 'uint160' },
{ name: 'expiration', type: 'uint48' }
],
name: 'approve',
outputs: [],
stateMutability: 'nonpayable',
type: 'function'
}
] as const
Complete Approval Function
async function ensureSpendingLimits(
tokenAddress: string,
spenderAddress: string,
amountNeeded: bigint,
writeContractAsync: (config: WriteContractParameters) => Promise<Hash>,
publicClient: PublicClient,
address: string
) {
// Step 1: Ensure ERC20 → Permit2 allowance
const currentErc20Allowance = await publicClient.readContract({
address: tokenAddress,
abi: erc20Abi,
functionName: 'allowance',
args: [address, PERMIT2_ADDRESS]
})
if (currentErc20Allowance < amountNeeded) {
// Some tokens (like USDT) require resetting to 0 first
try {
const hash = await writeContractAsync({
address: tokenAddress,
abi: erc20Abi,
functionName: 'approve',
args: [PERMIT2_ADDRESS, amountNeeded]
})
await publicClient.waitForTransactionReceipt({ hash })
} catch {
// Reset to 0, then approve
const hash0 = await writeContractAsync({
address: tokenAddress,
abi: erc20Abi,
functionName: 'approve',
args: [PERMIT2_ADDRESS, 0n]
})
await publicClient.waitForTransactionReceipt({ hash: hash0 })
const hash1 = await writeContractAsync({
address: tokenAddress,
abi: erc20Abi,
functionName: 'approve',
args: [PERMIT2_ADDRESS, amountNeeded]
})
await publicClient.waitForTransactionReceipt({ hash: hash1 })
}
}
// Step 2: Ensure Permit2 → Spender allowance
const currentPermit2Allowance = await publicClient.readContract({
address: PERMIT2_ADDRESS,
abi: [{
inputs: [
{ name: 'owner', type: 'address' },
{ name: 'token', type: 'address' },
{ name: 'spender', type: 'address' }
],
name: 'allowance',
outputs: [
{ name: 'amount', type: 'uint160' },
{ name: 'expiration', type: 'uint48' },
{ name: 'nonce', type: 'uint48' }
],
stateMutability: 'view',
type: 'function'
}],
functionName: 'allowance',
args: [address, tokenAddress, spenderAddress]
})
if (currentPermit2Allowance[0] < amountNeeded) {
const threeDaysSecs = 3n * 24n * 60n * 60n
const expiration = BigInt(Math.floor(Date.now() / 1000)) + threeDaysSecs
const hash = await writeContractAsync({
address: PERMIT2_ADDRESS,
abi: PERMIT2_APPROVE_ABI,
functionName: 'approve',
args: [tokenAddress, spenderAddress, amountNeeded, expiration]
})
await publicClient.waitForTransactionReceipt({ hash })
}
}
From the file to a first result
The download contains exactly the content displayed on this page. This copy may differ from the latest version in the source repository.
View the source and supporting filesThis download contains SKILL.md only. Any scripts, references, templates and dependencies are not included. Get the required files from the source, preserving their directory structure, before continuing.
1. Download the file
Download SKILL.mdFrom the root of a test project, save the file as .skill-downloads/permit2-allowance-flow-with-reset-mryiqfxg/SKILL.md, or use the command below. This folder is for inspecting the skill before installation.
Command for macOS/Linux or WSL: requires a POSIX shell and curl. On Windows without WSL, use the download button and create the folder manually.
mkdir -p '.skill-downloads/permit2-allowance-flow-with-reset-mryiqfxg' && curl --fail --show-error --location 'https://skills-guides.com/api/skills/permit2-allowance-flow-with-reset-mryiqfxg/download' --output '.skill-downloads/permit2-allowance-flow-with-reset-mryiqfxg/SKILL.md.part' && mv '.skill-downloads/permit2-allowance-flow-with-reset-mryiqfxg/SKILL.md.part' '.skill-downloads/permit2-allowance-flow-with-reset-mryiqfxg/SKILL.md'2. Check prerequisites
Read SKILL.md before using it. If it references scripts or external services, inspect them and prepare the required access in your test environment.
No recognized prerequisites section in this file. This does not mean it is self-contained: libraries, tools and access requirements may be mentioned elsewhere in the content or source repository.
The platforms on this page are catalog indications. The paths below follow tool documentation; they do not prove this skill has been run in those tools. No minimum tool version is confirmed for this listing.
3. Load the skill
After inspection, copy the complete folder into your project using one of the commands below, then open a new tool session. The command stops if a folder with the same name already exists: compare the files before replacing it.
Claude Code
test -f '.skill-downloads/permit2-allowance-flow-with-reset-mryiqfxg/SKILL.md' && test -s '.skill-downloads/permit2-allowance-flow-with-reset-mryiqfxg/SKILL.md' && mkdir -p '.claude/skills' && test ! -e '.claude/skills/permit2-allowance-flow' && cp -R '.skill-downloads/permit2-allowance-flow-with-reset-mryiqfxg' '.claude/skills/permit2-allowance-flow'Cursor
test -f '.skill-downloads/permit2-allowance-flow-with-reset-mryiqfxg/SKILL.md' && test -s '.skill-downloads/permit2-allowance-flow-with-reset-mryiqfxg/SKILL.md' && mkdir -p '.cursor/skills' && test ! -e '.cursor/skills/permit2-allowance-flow' && cp -R '.skill-downloads/permit2-allowance-flow-with-reset-mryiqfxg' '.cursor/skills/permit2-allowance-flow'Codex
test -f '.skill-downloads/permit2-allowance-flow-with-reset-mryiqfxg/SKILL.md' && test -s '.skill-downloads/permit2-allowance-flow-with-reset-mryiqfxg/SKILL.md' && mkdir -p '.agents/skills' && test ! -e '.agents/skills/permit2-allowance-flow' && cp -R '.skill-downloads/permit2-allowance-flow-with-reset-mryiqfxg' '.agents/skills/permit2-allowance-flow'OpenSkills
OpenSkills must already be installed. The local install/read procedure is checked with OpenSkills 1.5.0, which requires Node.js ≥ 20.6.0. Catalog slugs are not resolved automatically.
Official OpenSkills installationopenskills install './.skill-downloads/permit2-allowance-flow-with-reset-mryiqfxg'openskills read 'permit2-allowance-flow-with-reset-mryiqfxg'Reading should display the installed file content and its base directory. It does not run the skill or install its dependencies.
For an agent that reads AGENTS.md, then sync the index. This command changes AGENTS.md: review the diff before using it.
openskills syncManual method or another tool: attach SKILL.md to a conversation, or open it in an agent that can read files. Include any required supporting resources. No universal folder is recognized by every tool.
4. Try a first task
Use a short, synthetic input: an API endpoint, a small document or an interface brief. In a new conversation, attach the file or give the agent its path, then adapt this request:
Read .skill-downloads/permit2-allowance-flow-with-reset-mryiqfxg/SKILL.md and apply its instructions to this example: [describe a test input]. Identify any missing files, dependencies or access. Produce the result requested by the skill without inventing data or running unreviewed scripts.Expected result: output matching the skill instructions, or a precise list of missing prerequisites. A read confirmation alone does not validate a first use. If an external resource is unavailable, that path remains untested.
React Hook Form Best Practices
Development
Master React Hook Form: avoid anti-patterns, manage subscriptions, and handle reset, dirty state, and controlled inputs correctly.
Emotion to Mantine Migration
Development
Migrate Emotion styled-components to Mantine components with style props and CSS modules. Use when converting .styled.tsx files or removing @emotion imports.
Scope Creep Detector
Development
Analyzes git diffs against a stated intent to detect scope creep, unrelated files, broad PRs, and oversized changes, then suggests keep, split, or justify actions.