Skip to content

Commit

Permalink
Precise filtering for price update calls
Browse files Browse the repository at this point in the history
  • Loading branch information
noisekit committed Oct 29, 2024
1 parent c7bbd78 commit b41b9d8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
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;
}
}
}
42 changes: 34 additions & 8 deletions liquidity/lib/withERC7412/withERC7412.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
import { Network, deploymentHasERC7412, getMagicProvider } from '@snx-v3/useBlockchain';
import { ethers } from 'ethers';

const IS_DEBUG = window.localStorage.getItem('DEBUG') === 'true';

export const ERC7412_ABI = [
'error OracleDataRequired(address oracleContract, bytes oracleQuery)',
'error OracleDataRequired(address oracleContract, bytes oracleQuery, uint256 feeRequired)',
Expand Down Expand Up @@ -222,7 +224,7 @@ async function getMulticallTransaction(
),
};
const gasLimit = await provider.estimateGas(multicallTxn);
return { ...multicallTxn, gasLimit };
return { ...multicallTxn, gasLimit, _calls: calls };
}

/**
Expand All @@ -233,7 +235,12 @@ export const withERC7412 = async (
calls: (ethers.PopulatedTransaction & { requireSuccess?: boolean })[],
label: string,
from: string
): Promise<ethers.PopulatedTransaction & { gasLimit: ethers.BigNumber }> => {
): Promise<
ethers.PopulatedTransaction & {
gasLimit: ethers.BigNumber;
_calls?: (ethers.PopulatedTransaction & { requireSuccess?: boolean })[];
}
> => {
// 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());
Expand All @@ -247,7 +254,7 @@ export const withERC7412 = async (

while (true) {
try {
if (window.localStorage.getItem('DEBUG') === 'true') {
if (IS_DEBUG) {
await logMulticall({ network, calls, label });
}
return await getMulticallTransaction(network, calls, from, jsonRpcProvider);
Expand Down Expand Up @@ -348,6 +355,9 @@ export async function erc7412Call<T>(
from
);

const { _calls: newCalls } = newCall;
delete newCall._calls;

const res = await provider.call(newCall);

if (newCall.to?.toLowerCase() === Multicall3Contract.address.toLowerCase()) {
Expand All @@ -356,13 +366,29 @@ export async function erc7412Call<T>(
Multicall3Contract.abi
).decodeFunctionResult('aggregate3Value', res)[0];

if (IS_DEBUG) {
console.log(`[${label}] multicall`, decodedMultiCall);
}

// Remove the price updates
const responseWithoutPriceUpdates = decodedMultiCall.filter(
({ returnData }) => returnData !== '0x' // price updates have 0x as return data
);
const responseWithoutPriceUpdates: string[] = [];
const PythVerfier = await importPythVerfier(network.id, network.preset);
decodedMultiCall.forEach(({ returnData }, i) => {
if (newCalls?.[i]?.to !== PythVerfier.address) {
responseWithoutPriceUpdates.push(returnData);
}
});

return decode(responseWithoutPriceUpdates.map(({ returnData }) => returnData));
const decoded = decode(responseWithoutPriceUpdates);
if (IS_DEBUG) {
console.log(`[${label}] result`, decoded);
}
return decoded;
}

return decode(res);
const decoded = decode(res);
if (IS_DEBUG) {
console.log(`[${label}] result`, decoded);
}
return decoded;
}

0 comments on commit b41b9d8

Please sign in to comment.