Skip to content

Commit

Permalink
Fix loading positions on mainnet (#24)
Browse files Browse the repository at this point in the history
* fix loading positions on mainnet

* fix withERC7412 for none erc7412 deployments
  • Loading branch information
Rickk137 authored Oct 4, 2024
1 parent 49d3e2a commit d8f628a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 25 deletions.
57 changes: 36 additions & 21 deletions liquidity/lib/withERC7412/withERC7412.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
importMulticall3,
importPythERC7412Wrapper,
} from '@snx-v3/contracts';
import { Network, getMagicProvider } from '@snx-v3/useBlockchain';
import { Network, deploymentHasERC7412, getMagicProvider } from '@snx-v3/useBlockchain';
import { ethers } from 'ethers';

export const ERC7412_ABI = [
Expand Down Expand Up @@ -198,6 +198,35 @@ function extractPriceId(parsedError: { name: string; args: string[] }) {
}
}

async function getMulticallTransaction(
network: Network,
calls: (ethers.PopulatedTransaction & { requireSuccess?: boolean })[],
from: string,
provider: ethers.providers.JsonRpcProvider
) {
const Multicall3Contract = await importMulticall3(network.id, network.preset);
const Multicall3Interface = new ethers.utils.Interface(Multicall3Contract.abi);

const multicallTxn = {
from: from ? from : getDefaultFromAddress(network.name),
to: Multicall3Contract.address,
data: Multicall3Interface.encodeFunctionData('aggregate3Value', [
calls.map((call) => ({
target: call.to,
callData: call.data,
value: call.value ? ethers.BigNumber.from(call.value) : ethers.BigNumber.from(0),
requireSuccess: call.requireSuccess ?? true,
})),
]),
value: calls.reduce(
(totalValue, call) => (call.value ? totalValue.add(call.value) : totalValue),
ethers.BigNumber.from(0)
),
};
const gasLimit = await provider.estimateGas(multicallTxn);
return { ...multicallTxn, gasLimit };
}

/**
* If a tx requires ERC7412 pattern, wrap your tx with this function.
*/
Expand All @@ -210,8 +239,11 @@ export const withERC7412 = async (
// Make sure we're always using JSONRpcProvider, the web3 provider coming from the signer might have bugs causing errors to miss revert data
const jsonRpcProvider =
getMagicProvider() ?? new ethers.providers.JsonRpcProvider(network.rpcUrl());
const Multicall3Contract = await importMulticall3(network.id, network.preset);
const Multicall3Interface = new ethers.utils.Interface(Multicall3Contract.abi);

if (!(await deploymentHasERC7412(network.id, network.preset))) {
return await getMulticallTransaction(network, calls, from, jsonRpcProvider);
}

const AllErrorsContract = await importAllErrors(network.id, network.preset);
const PythERC7412Wrapper = await importPythERC7412Wrapper(network.id, network.preset);

Expand All @@ -220,24 +252,7 @@ export const withERC7412 = async (
if (window.localStorage.getItem('DEBUG') === 'true') {
await logMulticall({ network, calls, label });
}
const multicallTxn = {
from: from ? from : getDefaultFromAddress(network.name),
to: Multicall3Contract.address,
data: Multicall3Interface.encodeFunctionData('aggregate3Value', [
calls.map((call) => ({
target: call.to,
callData: call.data,
value: call.value ? ethers.BigNumber.from(call.value) : ethers.BigNumber.from(0),
requireSuccess: call.requireSuccess ?? true,
})),
]),
value: calls.reduce(
(totalValue, call) => (call.value ? totalValue.add(call.value) : totalValue),
ethers.BigNumber.from(0)
),
};
const gasLimit = await jsonRpcProvider.estimateGas(multicallTxn);
return { ...multicallTxn, gasLimit };
return await getMulticallTransaction(network, calls, from, jsonRpcProvider);
} catch (error: Error | any) {
console.error(error);
let errorData = extractErrorData(error);
Expand Down
2 changes: 1 addition & 1 deletion liquidity/ui/src/components/Positions/PositionsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const PositionsList = () => {
const { accountId } = useParams();
const { network } = useNetwork();

const { data: positionsByKey, isLoading: isLiquidityPositionsLoading } = useLiquidityPositions({
const { data: positionsByKey, isPending: isLiquidityPositionsLoading } = useLiquidityPositions({
accountId,
});

Expand Down
6 changes: 3 additions & 3 deletions liquidity/ui/src/components/Stats/StatsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ export const StatsList = () => {

const { data: usdTokens } = useGetUSDTokens();

const { data: positions, isLoading: isLiquidityPositionLoading } = useLiquidityPositions({
const { data: positions, isPending: isLiquidityPositionLoading } = useLiquidityPositions({
accountId: params.get('accountId') || undefined,
});

const { data: collateralTypes, isLoading: isCollateralTypesLoading } = useCollateralTypes();
const { data: collateralTypes, isPending: isCollateralTypesLoading } = useCollateralTypes();

const { data: accountCollaterals, isLoading: isAccountCollateralsLoading } = useAccountCollateral(
const { data: accountCollaterals, isPending: isAccountCollateralsLoading } = useAccountCollateral(
{
accountId: params.get('accountId') || undefined,
}
Expand Down

0 comments on commit d8f628a

Please sign in to comment.