# Approve Button

> ERC-20 approval gate. Shows "Approve" until the allowance covers the amount, then your action.

- Install: `npx shadcn@latest add @0xui/approve-button`
- Registry JSON: https://0xui.io/r/approve-button.json
- Source: https://github.com/fimbul-lab/0xui/blob/main/registry/default/components/approve-button.tsx

Wrap the action that spends a token. While the allowance is too low it shows "Approve USDC" as a [Transaction Button](https://0xui.io/components/transaction-button.md). Once the approval is mined, it renders your action instead. If the allowance already covers the amount, it skips straight to the action.

Connect the **Demo Wallet** to try it.

## Installation

```bash
npx shadcn@latest add @0xui/approve-button
```

## Usage

```tsx
import { ApproveButton } from "@/components/approve-button";

<ApproveButton token={usdc} spender={router} amount={amount} chainId={1}>
  <TransactionButton chainId={1} contract={swapCall}>Swap</TransactionButton>
</ApproveButton>;
```

## Exact or unlimited

By default it approves exactly `amount`. That's safest: the spender can't move more than this action needs. `unlimited` approves the maximum, which saves future approvals but lets the spender move all of the token. If you offer it, let the user choose.

```tsx
<ApproveButton unlimited={rememberApproval} …>
```

### Tokens that need a reset

Some tokens refuse to change a non-zero allowance to another non-zero value. USDT on mainnet is the well-known one. When such a token has a leftover allowance that is too low, the button first shows "Reset USDT approval", which sets it to `0`, and then "Approve USDT". That is two transactions.

Mainnet USDT is detected by address. For any other token that behaves this way, pass `resetFirst`:

```tsx
<ApproveButton token={token} spender={router} amount={amount} chainId={1} resetFirst>
  {action}
</ApproveButton>
```

## Props

Also accepts [Transaction Button](https://0xui.io/components/transaction-button.md) props such as `onSuccess` and `variant`.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `token` | `{ address, symbol }` | — | Token to approve. |
| `spender` | `Address` | — | Contract that will spend it. |
| `amount` | `bigint` | — | Amount the action needs. |
| `chainId` | `number` | — | Chain the token is on. |
| `unlimited` | `boolean` | `false` | Approve the maximum. |
| `resetFirst` | `boolean` | `true` for mainnet USDT | Set a leftover allowance to `0` before raising it. |
| `children` | `ReactNode` | — | Shown once approved. |

Headless: `useAllowance({ token, spender })`.
