@funkit/connect
Version:
Funkit Connect SDK elevates DeFi apps via web2 sign-ins and one-click checkouts.
185 lines (184 loc) • 9 kB
TypeScript
import type { FunAddress } from '@funkit/api-base';
import type { ApiCheckoutClientMetadata, ApiFunkitCheckoutActionParams, ApiFunkitCheckoutConfig } from '@funkit/utils';
import type { Address } from 'viem';
import type { PaymentMethodInfo } from '../../domains/paymentMethods';
import type { WithdrawalClient } from '../../wallets/Wallet';
import type { FunkitCheckoutQuoteResult } from '../FunkitHistoryContext';
export type FunkitCheckoutActionParams = ApiFunkitCheckoutActionParams;
export interface TokenInfo {
tokenAddress: Address;
tokenChainId: string;
}
export interface DynamicTargetAssetCandidate extends TokenInfo {
iconSrc: string;
tokenSymbol: string;
isDefault?: boolean;
badgeText?: string;
targetAssetMinAmount?: number;
/**
* Exchange rate of the target asset in terms of the staking token.
* Represents how many staking tokens one unit of the target asset can be exchanged for.
*
* Example: If `targetAsset`/`stakingToken` is USDT/hbUSDT and the rate is 1.01,
* 1 USDT can be exchanged for 1.01 hbUSDT.
*/
stakingTokenRate?: number;
}
export declare enum FunCheckoutStartingStep {
SOURCE_CHANGE = "source_change",
CONFIRMATION = "confirmation",
SELECT_ASSET = "select_asset"
}
export interface FunkitCheckoutConfig extends Omit<ApiFunkitCheckoutConfig, 'generateActionsParams' | 'customRecipient' | 'modalTitle' | 'iconSrc' | 'modalTitleMeta'> {
/** List of contract action params **/
generateActionsParams?: (targetAssetAmount: number, config?: {
targetAsset: Address;
targetChain: string;
}) => Promise<FunkitCheckoutActionParams[]>;
/** Custom recipient address of the checkout. If specified, a different checkout flow will be executed. It is not recommended to set this unless the Fun.xyz team advises it. **/
customRecipient?: FunAddress;
/** ****************************************
* Checkout ModalUI-related configurations *
*******************************************/
/** Title to show in the checkout modal. Defaults to "Checkout" **/
modalTitle?: string;
/** Additional information to show in the title of the checkout modal. Blank by default **/
modalTitleMeta?: string;
/** Icon to show in the checkout modal (50px x 50px). If not specified, no icon will be shown. **/
iconSrc?: null | string;
/** Title of the item being checked out. e.g. Staked Ether, XYZ Option, Solar Bond ABC **/
checkoutItemTitle: string;
/** amount of source token. it is not baseUnit **/
withdrawalSourceTokenBalance?: () => number;
/** Whether checkout should be started in Defi mode, which has various flow implications (most importantly targetAssetAmount must be set and is not editable throughout the checkout) */
isDefiMode?: boolean;
/** The step at which the checkout process should start */
startingStep?: FunCheckoutStartingStep;
/** Symbol of the source token. Required if startingStep is set to FunCheckoutStartingStep.CONFIRMATION. */
sourceTokenSymbol?: string;
/** ID of the source chain. Required if startingStep is set to FunCheckoutStartingStep.CONFIRMATION. */
sourceChain?: string;
/** Address of the source token on the source chain. Required if startingStep is set to FunCheckoutStartingStep.CONFIRMATION. */
sourceTokenAddress?: Address;
/** Address list of the tokens that cannot be used as source. e.g. tokens for staking vault */
disabledSourceTokens?: TokenInfo[];
/** *****************************
* Miscellaneous configurations *
********************************/
/** a list of candidate assets to be chosen dynamically */
dynamicTargetAssetCandidates?: DynamicTargetAssetCandidate[];
/** minimal USD needed to deposit */
getMinDepositUSD?: (asset: TokenInfo) => number;
/** For Checkout with Custom Action, QRCode needs special action type
* to specify the onchain action to execute after deposit */
qrcodeActionType?: string;
/** set this if you want enable vault like deposit*/
stakingToken?: DynamicTargetAssetCandidate;
}
export interface WithdrawalConfigBase {
/** Title to show in the checkout modal. Defaults to "Checkout" **/
modalTitle: string;
/** Additional information to show in the title of the checkout modal. Blank by default **/
modalTitleMeta?: string;
/** Icon to show in the checkout modal (50px x 50px). If not specified, no icon will be shown. **/
iconSrc?: null | string;
sourceTokenSymbol: string;
/** source chain id to withdraw **/
sourceChainId: string;
/** source token id to withdraw **/
sourceTokenAddress: Address;
/** Disable connected option for email login user */
disableConnectedWallet?: boolean;
/** minimal USD needed to withdraw */
getMinWithdrawalUSD?: (asset: TokenInfo) => number;
}
export interface WalletWithdrawalConfig extends WithdrawalConfigBase {
/** Wallet instance used to execute the quote */
wallet: WithdrawalClient;
}
export interface CustomWithdrawalConfig extends WithdrawalConfigBase {
withdrawCallback: (param: WithdrawalParam) => Promise<void>;
}
export interface WithdrawalParam {
quoteId: string;
funQuote: unknown;
targetAssetAddress: Address;
targetChainId: number;
destinationAddress: Address;
}
export type FunkitWithdrawalConfig = WalletWithdrawalConfig | CustomWithdrawalConfig;
export interface FunkitCheckoutValidationResult {
isValid: boolean;
message: string;
}
export interface FunkitCheckoutOnCloseResult {
isNewDeposit: boolean;
}
export type FunkitCheckoutResult = {
type: 'error' | 'success';
message: string;
metadata: object | Error;
};
export interface UseFunkitCheckoutProps {
/** @optional the checkout config object **/
config?: FunkitCheckoutConfig;
/** @optional fires when the checkout modal is opened **/
onOpen?: () => void;
/** @optional fires when the checkout modal is closed **/
onClose?: (result: FunkitCheckoutOnCloseResult) => void;
/** @optional fires when checkout config is done validating **/
onValidation?: (result: FunkitCheckoutValidationResult) => void;
/** @optional fires when a checkout quote is received **/
onEstimation?: (result: FunkitCheckoutQuoteResult) => void;
/** @optional fires when a checkout is confirmed with the funkit server. Returns the newly created checkoutId. **/
onConfirmation?: (checkoutId: string) => void;
/** @optional fires if the checkout fails at any point **/
onError?: (result: FunkitCheckoutResult) => void;
/** @optional fires after the checkout steps are completed **/
onSuccess?: (result: FunkitCheckoutResult) => void;
/**
* @optional
* fires if the checkout requires an active wallet connection. If not specified, defaults to funkit wallet connection.
* Call the provided `onLoginFinished()` callback once login is completed to resume the checkout flow (and unhide the soft-hidden modal).
* It is strongly recommended to call `onLoginFinished()` after login is completed. If not called, the checkout modal may remain hidden.
*/
onLoginRequired?: ({ onLoginFinished, }: {
onLoginFinished?: () => void;
}) => void;
/** @optional fires when a withdrawal is confirmed with the funkit server. Returns the newly created withdrawalId. **/
onWithdrawalConfirmation?: (withdrawalId: string) => void;
/** @optional fires if the withdrawal fails at any point **/
onWithdrawalError?: (result: FunkitCheckoutResult) => void;
}
/** Ensures that config is defined */
export interface UseFunkitCheckoutPropsWithFullConfig extends UseFunkitCheckoutProps {
config: FunkitCheckoutConfig;
}
/**
* Checkout Item for frontend use
*/
export interface FunkitActiveCheckoutItem extends Omit<ApiCheckoutClientMetadata, 'selectedPaymentMethodInfo' | 'initSettings'> {
/** The deposit address provided by server after the checkout is confirmed. If `null`, it is not confirmed yet. **/
depositAddress: null | Address;
/** Final settings the checkout was init-ed with **/
initSettings: UseFunkitCheckoutPropsWithFullConfig;
/** User's selected payment method information */
selectedPaymentMethodInfo: PaymentMethodInfo | null;
/** User's choice of source asset to fund the checkout operation **/
selectedSourceAssetInfo: {
address: Address | null;
symbol: string | null;
chainId: string;
iconSrc: string | null;
};
/** Time of completion of the active checkout item. For frontend use only. **/
completedTimestampMs: number | null;
sourceAssetAmount: number | null;
}
export interface FunkitActiveWithdrawalItem {
/** Unique identifier for frontend use only. */
id: string;
/** Final settings the withdrawal was init-ed with **/
initSettings: Partial<UseFunkitCheckoutPropsWithFullConfig>;
withdrawalSourceTokenBalance: () => number;
}