add version compatible implementation

This commit is contained in:
liangping 2023-06-19 21:33:50 +08:00
parent f69546d44f
commit 32b37f16c7
8 changed files with 115 additions and 18 deletions

View File

@ -37,7 +37,7 @@ function gotoPage(pageNum: number) {
</script>
<template>
<div class="my-5">
<div class="my-5 text-center">
<div v-if="total && limit" class="btn-group">
<button v-for="{ page, color } in pages" :key="page"
class="btn bg-gray-100 text-gray-500 hover:text-white border-none dark:bg-gray-800 dark:text-white" :class="{

View File

@ -62,6 +62,7 @@ const proposalInfo = ref();
{{ item?.content?.title }}
</RouterLink>
<div
v-if="item.content"
class="bg-[#f6f2ff] text-[#9c6cff] dark:bg-gray-600 dark:text-gray-300 inline-block rounded-full px-2 py-[1px] text-xs mb-1"
>
{{ showType(item.content['@type']) }}
@ -157,6 +158,7 @@ const proposalInfo = ref();
<div class="grid grid-cols-4 mt-2 mb-2">
<div class="col-span-2">
<div
v-if="item.content"
class="bg-[#f6f2ff] text-[#9c6cff] dark:bg-gray-600 dark:text-gray-300 inline-block rounded-full px-2 py-[1px] text-xs mb-1"
>
{{ showType(item.content['@type']) }}

View File

@ -6,11 +6,12 @@ import {
type RequestRegistry,
type AbstractRegistry,
findApiProfileByChain,
findApiProfileBySDKVersion,
registryChainProfile,
registryVersionProfile,
withCustomRequest,
} from './registry';
import { PageRequest,type Coin } from '@/types';
import { CUSTOM } from './custom_api/evmos'
export class BaseRestClient<R extends AbstractRegistry> {
endpoint: string;
@ -28,15 +29,37 @@ export class BaseRestClient<R extends AbstractRegistry> {
}
}
// dynamic all custom request implementations
function registeCustomRequest() {
const extensions: Record<string, any> = import.meta.glob('./clients/*.ts', { eager: true });
Object.values(extensions).forEach(m => {
if(m.store === 'version') {
registryVersionProfile(m.name, withCustomRequest(DEFAULT, m.requests))
} else {
registryChainProfile(m.name, withCustomRequest(DEFAULT, m.requests));
}
});
}
registeCustomRequest()
export class CosmosRestClient extends BaseRestClient<RequestRegistry> {
static newDefault(endpoint: string) {
return new CosmosRestClient(endpoint, DEFAULT)
}
static newStrategy(endpoint: string, chain: any) {
registryChainProfile('evmos', withCustomRequest(DEFAULT, CUSTOM))
const re = findApiProfileByChain(chain.chainName)
return new CosmosRestClient(endpoint, re || DEFAULT)
let req
if(chain) {
// find by name first
req = findApiProfileByChain(chain.chainName)
// if not found. try sdk version
if(!req && chain.versions?.cosmosSdk) {
req = findApiProfileBySDKVersion(chain.versions?.cosmosSdk)
}
}
return new CosmosRestClient(endpoint, req || DEFAULT)
}
// Auth Module

View File

@ -1,5 +1,9 @@
import type{ RequestRegistry } from '@/libs/registry'
import { DEFAULT } from '@/libs'
export const CUSTOM: Partial<RequestRegistry> = {
// which registry is store
export const store = 'name' // name or version
// Blockchain Name
export const name = 'evmos'
export const requests: Partial<RequestRegistry> = {
mint_inflation: { url: '/evmos/inflation/v1/inflation_rate', adapter: (data: any) => ({inflation: (Number(data.inflation_rate || 0)/ 100 ).toFixed(2)}) },
}

View File

@ -0,0 +1,67 @@
import type { RequestRegistry } from '@/libs/registry'
import { adapter } from '@/libs/registry'
import type {
GovParams,
GovProposal,
GovVote,
PaginatedProposalDeposit,
PaginatedProposalVotes,
PaginatedProposals,
Tally,
} from '@/types/';
// which registry is store
export const store = 'version' // name or version
// Blockchain Name
export const name = 'v0.46.7'
function proposalAdapter(p: any): GovProposal {
if(p) {
if(p.messages) p.content = p.messages[0].content
p.proposal_id = p.id
p.final_tally_result = {
yes: p.final_tally_result?.yes_count,
no: p.final_tally_result?.no_count,
no_with_veto: p.final_tally_result?.no_with_veto_count,
abstain: p.final_tally_result?.abstain_count,
}
}
return p
}
export const requests: Partial<RequestRegistry> = {
gov_params_voting: { url: '/cosmos/gov/v1/params/voting', adapter },
gov_params_tally: { url: '/cosmos/gov/v1/params/tallying', adapter },
gov_params_deposit: { url: '/cosmos/gov/v1/params/deposit', adapter },
gov_proposals: { url: '/cosmos/gov/v1/proposals', adapter: (source: any): PaginatedProposals => {
const proposals = source.proposals.map((p:any) => proposalAdapter(p))
return {
proposals,
pagination: source.pagination
}
}},
gov_proposals_proposal_id: {
url: '/cosmos/gov/v1/proposals/{proposal_id}',
adapter: (source: any): {proposal: GovProposal} => {
return {
proposal: proposalAdapter(source.proposal)
}
},
},
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,
},
}

View File

@ -185,28 +185,26 @@ export function findApiProfileByChain(
// if (!url) {
// throw new Error(`Unsupported version or name: ${name}`);
// }
return url;
}
export function findApiProfileBySDKVersion(
version: string,
): RequestRegistry {
): RequestRegistry | undefined {
let closestVersion: string | null = null;
for (const key in VERSION_REGISTRY) {
if (semver.satisfies(key, version)) {
for (const k in VERSION_REGISTRY) {
const key = k.replace('v', "")
// console.log(semver.gt(key, version), semver.gte(version, key), key, version)
if (semver.lte(key, version)) {
if (!closestVersion || semver.gt(key, closestVersion)) {
closestVersion = key;
closestVersion = k;
}
}
}
// console.log(`Closest version to ${version}: ${closestVersion}`, VERSION_REGISTRY);
if (!closestVersion) {
throw new Error(`Unsupported version: ${version}`);
return undefined;
}
console.log(`Closest version to ${version}: ${closestVersion}`);
return VERSION_REGISTRY[closestVersion];
}

View File

@ -149,6 +149,9 @@ export function fromLocal(lc: LocalConfig): ChainConfig {
{ denom: x.symbol.toLowerCase(), exponent: Number(x.exponent) },
],
}));
conf.versions = {
cosmosSdk: lc.sdk_version
}
conf.bech32Prefix = lc.addr_prefix;
conf.chainName = lc.chain_name;
conf.coinType = lc.coin_type;

View File

@ -2,7 +2,7 @@
"extends": "@vue/tsconfig/tsconfig.json",
"include": ["env.d.ts", "src/**/*", "src/**/*.vue"],
"compilerOptions": {
"ignoreDeprecations": "5.0",
// "ignoreDeprecations": "5.0",
"lib": [
"es2022",
"dom"