Skip to content

useCoinbaseAmount

Manage the withdrawal amount input (crypto + fiat) and read min / max / fee-reserve limits from the current selection.

When to use

On the amount form. Prefer this over wiring useCoinbaseLimits yourself when you are inside CoinbaseProvider.

Dual amount

The hook stores two linked strings:

  • amount — full-precision crypto (canonical; use for submit)
  • amountDisplay — bind this to the crypto <input>
  • amountFiat / setAmountFiat — fiat side (2 dp), recomputed from exchangeRate

Writing either side recomputes the other. Crypto remains the source of truth for validation and buildWithdrawalRequest. When isFiatAvailable is false (no exchange rate), hide the fiat field — setAmountFiat is a no-op.

Example

tsx
import { useCoinbaseAmount, useCoinbaseToken } from '@swapped/connect-sdk/react'

function AmountField() {
  const { selectedCurrency } = useCoinbaseToken()
  const {
    amountDisplay,
    setAmount,
    amountFiat,
    setAmountFiat,
    setMax,
    formatted,
    error,
    warning,
    setTouched,
    isValid,
    isFiatAvailable,
  } = useCoinbaseAmount()

  return (
    <div>
      <input
        value={amountDisplay}
        onChange={event => setAmount(event.target.value)}
        onBlur={() => setTouched(true)}
        inputMode="decimal"
      />
      {isFiatAvailable && (
        <input
          value={amountFiat}
          onChange={event => setAmountFiat(event.target.value)}
          onBlur={() => setTouched(true)}
          inputMode="decimal"
        />
      )}
      <button type="button" onClick={setMax}>
        Max
      </button>
      <p>
        Between {formatted.minAmount} and {formatted.maxAmount}{' '}
        {selectedCurrency}
        {formatted.minAmountFiat
          ? ` ($${formatted.minAmountFiat} – $${formatted.maxAmountFiat})`
          : ''}
      </p>
      {error && <p>{error.crypto.message}</p>}
      {error?.fiat && <p>{error.fiat.message}</p>}
      {warning && <p>{warning.crypto.message}</p>}
      <p>{isValid ? 'Ready' : 'Fix amount'}</p>
    </div>
  )
}

setAmount sanitizes crypto input (commas → dots, digits, single decimal point, leading zeros stripped) and truncates to the selected token’s display decimals (getTokenDisplayDecimals, overridable via CoinbaseProvider maxDecimals).

setAmountFiat sanitizes to 2 decimals and derives crypto at full token decimals so a fiat edit does not silently lose value. Bind the crypto input to amountDisplay, not amount.

Translating validation errors

error / warning are CoinbaseAmountError ({ crypto, fiat }). Both halves share the same code; the fiat half is null when no exchange rate. Switch on code for i18n:

tsx
import type { CoinbaseAmountError } from '@swapped/connect-sdk'

function translateAmountError(error: CoinbaseAmountError): string {
  switch (error.crypto.code) {
    case 'below_min':
      return `Minimum ${error.crypto.metadata.formattedMinAmount}`
    case 'above_balance':
      return `Balance is ${error.crypto.metadata.formattedBalanceAmount}`
    case 'above_spendable':
      return `Maximum ${error.crypto.metadata.formattedMaxAmount}`
    case 'amount_zero':
      return 'Amount cannot be 0'
    case 'invalid_amount':
    default:
      return error.crypto.message
  }
}

In 'balance' mode (default on CoinbaseProvider), above_spendable lands in warning instead of error — submit still works and startWithdrawal auto-caps via onAmountAdjusted. In 'spendable' mode it blocks as error.

Returns

FieldPurpose
amount / amountDisplay / setAmount / amountFloatCrypto amount (precise + display + setter)
amountFiat / setAmountFiat / amountFiatFloatFiat amount
exchangeRate / isFiatAvailableRate availability for the fiat side
setMaxFill Max for the current mode (raw balance or spendable)
minAmount / maxAmount / balanceMax / feeReserve + fiat mirrorsLimits
formattedDisplay-ready crypto and fiat limit strings
touched / setTouched / error / warning / isValid / requiresAdjustmentDual validation (error blocks; warning is soft spendable)
isLoading / limitsError / refetchLimitsLimits fetch state

Requires CoinbaseProvider.