diff --git a/liquidity/components/Pools/VaultRow.tsx b/liquidity/components/Pools/VaultRow.tsx index e5577f403..05015aacd 100644 --- a/liquidity/components/Pools/VaultRow.tsx +++ b/liquidity/components/Pools/VaultRow.tsx @@ -86,7 +86,7 @@ function VaultRowUi({ ) : ( - {apr?.toFixed(2) || '-'}% + {apr ? apr.toFixed(2) : '-'}% )} diff --git a/liquidity/lib/useApr/useApr.ts b/liquidity/lib/useApr/useApr.ts index 44c2fc2ae..79234095e 100644 --- a/liquidity/lib/useApr/useApr.ts +++ b/liquidity/lib/useApr/useApr.ts @@ -1,5 +1,5 @@ import { isBaseAndromeda } from '@snx-v3/isBaseAndromeda'; -import { useNetwork, useProvider } from '@snx-v3/useBlockchain'; +import { useNetwork } from '@snx-v3/useBlockchain'; import { useGetPnl } from '@snx-v3/useGetPnl'; import { useRewardsApr } from '@snx-v3/useRewardsApr'; import { wei } from '@synthetixio/wei'; @@ -7,14 +7,14 @@ import { useQuery } from '@tanstack/react-query'; export function useApr() { const { network } = useNetwork(); - const provider = useProvider(); + const { data: pnlData } = useGetPnl(); const { data: rewardsAprData } = useRewardsApr(); return useQuery({ queryKey: ['apr', network?.id], queryFn: async () => { - if (!provider || !pnlData || !rewardsAprData) throw 'Missing data required for useApr'; + if (!pnlData || !rewardsAprData) throw 'Missing data required for useApr'; // PNLS for the last week const { pnls } = pnlData; diff --git a/liquidity/lib/useGetPnl/useGetPnl.ts b/liquidity/lib/useGetPnl/useGetPnl.ts index d88ad157a..a38f3fa62 100644 --- a/liquidity/lib/useGetPnl/useGetPnl.ts +++ b/liquidity/lib/useGetPnl/useGetPnl.ts @@ -27,69 +27,75 @@ export const useGetPnl = () => { queryFn: async () => { if (!CoreProxy || !Multicall3 || !blocks) throw 'Missing data required for useGetPnl'; - 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 } - ); - }) - ); + 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 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] - ); + const [debt] = CoreProxy.interface.decodeFunctionResult('getVaultDebt', returnData[1]); + const [amount, value] = CoreProxy.interface.decodeFunctionResult( + 'getVaultCollateral', + returnData[0] + ); - return { - blockNumber, - amount, - value, - debt, - }; - }); + return { + blockNumber, + amount, + value, + debt, + }; + }); - const pnls: PnlData[] = []; + const pnls: PnlData[] = []; - decoded.forEach((data, i) => { - if (i === 0) { - return; - } + 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 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); + const pnlValue = previousDebt.sub(currentDebt); - pnls.push({ pnlValue, collateralAmount }); - }); + pnls.push({ pnlValue, collateralAmount }); + }); - return { - pnls, - }; + return { + pnls, + }; + } catch (error) { + console.error('Error fetching pnl', error); + return { pnls: [] }; + } }, + enabled: Boolean(block && CoreProxy && Multicall3 && network), }); }; diff --git a/liquidity/ui/src/pages/Home/Home.tsx b/liquidity/ui/src/pages/Home/Home.tsx index 3c6d80ce3..e6ed5fa71 100644 --- a/liquidity/ui/src/pages/Home/Home.tsx +++ b/liquidity/ui/src/pages/Home/Home.tsx @@ -46,7 +46,7 @@ export function Home() { isLoading={isLoading} collateralTypes={collateralTypes} liquidityPositionsById={liquidityPositionsById} - apr={aprData?.combinedApr || 0} + apr={aprData?.combinedApr} />