-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial work on lm rewards display
- Loading branch information
1 parent
c28ba9c
commit 93c7ad9
Showing
9 changed files
with
230 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ build | |
dist | ||
.env | ||
.DS_Store | ||
scripts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { selectMarket, useMarketStore } from '@/stores'; | ||
import { keepPreviousData, useQuery } from '@tanstack/react-query'; | ||
import BigNumber from 'bignumber.js'; | ||
import { useBorrowRate } from './useBorrowRate'; | ||
import { useRewards } from './useRewards'; | ||
import { useSupplyRate } from './useSupplyRate'; | ||
|
||
const COEFFICIENT = BigNumber(365).times(24).times(60).times(60); | ||
|
||
export const useApr = (marketParam?: string) => { | ||
const storeMarket = useMarketStore(selectMarket); | ||
const market = marketParam ?? storeMarket; | ||
const { data: rewardsData } = useRewards(market); | ||
const { data: supplyRate } = useSupplyRate(market); | ||
const { data: borrowRate } = useBorrowRate(market); | ||
|
||
return useQuery({ | ||
queryKey: ['apr', supplyRate, borrowRate, rewardsData], | ||
queryFn: async () => { | ||
if (!supplyRate || !borrowRate || !rewardsData) { | ||
return { | ||
supplyBaseApr: BigNumber(0), | ||
borrowBaseApr: BigNumber(0), | ||
supplyRewardApr: BigNumber(0), | ||
borrowRewardApr: BigNumber(0), | ||
netSupplyApr: BigNumber(0), | ||
netBorrowApr: BigNumber(0), | ||
}; | ||
} | ||
|
||
const { borrowRewardApr, supplyRewardApr } = rewardsData; | ||
|
||
const supplyBaseApr = supplyRate | ||
.times(COEFFICIENT) | ||
.dividedBy(BigNumber(10).pow(18)); | ||
const borrowBaseApr = borrowRate | ||
.times(COEFFICIENT) | ||
.dividedBy(BigNumber(10).pow(18)); | ||
|
||
const netSupplyApr = supplyBaseApr.plus(supplyRewardApr); | ||
const netBorrowApr = borrowBaseApr.minus(borrowRewardApr); | ||
|
||
return { | ||
supplyBaseApr, | ||
borrowBaseApr, | ||
supplyRewardApr: supplyRewardApr, | ||
borrowRewardApr: borrowRewardApr, | ||
netSupplyApr, | ||
netBorrowApr, | ||
}; | ||
}, | ||
enabled: !!supplyRate && !!borrowRate && !!rewardsData, | ||
refetchOnWindowFocus: false, | ||
placeholderData: keepPreviousData, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { appConfig } from '@/configs'; | ||
import { selectMarket, useMarketStore } from '@/stores'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import BigNumber from 'bignumber.js'; | ||
import dayjs from 'dayjs'; | ||
import utc from 'dayjs/plugin/utc'; | ||
import { useMemo } from 'react'; | ||
import { useMarketBasicsWithInterest } from './useMarketBasicsWithInterest'; | ||
import { useMarketConfiguration } from './useMarketConfiguration'; | ||
import { usePrice } from './usePrice'; | ||
|
||
dayjs.extend(utc); | ||
|
||
const calculateRewardsAprForPool = ( | ||
tokenAmount: BigNumber, | ||
tokenPrice: BigNumber, | ||
durationInDays: number, | ||
currentTvl: BigNumber | ||
) => { | ||
return tokenAmount | ||
.times(tokenPrice) | ||
.times(365) | ||
.dividedBy(durationInDays) | ||
.dividedBy(currentTvl); | ||
}; | ||
|
||
export const useRewards = (marketParam?: string) => { | ||
const storeMarket = useMarketStore(selectMarket); | ||
const market = marketParam ?? storeMarket; | ||
|
||
const { data: marketBasics } = useMarketBasicsWithInterest(market); | ||
const { data: priceData } = usePrice(market); | ||
const { data: marketConfiguration } = useMarketConfiguration(market); | ||
|
||
return useQuery({ | ||
queryKey: [ | ||
'rewards', | ||
market, | ||
marketBasics, | ||
priceData?.prices, | ||
marketConfiguration, | ||
], | ||
queryFn: async () => { | ||
if (!marketBasics || !priceData || !marketConfiguration) { | ||
return { | ||
supplyRewardApr: BigNumber(0), | ||
borrowRewardApr: BigNumber(0), | ||
}; | ||
} | ||
|
||
const rewardsConfig = appConfig.rewards[market]; | ||
|
||
const today = dayjs().utc().startOf('day'); | ||
const activeRewards = rewardsConfig.filter((reward) => { | ||
return ( | ||
(today.isAfter(dayjs(reward.startDate).utc().startOf('day')) || | ||
today.isSame(dayjs(reward.startDate).utc().startOf('day'))) && | ||
today.isBefore(dayjs(reward.endDate).utc().startOf('day')) | ||
); | ||
}); | ||
|
||
const { total_borrow_base, total_supply_base } = marketBasics; | ||
|
||
const totalBorrowValue = BigNumber(total_borrow_base.toString()) | ||
.times(priceData.prices[marketConfiguration.baseToken.bits]) | ||
.dividedBy(BigNumber(10).pow(marketConfiguration.baseTokenDecimals)); | ||
|
||
const totalSupplyValue = BigNumber(total_supply_base.toString()) | ||
.times(priceData.prices[marketConfiguration.baseToken.bits]) | ||
.dividedBy(BigNumber(10).pow(marketConfiguration.baseTokenDecimals)); | ||
|
||
return activeRewards | ||
.map((reward) => { | ||
const tokenPrice = priceData.prices[reward.assetId]; | ||
const durationInDays = reward.durationInDays; | ||
const supplyRewardPool = BigNumber(reward.poolSize).times( | ||
reward.supplyRewardPercentage | ||
); | ||
const borrowRewardPool = BigNumber(reward.poolSize).times( | ||
reward.borrowRewardPercentage | ||
); | ||
|
||
const supplyRewardApr = calculateRewardsAprForPool( | ||
supplyRewardPool, | ||
tokenPrice, | ||
durationInDays, | ||
totalSupplyValue | ||
); | ||
const borrowRewardApr = calculateRewardsAprForPool( | ||
borrowRewardPool, | ||
tokenPrice, | ||
durationInDays, | ||
totalBorrowValue | ||
); | ||
|
||
return { | ||
supplyRewardApr, | ||
borrowRewardApr, | ||
}; | ||
}) | ||
.reduce( | ||
(acc, curr) => { | ||
return { | ||
supplyRewardApr: acc.supplyRewardApr.plus(curr.supplyRewardApr), | ||
borrowRewardApr: acc.borrowRewardApr.plus(curr.borrowRewardApr), | ||
}; | ||
}, | ||
{ | ||
supplyRewardApr: BigNumber(0), | ||
borrowRewardApr: BigNumber(0), | ||
} | ||
); | ||
}, | ||
enabled: !!marketBasics && !!priceData && !!marketConfiguration, | ||
refetchOnWindowFocus: false, | ||
}); | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.