Skip to content

Commit

Permalink
Gateway/apr (#235)
Browse files Browse the repository at this point in the history
* 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
jmzwar and jmzwar authored Apr 12, 2024
1 parent 81d3783 commit acb98cb
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 221 deletions.
6 changes: 4 additions & 2 deletions liquidity/components/Pools/VaultRow.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FC } from 'react';
import { Amount } from '@snx-v3/Amount';
import { Button, Flex, Td, Text, Tr } from '@chakra-ui/react';
import { Button, Flex, Td, Text, Tooltip, Tr } from '@chakra-ui/react';
import { generatePath, Link, useLocation } from 'react-router-dom';
import { useConnectWallet } from '@web3-onboard/react';
import { CollateralType } from '@snx-v3/useCollateralTypes';
Expand Down Expand Up @@ -86,7 +86,9 @@ function VaultRowUi({
</>
) : (
<Td>
<Text>{apr ? apr.toFixed(2) : '-'}%</Text>
<Tooltip label="APR is a combination of past week pool performance and rewards.">
<Text>{apr ? apr.toFixed(2) : '-'}%</Text>
</Tooltip>
</Td>
)}
<Td textAlign="end">
Expand Down
4 changes: 1 addition & 3 deletions liquidity/lib/useApr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
"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",
"date-fns": "^2.30.0",
"react": "^18.2.0"
}
}
76 changes: 33 additions & 43 deletions liquidity/lib/useApr/useApr.ts
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,
});
}
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.

10 changes: 6 additions & 4 deletions liquidity/ui/src/pages/Pool/CollateralSection.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Box, Divider, Flex, Skeleton, Text } from '@chakra-ui/react';
import { Box, Divider, Flex, Skeleton, Text, Tooltip } from '@chakra-ui/react';
import { useVaultsData, VaultsDataType } from '@snx-v3/useVaultsData';
import React, { FC } from 'react';
import Wei, { wei } from '@synthetixio/wei';
Expand Down Expand Up @@ -98,9 +98,11 @@ export const CollateralSectionUi: FC<{
{isAprLoading ? (
<Skeleton mt={1} w={16} h={6} />
) : (
<Text fontWeight={700} fontSize="xl" color="white">
{`${apr ? apr?.toFixed(2) : '-'}%`}
</Text>
<Tooltip label="APR is a combination of past week pool performance and rewards.">
<Text fontWeight={700} fontSize="xl" color="white">
{`${apr ? apr?.toFixed(2) : '-'}%`}
</Text>
</Tooltip>
)}
</Flex>
</BorderBox>
Expand Down
36 changes: 2 additions & 34 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6708,15 +6708,13 @@ __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"
date-fns: "npm:^2.30.0"
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 +6915,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 +7106,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 acb98cb

Please sign in to comment.