From 68664720c0dddfc930630a02087a316506c06a85 Mon Sep 17 00:00:00 2001 From: thomasRalee Date: Wed, 28 Aug 2024 04:05:08 +0800 Subject: [PATCH] chore: add param store --- layer/icons/search-dense.vue | 14 ++++ layer/store/param.ts | 130 +++++++++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 layer/icons/search-dense.vue create mode 100644 layer/store/param.ts diff --git a/layer/icons/search-dense.vue b/layer/icons/search-dense.vue new file mode 100644 index 0000000..c2a71fd --- /dev/null +++ b/layer/icons/search-dense.vue @@ -0,0 +1,14 @@ + diff --git a/layer/store/param.ts b/layer/store/param.ts new file mode 100644 index 0000000..6f8c7e2 --- /dev/null +++ b/layer/store/param.ts @@ -0,0 +1,130 @@ +import { defineStore } from 'pinia' +import { + Pool, + MinModuleParams as MintModuleParams +} from '@injectivelabs/sdk-ts' +import { HttpClient, BigNumberInBase } from '@injectivelabs/utils' +import { injToken } from './../data/token' +import { mintApi, bankApi, stakingApi } from './../Service' +import { sharedToBalanceInToken } from './../utils/formatter' + +const ACTUAL_BLCOk_TIME = 0.625194000005722 +const ACTUAL_BLOCKS_PER_YEAR = 50441942.82048672 + +type ParamStoreState = { + injSupply: number + baseInflation: number + bondedTokens: number + blocksPerYear: number + actualBlockTime: number + actualBlocksPerYear: number + pool: Pool + mintParams: MintModuleParams +} + +const initialStateFactory = (): ParamStoreState => ({ + injSupply: 0, + baseInflation: 0, + bondedTokens: 0, + blocksPerYear: 0, + actualBlockTime: 0, + actualBlocksPerYear: 0, + pool: {} as Pool, + mintParams: {} as MintModuleParams +}) + +export const useSharedParamStore = defineStore('sharedParam', { + state: (): ParamStoreState => initialStateFactory(), + getters: { + apr: (state) => { + return new BigNumberInBase(state.actualBlocksPerYear) + .dividedBy(state.blocksPerYear) + .times(state.baseInflation) + .times(state.injSupply) + .div(state.bondedTokens) + } + }, + actions: { + async init() { + await Promise.all([ + this.fetchSupply(), + this.fetchInflation(), + this.fetchPool(), + this.fetchMintParams(), + this.fetchChainParams() + ]) + }, + + async fetchSupply() { + const paramsStore = useSharedParamStore() + + const injSupply = await bankApi.fetchSupplyOf(injToken.denom) + + paramsStore.$patch({ + injSupply: sharedToBalanceInToken({ value: injSupply.amount }) + }) + }, + + async fetchInflation() { + const paramsStore = useSharedParamStore() + + const { inflation } = await mintApi.fetchInflation() + + paramsStore.$patch({ + baseInflation: new BigNumberInBase(inflation).toFixed() + }) + }, + + async fetchPool() { + const paramsStore = useSharedParamStore() + + const pool = await stakingApi.fetchPool() + + paramsStore.$patch({ + pool, + bondedTokens: new BigNumberInBase(pool.bondedTokens).toFixed() + }) + }, + + async fetchMintParams() { + const paramsStore = useSharedParamStore() + + const mintParams = await mintApi.fetchModuleParams() + + paramsStore.$patch({ + mintParams, + blocksPerYear: new BigNumberInBase(mintParams.blocksPerYear).toFixed() + }) + }, + + async fetchChainParams() { + const paramStore = useSharedParamStore() + + const httpClient = new HttpClient('https://chains.cosmos.directory/') + + try { + const { data } = (await httpClient.get('injective')) as { + data: { + chain: { + params: { + actual_block_time: number + actual_blocks_per_year: number + } + } + } + } + + paramStore.$patch({ + actualBlockTime: data.chain.params.actual_block_time, + actualBlocksPerYear: data.chain.params.actual_blocks_per_year + }) + } catch (error: any) { + // silently throw + paramStore.$patch({ + actualBlockTime: ACTUAL_BLCOk_TIME, + actualBlocksPerYear: ACTUAL_BLOCKS_PER_YEAR + }) + } + } + } +})