implemented client

This commit is contained in:
liangping 2023-04-04 17:16:58 +08:00
parent c30a652465
commit 20099d88d8
10 changed files with 251 additions and 15 deletions

View File

@ -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, {})
}

156
src/libs/client.ts Normal file
View File

@ -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<T>(request: Request<T>, args: Record<string, any>, query="") {
let url = `${this.endpoint}${request.url}${query}`
Object.keys(args).forEach(k => {
url = url.replace(`{${k}}`, args[k])
})
return fetchData<T>(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, {})
}
}

View File

@ -1,3 +1,4 @@
export * from './address'
export * from './http'
export * from './misc'
export * from './misc'
export * from './api'

View File

@ -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<T> {
// use snake style, since the all return object use snake style.
export interface RequestRegistry {
auth_params: Request<any>
auth_accounts: Request<any>;
auth_accounts: Request<PaginabledAccounts>;
auth_account_address: Request<any>;
bank_params: Request<any>;
bank_balances_address: Request<any>;
bank_denoms_metadata: Request<any>;
bank_supply: Request<any>;
bank_denoms_metadata: Request<PaginatedDenomMetadata>;
bank_supply: Request<PaginatedSupply>;
bank_supply_by_denom: Request<any>;
distribution_params: Request<any>;

View File

@ -1,7 +1,6 @@
<script setup lang="ts">
import { useBlockchain, useFormatter, useMintStore, useStakingStore } from '@/stores';
import { onMounted } from 'vue';
import { useRoute } from 'vue-router';
import ValidatorCommissionRate from '@/components/ValidatorCommissionRate.vue'
import { consensusPubkeyToHexAddress, operatorAddressToAccount, pubKeyToValcons, valoperToPrefix } from '@/libs';
import { computed } from '@vue/reactivity';

View File

@ -95,12 +95,12 @@ export const useBlockchain = defineStore("blockchain", {
actions: {
async initial() {
await this.randomSetupEndpoint()
await useStakingStore().init()
useBankStore().initial()
useBaseStore().initial()
useGovStore().initial()
useMintStore().initial()
useBlockModule().initial()
// await useStakingStore().init()
// useBankStore().initial()
// useBaseStore().initial()
// useGovStore().initial()
// useMintStore().initial()
// useBlockModule().initial()
},
async randomSetupEndpoint() {

20
src/types/Pagination.ts Normal file
View File

@ -0,0 +1,20 @@
export interface Pagination {
key?: string;
total?: string;
}
export interface PaginatedResponse {
pagination: Pagination;
}
/*
class PaginatedResponse<T, K extends string> {
[key: string]: T[];
pagination: Pagination;
constructor(data: T[], pagination: Pagination, dataFieldName: K) {
this.pagination = pagination;
this[dataFieldName] = data;
}
}
// */

24
src/types/auth.ts Normal file
View File

@ -0,0 +1,24 @@
import type { PaginatedResponse } from "./Pagination";
import type { Key } from "./common";
export interface AuthAccount {
"@type": string,
"address": string,
"pub_key": Key,
"account_number": string,
"sequence": string
}
export interface AuthParam {
params: {
"max_memo_characters": string,
"tx_sig_limit": string,
"tx_size_cost_per_byte": string,
"sig_verify_cost_ed25519": string,
"sig_verify_cost_secp256k1": string
}
}
export interface PaginabledAccounts extends PaginatedResponse {
accounts: AuthAccount[]
}

30
src/types/bank.ts Normal file
View File

@ -0,0 +1,30 @@
import type { Coin } from "./Coin";
import type { PaginatedResponse } from "./Pagination";
export interface BankParams {
params: {
"send_enabled": string[],
"default_send_enabled": boolean;
}
}
export interface DenomMetadata {
"description": string,
"denom_units": {
"denom": string,
"exponent": number,
"aliases": string[]
}[],
"base": string,
"display": string,
"name": string,
"symbol": string
}
export interface PaginatedDenomMetadata extends PaginatedResponse{
metadatas: DenomMetadata[]
}
export interface PaginatedSupply extends PaginatedResponse {
supply: Coin[]
}

4
src/types/common.ts Normal file
View File

@ -0,0 +1,4 @@
export interface Key {
"@type": string,
"key": string,
}