import {z} from 'zod' export type GetApiV1LedgersLedgerIdFeesArgs = {ledgerId: string}; export type GetApiV1LedgersLedgerIdFeesOptions = {baseUrl: string, httpAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const coreBitcoinFee = z.object({satoshiPerVbyte: z.string()}); export const coreCurrentFeesBitcoin = z.object({high: coreBitcoinFee, medium: coreBitcoinFee, low: coreBitcoinFee, type: z.literal("Bitcoin")}); export const coreEthereumEIP1559Prices = z.object({basePrice: z.string(), priorityPrice: z.string()}); export const coreEthereumFee = z.object({gasPrice: z.string(), eip1559Prices: coreEthereumEIP1559Prices.optional()}); export const coreCurrentFeesEthereum = z.object({high: coreEthereumFee, medium: coreEthereumFee, low: coreEthereumFee, type: z.literal("Ethereum")}); export const coreStellarFee = z.object({baseFeeStroops: z.string()}); export const coreCurrentFeesStellar = z.object({high: coreStellarFee, medium: coreStellarFee, low: coreStellarFee, type: z.literal("Stellar")}); export const coreXrplFee = z.object({multiplier: z.string()}); export const coreCurrentFeesXRPL = z.object({high: coreXrplFee, medium: coreXrplFee, low: coreXrplFee, type: z.literal("XRPL")}); export const coreCurrentFees = z.discriminatedUnion("type", [coreCurrentFeesBitcoin, coreCurrentFeesEthereum, coreCurrentFeesStellar, coreCurrentFeesXRPL]); 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 }; /** * Get ledger's fee details * * GET /v1/ledgers/{ledgerId}/fees */ export const getApiV1LedgersLedgerIdFees = async (args: GetApiV1LedgersLedgerIdFeesArgs, options: GetApiV1LedgersLedgerIdFeesOptions) => { const {baseUrl, fetch: fetchFn = fetch, httpAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (httpAuth !== undefined) headers.set('authorization', `Bearer ${httpAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/ledgers/${encodeURIComponent(String(args.ledgerId))}/fees${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(coreCurrentFees, await res.json()) };