Skip to content

Commit

Permalink
chore: add param store
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasRalee committed Aug 27, 2024
1 parent 78d1021 commit 6866472
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 0 deletions.
14 changes: 14 additions & 0 deletions layer/icons/search-dense.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<template>
<svg
width="12"
height="12"
viewBox="0 0 12 12"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M8.80662 7.75792C9.39969 6.95048 9.75 5.95365 9.75 4.875C9.75 2.18261 7.56739 0 4.875 0C2.18261 0 0 2.18261 0 4.875C0 7.56739 2.18261 9.75 4.875 9.75C5.95394 9.75 6.95101 9.3995 7.75856 8.80614L7.75792 8.80662C7.78004 8.83661 7.80467 8.86533 7.83182 8.89248L10.7197 11.7803C11.0126 12.0732 11.4874 12.0732 11.7803 11.7803C12.0732 11.4874 12.0732 11.0126 11.7803 10.7197L8.89248 7.83182C8.86533 7.80467 8.83661 7.78004 8.80662 7.75792ZM9 4.875C9 7.15317 7.15317 9 4.875 9C2.59683 9 0.75 7.15317 0.75 4.875C0.75 2.59683 2.59683 0.75 4.875 0.75C7.15317 0.75 9 2.59683 9 4.875Z"
fill="currentColor"
/>
</svg>
</template>
130 changes: 130 additions & 0 deletions layer/store/param.ts
Original file line number Diff line number Diff line change
@@ -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
})
}
}
}
})

0 comments on commit 6866472

Please sign in to comment.