@funkit/connect
Version:
Funkit Connect SDK elevates DeFi apps via web2 sign-ins and one-click checkouts.
219 lines (218 loc) • 9.13 kB
TypeScript
/**
* Polymarket V2 PMCT withdrawal.
*
* Two flows are supported, dispatched on input shape:
*
* 1. **UDA / PMCT transfer** (legacy) — caller supplies `userId` +
* `sendPmctTransfer`. The backend handles the unwrap/route via
* PermissionedRamp. Returns a {@link CustomWithdrawalConfig}.
*
* 2. **Wallet unwrap** — caller supplies `proxyAddress`, a `publicClient`,
* and a batched `sendTransactions` primitive. `preWithdrawalAction`
* reads the pUSD allowance and returns the approve (when needed) +
* `unwrap()` to USDCe as an array of {@link WithdrawalTransaction}s.
* The SDK prepends those to the withdrawal txn and atomically broadcasts
* the batch via the integrator's `sendTransactions`. Returns a
* {@link WalletWithdrawalConfig}.
*/
import { type Address, type PublicClient, type TransactionReceipt } from 'viem';
import type { CustomWithdrawalConfig, FunkitCheckoutActionParams, FunkitWithdrawalConfig, WalletWithdrawalConfig } from '../../providers/FunkitCheckoutContext/types';
import type { WithdrawalTransaction } from '../../wallets/Wallet';
/** Polygon USDCe — used as sourceTokenAddress for withdrawals */
export declare const POLYGON_USDCE: Address;
/** Polymarket pUSD on Polygon — the integrator-held source token in the wallet-unwrap flow. */
export declare const PUSD_TOKEN: {
readonly address: `0x${string}`;
readonly symbol: "pUSD";
readonly iconSrc: "https://sdk-cdn.fun.xyz/images/pusd.svg";
};
export interface PolymarketWithdrawalCallbackConfig {
userId: string;
modalTitle?: string;
disableConnectedWallet?: boolean;
sourceTokenSymbol?: string;
defaultReceiveToken?: string;
sourceTokenAddress?: Address;
iconSrc?: string;
/**
* Execute the ERC-20 transfer of PMCT from the user's proxy to the UDA.
* Called after the UDA address is generated.
* Should return when the transaction has been submitted (not necessarily confirmed).
*/
sendPmctTransfer: (params: {
/** The UDA address to send PMCT to */
udaAddress: Address;
/** Amount in base units (from the quote) */
amountBaseUnit: bigint;
}) => Promise<void>;
}
/**
* Caller-supplied batched submitter. Receives the full ordered list of txs
* (helper-injected approve+unwrap, then the SDK's withdrawal txn). Must
* broadcast them atomically in a single user signature and return a tx hash
* that {@link WithdrawalClient.confirmTransaction} can wait on.
*/
export type PolymarketSendTransactions = (txs: WithdrawalTransaction[]) => Promise<string>;
export interface PolymarketWalletUnwrapWithdrawalConfig {
/** Polymarket proxy that holds pUSD and signs the batched tx. */
proxyAddress: Address;
/** Polygon public client used for the pre-flight allowance read. */
publicClient: PublicClient;
/**
* Batched send primitive. The SDK passes the full ordered list of txs —
* helper-emitted `approve(pUSD → offramp)` (only when allowance is
* insufficient) and `offramp.unwrap(USDCe, proxy, amount)` from
* `preWithdrawalAction`, followed by the withdrawal txn — and the
* integrator broadcasts them atomically in a single user signature.
*/
sendTransactions: PolymarketSendTransactions;
/**
* Optional override for waiting on a tx receipt. Defaults to
* `publicClient.waitForTransactionReceipt`.
*/
confirmTransaction?: (txHash: string, chainId: number) => Promise<TransactionReceipt>;
modalTitle?: string;
disableConnectedWallet?: boolean;
sourceTokenSymbol?: string;
sourceTokenAddress?: Address;
defaultReceiveToken?: string;
iconSrc?: string;
}
/**
* Combined input — supplies params for both flows. The runtime path is
* chosen at call time via the `enable-polymarket-wallet-withdrawal`
* Statsig gate (read synchronously through {@link checkFeatureGate}, so
* this stays a regular function callable from event handlers).
*/
export interface PolymarketWithdrawalConfig extends PolymarketWithdrawalCallbackConfig, PolymarketWalletUnwrapWithdrawalConfig {
}
/**
* Combined / gated entry point. Returns a {@link CustomWithdrawalConfig}
* (UDA path) or {@link WalletWithdrawalConfig} (wallet-unwrap path)
* depending on the gate value at call time.
*
* ```ts
* const withdrawalConfig = createPolymarketWithdrawalConfig({
* userId: walletAddress,
* sendPmctTransfer: async ({ udaAddress, amountBaseUnit }) => { ... },
* proxyAddress,
* publicClient,
* sendTransactions: async (txs) => myBatchedSend(txs),
* })
* ```
*/
export declare function createPolymarketWithdrawalConfig(config: PolymarketWithdrawalConfig): FunkitWithdrawalConfig;
/**
* Legacy UDA / PMCT transfer flow. Returns a {@link CustomWithdrawalConfig}.
*
* @deprecated Pass the combined {@link PolymarketWithdrawalConfig} instead so
* the wallet-unwrap rollout can be staged via the
* `enable-polymarket-wallet-withdrawal` Statsig gate. Direct callers of this
* single-flow overload bypass the gate.
*
* ```ts
* const withdrawalConfig = createPolymarketWithdrawalConfig({
* userId: walletAddress,
* sendPmctTransfer: async ({ udaAddress, amountBaseUnit }) => { ... },
* })
* ```
*/
export declare function createPolymarketWithdrawalConfig(config: PolymarketWithdrawalCallbackConfig): CustomWithdrawalConfig;
/**
* Wallet-flow with embedded pUSD → USDCe unwrap. Returns a
* {@link WalletWithdrawalConfig} whose `preWithdrawalAction` and `wallet`
* coordinate via a closure-scoped queue, so the integrator only sees the
* batched `sendTransactions` primitive.
*
* @deprecated Pass the combined {@link PolymarketWithdrawalConfig} instead so
* the wallet-unwrap rollout can be staged via the
* `enable-polymarket-wallet-withdrawal` Statsig gate. Direct callers of this
* single-flow overload bypass the gate.
*
* ```ts
* const withdrawalConfig = createPolymarketWithdrawalConfig({
* proxyAddress,
* publicClient,
* sendTransactions: async (txs) => myBatchedSend(txs),
* })
* ```
*/
export declare function createPolymarketWithdrawalConfig(config: PolymarketWalletUnwrapWithdrawalConfig): WalletWithdrawalConfig;
/** Polymarket CollateralOnramp on Polygon — permissionless wrap USDC.e → pUSD. */
export declare const COLLATERAL_ONRAMP_ADDRESS: Address;
export declare const COLLATERAL_ONRAMP_ABI: readonly [{
readonly name: "wrap";
readonly type: "function";
readonly inputs: readonly [{
readonly name: "_asset";
readonly type: "address";
}, {
readonly name: "_to";
readonly type: "address";
}, {
readonly name: "_amount";
readonly type: "uint256";
}];
readonly outputs: readonly [];
readonly stateMutability: "nonpayable";
}];
/** VaultDepositor on Polygon — same address used by other VaultDepositor flows. */
export declare const VAULT_DEPOSITOR_POLYGON: `0x${string}`;
/**
* Minimal VaultDepositor ABI — only the mediated `deposit` entrypoint is used.
* VaultDepositor pulls full msg.sender balance, replaces AMOUNT_PLACEHOLDER in
* callData with the actual amount, and forwards to the target vault.
*/
export declare const VAULT_DEPOSITOR_ABI: readonly [{
readonly name: "deposit";
readonly type: "function";
readonly stateMutability: "nonpayable";
readonly inputs: readonly [{
readonly name: "token";
readonly type: "address";
}, {
readonly name: "vault";
readonly type: "address";
}, {
readonly name: "callData";
readonly type: "bytes";
}, {
readonly name: "minAmountOut";
readonly type: "uint256";
}];
readonly outputs: readonly [{
readonly name: "returnData";
readonly type: "bytes";
}];
}];
/** Sentinel the VaultDepositor replaces with the actual deposit amount at exec time. */
export declare const AMOUNT_PLACEHOLDER = 115792089237316195423570985008687907853269984665640564039457584007912570601199n;
export interface PolymarketPerpsDepositConfig {
/**
* Polymarket Perps account credited with the deposit
*/
recipientAddress: Address;
}
/**
* Builds the `generateActionsParams` callback for a Polymarket Perps deposit.
*
* Both legs run inside a single Relay Router V3 `cleanupErc20sViaCall` sweep,
* each mediated by the VaultDepositor proxy so the flow composes with
* EXACT_INPUT quoting — the VaultDepositor pulls msg.sender's full balance and
* substitutes AMOUNT_PLACEHOLDER at exec time, so arrival amounts don't need to
* be known at quote time:
* 1. USDC.e → pUSD via `CollateralOnramp.wrap`, attributing pUSD to Relay
* Router V3.
* 2. pUSD → `POLYMARKET_PERPS_ADDRESS.deposit`, crediting `recipientAddress`.
*
* Usage:
* ```ts
* const checkoutConfig: FunkitCheckoutConfig = {
* // ...
* generateActionsParams: createPerpsGenerateActionParams({
* recipientAddress: proxyAddress,
* }),
* }
* ```
*/
export declare function createPerpsGenerateActionParams(config: PolymarketPerpsDepositConfig): () => Promise<FunkitCheckoutActionParams[]>;