-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* gateway * search params * add frame * remove slice * rolling 7 day average * replace with date fns * deps fix --------- Co-authored-by: jmzwar <[email protected]>
- Loading branch information
Showing
10 changed files
with
46 additions
and
221 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
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 |
---|---|---|
@@ -1,59 +1,49 @@ | ||
import { isBaseAndromeda } from '@snx-v3/isBaseAndromeda'; | ||
import { useNetwork } from '@snx-v3/useBlockchain'; | ||
import { useGetPnl } from '@snx-v3/useGetPnl'; | ||
import { useRewardsApr } from '@snx-v3/useRewardsApr'; | ||
import { wei } from '@synthetixio/wei'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { subDays, getUnixTime } from 'date-fns'; | ||
|
||
export function useApr() { | ||
export function useApr( | ||
poolId = '1', | ||
collateralType = '0xC74EA762CF06C9151CE074E6A569A5945B6302E7' | ||
) { | ||
const { network } = useNetwork(); | ||
|
||
const { data: pnlData } = useGetPnl(); | ||
const { data: rewardsAprData } = useRewardsApr(); | ||
|
||
return useQuery({ | ||
queryKey: ['apr', network?.id], | ||
queryFn: async () => { | ||
if (!pnlData || !rewardsAprData) throw 'Missing data required for useApr'; | ||
// PNLS for the last week | ||
const { pnls } = pnlData; | ||
|
||
const pnlsPercentWithRewards = pnls.map((pnl, i) => { | ||
const { pnlValue, collateralAmount } = pnl; | ||
// const rewards = rewardsUSDPerDay[i]; | ||
const rewardsOnDay = rewardsAprData[i]; | ||
|
||
// Add the amounts from rewards to the pnls from the vault | ||
// Divide by collateral amount to get the percentage | ||
const pnlPercent = pnlValue.div(collateralAmount).mul(100); | ||
const rewardsPercent = wei(rewardsOnDay).div(collateralAmount).mul(100); | ||
try { | ||
const now = getUnixTime(new Date()).toString(); | ||
const weekAgo = getUnixTime(subDays(new Date(), 7)).toString(); | ||
|
||
const params = new URLSearchParams({ | ||
poolId, | ||
collateralType, | ||
frame: 'day', | ||
fromTimestamp: weekAgo, | ||
toTimestamp: now, | ||
}); | ||
|
||
const response = await fetch( | ||
`https://api.synthetix.gateway.fm/api/v1/rewards/yield?${params.toString()}` | ||
); | ||
|
||
const data = await response.json(); | ||
|
||
const combinedApr = | ||
data.rollingAverages.reduce( | ||
(acc: number, currentValue: number) => acc + currentValue, | ||
0 | ||
) / data.rollingAverages.length; | ||
|
||
return { | ||
pnl: pnlPercent.toNumber(), | ||
rewards: rewardsPercent.toNumber(), | ||
combinedApr: combinedApr * 365, | ||
}; | ||
}); | ||
|
||
const weeklyAverageAprLP = pnlsPercentWithRewards.reduce((acc, { pnl }) => acc + pnl, 0); | ||
const weeklyAverageAprRewards = pnlsPercentWithRewards.reduce( | ||
(acc, { rewards }) => acc + rewards, | ||
0 | ||
); | ||
|
||
const dailyAverageAprLp = weeklyAverageAprLP / pnlsPercentWithRewards.length; | ||
const dailyAverageAprRewards = weeklyAverageAprRewards / pnlsPercentWithRewards.length; | ||
|
||
const aprPnl = dailyAverageAprLp * 365; | ||
const aprRewards = dailyAverageAprRewards * 365; | ||
const combinedApr = (dailyAverageAprLp + dailyAverageAprRewards) * 365; | ||
|
||
return { | ||
aprPnl, | ||
aprRewards, | ||
combinedApr, | ||
}; | ||
} catch (error) { | ||
return; | ||
} | ||
}, | ||
enabled: !!pnlData && !!rewardsAprData && isBaseAndromeda(network?.id, network?.preset), | ||
enabled: isBaseAndromeda(network?.id, network?.preset), | ||
staleTime: 60000, | ||
}); | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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