Skip to content

Commit

Permalink
gateway api for apr (#228)
Browse files Browse the repository at this point in the history
* gateway

* search params

---------

Co-authored-by: jmzwar <[email protected]>
  • Loading branch information
jmzwar and jmzwar authored Apr 10, 2024
1 parent 1e196ee commit bd4e8e6
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 213 deletions.
3 changes: 0 additions & 3 deletions liquidity/lib/useApr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
"dependencies": {
"@snx-v3/isBaseAndromeda": "workspace:*",
"@snx-v3/useBlockchain": "workspace:*",
"@snx-v3/useGetPnl": "workspace:*",
"@snx-v3/useRewardsApr": "workspace:*",
"@synthetixio/wei": "^2.74.4",
"@tanstack/react-query": "^5.8.3",
"react": "^18.2.0"
}
Expand Down
65 changes: 24 additions & 41 deletions liquidity/lib/useApr/useApr.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,42 @@
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() {
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;
try {
const params = new URLSearchParams({
poolId,
collateralType,
});

const response = await fetch(
`https://api.synthetix.gateway.fm/api/v1/rewards/yield?${params.toString()}`
);

const pnlsPercentWithRewards = pnls.map((pnl, i) => {
const { pnlValue, collateralAmount } = pnl;
// const rewards = rewardsUSDPerDay[i];
const rewardsOnDay = rewardsAprData[i];
const data = await response.json();

// 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);
const combinedApr =
data.rollingAverages.reduce(
(acc: number, currentValue: number) => acc + currentValue,
0
) / data.rollingAverages.length;

return {
pnl: pnlPercent.toNumber(),
rewards: rewardsPercent.toNumber(),
combinedApr,
};
});

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,
});
}
1 change: 0 additions & 1 deletion liquidity/lib/useGetPnl/index.ts

This file was deleted.

16 changes: 0 additions & 16 deletions liquidity/lib/useGetPnl/package.json

This file was deleted.

101 changes: 0 additions & 101 deletions liquidity/lib/useGetPnl/useGetPnl.ts

This file was deleted.

1 change: 0 additions & 1 deletion liquidity/lib/useRewardsApr/index.ts

This file was deleted.

16 changes: 0 additions & 16 deletions liquidity/lib/useRewardsApr/package.json

This file was deleted.

35 changes: 1 addition & 34 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6708,15 +6708,12 @@ __metadata:
dependencies:
"@snx-v3/isBaseAndromeda": "workspace:*"
"@snx-v3/useBlockchain": "workspace:*"
"@snx-v3/useGetPnl": "workspace:*"
"@snx-v3/useRewardsApr": "workspace:*"
"@synthetixio/wei": "npm:^2.74.4"
"@tanstack/react-query": "npm:^5.8.3"
react: "npm:^18.2.0"
languageName: unknown
linkType: soft

"@snx-v3/useBlockNumber@workspace:*, @snx-v3/useBlockNumber@workspace:liquidity/lib/useBlockNumber":
"@snx-v3/useBlockNumber@workspace:liquidity/lib/useBlockNumber":
version: 0.0.0-use.local
resolution: "@snx-v3/useBlockNumber@workspace:liquidity/lib/useBlockNumber"
dependencies:
Expand Down Expand Up @@ -6917,21 +6914,6 @@ __metadata:
languageName: unknown
linkType: soft

"@snx-v3/useGetPnl@workspace:*, @snx-v3/useGetPnl@workspace:liquidity/lib/useGetPnl":
version: 0.0.0-use.local
resolution: "@snx-v3/useGetPnl@workspace:liquidity/lib/useGetPnl"
dependencies:
"@snx-v3/isBaseAndromeda": "workspace:*"
"@snx-v3/useBlockchain": "workspace:*"
"@snx-v3/useCoreProxy": "workspace:*"
"@snx-v3/useMulticall3": "workspace:*"
"@synthetixio/wei": "npm:^2.74.4"
"@tanstack/react-query": "npm:^5.8.3"
ethers: "npm:^5.7.2"
react: "npm:^18.2.0"
languageName: unknown
linkType: soft

"@snx-v3/useLiquidityPosition@workspace:*, @snx-v3/useLiquidityPosition@workspace:liquidity/lib/useLiquidityPosition":
version: 0.0.0-use.local
resolution: "@snx-v3/useLiquidityPosition@workspace:liquidity/lib/useLiquidityPosition"
Expand Down Expand Up @@ -7123,21 +7105,6 @@ __metadata:
languageName: unknown
linkType: soft

"@snx-v3/useRewardsApr@workspace:*, @snx-v3/useRewardsApr@workspace:liquidity/lib/useRewardsApr":
version: 0.0.0-use.local
resolution: "@snx-v3/useRewardsApr@workspace:liquidity/lib/useRewardsApr"
dependencies:
"@snx-v3/constants": "workspace:*"
"@snx-v3/useBlockNumber": "workspace:*"
"@snx-v3/useBlockchain": "workspace:*"
"@synthetixio/wei": "npm:^2.74.4"
"@tanstack/react-query": "npm:^5.8.3"
date-fns: "npm:^2.30.0"
ethers: "npm:^5.7.2"
react: "npm:^18.2.0"
languageName: unknown
linkType: soft

"@snx-v3/useSpotMarketProxy@workspace:liquidity/lib/useSpotMarketProxy":
version: 0.0.0-use.local
resolution: "@snx-v3/useSpotMarketProxy@workspace:liquidity/lib/useSpotMarketProxy"
Expand Down

0 comments on commit bd4e8e6

Please sign in to comment.