Skip to content

Commit 2511196

Browse files
jmzwarjmzwar
and
jmzwar
authored
Chore/rpc issue (#219)
* update onboard * block number shennanigans * apy to apr * switch network order * dont show zero * testing * update useApr * add logs * enable logging * more logs * enabled for useGetPnl * remove logs * add netowkr --------- Co-authored-by: jmzwar <[email protected]>
1 parent 8b04f85 commit 2511196

File tree

4 files changed

+65
-59
lines changed

4 files changed

+65
-59
lines changed

liquidity/components/Pools/VaultRow.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function VaultRowUi({
8686
</>
8787
) : (
8888
<Td>
89-
<Text>{apr?.toFixed(2) || '-'}%</Text>
89+
<Text>{apr ? apr.toFixed(2) : '-'}%</Text>
9090
</Td>
9191
)}
9292
<Td textAlign="end">

liquidity/lib/useApr/useApr.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import { isBaseAndromeda } from '@snx-v3/isBaseAndromeda';
2-
import { useNetwork, useProvider } from '@snx-v3/useBlockchain';
2+
import { useNetwork } from '@snx-v3/useBlockchain';
33
import { useGetPnl } from '@snx-v3/useGetPnl';
44
import { useRewardsApr } from '@snx-v3/useRewardsApr';
55
import { wei } from '@synthetixio/wei';
66
import { useQuery } from '@tanstack/react-query';
77

88
export function useApr() {
99
const { network } = useNetwork();
10-
const provider = useProvider();
10+
1111
const { data: pnlData } = useGetPnl();
1212
const { data: rewardsAprData } = useRewardsApr();
1313

1414
return useQuery({
1515
queryKey: ['apr', network?.id],
1616
queryFn: async () => {
17-
if (!provider || !pnlData || !rewardsAprData) throw 'Missing data required for useApr';
17+
if (!pnlData || !rewardsAprData) throw 'Missing data required for useApr';
1818
// PNLS for the last week
1919
const { pnls } = pnlData;
2020

liquidity/lib/useGetPnl/useGetPnl.ts

+60-54
Original file line numberDiff line numberDiff line change
@@ -27,69 +27,75 @@ export const useGetPnl = () => {
2727
queryFn: async () => {
2828
if (!CoreProxy || !Multicall3 || !blocks) throw 'Missing data required for useGetPnl';
2929

30-
const returnValues = await Promise.all(
31-
blocks.map((block: number) => {
32-
return Multicall3.connect(
33-
new providers.JsonRpcBatchProvider(network?.rpcUrl())
34-
).callStatic.aggregate(
35-
[
36-
{
37-
target: CoreProxy.address,
38-
callData: CoreProxy.interface.encodeFunctionData('getVaultCollateral', [
39-
1,
40-
getsUSDCAddress(network?.id),
41-
]),
42-
},
43-
{
44-
target: CoreProxy.address,
45-
callData: CoreProxy.interface.encodeFunctionData('getVaultDebt', [
46-
1,
47-
getsUSDCAddress(network?.id),
48-
]),
49-
},
50-
],
51-
{ blockTag: block }
52-
);
53-
})
54-
);
30+
try {
31+
const returnValues = await Promise.all(
32+
blocks.map((block: number) => {
33+
return Multicall3.connect(
34+
new providers.JsonRpcBatchProvider(network?.rpcUrl())
35+
).callStatic.aggregate(
36+
[
37+
{
38+
target: CoreProxy.address,
39+
callData: CoreProxy.interface.encodeFunctionData('getVaultCollateral', [
40+
1,
41+
getsUSDCAddress(network?.id),
42+
]),
43+
},
44+
{
45+
target: CoreProxy.address,
46+
callData: CoreProxy.interface.encodeFunctionData('getVaultDebt', [
47+
1,
48+
getsUSDCAddress(network?.id),
49+
]),
50+
},
51+
],
52+
{ blockTag: block }
53+
);
54+
})
55+
);
5556

56-
const decoded = returnValues.map((data) => {
57-
const [blockNumber, returnData] = data;
57+
const decoded = returnValues.map((data) => {
58+
const [blockNumber, returnData] = data;
5859

59-
const [debt] = CoreProxy.interface.decodeFunctionResult('getVaultDebt', returnData[1]);
60-
const [amount, value] = CoreProxy.interface.decodeFunctionResult(
61-
'getVaultCollateral',
62-
returnData[0]
63-
);
60+
const [debt] = CoreProxy.interface.decodeFunctionResult('getVaultDebt', returnData[1]);
61+
const [amount, value] = CoreProxy.interface.decodeFunctionResult(
62+
'getVaultCollateral',
63+
returnData[0]
64+
);
6465

65-
return {
66-
blockNumber,
67-
amount,
68-
value,
69-
debt,
70-
};
71-
});
66+
return {
67+
blockNumber,
68+
amount,
69+
value,
70+
debt,
71+
};
72+
});
7273

73-
const pnls: PnlData[] = [];
74+
const pnls: PnlData[] = [];
7475

75-
decoded.forEach((data, i) => {
76-
if (i === 0) {
77-
return;
78-
}
76+
decoded.forEach((data, i) => {
77+
if (i === 0) {
78+
return;
79+
}
7980

80-
const previousDebt = wei(decoded[i - 1].debt, 18, true);
81-
// Take the previous collateral amount
82-
const collateralAmount = wei(decoded[i - 1].amount, 18, true);
83-
const currentDebt = wei(data.debt, 18, true);
81+
const previousDebt = wei(decoded[i - 1].debt, 18, true);
82+
// Take the previous collateral amount
83+
const collateralAmount = wei(decoded[i - 1].amount, 18, true);
84+
const currentDebt = wei(data.debt, 18, true);
8485

85-
const pnlValue = previousDebt.sub(currentDebt);
86+
const pnlValue = previousDebt.sub(currentDebt);
8687

87-
pnls.push({ pnlValue, collateralAmount });
88-
});
88+
pnls.push({ pnlValue, collateralAmount });
89+
});
8990

90-
return {
91-
pnls,
92-
};
91+
return {
92+
pnls,
93+
};
94+
} catch (error) {
95+
console.error('Error fetching pnl', error);
96+
return { pnls: [] };
97+
}
9398
},
99+
enabled: Boolean(block && CoreProxy && Multicall3 && network),
94100
});
95101
};

liquidity/ui/src/pages/Home/Home.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export function Home() {
4646
isLoading={isLoading}
4747
collateralTypes={collateralTypes}
4848
liquidityPositionsById={liquidityPositionsById}
49-
apr={aprData?.combinedApr || 0}
49+
apr={aprData?.combinedApr}
5050
/>
5151
<AvailableCollateral />
5252
</Flex>

0 commit comments

Comments
 (0)