Mp 3237 handle multiple active incentives (#372)
This commit is contained in:
parent
8df8aa6a12
commit
de185bf823
@ -1,5 +1,5 @@
|
|||||||
import getMarket from 'api/markets/getMarket'
|
import getMarket from 'api/markets/getMarket'
|
||||||
import getAssetIncentive from 'api/incentives/getAssetIncentive'
|
import getTotalActiveEmissionValue from 'api/incentives/getTotalActiveEmissionValue'
|
||||||
import getUnderlyingLiquidityAmount from 'api/markets/getMarketUnderlyingLiquidityAmount'
|
import getUnderlyingLiquidityAmount from 'api/markets/getMarketUnderlyingLiquidityAmount'
|
||||||
import { BN } from 'utils/helpers'
|
import { BN } from 'utils/helpers'
|
||||||
import { SECONDS_IN_A_YEAR } from 'utils/constants'
|
import { SECONDS_IN_A_YEAR } from 'utils/constants'
|
||||||
@ -11,28 +11,26 @@ export default async function calculateAssetIncentivesApy(
|
|||||||
denom: string,
|
denom: string,
|
||||||
): Promise<BigNumber | null> {
|
): Promise<BigNumber | null> {
|
||||||
try {
|
try {
|
||||||
const [assetIncentive, market] = await Promise.all([getAssetIncentive(denom), getMarket(denom)])
|
const [totalActiveEmissionValue, market] = await Promise.all([
|
||||||
|
getTotalActiveEmissionValue(denom),
|
||||||
|
getMarket(denom),
|
||||||
|
])
|
||||||
|
|
||||||
if (!assetIncentive) return null
|
if (!totalActiveEmissionValue) return null
|
||||||
|
|
||||||
const [marketLiquidityAmount, assetPrice, baseAssetPrice] = await Promise.all([
|
const [marketLiquidityAmount, assetPrice] = await Promise.all([
|
||||||
getUnderlyingLiquidityAmount(market),
|
getUnderlyingLiquidityAmount(market),
|
||||||
getPrice(denom),
|
getPrice(denom),
|
||||||
getPrice(assetIncentive.denom),
|
|
||||||
])
|
])
|
||||||
|
|
||||||
const assetDecimals = (ASSETS.find(byDenom(denom)) as Asset).decimals
|
const assetDecimals = (ASSETS.find(byDenom(denom)) as Asset).decimals
|
||||||
const baseDecimals = (ASSETS.find(byDenom(assetIncentive.denom)) as Asset).decimals
|
|
||||||
|
|
||||||
const marketLiquidityValue = BN(marketLiquidityAmount)
|
const marketLiquidityValue = BN(marketLiquidityAmount)
|
||||||
.shiftedBy(-assetDecimals)
|
.shiftedBy(-assetDecimals)
|
||||||
.multipliedBy(assetPrice)
|
.multipliedBy(assetPrice)
|
||||||
|
|
||||||
const marketReturns = BN(market.liquidityRate).multipliedBy(marketLiquidityValue)
|
const marketReturns = BN(market.liquidityRate).multipliedBy(marketLiquidityValue)
|
||||||
const annualEmission = BN(assetIncentive.emission_rate)
|
const annualEmission = totalActiveEmissionValue.multipliedBy(SECONDS_IN_A_YEAR)
|
||||||
.multipliedBy(SECONDS_IN_A_YEAR)
|
|
||||||
.shiftedBy(-baseDecimals)
|
|
||||||
.multipliedBy(baseAssetPrice)
|
|
||||||
|
|
||||||
const totalAnnualReturnsValue = annualEmission.plus(marketReturns)
|
const totalAnnualReturnsValue = annualEmission.plus(marketReturns)
|
||||||
const incentivesApy = totalAnnualReturnsValue.dividedBy(marketLiquidityValue).multipliedBy(100)
|
const incentivesApy = totalAnnualReturnsValue.dividedBy(marketLiquidityValue).multipliedBy(100)
|
||||||
|
@ -1,20 +0,0 @@
|
|||||||
import { getIncentivesQueryClient } from 'api/cosmwasm-client'
|
|
||||||
import { ActiveEmission } from 'types/generated/mars-incentives/MarsIncentives.types'
|
|
||||||
|
|
||||||
export default async function getAssetIncentive(denom: string): Promise<ActiveEmission | null> {
|
|
||||||
try {
|
|
||||||
const client = await getIncentivesQueryClient()
|
|
||||||
const activeEmissions = await client.activeEmissions({
|
|
||||||
collateralDenom: denom,
|
|
||||||
})
|
|
||||||
|
|
||||||
if (activeEmissions.length === 0) {
|
|
||||||
throw 'Asset has no active incentive emission.'
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: handle multiple emissions https://delphilabs.atlassian.net/browse/MP-3237
|
|
||||||
return activeEmissions[0]
|
|
||||||
} catch (ex) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
}
|
|
35
src/api/incentives/getTotalActiveEmissionValue.ts
Normal file
35
src/api/incentives/getTotalActiveEmissionValue.ts
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import { getIncentivesQueryClient } from 'api/cosmwasm-client'
|
||||||
|
import getPrice from 'api/prices/getPrice'
|
||||||
|
import { ASSETS } from 'constants/assets'
|
||||||
|
import { BN_ZERO } from 'constants/math'
|
||||||
|
import { byDenom } from 'utils/array'
|
||||||
|
import { BN } from 'utils/helpers'
|
||||||
|
|
||||||
|
export default async function getTotalActiveEmissionValue(
|
||||||
|
denom: string,
|
||||||
|
): Promise<BigNumber | null> {
|
||||||
|
try {
|
||||||
|
const client = await getIncentivesQueryClient()
|
||||||
|
const activeEmissions = await client.activeEmissions({
|
||||||
|
collateralDenom: denom,
|
||||||
|
})
|
||||||
|
|
||||||
|
if (activeEmissions.length === 0) {
|
||||||
|
throw 'Asset has no active incentive emission.'
|
||||||
|
}
|
||||||
|
|
||||||
|
const prices = await Promise.all(
|
||||||
|
activeEmissions.map((activeEmission) => getPrice(activeEmission.denom)),
|
||||||
|
)
|
||||||
|
|
||||||
|
return activeEmissions.reduce((accumulation, current, index) => {
|
||||||
|
const price = prices[index]
|
||||||
|
const decimals = 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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user