From 9c660d8da22b232d484c9810fff5ea435f27b1ed Mon Sep 17 00:00:00 2001 From: ivivanov Date: Sat, 27 Jan 2024 12:04:05 +0200 Subject: [PATCH 1/3] add chain specific inflation calc --- chains/mainnet/nolus.json | 27 +++++++++++++++++++++++++++ src/libs/client.ts | 2 +- src/libs/clients/nolus.ts | 26 ++++++++++++++++++++++++++ src/libs/http.ts | 2 +- src/libs/registry.ts | 4 ++-- 5 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 chains/mainnet/nolus.json create mode 100644 src/libs/clients/nolus.ts diff --git a/chains/mainnet/nolus.json b/chains/mainnet/nolus.json new file mode 100644 index 00000000..81bdd484 --- /dev/null +++ b/chains/mainnet/nolus.json @@ -0,0 +1,27 @@ +{ + "chain_name": "nolus", + "coingecko": "nolus", + "api": [ + {"provider": "Nolus", "address": "https://pirin-cl.nolus.network:1317"}, + {"provider": "LavenderFive", "address": "https://nolus-api.lavenderfive.com:443"}, + {"provider": "Allnodes", "address": "https://nolus-rest.publicnode.com"} + ], + "rpc": [ + {"provider": "Nolus", "address": "https://pirin-cl.nolus.network:26657"}, + {"provider": "LavenderFive", "address": "https://nolus-rpc.lavenderfive.com:443"}, + {"provider": "Allnodes", "address": "https://nolus-rpc.publicnode.com:443"} + ], + "snapshot_provider": "", + "sdk_version": "v0.47.6", + "coin_type": "118", + "min_tx_fee": "0", + "addr_prefix": "nolus", + "logo": "/logos/nolus.svg", + "assets": [{ + "base": "unls", + "symbol": "NLS", + "exponent": "6", + "coingecko_id": "nolus", + "logo": "/logos/nolus.svg" + }] +} diff --git a/src/libs/client.ts b/src/libs/client.ts index 359ac486..75423eb1 100644 --- a/src/libs/client.ts +++ b/src/libs/client.ts @@ -20,7 +20,7 @@ export class BaseRestClient { this.endpoint = endpoint; this.registry = registry; } - async request(request: Request, args: Record, query = '', adapter?: (source: any) => T ) { + async request(request: Request, args: Record, query = '', adapter?: (source: any) => Promise ) { let url = `${request.url.startsWith("http")?'':this.endpoint}${request.url}${query}`; Object.keys(args).forEach((k) => { url = url.replace(`{${k}}`, args[k] || ''); diff --git a/src/libs/clients/nolus.ts b/src/libs/clients/nolus.ts new file mode 100644 index 00000000..b318a037 --- /dev/null +++ b/src/libs/clients/nolus.ts @@ -0,0 +1,26 @@ +import type { RequestRegistry } from '@/libs/registry' +import { CosmosRestClient } from '@/libs/client'; +import { useBlockchain } from '@/stores'; +import { adapter } from '@/libs/registry' + +// Blockchain Name +export const name = 'nolus'; + +// nolus custom request +export const requests: Partial = { + mint_inflation: { + url: '/nolus/mint/v1beta1/annual_inflation', + adapter: async (data: any): Promise => { + try { + const client = CosmosRestClient.newDefault(useBlockchain().endpoint.address) + const staking = await client.getStakingPool() + const inflation = Number(data.annual_inflation) / Number(staking.pool.bonded_tokens) || 0; + return { inflation: inflation.toString() }; + } catch (error) { + console.log("Error in adapter:", error); + return { inflation: "0" }; + } + } + }, + bank_supply_by_denom: { url: "/cosmos/bank/v1beta1/supply/by_denom?denom={denom}", adapter } +}; diff --git a/src/libs/http.ts b/src/libs/http.ts index 0c134fa8..8334d647 100644 --- a/src/libs/http.ts +++ b/src/libs/http.ts @@ -2,7 +2,7 @@ import fetch from 'cross-fetch'; export async function fetchData( url: string, - adapter: (source: any) => T + adapter: (source: any) => Promise ): Promise { const response = await fetch(url); if (!response.ok) { diff --git a/src/libs/registry.ts b/src/libs/registry.ts index 1378ad27..f65edfcf 100644 --- a/src/libs/registry.ts +++ b/src/libs/registry.ts @@ -45,7 +45,7 @@ import type { PaginatedTxs, Tx, TxResponse } from '@/types'; import semver from 'semver' export interface Request { url: string; - adapter: (source: any) => T; + adapter: (source: any) => Promise; } export interface AbstractRegistry { @@ -154,7 +154,7 @@ export interface RequestRegistry extends AbstractRegistry { interchain_security_ccv_provider_validator_consumer_addr: Request<{consumer_address: string}> } -export function adapter(source: any): T { +export function adapter(source: any): Promise { return source; } From 9a496f89e97c4e1f912da95b1ab89a3529583b81 Mon Sep 17 00:00:00 2001 From: ivivanov Date: Tue, 30 Jan 2024 12:11:11 +0200 Subject: [PATCH 2/3] refator custom clients to use async adaptor --- src/libs/clients/evmos.ts | 6 +++--- src/libs/clients/osmosis.ts | 6 +++--- src/libs/clients/v0.46.0.ts | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/libs/clients/evmos.ts b/src/libs/clients/evmos.ts index 09229b98..9adb405d 100644 --- a/src/libs/clients/evmos.ts +++ b/src/libs/clients/evmos.ts @@ -21,11 +21,11 @@ function proposalAdapter(p: any): GovProposal { } export const requests: Partial = { - mint_inflation: { url: '/evmos/inflation/v1/inflation_rate', adapter: (data: any) => ({inflation: (Number(data.inflation_rate || 0)/ 100 ).toFixed(2)}) }, + mint_inflation: { url: '/evmos/inflation/v1/inflation_rate', adapter: async (data: any) => ({inflation: (Number(data.inflation_rate || 0)/ 100 ).toFixed(2)}) }, 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 => { + gov_proposals: { url: '/cosmos/gov/v1/proposals', adapter: async (source: any): Promise => { const proposals = source.proposals.map((p:any) => proposalAdapter(p)) return { proposals, @@ -34,7 +34,7 @@ export const requests: Partial = { }}, gov_proposals_proposal_id: { url: '/cosmos/gov/v1/proposals/{proposal_id}', - adapter: (source: any): {proposal: GovProposal} => { + adapter: async (source: any): Promise<{proposal: GovProposal}> => { return { proposal: proposalAdapter(source.proposal) } diff --git a/src/libs/clients/osmosis.ts b/src/libs/clients/osmosis.ts index aa42df0b..6dc384b3 100644 --- a/src/libs/clients/osmosis.ts +++ b/src/libs/clients/osmosis.ts @@ -26,7 +26,7 @@ function proposalAdapter(p: any): GovProposal { export const requests: Partial = { mint_inflation: { url: `https://public-osmosis-api.numia.xyz/apr?start_date=${new Date(new Date().getTime() - 186400*1000).toISOString().split('T')[0]}&end_date=${new Date().toISOString().split('T')[0]}`, - adapter: (data: any) => { + adapter: async (data: any) => { const [first] = data return {inflation: String(Number(first?.apr|| "0")/100.0)} } @@ -34,7 +34,7 @@ export const requests: Partial = { 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 => { + gov_proposals: { url: '/cosmos/gov/v1/proposals', adapter: async (source: any): Promise => { const proposals = source.proposals.map((p:any) => proposalAdapter(p)) return { proposals, @@ -43,7 +43,7 @@ export const requests: Partial = { }}, gov_proposals_proposal_id: { url: '/cosmos/gov/v1/proposals/{proposal_id}', - adapter: (source: any): {proposal: GovProposal} => { + adapter: async (source: any): Promise<{proposal: GovProposal}> => { return { proposal: proposalAdapter(source.proposal) } diff --git a/src/libs/clients/v0.46.0.ts b/src/libs/clients/v0.46.0.ts index d8a17d30..6c3d814e 100644 --- a/src/libs/clients/v0.46.0.ts +++ b/src/libs/clients/v0.46.0.ts @@ -33,7 +33,7 @@ export const requests: Partial = { 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 => { + gov_proposals: { url: '/cosmos/gov/v1/proposals', adapter: async (source: any): Promise => { const proposals = source.proposals.map((p:any) => proposalAdapter(p)) return { proposals, @@ -42,7 +42,7 @@ export const requests: Partial = { }}, gov_proposals_proposal_id: { url: '/cosmos/gov/v1/proposals/{proposal_id}', - adapter: (source: any): {proposal: GovProposal} => { + adapter: async (source: any): Promise<{proposal: GovProposal}> => { return { proposal: proposalAdapter(source.proposal) } From 4e0606b8ab01698646f3447faa2562832b69a0f4 Mon Sep 17 00:00:00 2001 From: Salman Wahib Date: Wed, 31 Jan 2024 12:03:48 +0000 Subject: [PATCH 3/3] feat: libs: add Promise.resolve in `getGovProposalTally` In the getGovProposalTally method, added `Promise.resolve` to the adapter function to ensure it returns a Promise. This resolves the TypeScript error related to the return type mismatch. Signed-off-by: Salman Wahib --- src/libs/client.ts | 8 +++++--- yarn.lock | 12 ++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/libs/client.ts b/src/libs/client.ts index 75423eb1..1238495e 100644 --- a/src/libs/client.ts +++ b/src/libs/client.ts @@ -162,13 +162,15 @@ export class CosmosRestClient extends BaseRestClient { } async getGovProposalTally(proposal_id: string) { return this.request(this.registry.gov_proposals_tally, { proposal_id }, undefined, (source: any) => { - return {tally: { + return Promise.resolve({ tally: { yes: source.tally.yes || source.tally.yes_count, abstain: source.tally.abstain || source.tally.abstain_count, no: source.tally.no || source.tally.no_count, no_with_veto: source.tally.no_with_veto || source.tally.no_with_veto_count, - }}; - }); + }, + }); + } + ); } async getGovProposalVotes(proposal_id: string, page?: PageRequest) { if(!page) page = new PageRequest() diff --git a/yarn.lock b/yarn.lock index 930383d7..9cf351cb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2184,6 +2184,11 @@ estree-walker "^2.0.2" picomatch "^2.3.1" +"@shikijs/core@1.0.0-beta.1": + version "1.0.0-beta.1" + resolved "https://registry.yarnpkg.com/@shikijs/core/-/core-1.0.0-beta.1.tgz#40d21605cfc30c9d83bdc3c03414d478930ec30c" + integrity sha512-z3gdznaRj/DJSLQdR2Gdx6AB3e5+Il/kSGdLTGHI7HnalgPL15RbGgBSdLHW8rKAz4+dezAcTdnxm8z6YTu7nA== + "@sinclair/typebox@^0.24.1": version "0.24.51" resolved "https://registry.yarnpkg.com/@sinclair/typebox/-/typebox-0.24.51.tgz#645f33fe4e02defe26f2f5c0410e1c094eac7f5f" @@ -6008,6 +6013,13 @@ shelljs@0.8.5: interpret "^1.0.0" rechoir "^0.6.2" +shiki@^1.0.0-beta.0: + version "1.0.0-beta.1" + resolved "https://registry.yarnpkg.com/shiki/-/shiki-1.0.0-beta.1.tgz#fbff19e1ba180e3093f274b00fd6578237d157bd" + integrity sha512-iq32WxjemJVlAHg5HjYoL1qCxGmvyh3Z8kr2E/gMTQdcxyXxTpMhahIC7myxFBapAk9o8QN8mxCpr4JT5rqpRQ== + dependencies: + "@shikijs/core" "1.0.0-beta.1" + side-channel@^1.0.4: version "1.0.4" resolved "https://registry.yarnpkg.com/side-channel/-/side-channel-1.0.4.tgz#efce5c8fdc104ee751b25c58d4290011fa5ea2cf"