import type { HypercoreSigner } from '@funkit/connect-core/clients/hypercore'; import type { FunkitSolanaAdapter } from '@funkit/fun-relay'; import type { WithdrawalClient } from '../../wallets/Wallet'; /** Relay's names for the VMs a Convert source or destination can live on. */ export type ConvertVm = 'evm' | 'svm' | 'hypevm'; /** * A token the host lists for Convert, by Fun chain id and token address. * Every chain's native asset (ETH, BNB, SOL, Hypercore perps USDC) uses the * `NATIVE_TOKEN` placeholder, the same as deposit configs; the SDK resolves * chain-specific ids from it. Balance entries name these same addresses. * Decimals are resolved from Fun's asset list, not supplied here. * @experimental */ export interface ConvertAsset { chainId: string; address: string; symbol: string; /** * Icon to show for this token. Optional: the SDK falls back to its own * logo. Must be an http(s) URL. */ iconSrc?: string; } /** * A listed asset with the decimals the SDK resolved from Fun's asset list, so * the host never has to. What {@link GetConvertBalances} receives. * @experimental */ export interface ConvertResolvedAsset extends ConvertAsset { decimals: number; } /** * The wallet that signs on one source chain, in the shape the SDK already * executes with: * - `evm`: a {@link WithdrawalClient}. `sendTransaction` must return a real * on-chain hash; `sendTransactions` is opt-in and must also return a tx hash, * never a wallet calls id. Its `adaptedWallet` isn't read: Solana and * Hypercore sources use their own entries below. * - `svm`: a {@link FunkitSolanaAdapter} from `@funkit/solana-kit` or * `@funkit/solana-web3`. * - `hypevm`: a {@link HypercoreSigner}; the SDK builds the Relay action wallet * from it. * @experimental */ export type ConvertSourceWallet = { vm: 'evm'; wallet: Omit; } | { vm: 'svm'; adapter: FunkitSolanaAdapter; } | { vm: 'hypevm'; signer: HypercoreSigner; }; /** * Resolves the signing wallet for a source asset. Called when the user picks a * source or flips direction, never for assets they haven't chosen. The asset * lets one chain resolve to different wallets, e.g. Polygon pUSD to the * Polymarket proxy client and other Polygon tokens to the EOA. * @experimental */ export type GetConvertSourceWallet = (params: { asset: ConvertResolvedAsset; }) => Promise; /** * One listed asset's balance, as the host read it: a base-unit integer string * in the decimals the SDK passed. `"0"` means a real zero. * @experimental */ export interface ConvertAssetBalance { chainId: string; address: string; balance: string; /** USD per whole token. A missing or invalid price makes the asset unavailable. */ unitPriceUsd: number; } /** * Reads live balances for the assets it's given, in one call. * - Polled with every listed asset while the Convert screen is open. * - Called with only the source asset right before signing; answer for * exactly the assets passed. * - Read fresh from the host's balance source, not from page state. * - Exclude anything the user can't move (Hypercore locked collateral, margin). * - Leave out any asset that couldn't be read; the SDK shows it as * unavailable, never as 0. Don't throw: an omission already says * everything a throw could, per asset instead of for all of them. * @experimental */ export type GetConvertBalances = (params: { assets: ConvertResolvedAsset[]; }) => Promise; /** A chain-native transaction id: an EVM hash or a base58 Solana signature. */ export interface ConvertTransaction { chainId: string; txId: string; } /** * Fired once, after Relay reports destination settlement. A source receipt * alone never fires it. * @experimental */ export interface ConvertSuccessResult { requestId: string; source: ConvertResolvedAsset & { walletAddress: string; }; destination: ConvertResolvedAsset & { walletAddress: string; }; amountInBaseUnits: string; quotedAmountOutBaseUnits: string; /** Only when the provider reports a measured amount; never the quote. */ settledAmountOutBaseUnits?: string; /** May be empty for a leg with no reported on-chain transaction. */ sourceTransactions: ConvertTransaction[]; destinationTransactions: ConvertTransaction[]; } /** * Where a conversion stopped: * - `preSign`: the pre-sign check rejected the quote; nothing was signed. * - `source`: the source-chain step (signing, sending, switching chain) * failed before funds moved; safe to retry. * - `unknown`: it may have been submitted but can't be tracked; check the * wallet, and never retry. * - `settlement`: Relay's fill on the destination failed or was refunded. * @experimental */ export type ConvertErrorStage = 'preSign' | 'source' | 'unknown' | 'settlement'; /** @experimental */ export interface ConvertErrorResult { stage: ConvertErrorStage; /** The user declined a signature; not worth alerting on. */ userRejected: boolean; /** For logs and debugging; not localized or meant for display. */ message: string; /** Whatever was thrown, untouched: a viem, Relay or host wallet error. */ error: unknown; } /** * Where converted funds land, by destination VM. No signing happens on the * destination side. A destination with no resolvable recipient is shown as * unavailable. * @experimental */ export interface ConvertRecipientAddresses { evm?: string; svm?: string; /** Falls back to `evm`; Hyperliquid accounts share the EVM address. */ hypevm?: string; /** * Recipient for one destination asset when it differs from the VM default, * e.g. a Polymarket proxy for pUSD. Return undefined to use the default. */ getCustomRecipient?: (asset: ConvertResolvedAsset) => Promise; } /** * Everything the Convert screen needs from the host, in one place. * @experimental */ export interface ConvertConfig { assets: ConvertAsset[]; recipientAddresses: ConvertRecipientAddresses; getSourceWallet: GetConvertSourceWallet; getBalances: GetConvertBalances; onSuccess?: (result: ConvertSuccessResult) => void; onError?: (error: ConvertErrorResult) => void; /** * Close the modal a few seconds after a conversion settles, once the host's * own UI reflects the new balances. Off, or if the user has moved to another * tab meanwhile, the screen returns to Convert for another conversion instead. */ closeOnSuccess?: boolean; }