mars-v2-frontend/src/api/incentives/getTotalActiveEmissionValue.ts
Linkie Link fb830c08cc
Added chain agnostic v2 (#710)
* update assets config and chains

* make clients dynamic

* feat: formatted ChainSelect

* fix infinite rerender on trade page

* feat: added NTRN icon

* fix: fixed ChainInfoID

* fix: fixed autoLendEnabled for NTRN

* fix: fixed the navigation and dependencies

* fix: fixed the pricefeed id

* fix: fixed the header menu

* fix: fixed the trading charts

* fix: fixed the healthbars

* fix: fixed naming of pion-1

* feast: updated xdefi image

* env: updated contracts

* make localStorage chain agnostic

* fix: made the selected chain persistant

* fix: fixed the wallet providers

* fix: updated auto connect

* fix: fixed auto connecting

* fix: added ChainSelect to focusMode

* store raw strings in localstorage

* 🔥 remnove tests

* update caching keys + disconnect wallet on change chain

* fix: fixed the chain select

* env: bumped version

---------

Co-authored-by: Bob van der Helm <34470358+bobthebuidlr@users.noreply.github.com>
2024-01-03 15:50:38 +01:00

43 lines
1.3 KiB
TypeScript

import { cacheFn, emissionsCache } from 'api/cache'
import { getIncentivesQueryClient } from 'api/cosmwasm-client'
import getPrice from 'api/prices/getPrice'
import { BN_ZERO } from 'constants/math'
import { byDenom } from 'utils/array'
import { BN } from 'utils/helpers'
export default async function getTotalActiveEmissionValue(
chainConfig: ChainConfig,
denom: string,
): Promise<BigNumber | null> {
try {
const client = await getIncentivesQueryClient(chainConfig)
const activeEmissions = await cacheFn(
() =>
client.activeEmissions({
collateralDenom: denom,
}),
emissionsCache,
`emission/${denom}`,
60,
)
if (activeEmissions.length === 0) {
throw 'Asset has no active incentive emission.'
}
const prices = await Promise.all(
activeEmissions.map((activeEmission) => getPrice(chainConfig, activeEmission.denom)),
)
return activeEmissions.reduce((accumulation, current, index) => {
const price = prices[index]
const decimals = chainConfig.assets.find(byDenom(current.denom))?.decimals as number
const emissionValue = BN(current.emission_rate).shiftedBy(-decimals).multipliedBy(price)
return accumulation.plus(emissionValue)
}, BN_ZERO)
} catch (ex) {
return null
}
}