Skip to content

Commit

Permalink
Chore/rpc issue (#219)
Browse files Browse the repository at this point in the history
* 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 <james@jmzwar.com>
  • Loading branch information
jmzwar and jmzwar authored Apr 4, 2024
1 parent 8b04f85 commit 2511196
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 59 deletions.
2 changes: 1 addition & 1 deletion liquidity/components/Pools/VaultRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function VaultRowUi({
</>
) : (
<Td>
<Text>{apr?.toFixed(2) || '-'}%</Text>
<Text>{apr ? apr.toFixed(2) : '-'}%</Text>
</Td>
)}
<Td textAlign="end">
Expand Down
6 changes: 3 additions & 3 deletions liquidity/lib/useApr/useApr.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
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';
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;

Expand Down
114 changes: 60 additions & 54 deletions liquidity/lib/useGetPnl/useGetPnl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
});
};
2 changes: 1 addition & 1 deletion liquidity/ui/src/pages/Home/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function Home() {
isLoading={isLoading}
collateralTypes={collateralTypes}
liquidityPositionsById={liquidityPositionsById}
apr={aprData?.combinedApr || 0}
apr={aprData?.combinedApr}
/>
<AvailableCollateral />
</Flex>
Expand Down

0 comments on commit 2511196

Please sign in to comment.