implement custom api for evmos
This commit is contained in:
parent
98e4c3d1c2
commit
4a494738f9
@ -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, {}),
|
||||
};
|
||||
|
@ -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<R extends AbstractRegistry> {
|
||||
endpoint: string;
|
||||
@ -22,7 +25,7 @@ export class BaseRestClient<R extends AbstractRegistry> {
|
||||
Object.keys(args).forEach((k) => {
|
||||
url = url.replace(`{${k}}`, args[k] || '');
|
||||
});
|
||||
return fetchData<T>(url, adapter);
|
||||
return fetchData<T>(url, request.adapter);
|
||||
}
|
||||
}
|
||||
|
||||
@ -30,6 +33,13 @@ 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)
|
||||
}
|
||||
|
||||
// Auth Module
|
||||
async getAuthAccounts(page?: PageRequest) {
|
||||
if(!page) page = new PageRequest()
|
||||
@ -49,12 +59,19 @@ export class CosmosRestClient extends BaseRestClient<RequestRegistry> {
|
||||
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() {
|
||||
|
4
src/libs/custom_api/evmos.ts
Normal file
4
src/libs/custom_api/evmos.ts
Normal file
@ -0,0 +1,4 @@
|
||||
import { DEFAULT, type RequestRegistry } from '@/libs'
|
||||
export const CUSTOM: Partial<RequestRegistry> = {
|
||||
mint_inflation: { url: '/evmos/inflation/v1/inflation_rate', adapter: data => ({inflation: Number(data.inflation_rate || 0)/ 100}) },
|
||||
}
|
@ -155,36 +155,46 @@ export function adapter<T>(source: any): T {
|
||||
return source;
|
||||
}
|
||||
|
||||
export interface Registry {
|
||||
export interface ApiProfileRegistry {
|
||||
[key: string]: RequestRegistry;
|
||||
}
|
||||
|
||||
export function withCustomAdapter<T extends RequestRegistry>(
|
||||
export function withCustomRequest<T extends RequestRegistry>(
|
||||
target: T,
|
||||
source?: Partial<T>
|
||||
): 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];
|
||||
}
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user