import {z} from 'zod' export type GetApiLedgerAssetsAssetCodeNetworkTokensArgs = {assetCode: string}; export type GetApiLedgerAssetsAssetCodeNetworkTokensOptions = {baseUrl?: string | undefined, HMACAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const ledgerSvcBlockchainNetwork = z.object({id: z.string(), name: z.string(), blockTime: z.number().int().gte(1), confirmations: z.number().gte(1), coinName: z.string(), coinSymbol: z.string(), supportsMemo: z.boolean().optional(), alias: z.string().optional(), addressExplorerUrl: z.string().optional(), txExplorerUrl: z.string().optional()}); export const tokenNetwork = z.object({code: z.string(), decimals: z.number().int().gte(0), contractAddress: z.string(), minimumDeposit: z.number(), depositAvailable: z.boolean().optional(), withdrawalAvailable: z.boolean().optional(), withdrawalFee: z.number().gte(0).optional(), platformFeePercentage: z.number().gte(0).optional(), networkInfo: ledgerSvcBlockchainNetwork}); export const tokenDetails = z.object({symbol: z.string(), name: z.string(), logoUrl: z.string(), supportedNetworks: z.array(tokenNetwork)}); export const assetBlockchainTokens = z.object({asset: z.string(), tokens: z.array(tokenDetails)}); 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 stablecoin tokens supported for account. * * GET /ledger/assets/{assetCode}/network-tokens */ export const getApiLedgerAssetsAssetCodeNetworkTokens = async (args: GetApiLedgerAssetsAssetCodeNetworkTokensArgs, options?: GetApiLedgerAssetsAssetCodeNetworkTokensOptions) => { const {baseUrl = "https://external.dev.onboardpay.co", fetch: fetchFn = fetch, HMACAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (HMACAuth !== undefined) headers.set("x-api-key", HMACAuth) const url = `${baseUrl.replace(/\/+$/, '')}/ledger/assets/${encodeURIComponent(String(args.assetCode))}/network-tokens${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(assetBlockchainTokens, await res.json()) };