implement custom api for evmos
This commit is contained in:
parent
98e4c3d1c2
commit
4a494738f9
@ -1,8 +1,6 @@
|
|||||||
import {
|
import {
|
||||||
type RequestRegistry,
|
type RequestRegistry,
|
||||||
type Registry,
|
|
||||||
adapter,
|
adapter,
|
||||||
withCustomAdapter,
|
|
||||||
} from './registry';
|
} from './registry';
|
||||||
|
|
||||||
export const DEFAULT: RequestRegistry = {
|
export const DEFAULT: RequestRegistry = {
|
||||||
@ -193,11 +191,3 @@ export const DEFAULT: RequestRegistry = {
|
|||||||
adapter,
|
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 { DEFAULT } from '@/libs';
|
||||||
import {
|
import {
|
||||||
adapter,
|
adapter,
|
||||||
withCustomAdapter,
|
|
||||||
type Request,
|
type Request,
|
||||||
type RequestRegistry,
|
type RequestRegistry,
|
||||||
type Registry,
|
type Registry,
|
||||||
type AbstractRegistry,
|
type AbstractRegistry,
|
||||||
|
findApiProfileByChain,
|
||||||
|
registryChainProfile,
|
||||||
|
withCustomRequest,
|
||||||
} from './registry';
|
} from './registry';
|
||||||
import { PageRequest } from '@/types';
|
import { PageRequest } from '@/types';
|
||||||
|
import { CUSTOM } from './custom_api/evmos'
|
||||||
|
|
||||||
export class BaseRestClient<R extends AbstractRegistry> {
|
export class BaseRestClient<R extends AbstractRegistry> {
|
||||||
endpoint: string;
|
endpoint: string;
|
||||||
@ -22,7 +25,7 @@ export class BaseRestClient<R extends AbstractRegistry> {
|
|||||||
Object.keys(args).forEach((k) => {
|
Object.keys(args).forEach((k) => {
|
||||||
url = url.replace(`{${k}}`, args[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) {
|
static newDefault(endpoint: string) {
|
||||||
return new CosmosRestClient(endpoint, DEFAULT)
|
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
|
// Auth Module
|
||||||
async getAuthAccounts(page?: PageRequest) {
|
async getAuthAccounts(page?: PageRequest) {
|
||||||
if(!page) page = new PageRequest()
|
if(!page) page = new PageRequest()
|
||||||
@ -49,12 +59,19 @@ export class CosmosRestClient extends BaseRestClient<RequestRegistry> {
|
|||||||
async getBankDenomMetadata() {
|
async getBankDenomMetadata() {
|
||||||
return this.request(this.registry.bank_denoms_metadata, {});
|
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()}`;
|
const query =`?${page.toQueryString()}`;
|
||||||
return this.request(this.registry.bank_supply, {}, query);
|
return this.request(this.registry.bank_supply, {}, query);
|
||||||
}
|
}
|
||||||
async getBankSupplyByDenom(denom: string) {
|
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
|
// Distribution Module
|
||||||
async getDistributionParams() {
|
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;
|
return source;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Registry {
|
export interface ApiProfileRegistry {
|
||||||
[key: string]: RequestRegistry;
|
[key: string]: RequestRegistry;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function withCustomAdapter<T extends RequestRegistry>(
|
export function withCustomRequest<T extends RequestRegistry>(
|
||||||
target: T,
|
target: T,
|
||||||
source?: Partial<T>
|
source?: Partial<T>
|
||||||
): T {
|
): T {
|
||||||
return source ? Object.assign({}, target, source) : target;
|
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,
|
name: string,
|
||||||
registry: Registry
|
|
||||||
): RequestRegistry {
|
): RequestRegistry {
|
||||||
const url = registry[name];
|
const url = NAME_REGISTRY[name];
|
||||||
if (!url) {
|
// if (!url) {
|
||||||
throw new Error(`Unsupported version or name: ${name}`);
|
// throw new Error(`Unsupported version or name: ${name}`);
|
||||||
}
|
// }
|
||||||
|
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function findConfigByVersion(
|
export function findApiProfileBySDKVersion(
|
||||||
version: string,
|
version: string,
|
||||||
registry: Registry
|
|
||||||
): RequestRegistry {
|
): RequestRegistry {
|
||||||
let closestVersion: string | null = null;
|
let closestVersion: string | null = null;
|
||||||
|
|
||||||
for (const key in registry) {
|
for (const key in VERSION_REGISTRY) {
|
||||||
if (semver.satisfies(key, version)) {
|
if (semver.satisfies(key, version)) {
|
||||||
if (!closestVersion || semver.gt(key, closestVersion)) {
|
if (!closestVersion || semver.gt(key, closestVersion)) {
|
||||||
closestVersion = key;
|
closestVersion = key;
|
||||||
@ -198,5 +208,5 @@ export function findConfigByVersion(
|
|||||||
|
|
||||||
console.log(`Closest version to ${version}: ${closestVersion}`);
|
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) {
|
async setRestEndpoint(endpoint: Endpoint) {
|
||||||
this.connErr = '';
|
this.connErr = '';
|
||||||
this.endpoint = endpoint;
|
this.endpoint = endpoint;
|
||||||
this.rpc = new CosmosRestClient(endpoint.address, DEFAULT);
|
this.rpc = CosmosRestClient.newStrategy(endpoint.address, this.current);
|
||||||
localStorage.setItem(
|
localStorage.setItem(
|
||||||
`endpoint-${this.chainName}`,
|
`endpoint-${this.chainName}`,
|
||||||
JSON.stringify(endpoint)
|
JSON.stringify(endpoint)
|
||||||
|
Loading…
Reference in New Issue
Block a user