UNPKG

@funkit/connect

Version:

Funkit Connect SDK elevates DeFi apps via web2 sign-ins and one-click checkouts.

111 lines (110 loc) 4.27 kB
import { type PaymentMethodInfo } from '~/domains/paymentMethods'; import type { AssetHoldingsItem } from '~/utils/assets'; import type { FunkitCheckoutConfig } from '../../../providers/FunkitCheckoutContext'; export interface AmountInputInitOptions { checkoutConfig: FunkitCheckoutConfig; paymentMethodInfo: PaymentMethodInfo; sourceHolding: AssetHoldingsItem | null; /** Default amount, in fiat since that's the default mode */ defaultAmount: number | undefined; /** Target asset unit price, in USD */ unitPrice: number; /** Maximum checkout amount, in USD */ maxUsd: number; /** Minimum checkout amount, in USD */ minUsd?: number; /** Currency code for the currently selected fiat currency. Defaults to USD */ fiatCurrency?: string; /** Locale */ locale: Intl.LocalesArgument; /** Quick options, in fiat (not %) */ quickOptions?: number[]; /** Exchange rate of the `fiatCurrency` to USD */ fiatCurrencyExchangeRate: number | undefined; /** Whether meld is enabled */ meldEnabled: boolean; } export interface AmountInputState { /** Current amount, in target asset. Undefined if input is empty */ assetAmount: number | undefined; /** Current amount, in fiat. Undefined if input is empty */ fiatAmount: number | undefined; /** Fiat currency */ fiatCurrency: string; /** Current input value (with leading currency symbol if in fiat) */ inputValue: string; /** Whether the input is currently in fiat */ isInputInFiat: boolean; /** Locale */ locale: Intl.LocalesArgument; /** Estimated available amount, in USD (defaults to unlimited) */ usdAvailableAmount: number | null; /** Maximum amount, in USD (defaults to unlimited) */ usdMaxAmount: number | null; /** Minimum amount, in USD (defaults to 0) */ usdMinAmount: number | null; /** Exchange rate of the `fiatCurrency` to USD */ fiatCurrencyExchangeRate: number | undefined; /** Whether meld is enabled */ meldEnabled: boolean; /** Whether the input amount is in vault deposit mode * https://linear.app/funxyz/issue/CUS-142/product-enable-customers-to-pass-in-an-exchange-rate-for-vault **/ isVaultDeposit: boolean; } /** Derived state includes additional fields that are pure functions of base state */ export interface AmountInputDerivedState extends AmountInputState { inputDecimals: number; inputError: AmountInputError | null; inputAffix: { content: string; type: 'prefix' | 'suffix'; }; isValid: boolean; placeholder: string; } export type AmountInputAction = { /** Switches between fiat and crypto input mode */ type: 'toggleInputInFiat'; } | { /** Sets the input value as string */ type: 'setInputValue'; inputValue: string; unitPrice: number; } | { /** Sets the input value as fiat amount */ type: 'setFiatAmount'; fiatAmount: number; unitPrice: number; } | { /** Changes formatting to new currency code */ type: 'setFiatCurrency'; fiatCurrency: string; }; export type AmountInputError = { /** Current amount is above available balance */ type: 'aboveAvailable'; usdAvailableAmount: number; } | { /** Current amount is above maximum */ type: 'aboveMax'; usdMaxAmount: number; } | { /** Current amount is below minimum */ type: 'belowMin'; usdMinAmount: number; } | { /** Available balance is 0 */ type: 'noAvailableBalance'; } | { /** Input is empty */ type: 'noInput'; }; /** * Calculate initial input amount for account balance deposits. * Return half the balance and round up to nearest 10, 100, 1000 etc */ export declare function calcInitialFiatAmount(balance: number): number; export declare function initializeState({ checkoutConfig, defaultAmount, fiatCurrency, locale, maxUsd, minUsd, paymentMethodInfo, quickOptions, sourceHolding, unitPrice: realUnitPrice, fiatCurrencyExchangeRate, meldEnabled, }: AmountInputInitOptions): AmountInputState; export declare function reduceState(state: AmountInputState, action: AmountInputAction): AmountInputState; export declare function getDerivedState(state: AmountInputState, targetAssetTicker: string): AmountInputDerivedState;