import {z} from 'zod' export type GetApiV1LedgersLedgerIdArgs = {ledgerId: string}; export type GetApiV1LedgersLedgerIdOptions = {baseUrl: string, httpAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const coreLedgerParametersAlgorand = z.object({genesisId: z.string().max(50), genesisHash: z.string().min(44).max(44), nativeTickerSymbol: z.string().min(1).max(100), nativeTickerName: z.string().min(1).max(100), nativeTickerDecimals: z.number().int(), type: z.literal("Algorand")}); export const coreBitcoinLikeVaultLedgerIdentifier = z.enum(["btc", "ltc", "bch", "doge", "dash"]); export const coreNetwork = z.enum(["mainnet", "testnet", "regtest"]); export const coreLedgerParametersBitcoin = z.object({supportsSegWit: z.boolean(), vaultLedgerIdentifier: coreBitcoinLikeVaultLedgerIdentifier, network: coreNetwork, tickerSymbol: z.string().min(1).max(100), tickerName: z.string().min(1).max(100), dustLimit: z.string(), type: z.literal("Bitcoin")}); export const coreLedgerParametersCardano = z.object({nativeTickerSymbol: z.string().min(1).max(100), nativeTickerName: z.string().min(1).max(100), testnetMagic: z.number().int().gte(0).lte(4294967295).optional(), type: z.literal("Cardano")}); export const coreEthereumLikeVaultLedgerIdentifier = z.enum(["eth", "etc"]); export const coreLedgerParametersEthereum = z.object({chainId: z.number().int(), vaultLedgerIdentifier: coreEthereumLikeVaultLedgerIdentifier, nativeTickerSymbol: z.string().min(1).max(100), nativeTickerName: z.string().min(1).max(100), supportsEIP1559: z.boolean(), type: z.literal("Ethereum")}); export const coreLedgerParametersHedera = z.object({nativeTickerSymbol: z.string().min(1).max(100), nativeTickerName: z.string().min(1).max(100), type: z.literal("Hedera")}); export const coreLedgerParametersSolana = z.object({nativeTickerSymbol: z.string().min(1).max(100), nativeTickerName: z.string().min(1).max(100), lamportsPerSignature: z.string(), type: z.literal("Solana")}); export const coreLedgerParametersStellar = z.object({nativeTickerSymbol: z.string().min(1).max(100), nativeTickerName: z.string().min(1).max(100), networkPassphrase: z.string().min(12).max(100), type: z.literal("Stellar")}); export const coreSubstrateFeeParameters = z.object({baseExtrinsic: z.number().int().gte(0), readWeight: z.number().int().gte(0), writeWeight: z.number().int().gte(0), transactionByteFee: z.string(), weightToFeePolynomial: z.array(z.string()), type: z.literal("TransactionPayment")}); export const coreSubstrateMethodFeeParameters = z.object({base: z.number().int().gte(0), reads: z.number().int().gte(0), writes: z.number().int().gte(0), type: z.literal("MethodWeight")}); export const coreSubstrateMethodInfo = z.object({name: z.string(), methodId: z.number().int(), feeParameters: coreSubstrateMethodFeeParameters}); export const coreSubstratePalletInfo = z.object({name: z.string(), moduleId: z.number().int(), methods: z.array(coreSubstrateMethodInfo)}); export const coreSubstrateChainType = z.enum(["RelayChain", "AssetHub"]); export const coreSubstrateRuntimeInfo = z.object({transactionVersion: z.number().int(), feeParameters: coreSubstrateFeeParameters, pallets: z.array(coreSubstratePalletInfo), chainType: coreSubstrateChainType.optional()}); export const coreLedgerParametersSubstrate = z.object({genesisHash: z.string(), addressPrefix: z.number().int().gte(0).lte(16383), nativeTickerSymbol: z.string().min(1).max(100), nativeTickerName: z.string().min(1).max(100), nativeTickerDecimals: z.number().int(), supportedRuntimeVersions: z.array(coreSubstrateRuntimeInfo), type: z.literal("Substrate")}); export const coreLedgerParametersTezos = z.object({nativeTickerSymbol: z.string().min(1).max(100), nativeTickerName: z.string().min(1).max(100), chainId: z.string(), type: z.literal("Tezos")}); export const coreLedgerParametersTron = z.object({nativeTickerSymbol: z.string().min(1).max(100), nativeTickerName: z.string().min(1).max(100), type: z.literal("Tron")}); export const coreLedgerParametersXRPL = z.object({nativeTickerSymbol: z.string().min(1).max(100), nativeTickerName: z.string().min(1).max(100), networkId: z.number().int(), type: z.literal("XRPL")}); export const coreLedgerParameters = z.discriminatedUnion("type", [coreLedgerParametersAlgorand, coreLedgerParametersBitcoin, coreLedgerParametersCardano, coreLedgerParametersEthereum, coreLedgerParametersHedera, coreLedgerParametersSolana, coreLedgerParametersStellar, coreLedgerParametersSubstrate, coreLedgerParametersTezos, coreLedgerParametersTron, coreLedgerParametersXRPL]); export const coreUserReference = z.object({id: z.string(), domainId: z.string()}); export const coreStringsMap = z.record(z.string(), z.string()); export const coreEntityMetadata = z.object({description: z.string().max(250).optional(), revision: z.number().int().gte(1), createdAt: z.string(), createdBy: coreUserReference.optional(), lastModifiedAt: z.string(), lastModifiedBy: coreUserReference.optional(), customProperties: coreStringsMap}); export const coreLedger = z.object({id: z.string().min(1).max(100), alias: z.string().min(1).max(200), parameters: coreLedgerParameters, metadata: coreEntityMetadata}); export const coreTrustedLedger = z.object({data: coreLedger, signature: z.string(), signingKey: z.string()}); 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 details * * GET /v1/ledgers/{ledgerId} */ export const getApiV1LedgersLedgerId = async (args: GetApiV1LedgersLedgerIdArgs, options: GetApiV1LedgersLedgerIdOptions) => { 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))}${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(coreTrustedLedger, await res.json()) };