Skip to content

Commit

Permalink
Revert "gateway api for apr (#228)" (#230)
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
jmzwar and jmzwar authored Apr 10, 2024
1 parent bd4e8e6 commit 029d15c
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 25 deletions.
3 changes: 3 additions & 0 deletions liquidity/lib/useApr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"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: 41 additions & 24 deletions liquidity/lib/useApr/useApr.ts
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,
});
}
1 change: 1 addition & 0 deletions liquidity/lib/useGetPnl/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useGetPnl';
16 changes: 16 additions & 0 deletions liquidity/lib/useGetPnl/package.json
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"
}
}
101 changes: 101 additions & 0 deletions liquidity/lib/useGetPnl/useGetPnl.ts
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),
});
};
1 change: 1 addition & 0 deletions liquidity/lib/useRewardsApr/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useRewardsApr';
16 changes: 16 additions & 0 deletions liquidity/lib/useRewardsApr/package.json
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"
}
}
35 changes: 34 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6708,12 +6708,15 @@ __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:liquidity/lib/useBlockNumber":
"@snx-v3/useBlockNumber@workspace:*, @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 @@ -6914,6 +6917,21 @@ __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 @@ -7105,6 +7123,21 @@ __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 029d15c

Please sign in to comment.