import type { AuthAccount, Block, ClientStateWithProof, Coin, ConnectionWithProof, DenomTrace, Group, GroupProposal, GroupTallyResult, NodeInfo, PaginabledAccounts, PaginatedGroupProposals, PaginatedGroups, PaginatedIBCChannels, PaginatedIBCConnections, PaginatedTendermintValidator, } from '@/types'; import type { BankParams, PaginatedBalances, PaginatedDenomMetadata, PaginatedSupply, } from '@/types/bank'; import type { DistributionParams, PaginatedSlashes, } from '@/types/distribution'; import type { GovParams, GovProposal, GovVote, PaginatedProposalDeposit, PaginatedProposalVotes, PaginatedProposals, Tally, } from '@/types/gov'; import type { PaginatedSigningInfo, SlashingParam } from '@/types/slashing'; import type { Delegation, PaginatedDelegations, PaginatedRedelegations, PaginatedUnbonding, PaginatedValdiators, StakingParam, StakingPool, Validator, } from '@/types/staking'; import type { PaginatedTxs, Tx, TxResponse } from '@/types'; import semver from 'semver'; export interface Request { url: string; adapter: (source: any) => Promise; } export interface AbstractRegistry { [key: string]: Request; } // use snake style, since the all return object use snake style. export interface RequestRegistry extends AbstractRegistry { auth_params: Request; auth_accounts: Request; auth_account_address: Request<{ account: AuthAccount }>; bank_params: Request; bank_balances_address: Request; bank_denoms_metadata: Request; bank_supply: Request; bank_supply_by_denom: Request<{ amount: Coin }>; distribution_params: Request; distribution_validator_commission: Request<{ commission?: { commission?: Coin[] }; }>; distribution_validator_outstanding_rewards: Request<{ rewards?: { rewards?: Coin[] }; }>; distribution_validator_slashes: Request; distribution_community_pool: Request<{ pool: Coin[] }>; distribution_delegator_rewards: Request<{ rewards: { validator_address: string; reward: Coin[]; }[]; total: Coin[]; }>; mint_inflation: Request<{ inflation: string }>; mint_params: Request<{ params: { mint_denom: string; blocks_per_year: string; }; }>; mint_annual_provisions: Request<{ annual_provisions: string }>; slashing_params: Request<{ params: SlashingParam }>; slashing_signing_info: Request; gov_params_voting: Request; gov_params_tally: Request; gov_params_deposit: Request; gov_proposals: Request; gov_proposals_proposal_id: Request<{ proposal: GovProposal }>; gov_proposals_deposits: Request; gov_proposals_tally: Request<{ tally: Tally }>; gov_proposals_votes: Request; gov_proposals_votes_voter: Request<{ vote: GovVote }>; staking_deletations: Request; staking_delegator_redelegations: Request; staking_delegator_unbonding_delegations: Request; staking_delegator_validators: Request; staking_params: Request; staking_pool: Request; staking_validators: Request; staking_validators_address: Request<{ validator: Validator }>; staking_validators_delegations: Request; staking_validators_delegations_delegator: Request<{ delegation_response: Delegation; }>; staking_validators_delegations_unbonding_delegations: Request; base_tendermint_abci_query: Request; base_tendermint_block_latest: Request; base_tendermint_block_height: Request; base_tendermint_node_info: Request; base_tendermint_validatorsets_latest: Request; base_tendermint_validatorsets_height: Request; params: Request<{ param: any }>; group_groups: Request; group_groups_by_admin: Request; group_groups_by_member: Request; group_proposal: Request<{ proposal: GroupProposal }>; group_proposal_tally: Request<{ tally: GroupTallyResult }>; group_proposals_by_group_policy: Request; tx_txs: Request; tx_txs_block: Request; tx_hash: Request<{ tx: Tx; tx_response: TxResponse }>; ibc_app_ica_controller_params: Request; ibc_app_ica_host_params: Request; ibc_app_transfer_escrow_address: Request; ibc_app_transfer_denom_traces: Request; ibc_app_transfer_denom_traces_hash: Request<{ denom_trace: DenomTrace; }>; ibc_core_channel_channels: Request; ibc_core_channel_channels_next_sequence: Request<{ next_sequence_receive: string; proof: string; proof_height: { revision_number: string; revision_height: string; }; }>; ibc_core_channel_channels_acknowledgements: Request; ibc_core_channel_connections_channels: Request; ibc_core_connection_connections: Request; ibc_core_connection_connections_connection_id: Request; ibc_core_connection_connections_connection_id_client_state: Request; interchain_security_ccv_provider_validator_consumer_addr: Request<{ consumer_address: string; }>; interchain_security_provider_opted_in_validators: Request<{ validators_provider_addresses: string[]; }>; interchain_security_consumer_validators: Request<{ validators: { provider_address: string; consumer_key: { ed25519: string }; power: string; }[]; }>; } export function adapter(source: any): Promise { return source; } export interface ApiProfileRegistry { [key: string]: RequestRegistry; } export function withCustomRequest( target: T, source?: Partial ): T { return source ? Object.assign({}, target, source) : target; } // SDK Version Profile Registry export const VERSION_REGISTRY: ApiProfileRegistry = {}; // ChainName Profile Registory export const NAME_REGISTRY: ApiProfileRegistry = {}; export function registryVersionProfile( version: string, requests: RequestRegistry ) { VERSION_REGISTRY[version] = requests; } export function registryChainProfile( version: string, requests: RequestRegistry ) { NAME_REGISTRY[version] = requests; } export function findApiProfileByChain(name: string): RequestRegistry { const url = NAME_REGISTRY[name]; // if (!url) { // throw new Error(`Unsupported version or name: ${name}`); // } return url; } export function findApiProfileBySDKVersion( version: string ): RequestRegistry | undefined { let closestVersion: string | null = null; const chain_version = version.match(/(\d+\.\d+\.?\d*)/g) || ['']; for (const k in VERSION_REGISTRY) { const key = k.replace('v', ''); if (semver.lte(key, chain_version[0])) { if (!closestVersion || semver.gt(key, closestVersion)) { closestVersion = k; } } } // console.log(`Closest version to ${version}: ${closestVersion}`, VERSION_REGISTRY); if (!closestVersion) { return undefined; } return VERSION_REGISTRY[closestVersion]; }