Skip to content

useExchangePayAmount ​

Manage the deposit amount input and read the minimum / maximum from the current currency selection.

When to use ​

On the amount form. Prefer this over reading selectedCurrency.minAmountFiat yourself when you are inside ExchangePayContextProvider.

Example ​

tsx
import {
  useExchangePayAmount,
  useExchangePayCurrency,
} from '@swapped/connect-sdk/react'

function AmountField() {
  const { selectedCurrency } = useExchangePayCurrency()
  const {
    amount,
    setAmount,
    formattedMinAmount,
    formattedMaxAmount,
    error,
    onBlur,
    setToMin,
    isValid,
  } = useExchangePayAmount()

  return (
    <div>
      <input
        value={amount}
        onChange={event => setAmount(event.target.value)}
        onBlur={onBlur}
        inputMode="decimal"
      />
      <p>
        Minimum ${formattedMinAmount}
        {formattedMaxAmount ? ` · Maximum ${formattedMaxAmount}` : ''}
        {selectedCurrency ? ` for ${selectedCurrency.symbol}` : ''}
      </p>
      {error && <p>{error.message}</p>}
      {!isValid && error?.code === 'below_min' && (
        <button type="button" onClick={setToMin}>
          Update order
        </button>
      )}
      <p>{isValid ? 'Ready' : 'Fix amount'}</p>
    </div>
  )
}

Values below the minimum are allowed while typing; validation surfaces via error / isValid after touch. setAmount sanitizes input (commas → dots, digits, always 2 decimal places). Exchange Pay is USD-only, so setFormatDefaults({ fiatDecimals, maxFiatDecimals }) does not change this.

Translating validation errors ​

error is structured ({ code, message, metadata }). Switch on code for i18n; use message as an English fallback:

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

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

Optional tighter max ​

The currency already has a config maximum (maxAmountFiat from remote config: default $1,000,000, KuCoin Pay $45,000, Bybit Pay $40,000). canSubmit includes that cap. Pass maxAmountFiat only to tighten it (Math.min with the config value):

tsx
const { amount, setAmount } = useExchangePayAmount({
  clampToMax: true,
  maxAmountFiat: 500, // cannot raise the config cap
})

When tightening the max this way, also gate submit with this hook’s isValid (selection canSubmit uses the currency / config maximum).

To bump the amount up to the new currency minimum when the token changes, pass adjustAmountOnCurrencyChange on ExchangePayContextProvider.

Options ​

OptionDefaultPurpose
clampToMaxfalseClamp input down to maxAmountFiat while typing
maxAmountFiat-Optional tighter maximum (cannot raise the config cap)

Returns ​

FieldPurpose
amount / setAmount / amountFloatControlled input
minAmountFiat / formattedMinAmountMinimum (from selected currency)
maxAmountFiat / formattedMaxAmountMaximum (currency / config, optionally tightened)
touched / setTouched / onBlur / resetTouchedTouch state
setToMinFill the currency minimum
error / isValidStructured validation (error.code for i18n)
isLoadingCurrencies still loading

Requires ExchangePayContextProvider.