diff --git a/src/libs/api.ts b/src/libs/api.ts index fcfabe89..9bef3c80 100644 --- a/src/libs/api.ts +++ b/src/libs/api.ts @@ -1,8 +1,6 @@ import { type RequestRegistry, - type Registry, adapter, - withCustomAdapter, } from './registry'; export const DEFAULT: RequestRegistry = { @@ -193,11 +191,3 @@ export const DEFAULT: RequestRegistry = { adapter, }, }; - -export const VERSION_REGISTRY: Registry = { - '0.46.1': DEFAULT, -}; - -export const NAME_REGISTRY: Registry = { - evmos: withCustomAdapter(DEFAULT, {}), -}; diff --git a/src/libs/client.ts b/src/libs/client.ts index 49841250..cddac94e 100644 --- a/src/libs/client.ts +++ b/src/libs/client.ts @@ -2,13 +2,16 @@ import { fetchData } from '@/libs'; import { DEFAULT } from '@/libs'; import { adapter, - withCustomAdapter, type Request, type RequestRegistry, type Registry, type AbstractRegistry, + findApiProfileByChain, + registryChainProfile, + withCustomRequest, } from './registry'; import { PageRequest } from '@/types'; +import { CUSTOM } from './custom_api/evmos' export class BaseRestClient { endpoint: string; @@ -22,7 +25,7 @@ export class BaseRestClient { Object.keys(args).forEach((k) => { url = url.replace(`{${k}}`, args[k] || ''); }); - return fetchData(url, adapter); + return fetchData(url, request.adapter); } } @@ -30,6 +33,13 @@ export class CosmosRestClient extends BaseRestClient { 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) + } + // Auth Module async getAuthAccounts(page?: PageRequest) { if(!page) page = new PageRequest() @@ -49,12 +59,19 @@ export class CosmosRestClient extends BaseRestClient { async getBankDenomMetadata() { return this.request(this.registry.bank_denoms_metadata, {}); } - async getBankSupply(page?: PageRequest) { if(!page) page = new PageRequest() + async getBankSupply(page?: PageRequest) { + if(!page) page = new PageRequest() const query =`?${page.toQueryString()}`; return this.request(this.registry.bank_supply, {}, query); } async getBankSupplyByDenom(denom: string) { - return this.request(this.registry.bank_supply_by_denom, { denom }); + let supply; + try{ + supply = await this.request(this.registry.bank_supply_by_denom, { denom }); + } catch(err) { + supply = await this.request({url: "/cosmos/bank/v1beta1/supply/by_denom?denom={denom}", adapter }, { denom }); + } + return supply } // Distribution Module async getDistributionParams() { diff --git a/src/libs/custom_api/evmos.ts b/src/libs/custom_api/evmos.ts new file mode 100644 index 00000000..ee333137 --- /dev/null +++ b/src/libs/custom_api/evmos.ts @@ -0,0 +1,4 @@ +import { DEFAULT, type RequestRegistry } from '@/libs' +export const CUSTOM: Partial = { + mint_inflation: { url: '/evmos/inflation/v1/inflation_rate', adapter: data => ({inflation: Number(data.inflation_rate || 0)/ 100}) }, +} diff --git a/src/libs/registry.ts b/src/libs/registry.ts index 3d77839a..45c343a0 100644 --- a/src/libs/registry.ts +++ b/src/libs/registry.ts @@ -155,36 +155,46 @@ export function adapter(source: any): T { return source; } -export interface Registry { +export interface ApiProfileRegistry { [key: string]: RequestRegistry; } -export function withCustomAdapter( +export function withCustomRequest( target: T, source?: Partial ): T { return source ? Object.assign({}, target, source) : target; } -export function findConfigByName( +// 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, - registry: Registry ): RequestRegistry { - const url = registry[name]; - if (!url) { - throw new Error(`Unsupported version or name: ${name}`); - } + const url = NAME_REGISTRY[name]; + // if (!url) { + // throw new Error(`Unsupported version or name: ${name}`); + // } return url; } -export function findConfigByVersion( +export function findApiProfileBySDKVersion( version: string, - registry: Registry ): RequestRegistry { let closestVersion: string | null = null; - for (const key in registry) { + for (const key in VERSION_REGISTRY) { if (semver.satisfies(key, version)) { if (!closestVersion || semver.gt(key, closestVersion)) { closestVersion = key; @@ -198,5 +208,5 @@ export function findConfigByVersion( console.log(`Closest version to ${version}: ${closestVersion}`); - return registry[closestVersion]; + return VERSION_REGISTRY[closestVersion]; } diff --git a/src/stores/useBlockchain.ts b/src/stores/useBlockchain.ts index 4fb7d5ce..5dbb0dfc 100644 --- a/src/stores/useBlockchain.ts +++ b/src/stores/useBlockchain.ts @@ -165,7 +165,7 @@ export const useBlockchain = defineStore('blockchain', { async setRestEndpoint(endpoint: Endpoint) { this.connErr = ''; this.endpoint = endpoint; - this.rpc = new CosmosRestClient(endpoint.address, DEFAULT); + this.rpc = CosmosRestClient.newStrategy(endpoint.address, this.current); localStorage.setItem( `endpoint-${this.chainName}`, JSON.stringify(endpoint)