import {z} from 'zod' export type BigDecimalFromString = string; export type BooleanFromString = 'true' | 'false'; export type NonNegativeInt = number; export type ExtendedPostBridgeData = {_tag: 'extended', vaultId: NonNegativeInt}; export type StarkwareBtcfiCampaignPostBridgeData = {_tag: 'starkwarebtcfi'}; export type WirexWrapPostBridgeData = {_tag: 'wirexwrap'}; export type WirexWrapSandboxPostBridgeData = {_tag: 'wirexwrapsandbox'}; export type StarknetFelt = string; export type StarkwareBtcfiCampaignV2PostBridgeData = {_tag: 'starkwarebtcfiv2', spans: Array}; export type AaveV3SupplyPostBridgeData = {_tag: 'aavev3supply'}; export type RocketFoundationDepositPostBridgeData = {_tag: 'rocketfoundationdeposit'}; export type MapleDepositPostBridgeData = {_tag: 'mapledeposit'}; export type CreateApiQuoteUserArgs = {body: {token: string, chainIn: string, chainOut: string, amount: BigDecimalFromString, amountNative?: BigDecimalFromString | undefined, mode: 'pay' | 'receive', isSda?: BooleanFromString | undefined, depositor: string, recipient: string, postBridgeData?: (ExtendedPostBridgeData | StarkwareBtcfiCampaignPostBridgeData | WirexWrapPostBridgeData | WirexWrapSandboxPostBridgeData | StarkwareBtcfiCampaignV2PostBridgeData | AaveV3SupplyPostBridgeData | RocketFoundationDepositPostBridgeData | MapleDepositPostBridgeData) | undefined, webhookUrl?: string | undefined}}; export type CreateApiQuoteUserOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const bigDecimalFromString = z.string(); export const bigDecimalFromNumber = z.number(); export const nonNegativeInt = z.number().int().gte(0); export const extendedPostBridgeData = z.object({_tag: z.literal("extended"), vaultId: nonNegativeInt}); export const starkwareBtcfiCampaignPostBridgeData = z.object({_tag: z.literal("starkwarebtcfi")}); export const wirexWrapPostBridgeData = z.object({_tag: z.literal("wirexwrap")}); export const wirexWrapSandboxPostBridgeData = z.object({_tag: z.literal("wirexwrapsandbox")}); export const starknetFelt = z.string(); export const starkwareBtcfiCampaignV2PostBridgeData = z.object({_tag: z.literal("starkwarebtcfiv2"), spans: z.array(starknetFelt)}); export const aaveV3SupplyPostBridgeData = z.object({_tag: z.literal("aavev3supply")}); export const rocketFoundationDepositPostBridgeData = z.object({_tag: z.literal("rocketfoundationdeposit")}); export const mapleDepositPostBridgeData = z.object({_tag: z.literal("mapledeposit")}); export const connectedSingleBridgeQuoteResponseSchema = z.object({_tag: z.literal("bridge"), chainIn: z.string(), chainOut: z.string(), payAmount: bigDecimalFromString, payAmountUsd: bigDecimalFromNumber, receiveAmount: bigDecimalFromString, receiveAmountUsd: bigDecimalFromNumber, fees: z.object({fee: z.string(), feeUsd: z.number(), gasFee: bigDecimalFromString, gasFeeUsd: bigDecimalFromNumber, sourceGasFee: bigDecimalFromString, sourceGasFeeUsd: bigDecimalFromNumber, platformFee: z.string(), platformFeeUsd: z.number(), percentageFee: z.string(), percentageFeeUsd: z.number(), percentageFeeAmount: bigDecimalFromNumber.nullable(), percentageFeeThreshold: bigDecimalFromNumber.nullable(), sponsoredFees: z.object({fee: z.string(), feeUsd: z.number(), gasFee: bigDecimalFromString, gasFeeUsd: bigDecimalFromNumber, sourceGasFee: bigDecimalFromString, sourceGasFeeUsd: bigDecimalFromNumber, platformFee: z.string(), platformFeeUsd: z.number(), percentageFee: z.string(), percentageFeeUsd: z.number()}).optional()}), promotion: z.object({name: z.string(), maxLimitUsd: bigDecimalFromNumber.nullable()}).optional(), gasBoost: z.object({amountNative: bigDecimalFromString, amountNativeUsd: bigDecimalFromNumber, amountNativeTokenCost: bigDecimalFromString}).optional(), speed: z.enum(["fast", "standard", "slow"]).optional(), estimatedDuration: z.number().gte(0).optional(), token: z.string(), depositor: z.string(), recipient: z.string(), postBridgeData: z.union([extendedPostBridgeData, starkwareBtcfiCampaignPostBridgeData, wirexWrapPostBridgeData, wirexWrapSandboxPostBridgeData, starkwareBtcfiCampaignV2PostBridgeData, aaveV3SupplyPostBridgeData, rocketFoundationDepositPostBridgeData, mapleDepositPostBridgeData]).optional(), webhookUrl: z.string().optional(), expiresAt: z.string(), quoteId: z.string(), flowCredits: bigDecimalFromNumber}); export const connectedBridgeSwapQuoteResponseSchema = z.object({_tag: z.literal("bridgeSwap"), chainIn: z.string(), chainOut: z.string(), payAmount: bigDecimalFromString, payAmountUsd: bigDecimalFromNumber, receiveAmount: bigDecimalFromString, receiveAmountUsd: bigDecimalFromNumber, fees: z.object({fee: z.string(), feeUsd: z.number(), gasFee: bigDecimalFromString, gasFeeUsd: bigDecimalFromNumber, sourceGasFee: bigDecimalFromString, sourceGasFeeUsd: bigDecimalFromNumber, platformFee: z.string(), platformFeeUsd: z.number(), percentageFee: z.string(), percentageFeeUsd: z.number(), percentageFeeAmount: bigDecimalFromNumber.nullable(), percentageFeeThreshold: bigDecimalFromNumber.nullable(), sponsoredFees: z.object({fee: z.string(), feeUsd: z.number(), gasFee: bigDecimalFromString, gasFeeUsd: bigDecimalFromNumber, sourceGasFee: bigDecimalFromString, sourceGasFeeUsd: bigDecimalFromNumber, platformFee: z.string(), platformFeeUsd: z.number(), percentageFee: z.string(), percentageFeeUsd: z.number()}).optional()}), promotion: z.object({name: z.string(), maxLimitUsd: bigDecimalFromNumber.nullable()}).optional(), gasBoost: z.object({amountNative: bigDecimalFromString, amountNativeUsd: bigDecimalFromNumber, amountNativeTokenCost: bigDecimalFromString}).optional(), speed: z.enum(["fast", "standard", "slow"]).optional(), estimatedDuration: z.number().gte(0).optional(), tokenIn: z.string(), tokenOut: z.string(), bridgeToken: z.string(), bridgePayAmount: bigDecimalFromString, bridgePayAmountUsd: bigDecimalFromNumber, bridgeReceiveAmount: bigDecimalFromString, minReceiveAmount: bigDecimalFromString, minReceiveAmountUsd: bigDecimalFromNumber, usdPriceTokenIn: bigDecimalFromNumber, usdPriceTokenOut: bigDecimalFromNumber, isAtomicSwap: z.boolean().optional(), depositor: z.string(), recipient: z.string(), postBridgeData: z.union([extendedPostBridgeData, starkwareBtcfiCampaignPostBridgeData, wirexWrapPostBridgeData, wirexWrapSandboxPostBridgeData, starkwareBtcfiCampaignV2PostBridgeData, aaveV3SupplyPostBridgeData, rocketFoundationDepositPostBridgeData, mapleDepositPostBridgeData]).optional(), webhookUrl: z.string().optional(), expiresAt: z.string(), quoteId: z.string(), flowCredits: bigDecimalFromNumber}); export const connectedDepositAddressBridgeQuoteResponseSchema = z.object({_tag: z.literal("depositAddressBridge"), chainIn: z.string(), chainOut: z.string(), payAmount: bigDecimalFromString, payAmountUsd: bigDecimalFromNumber, receiveAmount: bigDecimalFromString, receiveAmountUsd: bigDecimalFromNumber, fees: z.object({fee: z.string(), feeUsd: z.number(), gasFee: bigDecimalFromString, gasFeeUsd: bigDecimalFromNumber, sourceGasFee: bigDecimalFromString, sourceGasFeeUsd: bigDecimalFromNumber, platformFee: z.string(), platformFeeUsd: z.number(), percentageFee: z.string(), percentageFeeUsd: z.number(), percentageFeeAmount: bigDecimalFromNumber.nullable(), percentageFeeThreshold: bigDecimalFromNumber.nullable(), sponsoredFees: z.object({fee: z.string(), feeUsd: z.number(), gasFee: bigDecimalFromString, gasFeeUsd: bigDecimalFromNumber, sourceGasFee: bigDecimalFromString, sourceGasFeeUsd: bigDecimalFromNumber, platformFee: z.string(), platformFeeUsd: z.number(), percentageFee: z.string(), percentageFeeUsd: z.number()}).optional()}), promotion: z.object({name: z.string(), maxLimitUsd: bigDecimalFromNumber.nullable()}).optional(), gasBoost: z.object({amountNative: bigDecimalFromString, amountNativeUsd: bigDecimalFromNumber, amountNativeTokenCost: bigDecimalFromString}).optional(), speed: z.enum(["fast", "standard", "slow"]).optional(), estimatedDuration: z.number().gte(0).optional(), token: z.string(), depositor: z.string(), recipient: z.string(), postBridgeData: z.union([extendedPostBridgeData, starkwareBtcfiCampaignPostBridgeData, wirexWrapPostBridgeData, wirexWrapSandboxPostBridgeData, starkwareBtcfiCampaignV2PostBridgeData, aaveV3SupplyPostBridgeData, rocketFoundationDepositPostBridgeData, mapleDepositPostBridgeData]).optional(), webhookUrl: z.string().optional(), expiresAt: z.string(), quoteId: z.string(), flowCredits: bigDecimalFromNumber}); export const createApiQuoteUserResponse = z.union([connectedSingleBridgeQuoteResponseSchema, connectedBridgeSwapQuoteResponseSchema, connectedDepositAddressBridgeQuoteResponseSchema]); 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 }; /** * User Quote * * POST /quote/user * * Also accepts auth: legacyApiKey */ export const createApiQuoteUser = async (args: CreateApiQuoteUserArgs, options?: CreateApiQuoteUserOptions) => { const {baseUrl = "https://api.rhino.fi/bridge", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/quote/user${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiQuoteUserResponse, await res.json()) };