Skip to content

Commit

Permalink
Deterministic multicall cleanup (#48)
Browse files Browse the repository at this point in the history
* Precise filtering for price update calls

* Catch 0x Multicall return in withERC7412 helper

* Add testnets scripts

* Fix for withERC7412
  • Loading branch information
noisekit authored Nov 4, 2024
1 parent 877a0e2 commit e81f269
Show file tree
Hide file tree
Showing 23 changed files with 193 additions and 73 deletions.
6 changes: 5 additions & 1 deletion liquidity/cypress/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
"version": "0.0.9",
"scripts": {
"anvil:arbitrum": "anvil --auto-impersonate --chain-id 42161 --fork-url wss://arbitrum-mainnet.infura.io/ws/v3/$INFURA_KEY --timeout 5000 --retries 0 --no-rate-limit --steps-tracing --fork-block-number 269244293",
"anvil:arbitrum-sepolia": "anvil --auto-impersonate --chain-id 421614 --fork-url wss://arbitrum-sepolia.infura.io/ws/v3/$INFURA_KEY --timeout 5000 --retries 0 --no-rate-limit --steps-tracing",
"anvil:sepolia": "anvil --auto-impersonate --chain-id 11155111 --fork-url wss://sepolia.infura.io/ws/v3/$INFURA_KEY --timeout 5000 --retries 0 --no-rate-limit --steps-tracing",
"anvil:base": "anvil --auto-impersonate --chain-id 8453 --fork-url wss://base-mainnet.infura.io/ws/v3/$INFURA_KEY --timeout 5000 --retries 0 --no-rate-limit --steps-tracing --fork-block-number 21781780",
"anvil:base-sepolia": "anvil --auto-impersonate --chain-id 84532 --fork-url wss://base-sepolia.infura.io/ws/v3/$INFURA_KEY --timeout 5000 --retries 0 --no-rate-limit --steps-tracing",
"cy": "NODE_ENV=test cypress open --component --browser chrome",
"e2e:arbitrum": "NODE_ENV=test CYPRESS_CHAIN_ID=42161 CYPRESS_PRESET=main cypress open --e2e --browser chrome --config specPattern='./cypress/e2e/42161-main/*.e2e.js'",
"e2e:base": "NODE_ENV=test CYPRESS_CHAIN_ID=8453 CYPRESS_PRESET=andromeda cypress open --e2e --browser chrome --config specPattern='./cypress/e2e/8453-andromeda/*.e2e.js'",
"sync-time": "ts-node bin/syncTime.ts",
"update-prices": "ts-node bin/doAllPriceUpdates.ts 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80",
"update-prices:arbitrum": "NODE_ENV=test CYPRESS_CHAIN_ID=42161 CYPRESS_PRESET=main yarn update-prices",
"update-prices:base": "NODE_ENV=test CYPRESS_CHAIN_ID=8453 CYPRESS_PRESET=andromeda yarn update-prices"
"update-prices:arbitrum-sepolia": "NODE_ENV=test CYPRESS_CHAIN_ID=421614 CYPRESS_PRESET=main yarn update-prices",
"update-prices:base": "NODE_ENV=test CYPRESS_CHAIN_ID=8453 CYPRESS_PRESET=andromeda yarn update-prices",
"update-prices:base-sepolia": "NODE_ENV=test CYPRESS_CHAIN_ID=84532 CYPRESS_PRESET=andromeda yarn update-prices"
},
"devDependencies": {
"@chakra-ui/react": "^2.8.2",
Expand Down
20 changes: 14 additions & 6 deletions liquidity/lib/useAccountCollateral/useAccountCollateral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,13 @@ export function useAccountCollateral({
});
const allCalls = [...calls];

allCalls.unshift(
(await getPriceUpdates((await getPythFeedIds(network)) as string[], network)) as any
);
const priceUpdateTx = (await getPriceUpdates(
(await getPythFeedIds(network)) as string[],
network
).catch(() => undefined)) as any;
if (priceUpdateTx) {
allCalls.unshift(priceUpdateTx);
}

const data = await erc7412Call(network, provider, allCalls, decoder, 'useAccountCollateral');

Expand Down Expand Up @@ -173,9 +177,13 @@ export function useAccountSpecificCollateral(accountId?: string, collateralAddre
});
const allCalls = [...calls];

allCalls.unshift(
(await getPriceUpdates((await getPythFeedIds(network)) as string[], network)) as any
);
const priceUpdateTx = (await getPriceUpdates(
(await getPythFeedIds(network)) as string[],
network
).catch(() => undefined)) as any;
if (priceUpdateTx) {
allCalls.unshift(priceUpdateTx);
}

const data = await erc7412Call(
network,
Expand Down
11 changes: 10 additions & 1 deletion liquidity/lib/useBlockchain/magic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ export class MagicProvider extends ethers.providers.JsonRpcProvider {
if (method === 'eth_accounts') {
return [this.magicWallet];
}
return super.send(method, params);
try {
const result = await super.send(method, params);
// eslint-disable-next-line no-console
console.log('MAGIC.send', { method, params, result });
return result;
} catch (error) {
// eslint-disable-next-line no-console
console.log('MAGIC.send ERROR', { method, params, error });
throw error;
}
}
}
9 changes: 7 additions & 2 deletions liquidity/lib/useBorrow/useBorrow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,15 @@ export const useBorrow = ({
}

const walletAddress = await signer.getAddress();
const erc7412Tx = await withERC7412(network, calls, 'useBorrow', walletAddress);
const { multicallTxn: erc7412Tx, gasLimit } = await withERC7412(
network,
calls,
'useBorrow',
walletAddress
);

const gasOptionsForTransaction = formatGasPriceForTransaction({
gasLimit: erc7412Tx.gasLimit,
gasLimit,
gasPrices,
gasSpeed,
});
Expand Down
9 changes: 7 additions & 2 deletions liquidity/lib/useClaimAllRewards/useClaimAllRewards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,15 @@ export function useClaimAllRewards(
calls.unshift(priceUpdateTx as any);
}

const erc7412Tx = await withERC7412(network, calls, 'useClaimAllRewards', walletAddress);
const { multicallTxn: erc7412Tx, gasLimit } = await withERC7412(
network,
calls,
'useClaimAllRewards',
walletAddress
);

const gasOptionsForTransaction = formatGasPriceForTransaction({
gasLimit: erc7412Tx.gasLimit,
gasLimit,
gasPrices,
gasSpeed,
});
Expand Down
9 changes: 7 additions & 2 deletions liquidity/lib/useClearDebt/useClearDebt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,15 @@ export const useClearDebt = ({
calls.unshift(priceUpdateTx as any);
}
const walletAddress = await signer.getAddress();
const erc7412Tx = await withERC7412(network, calls, 'useRepay', walletAddress);
const { multicallTxn: erc7412Tx, gasLimit } = await withERC7412(
network,
calls,
'useRepay',
walletAddress
);

const gasOptionsForTransaction = formatGasPriceForTransaction({
gasLimit: erc7412Tx.gasLimit,
gasLimit,
gasPrices,
gasSpeed,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,15 @@ export const useCollateralPriceUpdates = (customNetwork?: Network) => {
}

if (outdatedPriceIds.length) {
return {
...(await getPriceUpdates(outdatedPriceIds, network)),
from: walletAddress,
};
const priceUpdateTx = (await getPriceUpdates(outdatedPriceIds, network).catch(
() => undefined
)) as any;
if (priceUpdateTx) {
return {
...priceUpdateTx,
from: walletAddress,
};
}
}

return null;
Expand Down
14 changes: 8 additions & 6 deletions liquidity/lib/useCollateralPrices/useCollateralPrices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,14 @@ export const useCollateralPrices = (customNetwork?: Network) => {
const { calls, decoder } = await loadPrices({ CoreProxy, collateralAddresses });

const allCalls = [...calls];
allCalls.unshift(
(await getPriceUpdates(
(await getPythFeedIds(targetNetwork)) as string[],
targetNetwork
)) as any
);

const priceUpdateTx = (await getPriceUpdates(
(await getPythFeedIds(targetNetwork)) as string[],
targetNetwork
).catch(() => undefined)) as any;
if (priceUpdateTx) {
allCalls.unshift(priceUpdateTx);
}

const prices = await erc7412Call(
targetNetwork,
Expand Down
9 changes: 7 additions & 2 deletions liquidity/lib/useDeposit/useDeposit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,15 @@ export const useDeposit = ({
calls.unshift(priceUpdateTx as any);
}

const erc7412Tx = await withERC7412(network, calls, 'useDeposit', walletAddress);
const { multicallTxn: erc7412Tx, gasLimit } = await withERC7412(
network,
calls,
'useDeposit',
walletAddress
);

const gasOptionsForTransaction = formatGasPriceForTransaction({
gasLimit: erc7412Tx.gasLimit,
gasLimit,
gasPrices,
gasSpeed,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,15 @@ export const useDepositBaseAndromeda = ({

const walletAddress = await signer.getAddress();

const erc7412Tx = await withERC7412(
const { multicallTxn: erc7412Tx, gasLimit } = await withERC7412(
network,
calls,
'useDepositBaseAndromeda',
walletAddress
);

const gasOptionsForTransaction = formatGasPriceForTransaction({
gasLimit: erc7412Tx.gasLimit,
gasLimit,
gasPrices,
gasSpeed,
});
Expand Down
10 changes: 7 additions & 3 deletions liquidity/lib/useLiquidityPosition/useLiquidityPosition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,13 @@ export const useLiquidityPosition = ({

const allCalls = priceCalls.concat(positionCalls).concat(accountCollateralCalls);

allCalls.unshift(
(await getPriceUpdates((await getPythFeedIds(network)) as string[], network)) as any
);
const priceUpdateTx = (await getPriceUpdates(
(await getPythFeedIds(network)) as string[],
network
).catch(() => undefined)) as any;
if (priceUpdateTx) {
allCalls.unshift(priceUpdateTx);
}

return await erc7412Call(
network,
Expand Down
10 changes: 7 additions & 3 deletions liquidity/lib/useLiquidityPositions/useLiquidityPositions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,13 @@ export const useLiquidityPositions = ({ accountId }: { accountId?: string }) =>
const allCalls = priceCalls.concat(positionCalls).concat(availableCollateralCalls);
const singlePositionDecoder = positionCallsAndData.at(0)?.decoder;

allCalls.unshift(
(await getPriceUpdates((await getPythFeedIds(network)) as string[], network)) as any
);
const priceUpdateTx = (await getPriceUpdates(
(await getPythFeedIds(network)) as string[],
network
).catch(() => undefined)) as any;
if (priceUpdateTx) {
allCalls.unshift(priceUpdateTx);
}

return await erc7412Call(
network,
Expand Down
14 changes: 9 additions & 5 deletions liquidity/lib/usePoolConfiguration/usePoolConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,22 @@ export const usePoolConfiguration = (poolId?: string) => {
maxDebtShareValue: weightD18,
}));

const calls = await Promise.all(
const allCalls = await Promise.all(
markets.map((m) => CoreProxy.populateTransaction.isMarketCapacityLocked(m.id))
);

calls.unshift(
(await getPriceUpdates((await getPythFeedIds(network)) as string[], network)) as any
);
const priceUpdateTx = (await getPriceUpdates(
(await getPythFeedIds(network)) as string[],
network
).catch(() => undefined)) as any;
if (priceUpdateTx) {
allCalls.unshift(priceUpdateTx);
}

const decoded = await erc7412Call(
network,
provider,
calls,
allCalls,
(encoded) => {
const result = Array.isArray(encoded) ? encoded : [encoded];
return result.map((x) =>
Expand Down
6 changes: 3 additions & 3 deletions liquidity/lib/usePoolsList/usePoolsList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ export function usePoolsList() {
}

export function usePool(networkId: number, poolId: string) {
const { data, isLoading } = usePoolsList();
const { data, isPending } = usePoolsList();

// TODO: In the future if we have multiple pools per network filter by poolId also
return {
data: data?.synthetixPools.find(
(p) => p.network.id === networkId && p?.poolInfo?.[0]?.pool?.id === poolId
(p) => p?.network?.id === networkId && p?.poolInfo?.[0]?.pool?.id === poolId
),
isLoading,
isLoading: isPending,
};
}

Expand Down
9 changes: 7 additions & 2 deletions liquidity/lib/useRepay/useRepay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,15 @@ export const useRepay = ({
calls.unshift(priceUpdateTx as any);
}

const erc7412Tx = await withERC7412(network, calls, 'useRepay', walletAddress);
const { multicallTxn: erc7412Tx, gasLimit } = await withERC7412(
network,
calls,
'useRepay',
walletAddress
);

const gasOptionsForTransaction = formatGasPriceForTransaction({
gasLimit: erc7412Tx.gasLimit,
gasLimit,
gasPrices,
gasSpeed,
});
Expand Down
9 changes: 7 additions & 2 deletions liquidity/lib/useRepayBaseAndromeda/useRepayBaseAndromeda.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,15 @@ export const useRepayBaseAndromeda = ({
}

const walletAddress = await signer.getAddress();
const erc7412Tx = await withERC7412(network, calls, 'useRepay', walletAddress);
const { multicallTxn: erc7412Tx, gasLimit } = await withERC7412(
network,
calls,
'useRepay',
walletAddress
);

const gasOptionsForTransaction = formatGasPriceForTransaction({
gasLimit: erc7412Tx.gasLimit,
gasLimit,
gasPrices,
gasSpeed,
});
Expand Down
9 changes: 7 additions & 2 deletions liquidity/lib/useUndelegate/useUndelegate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,15 @@ export const useUndelegate = ({
calls.unshift(priceUpdateTx as any);
}

const erc7412Tx = await withERC7412(network, calls, 'useUndelegate', walletAddress);
const { multicallTxn: erc7412Tx, gasLimit } = await withERC7412(
network,
calls,
'useUndelegate',
walletAddress
);

const gasOptionsForTransaction = formatGasPriceForTransaction({
gasLimit: erc7412Tx.gasLimit,
gasLimit,
gasPrices,
gasSpeed,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,15 @@ export const useUndelegateBaseAndromeda = ({

const walletAddress = await signer.getAddress();

const erc7412Tx = await withERC7412(network, calls, 'useUndelegateBase', walletAddress);
const { multicallTxn: erc7412Tx, gasLimit } = await withERC7412(
network,
calls,
'useUndelegateBase',
walletAddress
);

const gasOptionsForTransaction = formatGasPriceForTransaction({
gasLimit: erc7412Tx.gasLimit,
gasLimit,
gasPrices,
gasSpeed,
});
Expand Down
17 changes: 9 additions & 8 deletions liquidity/lib/useVaultsData/useVaultsData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,20 @@ export const useVaultsData = (poolId?: number, customNetwork?: Network) => {
)
);

const calls = await Promise.all([collateralCallsP, debtCallsP]);
const allCalls = await Promise.all([collateralCallsP, debtCallsP]);

calls.unshift(
(await getPriceUpdates(
(await getPythFeedIds(targetNetwork)) as string[],
targetNetwork
)) as any
);
const priceUpdateTx = (await getPriceUpdates(
(await getPythFeedIds(targetNetwork)) as string[],
targetNetwork
).catch(() => undefined)) as any;
if (priceUpdateTx) {
allCalls.unshift(priceUpdateTx);
}

return await erc7412Call(
targetNetwork,
provider,
calls.flat(),
allCalls.flat(),
(multicallResult) => {
if (!Array.isArray(multicallResult)) throw Error('Expected array');

Expand Down
Loading

0 comments on commit e81f269

Please sign in to comment.