-
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.
Browse files
Browse the repository at this point in the history
This reverts commit bd4e8e6. Co-authored-by: jmzwar <james@jmzwar.com>
- Loading branch information
Showing
8 changed files
with
213 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,59 @@ | ||
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'; | ||
|
||
export function useApr( | ||
poolId = '1', | ||
collateralType = '0xC74EA762CF06C9151CE074E6A569A5945B6302E7' | ||
) { | ||
export function useApr() { | ||
const { network } = useNetwork(); | ||
|
||
const { data: pnlData } = useGetPnl(); | ||
const { data: rewardsAprData } = useRewardsApr(); | ||
|
||
return useQuery({ | ||
queryKey: ['apr', network?.id], | ||
queryFn: async () => { | ||
try { | ||
const params = new URLSearchParams({ | ||
poolId, | ||
collateralType, | ||
}); | ||
|
||
const response = await fetch( | ||
`https://api.synthetix.gateway.fm/api/v1/rewards/yield?${params.toString()}` | ||
); | ||
if (!pnlData || !rewardsAprData) throw 'Missing data required for useApr'; | ||
// PNLS for the last week | ||
const { pnls } = pnlData; | ||
|
||
const data = await response.json(); | ||
const pnlsPercentWithRewards = pnls.map((pnl, i) => { | ||
const { pnlValue, collateralAmount } = pnl; | ||
// const rewards = rewardsUSDPerDay[i]; | ||
const rewardsOnDay = rewardsAprData[i]; | ||
|
||
const combinedApr = | ||
data.rollingAverages.reduce( | ||
(acc: number, currentValue: number) => acc + currentValue, | ||
0 | ||
) / data.rollingAverages.length; | ||
// 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); | ||
|
||
return { | ||
combinedApr, | ||
pnl: pnlPercent.toNumber(), | ||
rewards: rewardsPercent.toNumber(), | ||
}; | ||
} catch (error) { | ||
return; | ||
} | ||
}); | ||
|
||
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, | ||
}; | ||
}, | ||
enabled: isBaseAndromeda(network?.id, network?.preset), | ||
enabled: !!pnlData && !!rewardsAprData && isBaseAndromeda(network?.id, network?.preset), | ||
staleTime: 60000, | ||
}); | ||
} |
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 @@ | ||
export * from './useGetPnl'; |
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,16 @@ | ||
{ | ||
"name": "@snx-v3/useGetPnl", | ||
"private": true, | ||
"main": "index.ts", | ||
"version": "0.0.2", | ||
"dependencies": { | ||
"@snx-v3/isBaseAndromeda": "workspace:*", | ||
"@snx-v3/useBlockchain": "workspace:*", | ||
"@snx-v3/useCoreProxy": "workspace:*", | ||
"@snx-v3/useMulticall3": "workspace:*", | ||
"@synthetixio/wei": "^2.74.4", | ||
"@tanstack/react-query": "^5.8.3", | ||
"ethers": "^5.7.2", | ||
"react": "^18.2.0" | ||
} | ||
} |
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,101 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { useBlockNumber } from '../useBlockNumber'; | ||
import { useCoreProxy } from '@snx-v3/useCoreProxy'; | ||
import { getsUSDCAddress } from '@snx-v3/isBaseAndromeda'; | ||
import { useNetwork } from '@snx-v3/useBlockchain'; | ||
import Wei, { wei } from '@synthetixio/wei'; | ||
import { useMulticall3 } from '@snx-v3/useMulticall3'; | ||
import { providers } from 'ethers'; | ||
|
||
interface PnlData { | ||
pnlValue: Wei; | ||
collateralAmount: Wei; | ||
} | ||
|
||
export const useGetPnl = () => { | ||
const { data: block } = useBlockNumber(); | ||
|
||
const { data: CoreProxy } = useCoreProxy(); | ||
const { data: Multicall3 } = useMulticall3(); | ||
|
||
const { network } = useNetwork(); | ||
|
||
const blocks = block?.lastPeriodBlocks; | ||
|
||
return useQuery({ | ||
queryKey: ['pnl', blocks?.join(',')], | ||
queryFn: async () => { | ||
if (!CoreProxy || !Multicall3 || !blocks) throw 'Missing data required for useGetPnl'; | ||
|
||
try { | ||
const returnValues = await Promise.all( | ||
blocks.map((block: number) => { | ||
return Multicall3.connect( | ||
new providers.JsonRpcBatchProvider(network?.rpcUrl()) | ||
).callStatic.aggregate( | ||
[ | ||
{ | ||
target: CoreProxy.address, | ||
callData: CoreProxy.interface.encodeFunctionData('getVaultCollateral', [ | ||
1, | ||
getsUSDCAddress(network?.id), | ||
]), | ||
}, | ||
{ | ||
target: CoreProxy.address, | ||
callData: CoreProxy.interface.encodeFunctionData('getVaultDebt', [ | ||
1, | ||
getsUSDCAddress(network?.id), | ||
]), | ||
}, | ||
], | ||
{ blockTag: block } | ||
); | ||
}) | ||
); | ||
|
||
const decoded = returnValues.map((data) => { | ||
const [blockNumber, returnData] = data; | ||
|
||
const [debt] = CoreProxy.interface.decodeFunctionResult('getVaultDebt', returnData[1]); | ||
const [amount, value] = CoreProxy.interface.decodeFunctionResult( | ||
'getVaultCollateral', | ||
returnData[0] | ||
); | ||
|
||
return { | ||
blockNumber, | ||
amount, | ||
value, | ||
debt, | ||
}; | ||
}); | ||
|
||
const pnls: PnlData[] = []; | ||
|
||
decoded.forEach((data, i) => { | ||
if (i === 0) { | ||
return; | ||
} | ||
|
||
const previousDebt = wei(decoded[i - 1].debt, 18, true); | ||
// Take the previous collateral amount | ||
const collateralAmount = wei(decoded[i - 1].amount, 18, true); | ||
const currentDebt = wei(data.debt, 18, true); | ||
|
||
const pnlValue = previousDebt.sub(currentDebt); | ||
|
||
pnls.push({ pnlValue, collateralAmount }); | ||
}); | ||
|
||
return { | ||
pnls, | ||
}; | ||
} catch (error) { | ||
console.error('Error fetching pnl', error); | ||
return { pnls: [] }; | ||
} | ||
}, | ||
enabled: Boolean(block && CoreProxy && Multicall3 && network), | ||
}); | ||
}; |
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 @@ | ||
export * from './useRewardsApr'; |
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,16 @@ | ||
{ | ||
"name": "@snx-v3/useRewardsApr", | ||
"private": true, | ||
"main": "index.ts", | ||
"version": "0.0.2", | ||
"dependencies": { | ||
"@snx-v3/constants": "workspace:*", | ||
"@snx-v3/useBlockNumber": "workspace:*", | ||
"@snx-v3/useBlockchain": "workspace:*", | ||
"@synthetixio/wei": "^2.74.4", | ||
"@tanstack/react-query": "^5.8.3", | ||
"date-fns": "^2.30.0", | ||
"ethers": "^5.7.2", | ||
"react": "^18.2.0" | ||
} | ||
} |
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