import {z} from 'zod' export type GetApiV2BlockchainMasterchainMasterchainSeqnoConfigArgs = {masterchain_seqno: number}; export type GetApiV2BlockchainMasterchainMasterchainSeqnoConfigOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const configProposalSetup = z.object({bit_price: z.number().int(), cell_price: z.number().int(), max_losses: z.number().int(), max_store_sec: z.number().int(), max_tot_rounds: z.number().int(), min_store_sec: z.number().int(), min_tot_rounds: z.number().int(), min_wins: z.number().int()}); export const workchainDescr = z.object({accept_msgs: z.boolean(), active: z.boolean(), actual_min_split: z.number().int(), basic: z.number().int(), enabled_since: z.number().int(), flags: z.number().int(), max_split: z.number().int(), min_split: z.number().int(), version: z.number().int(), workchain: z.number().int(), zerostate_file_hash: z.string(), zerostate_root_hash: z.string()}); export const gasLimitPrices = z.object({block_gas_limit: z.number().int(), delete_due_limit: z.number().int(), flat_gas_limit: z.number().int().optional(), flat_gas_price: z.number().int().optional(), freeze_due_limit: z.number().int(), gas_credit: z.number().int(), gas_limit: z.number().int(), gas_price: z.number().int(), special_gas_limit: z.number().int().optional()}); export const blockParamLimits = z.object({hard_limit: z.number().int(), soft_limit: z.number().int(), underload: z.number().int()}); export const blockLimits = z.object({bytes: blockParamLimits, gas: blockParamLimits, lt_delta: blockParamLimits}); export const msgForwardPrices = z.object({bit_price: z.number().int(), cell_price: z.number().int(), first_frac: z.number().int(), ihr_price_factor: z.number().int(), lump_price: z.number().int(), next_frac: z.number().int()}); export const newConsensusConfig = z.object({first_block_timeout_ms: z.number().int().optional(), flags: z.number().int(), max_leader_window_desync: z.number().int().optional(), noncritical_params: z.record(z.string(), z.number().int()).optional(), slots_per_leader_window: z.number().int(), target_rate_ms: z.number().int().optional(), use_quic: z.boolean()}); export const validatorsSet = z.object({list: z.array(z.object({adnl_addr: z.string().optional(), public_key: z.string(), weight: z.number().int()})), main: z.number().int(), total: z.number().int(), total_weight: z.string().optional(), utime_since: z.number().int(), utime_until: z.number().int()}); export const misbehaviourPunishmentConfig = z.object({default_flat_fine: z.number().int(), default_proportional_fine: z.number().int(), long_flat_mult: z.number().int(), long_interval: z.number().int(), long_proportional_mult: z.number().int(), medium_flat_mult: z.number().int(), medium_interval: z.number().int(), medium_proportional_mult: z.number().int(), severity_flat_mult: z.number().int(), severity_proportional_mult: z.number().int(), unpunishable_interval: z.number().int()}); export const sizeLimitsConfig = z.object({max_acc_state_bits: z.number().int().optional(), max_acc_state_cells: z.number().int().optional(), max_ext_msg_depth: z.number().int(), max_ext_msg_size: z.number().int(), max_library_cells: z.number().int(), max_msg_bits: z.number().int(), max_msg_cells: z.number().int(), max_vm_data_depth: z.number().int()}); export const oracle = z.object({address: z.string(), secp_pubkey: z.string()}); export const oracleBridgeParams = z.object({bridge_addr: z.string(), external_chain_address: z.string(), oracle_multisig_address: z.string(), oracles: z.array(oracle)}); export const jettonBridgePrices = z.object({bridge_burn_fee: z.number().int(), bridge_mint_fee: z.number().int(), discover_gas_consumption: z.number().int(), minter_min_gram_for_storage: z.number().int(), minter_min_tons_for_storage: z.number().int().optional(), wallet_gas_consumption: z.number().int(), wallet_min_gram_for_storage: z.number().int(), wallet_min_tons_for_storage: z.number().int().optional()}); export const jettonBridgeParams = z.object({bridge_address: z.string(), burn_bridge_fee: z.number().int().optional(), external_chain_address: z.string().optional(), oracles: z.array(oracle), oracles_address: z.string(), prices: jettonBridgePrices.optional(), state_flags: z.number().int()}); export const blockchainConfig = z.object({"0": z.string(), "1": z.string(), "2": z.string(), "3": z.string().optional(), "4": z.string(), "5": z.object({blackhole_addr: z.string().optional(), fee_burn_denom: z.number().int(), fee_burn_nom: z.number().int()}).optional(), "6": z.object({mint_add_price: z.number().int(), mint_new_price: z.number().int()}).optional(), "7": z.object({currencies: z.array(z.object({amount: z.string(), currency_id: z.number().int()}))}).optional(), "8": z.object({capabilities: z.number().int(), version: z.number().int()}).optional(), "9": z.object({mandatory_params: z.array(z.number().int())}).optional(), "10": z.object({critical_params: z.array(z.number().int())}).optional(), "11": z.object({critical_params: configProposalSetup, normal_params: configProposalSetup}).optional(), "12": z.object({workchains: z.array(workchainDescr)}).optional(), "13": z.object({bit_price: z.number().int(), cell_price: z.number().int(), deposit: z.number().int()}).optional(), "14": z.object({basechain_block_fee: z.number().int(), masterchain_block_fee: z.number().int()}).optional(), "15": z.object({elections_end_before: z.number().int(), elections_start_before: z.number().int(), stake_held_for: z.number().int(), validators_elected_for: z.number().int()}).optional(), "16": z.object({max_main_validators: z.number().int(), max_validators: z.number().int(), min_validators: z.number().int()}).optional(), "17": z.object({max_stake: z.string(), max_stake_factor: z.number().int(), min_stake: z.string(), min_total_stake: z.string()}).optional(), "18": z.object({storage_prices: z.array(z.object({bit_price_ps: z.number().int(), cell_price_ps: z.number().int(), mc_bit_price_ps: z.number().int(), mc_cell_price_ps: z.number().int(), utime_since: z.number().int()}))}).optional(), "20": z.object({gas_limits_prices: gasLimitPrices}).optional(), "21": z.object({gas_limits_prices: gasLimitPrices}).optional(), "22": z.object({block_limits: blockLimits}).optional(), "23": z.object({block_limits: blockLimits}).optional(), "24": z.object({msg_forward_prices: msgForwardPrices}).optional(), "25": z.object({msg_forward_prices: msgForwardPrices}).optional(), "28": z.object({flags: z.number().int().optional(), mc_catchain_lifetime: z.number().int(), shard_catchain_lifetime: z.number().int(), shard_validators_lifetime: z.number().int(), shard_validators_num: z.number().int(), shuffle_mc_validators: z.boolean().optional()}).optional(), "29": z.object({attempt_duration: z.number().int(), catchain_max_blocks_coeff: z.number().int().optional(), catchain_max_deps: z.number().int(), consensus_timeout_ms: z.number().int(), fast_attempts: z.number().int(), flags: z.number().int().optional(), max_block_bytes: z.number().int(), max_collated_bytes: z.number().int(), new_catchain_ids: z.boolean().optional(), next_candidate_delay_ms: z.number().int(), proto_version: z.number().int().optional(), round_candidates: z.number().int()}).optional(), "30": z.object({mc: newConsensusConfig.optional(), shard: newConsensusConfig.optional()}).optional(), "31": z.object({fundamental_smc_addr: z.array(z.string())}).optional(), "32": validatorsSet.optional(), "33": validatorsSet.optional(), "34": validatorsSet.optional(), "35": validatorsSet.optional(), "36": validatorsSet.optional(), "37": validatorsSet.optional(), "40": z.object({misbehaviour_punishment_config: misbehaviourPunishmentConfig}).optional(), "43": z.object({size_limits_config: sizeLimitsConfig}).optional(), "44": z.object({accounts: z.array(z.string()), suspended_until: z.number().int()}), "45": z.object({contracts: z.array(z.object({code_hash: z.string(), gas_usage: z.number().int()}))}).optional(), "71": z.object({oracle_bridge_params: oracleBridgeParams}).optional(), "72": z.object({oracle_bridge_params: oracleBridgeParams}).optional(), "73": z.object({oracle_bridge_params: oracleBridgeParams}).optional(), "79": z.object({jetton_bridge_params: jettonBridgeParams}).optional(), "81": z.object({jetton_bridge_params: jettonBridgeParams}).optional(), "82": z.object({jetton_bridge_params: jettonBridgeParams}).optional(), raw: 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 blockchain config from a specific block, if present. * * GET /v2/blockchain/masterchain/{masterchain_seqno}/config */ export const getApiV2BlockchainMasterchainMasterchainSeqnoConfig = async (args: GetApiV2BlockchainMasterchainMasterchainSeqnoConfigArgs, options?: GetApiV2BlockchainMasterchainMasterchainSeqnoConfigOptions) => { const {baseUrl = "https://tonapi.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v2/blockchain/masterchain/${encodeURIComponent(String(args.masterchain_seqno))}/config${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(blockchainConfig, await res.json()) };