UNPKG

@funkit/connect

Version:

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

67 lines (66 loc) 3.37 kB
import { type QueryClient } from '@tanstack/react-query'; import type { Address } from 'viem'; /** * Shared query key for a Polymarket account's USD balance, keyed by address. * Keying by address (not by the opaque getter fn) is what lets the "Deposit to" * dropdown and the in-modal transfer step dedupe into a single cache entry — so * navigating between them reuses the already-loaded balance instead of * refetching from scratch (no loading flash on entry). */ export declare const polymarketAccountBalanceQueryKey: (address: Address | undefined) => readonly ["polymarket", "accountBalanceUsd", `0x${string}` | null]; /** * Optimistically moves a transfer's amount between the two accounts' displayed * balances — call right after the integrator accepts the transfer. `amountUsd` * is the exact string the user entered (validated numeric, ≤6 decimals), the * same string handed to `transfer` — the math runs in 6-decimal fixed point, * never through a float round-trip. * * This is the standard TanStack optimistic-update sequence: cancel in-flight * fetches, write the new values with `setQueryData` so every reader (deposit * picker, transfer step) updates immediately, and let the server win again * later. "Later" is a settlement window rather than the next poll because the * integrator's backend is eventually consistent — it keeps reporting the * pre-transfer balances for a few seconds after accepting the transfer, so * polls inside the window keep the displayed value instead of flashing back. */ export declare function applyOptimisticTransferBalances(queryClient: QueryClient, transfer: { fromAddress: Address; toAddress: Address; /** Exact entered amount (validated numeric string, ≤6 decimals). */ amountUsd: string; }): void; /** The fields a balance reader needs from a Polymarket account. */ export interface PolymarketBalanceAccount { address: Address; /** Integrator-owned live balance fetcher (USD). */ getBalanceUsd: () => Promise<number>; } export interface PolymarketAccountBalance { /** Latest polled balance, or `undefined` until the first fetch resolves. */ balanceUsd: number | undefined; } export interface UsePolymarketAccountBalancesOptions { /** * How often (ms) to poll each account's balance. Defaults to * {@link DEFAULT_BALANCE_REFETCH_MS}. A short interval reconciles the * displayed balance with the server soon after a transfer settles. */ refetchIntervalMs?: number; } /** * Live USD balances for the Polymarket Predictions and Perps accounts, backed * by a single TanStack Query cache keyed by account address. Both the "Deposit * to" dropdown and the in-modal transfer step read through this hook, so they * share one store: whichever screen loads first populates the balance, and the * other reads it instantly on navigation. After a transfer, * {@link applyOptimisticTransferBalances} shifts the cached values immediately * and the short poll reconciles them with the server once the settlement * window closes. */ export declare function usePolymarketAccountBalances(accounts: { predictions: PolymarketBalanceAccount | undefined; perps: PolymarketBalanceAccount | undefined; }, options?: UsePolymarketAccountBalancesOptions): { predictions: PolymarketAccountBalance; perps: PolymarketAccountBalance; };