From 20099d88d8512f18a891de5508e25d2c98ba8e1d Mon Sep 17 00:00:00 2001 From: liangping <18786721@qq.com> Date: Tue, 4 Apr 2023 17:16:58 +0800 Subject: [PATCH] implemented client --- src/libs/api.ts | 8 +- src/libs/client.ts | 156 ++++++++++++++++++++ src/libs/index.ts | 3 +- src/libs/registry.ts | 8 +- src/modules/[chain]/staking/[validator].vue | 1 - src/stores/useBlockchain.ts | 12 +- src/types/Pagination.ts | 20 +++ src/types/auth.ts | 24 +++ src/types/bank.ts | 30 ++++ src/types/common.ts | 4 + 10 files changed, 251 insertions(+), 15 deletions(-) create mode 100644 src/libs/client.ts create mode 100644 src/types/Pagination.ts create mode 100644 src/types/auth.ts create mode 100644 src/types/bank.ts create mode 100644 src/types/common.ts diff --git a/src/libs/api.ts b/src/libs/api.ts index c4e845f3..70443fab 100644 --- a/src/libs/api.ts +++ b/src/libs/api.ts @@ -1,6 +1,6 @@ import { type RequestRegistry, type Registry, adapter, withCustomAdapter } from "./registry"; -const DEFAULT: RequestRegistry = { +export const DEFAULT: RequestRegistry = { auth_params: { url: "/cosmos/auth/v1beta1/params", adapter }, auth_accounts: { url: "/cosmos/auth/v1beta1/accounts", adapter }, auth_account_address: { url: "/cosmos/auth/v1beta1/accounts/{address}", adapter }, @@ -23,7 +23,7 @@ const DEFAULT: RequestRegistry = { gov_proposals_deposits: { url: "/cosmos/gov/v1/proposals/{proposal_id}/deposits", adapter }, gov_proposals_tally: { url: "/cosmos/gov/v1/proposals/{proposal_id}/tally", adapter }, gov_proposals_votes: { url: "/cosmos/gov/v1/proposals/{proposal_id}/votes", adapter }, - gov_proposals_votes_voter: { url: "/cosmos/gov/v1/proposals/{proposal_id}/votes/voter", adapter }, + gov_proposals_votes_voter: { url: "/cosmos/gov/v1/proposals/{proposal_id}/votes/{voter}", adapter }, staking_deletations: { url: "/cosmos/staking/v1beta1/delegations/{delegator_addr}", adapter }, staking_delegator_redelegations: { url: "/cosmos/staking/v1beta1/delegators/{delegator_addr}/redelegations", adapter }, staking_delegator_unbonding_delegations: { url: "/cosmos/staking/v1beta1/delegators/{delegator_addr}/unbonding_delegations", adapter }, @@ -46,11 +46,11 @@ const DEFAULT: RequestRegistry = { tx_hash: { url: "/cosmos/tx/v1beta1/txs/{hash}", adapter }, }; -const VERSION_REGISTRY: Registry = { +export const VERSION_REGISTRY: Registry = { "0.46.1": DEFAULT } -const NAME_REGISTRY: Registry = { +export const NAME_REGISTRY: Registry = { "evmos": withCustomAdapter(DEFAULT, {}) } diff --git a/src/libs/client.ts b/src/libs/client.ts new file mode 100644 index 00000000..18f4e10a --- /dev/null +++ b/src/libs/client.ts @@ -0,0 +1,156 @@ +import { fetchData } from '@/libs'; +import { DEFAULT } from '@/libs' +import { adapter, type Request, type RequestRegistry } from './registry'; + +export class CosmosRestClient { + endpoint: string; + registry: RequestRegistry; + constructor(endpoint: string) { + this.endpoint = endpoint + this.registry = DEFAULT + } + async request(request: Request, args: Record, query="") { + let url = `${this.endpoint}${request.url}${query}` + Object.keys(args).forEach(k => { + url = url.replace(`{${k}}`, args[k]) + }) + return fetchData(url, adapter) + } + // Auth Module + async getAuthAccounts() { + return this.request(this.registry.auth_accounts, {}) + } + async getAuthAccount(address: string) { + return this.request(this.registry.auth_account_address, {address}) + } + // Bank Module + async getBankParams() { + return this.request(this.registry.bank_params, {}) + } + async getBankBalances(address: string) { + return this.request(this.registry.bank_balances_address, {address}) + } + async getBankDenomMetadata() { + return this.request(this.registry.bank_denoms_metadata, {}) + } + async getBankSupply() { + return this.request(this.registry.bank_supply, {}) + } + async getBankSupplyByDenom(denom: string) { + return this.request(this.registry.bank_supply_by_denom, {denom}) + } + // Distribution Module + async getDistributionParams() { + return this.request(this.registry.distribution_params, {}) + } + async getDistributionValidatorCommission(validator_address: string) { + return this.request(this.registry.distribution_validator_commission, {validator_address}) + } + async getDistributionValidatorOutstandingRewards(validator_address: string) { + return this.request(this.registry.distribution_validator_outstanding_rewards, {validator_address}) + } + async getDistributionValidatorSlashes(validator_address: string) { + return this.request(this.registry.distribution_validator_slashes, {validator_address}) + } + // Slashing + async getSlashingParams() { + return this.request(this.registry.slashing_params, {}) + } + async getSlashingSigningInfos() { + return this.request(this.registry.slashing_signing_info, {}) + } + // Gov + async getGovParamsVoting() { + return this.request(this.registry.gov_params_voting, {}) + } + async getGovParamsDeposit() { + return this.request(this.registry.gov_params_deposit, {}) + } + async getGovParamsTally() { + return this.request(this.registry.gov_params_tally, {}) + } + async getGovProposals() { + return this.request(this.registry.gov_proposals, {}) + } + async getGovProposal(proposal_id: string) { + return this.request(this.registry.gov_proposals_proposal_id, {proposal_id}) + } + async getGovProposalDeposits(proposal_id: string) { + return this.request(this.registry.gov_proposals_deposits, {proposal_id}) + } + async getGovProposalTally(proposal_id: string) { + return this.request(this.registry.gov_proposals_tally, {proposal_id}) + } + async getGovProposalVotes(proposal_id: string) { + return this.request(this.registry.gov_proposals_votes, {proposal_id}) + } + async getGovProposalVotesVoter(proposal_id: string, voter: string ) { + return this.request(this.registry.gov_proposals_votes_voter, {proposal_id, voter}) + } + // staking + async getStakingDelegations(delegator_addr: string) { + return this.request(this.registry.staking_deletations, {delegator_addr}) + } + async getStakingDelegatorRedelegations(delegator_addr: string) { + return this.request(this.registry.staking_delegator_redelegations, {delegator_addr}) + } + async getStakingDelegatorUnbonding(delegator_addr: string) { + return this.request(this.registry.staking_delegator_unbonding_delegations, {delegator_addr}) + } + async getStakingDelegatorValidators(delegator_addr: string) { + return this.request(this.registry.staking_delegator_validators, {delegator_addr}) + } + async getStakingParams() { + return this.request(this.registry.staking_params, {}) + } + async getStakingPool() { + return this.request(this.registry.staking_pool, {}) + } + async getStakingValidators() { + return this.request(this.registry.staking_validators, {}) + } + async getStakingValidator(validator_addr: string) { + return this.request(this.registry.staking_validators_address, {validator_addr}) + } + async getStakingValidatorsDelegations(validator_addr: string) { + return this.request(this.registry.staking_validators_delegations, {validator_addr}) + } + async getStakingValidatorsDelegationsDelegator(validator_addr: string, delegator_addr: string) { + return this.request(this.registry.staking_validators_delegations_delegator, {validator_addr, delegator_addr}) + } + async getStakingValidatorsDelegationsUnbonding(validator_addr: string, delegator_addr: string) { + return this.request(this.registry.staking_validators_delegations_unbonding_delegations, {validator_addr, delegator_addr}) + } + + //tendermint + async getBaseAbciQuery() { + return this.request(this.registry.base_tendermint_abci_query, {}) + } + async getBaseBlockLatest() { + return this.request(this.registry.base_tendermint_block_latest, {}) + } + async getBaseBlockAt(height: string|number) { + return this.request(this.registry.base_tendermint_block_height, {height}) + } + async getBaseNodeInfo() { + return this.request(this.registry.base_tendermint_node_info, {}) + } + async getBaseValidatorsetAt(height: string|number) { + return this.request(this.registry.base_tendermint_validatorsets_height, {height}) + } + async getBaseValidatorsetLatest() { + return this.request(this.registry.base_tendermint_block_latest, {}) + } + // tx + async getTxsBySender(sender: string) { + const query = `?events=message.sender='${sender}'&pagination.reverse=true` + return this.request(this.registry.tx_txs, {}, query) + } + async getTxsAt(height: string|number) { + return this.request(this.registry.tx_txs_block, {height}) + } + async getTx(hash: string) { + return this.request(this.registry.tx_hash, {}) + } + +} diff --git a/src/libs/index.ts b/src/libs/index.ts index 42332c20..db82db54 100644 --- a/src/libs/index.ts +++ b/src/libs/index.ts @@ -1,3 +1,4 @@ export * from './address' export * from './http' -export * from './misc' \ No newline at end of file +export * from './misc' +export * from './api' \ No newline at end of file diff --git a/src/libs/registry.ts b/src/libs/registry.ts index 3e396597..456631cf 100644 --- a/src/libs/registry.ts +++ b/src/libs/registry.ts @@ -1,6 +1,8 @@ import type { Block, NodeInfo, SlashingSigningInfo } from "@/types"; +import type { PaginabledAccounts } from "@/types/Auth"; import type { Tx } from "@/types/Tx"; import type { Txs } from "@/types/Txs"; +import type { PaginatedDenomMetadata, PaginatedSupply } from "@/types/bank"; import semver from "semver"; @@ -12,13 +14,13 @@ export interface Request { // use snake style, since the all return object use snake style. export interface RequestRegistry { auth_params: Request - auth_accounts: Request; + auth_accounts: Request; auth_account_address: Request; bank_params: Request; bank_balances_address: Request; - bank_denoms_metadata: Request; - bank_supply: Request; + bank_denoms_metadata: Request; + bank_supply: Request; bank_supply_by_denom: Request; distribution_params: Request; diff --git a/src/modules/[chain]/staking/[validator].vue b/src/modules/[chain]/staking/[validator].vue index c9e73932..1b19ee12 100644 --- a/src/modules/[chain]/staking/[validator].vue +++ b/src/modules/[chain]/staking/[validator].vue @@ -1,7 +1,6 @@