UNPKG

@funkit/connect

Version:

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

133 lines (132 loc) 4.76 kB
import type { Dnum } from 'dnum'; import type { PaymentMethod } from '../../domains/paymentMethods'; import type { FunkitCheckoutQuoteResult } from '../../domains/quote'; export declare enum QuoteMode { EXACT_IN = "EXACT_IN", EXACT_OUT = "EXACT_OUT", ONE_TO_ONE = "ONE_TO_ONE" } export declare enum QuoteBuildType { EXACT_IN = "exactIn", EXACT_OUT = "exactOut" } export declare enum RetryActionType { CALIBRATE_AMOUNT = "calibrateAmount", REFETCH_BALANCE = "refetchBalance", GIVE_UP = "giveUp" } export declare enum RetryErrorReason { INSUFFICIENT_BALANCE = "insufficientBalance", INSUFFICIENT_AMOUNT = "insufficientAmount", INTERNAL = "internal" } export declare enum QuickOptionType { SET_TOKEN_AMOUNT = "setTokenAmount", SET_FIAT_AMOUNT = "setFiatAmount" } export interface QuoteRetryError { reason: RetryErrorReason; maxTargetAssetAmount?: Dnum; minTargetAssetAmount?: Dnum; } export type QuoteBuildResult = { type: QuoteBuildType.EXACT_IN; fromAmountBaseUnit: string; } | { type: QuoteBuildType.EXACT_OUT; toAmountBaseUnit: string; }; export type RetryAction = { type: RetryActionType.CALIBRATE_AMOUNT; newAmountBaseUnit: Dnum; } | { type: RetryActionType.REFETCH_BALANCE; } | { type: RetryActionType.GIVE_UP; }; export interface QuoteDisplayAmounts { /** The primary dollar/token amount shown at the top of the confirmation screen */ confirmationScreenDisplayAmount: string; /** "You send" line */ sendAmount: string; /** "You receive" line — null when the value cannot be derived (e.g. missing metadata) */ receiveAmount: string | null; /** Whether the send amount is exact (vs estimated) */ isSendExact: boolean; /** Whether the receive amount is exact (vs estimated) */ isReceiveExact: boolean; /** Total fees in USD. '0' for ONE_TO_ONE */ feesUsd: string; } export interface MaxSendableParams { /** Token balance as a Dnum */ tokenBalanceBaseUnit: Dnum; /** Whether the token is the chain's native token (ETH, MATIC, etc.) */ isNativeToken: boolean; /** Estimated gas price in wei (required for native tokens) */ gasEstimateWei?: Dnum; /** Token price in USD (used for fee estimation in token terms) */ unitPrice: number; /** Checkout target chain ID */ targetChainId: string; /** Source asset chain ID */ assetChainId: string; /** Payment method */ paymentMethod: PaymentMethod | undefined; } export interface UsdAvailableAmountParams { /** Source asset USD value */ assetUsdAmount: number | null | undefined; /** Checkout target chain ID */ targetChainId: string; /** Source asset chain ID */ assetChainId: string | undefined; /** Payment method */ paymentMethod: PaymentMethod | undefined; } export interface QuoteBuilder { readonly mode: QuoteMode; /** Whether the quote mode is EXACT_IN */ isExactIn(): boolean; /** Compute the USD available amount for the source asset, accounting for fees where applicable */ getUsdAvailableAmount(params: UsdAvailableAmountParams): number | null; /** Compute max sendable token amount for the max button */ getMaxSendableAmount(params: MaxSendableParams): Dnum; /** Build the quote request params from the user's input */ buildQuoteRequest(params: { tokenAmountBaseUnit: Dnum; }): QuoteBuildResult; /** Handle quote failure. Returns action to take. */ handleRetry(params: { error: QuoteRetryError; }): RetryAction; /** Derive display amounts for confirmation screen. */ getDisplayAmounts(params: { quote: FunkitCheckoutQuoteResult; /** The user's input in human-readable form (e.g., 15.9) */ inputAmount: number; /** Target asset ticker (e.g. "USDC") — used for stablecoin display logic */ targetAssetTicker: string; }): QuoteDisplayAmounts; /** Whether the confirmation screen can show a dollar amount before the quote resolves */ hasKnownAmountBeforeQuote(config: { targetAssetAmount?: number; targetAssetTicker: string; }): boolean; /** Compute the token/fiat amount for a quick-option percentage button. * Returns the action to dispatch to the state reducer. */ computeQuickOptionAmount(params: { percentage: number; tokenBalanceBaseUnit: Dnum | null; frozenUnitPrice: number | null; usdAvailableAmount: number | null; maxSendableAmount: Dnum | null; }): { type: QuickOptionType.SET_TOKEN_AMOUNT; tokenAmountBaseUnit: Dnum; frozenUnitPrice: number; } | { type: QuickOptionType.SET_FIAT_AMOUNT; fiatAmount: number; }; }