import {z} from 'zod' export type BlockchainRpcAuth = {type: 'RPC_AUTH_BASIC' | 'RPC_AUTH_BEARER' | 'RPC_AUTH_CUSTOM_HEADER', username?: string | undefined, password?: string | undefined, token?: string | undefined, headerName?: string | undefined, headerValue?: string | undefined}; export type BlockchainDeclaredProperties = {chainName: string, chainId: number, symbolName: string, decimals?: number | undefined, rpcUrls: Array, environmentType: 'ENVIRONMENT_TYPE_MAINNET' | 'ENVIRONMENT_TYPE_TESTNET', blockExplorerUrl?: string | undefined, blockExplorerTransactionPath?: string | undefined, blockExplorerAddressPath?: string | undefined, networkId?: number | undefined, hasFee?: boolean | undefined, isPoa?: boolean | undefined, hasLayeredFee?: boolean | undefined, nodeType?: ('NODE_TYPE_CORE_GETH' | 'NODE_TYPE_FANTOM' | 'NODE_TYPE_ERIGON' | 'NODE_TYPE_PARITY' | 'NODE_TYPE_BESU') | undefined, transactionFormat?: number | undefined, baseAssetTenantIds?: Array | undefined, explorerApiUrl?: string | undefined, explorerApiKey?: string | undefined, isTraceEnabled?: boolean | undefined, rpcAuth?: BlockchainRpcAuth | undefined}; export type CreateBlockchainRequest = {declaredProperties: BlockchainDeclaredProperties}; export type CreateApiBlockchainLinkBlockchainsArgs = {'Idempotency-Key'?: string | undefined, body: CreateBlockchainRequest}; export type CreateApiBlockchainLinkBlockchainsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const blockchainRpcAuth = z.object({type: z.enum(["RPC_AUTH_BASIC", "RPC_AUTH_BEARER", "RPC_AUTH_CUSTOM_HEADER"]), username: z.string().optional(), password: z.string().optional(), token: z.string().optional(), headerName: z.string().optional(), headerValue: z.string().optional()}); export const blockchainDeclaredProperties = z.object({chainName: z.string(), chainId: z.number(), symbolName: z.string(), decimals: z.number().optional(), rpcUrls: z.array(z.string()), environmentType: z.enum(["ENVIRONMENT_TYPE_MAINNET", "ENVIRONMENT_TYPE_TESTNET"]), blockExplorerUrl: z.string().optional(), blockExplorerTransactionPath: z.string().optional(), blockExplorerAddressPath: z.string().optional(), networkId: z.number().optional(), hasFee: z.boolean().optional(), isPoa: z.boolean().optional(), hasLayeredFee: z.boolean().optional(), nodeType: z.enum(["NODE_TYPE_CORE_GETH", "NODE_TYPE_FANTOM", "NODE_TYPE_ERIGON", "NODE_TYPE_PARITY", "NODE_TYPE_BESU"]).optional(), transactionFormat: z.number().optional(), baseAssetTenantIds: z.array(z.string()).optional(), explorerApiUrl: z.string().optional(), explorerApiKey: z.string().optional(), isTraceEnabled: z.boolean().optional(), rpcAuth: blockchainRpcAuth.optional()}); export const blockchain = z.object({id: z.string(), blockchainState: z.enum(["BLOCKCHAIN_STATE_CREATED", "BLOCKCHAIN_STATE_ACTIVATING", "BLOCKCHAIN_STATE_ACTIVATED", "BLOCKCHAIN_STATE_DEACTIVATING", "BLOCKCHAIN_STATE_DEACTIVATED", "BLOCKCHAIN_STATE_TECHNICAL_FAILURE"]), declaredProperties: blockchainDeclaredProperties, createdAtUtc: z.number(), updatedAtUtc: z.number(), validationSessionId: z.string().optional(), validationStatus: z.enum(["VALIDATION_STATUS_PENDING", "VALIDATION_STATUS_IN_PROGRESS", "VALIDATION_STATUS_SUCCEEDED", "VALIDATION_STATUS_FAILED", "VALIDATION_STATUS_TECHNICAL_FAILURE"]).optional(), validationCreatedAtUtc: z.number().optional(), validationUpdatedAtUtc: z.number().optional(), validationCompletedAtUtc: z.number().optional(), validationFailureReasons: z.array(z.string()), failedStep: z.enum(["FAILED_STEP_CONFIGURATION", "FAILED_STEP_RESOURCES_ALLOCATION", "FAILED_STEP_TEST_TX_EXTERNAL_TO_FIREBLOCKS", "FAILED_STEP_TEST_TX_FIREBLOCKS_TO_FIREBLOCKS"]).optional()}); export const createBlockchainResponse = z.object({blockchain: blockchain, message: 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 }; /** * Create a new blockchain * * POST /blockchain_link/blockchains */ export const createApiBlockchainLinkBlockchains = async (args: CreateApiBlockchainLinkBlockchainsArgs, options?: CreateApiBlockchainLinkBlockchainsOptions) => { const {baseUrl = "https://api.fireblocks.io/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) const url = `${baseUrl.replace(/\/+$/, '')}/blockchain_link/blockchains${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(createBlockchainResponse, await res.json()) };