{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "approve-button",
  "title": "Approve Button",
  "description": "ERC-20 approval gate: reads the allowance (useReadContract allowance) and shows an 'Approve <symbol>' TransactionButton until it covers the amount, then renders children (your action); skips straight to children when already approved. Exact amount by default, unlimited (maxUint256) opt-in. Refetches the allowance after approval. Exports useAllowance. For tokens that reject a non-zero to non-zero allowance change (mainnet USDT, or any token with resetFirst) it first sends 'Reset <token> approval' to set the allowance to 0.",
  "dependencies": [
    "wagmi",
    "viem"
  ],
  "registryDependencies": [
    "@0xui/transaction-button"
  ],
  "files": [
    {
      "path": "registry/default/components/approve-button.tsx",
      "content": "// Docs: https://0xui.io/components/approve-button.md\nimport type { ReactNode } from \"react\";\nimport { erc20Abi, maxUint256, type Address } from \"viem\";\nimport { useConnection, useReadContract } from \"wagmi\";\nimport { TransactionButton, type TransactionButtonProps } from \"@/components/transaction-button\";\n\nexport type UseAllowanceParameters = {\n  /** ERC-20 contract. */\n  token: Address;\n  /** Contract that will spend the tokens, e.g. a router. */\n  spender: Address;\n  /** Owner. Defaults to the connected account. */\n  owner?: Address;\n  chainId?: number;\n};\n\n/** Current ERC-20 allowance of `spender` over the owner's tokens. */\nexport function useAllowance({ token, spender, owner, chainId }: UseAllowanceParameters) {\n  const { address } = useConnection();\n  const account = owner ?? address;\n  return useReadContract({\n    address: token,\n    abi: erc20Abi,\n    functionName: \"allowance\",\n    args: [account!, spender],\n    chainId,\n    query: { enabled: Boolean(account) },\n  });\n}\n\n/**\n * Tokens that revert when a non-zero allowance is changed to another\n * non-zero value, as `chainId:address` in lower case. Mainnet USDT is the\n * well-known one.\n */\nconst RESET_FIRST = new Set([\"1:0xdac17f958d2ee523a2206206994597c13d831ec7\"]);\n\n/** True for a token whose leftover allowance must be set to 0 before it can be raised. */\nexport function requiresAllowanceReset(chainId: number, token: Address) {\n  return RESET_FIRST.has(`${chainId}:${token.toLowerCase()}`);\n}\n\nexport type ApproveButtonProps = Omit<\n  TransactionButtonProps,\n  \"contract\" | \"transaction\" | \"children\" | \"chainId\"\n> & {\n  /** Token to approve. */\n  token: { address: Address; symbol: string };\n  /** Contract that will spend the tokens. */\n  spender: Address;\n  /** Amount the next action needs, in the token's smallest unit. */\n  amount: bigint;\n  /** Chain the token lives on. */\n  chainId: number;\n  /** Approve the maximum instead of `amount`. Saves future approvals, but the spender can then move all of the token. */\n  unlimited?: boolean;\n  /**\n   * Set a non-zero allowance to 0 before raising it, as a first\n   * transaction. Defaults to true for mainnet USDT, which reverts otherwise.\n   */\n  resetFirst?: boolean;\n  /** Rendered once the allowance covers `amount`, usually the TransactionButton for the real action. */\n  children: ReactNode;\n};\n\n/**\n * Approval gate for ERC-20 actions. Shows \"Approve <token>\" while the\n * allowance is too low, then renders `children` (your action) once it\n * covers `amount`. Skips straight to `children` when nothing needs\n * approving. For tokens like USDT it first shows \"Reset <token> approval\",\n * which sets a leftover allowance to 0.\n */\nexport function ApproveButton({\n  token,\n  spender,\n  amount,\n  chainId,\n  unlimited = false,\n  resetFirst,\n  children,\n  onSuccess,\n  ...buttonProps\n}: ApproveButtonProps) {\n  const { status } = useConnection();\n  const allowance = useAllowance({ token: token.address, spender, chainId });\n\n  const approved = allowance.data !== undefined && allowance.data >= amount;\n  if (amount === 0n || approved) return <>{children}</>;\n\n  const checking = status === \"connected\" && allowance.isPending;\n  const reset =\n    (resetFirst ?? requiresAllowanceReset(chainId, token.address)) &&\n    allowance.data !== undefined &&\n    allowance.data > 0n;\n\n  return (\n    <TransactionButton\n      // A fresh button for the approval that follows a reset.\n      key={reset ? \"reset\" : \"approve\"}\n      {...buttonProps}\n      chainId={chainId}\n      contract={{\n        address: token.address,\n        abi: erc20Abi,\n        functionName: \"approve\",\n        args: [spender, reset ? 0n : unlimited ? maxUint256 : amount],\n      }}\n      disabledReason={checking ? \"Checking allowance…\" : buttonProps.disabledReason}\n      agentConfirm={\n        buttonProps.agentConfirm ??\n        (reset\n          ? `Reset ${token.symbol} approval: sets ${spender}'s allowance to 0`\n          : `Approve ${token.symbol}: lets ${spender} spend ${unlimited ? `all of your ${token.symbol}` : `the ${token.symbol} this action needs`}`)\n      }\n      successLabel={reset ? \"Approval reset\" : \"Approved\"}\n      resetAfter={false}\n      onSuccess={async (receipt) => {\n        await allowance.refetch();\n        onSuccess?.(receipt);\n      }}\n    >\n      {reset ? `Reset ${token.symbol} approval` : `Approve ${token.symbol}`}\n    </TransactionButton>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "docs": "Docs: https://0xui.io/components/approve-button.md",
  "categories": [
    "transactions"
  ],
  "type": "registry:component"
}