import {z} from 'zod' export type GetApiMarketWalletSwapsArgs = {size: unknown, page: unknown, address: unknown}; export type GetApiMarketWalletSwapsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const marketWalletSwapResponseDto = z.object({block_number: z.number(), tx_number: z.string(), created_at: z.string(), creator: z.string(), sender: z.string(), recipient: z.string(), tx_type: z.enum(["GalaChainDex:Trade", "DexV3Contract:Swap"]), tx_subtype: z.literal("Swap"), quantity: z.number(), tx_validation_code: z.literal("0"), tx_validation_enum: z.literal("VALID"), usd_volume: z.number(), swap_token_0_amt: z.number(), swap_token_1_amt: z.number(), swap_token_0_name: z.string().nullable(), swap_token_1_name: z.string().nullable(), swap_token_0_image: z.string().nullable(), swap_token_1_image: z.string().nullable(), swap_token_0_asset_id: z.string().nullable(), swap_token_1_asset_id: z.string().nullable(), unique_key: z.string(), pool_hash: z.string()}); export const getApiMarketWalletSwapsResponse = z.array(marketWalletSwapResponseDto); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * A wallet’s swaps from our own indexed trades (legacy and new contract), newest first, in the get_wallet_txs row shape the My Swaps history parses * * GET /market/wallet-swaps */ export const getApiMarketWalletSwaps = async (args: GetApiMarketWalletSwapsArgs, options?: GetApiMarketWalletSwapsOptions) => { const {baseUrl = "https://swap-backend.stage.defi.ovh.gala.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.size != null) query.append("size", String(args.size)) if (args.page != null) query.append("page", String(args.page)) if (args.address != null) query.append("address", String(args.address)) const url = `${baseUrl.replace(/\/+$/, '')}/market/wallet-swaps${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiMarketWalletSwapsResponse, await res.json()) };