Skip to content

Commit

Permalink
Fix Multicall for Optimism (#133)
Browse files Browse the repository at this point in the history
* Fix Multicall for Optimism

* Rename useMulticall3 to useTrustedMulticallForwarder
  • Loading branch information
noisekit authored Jan 5, 2025
1 parent f3c28dc commit 4ba1256
Show file tree
Hide file tree
Showing 23 changed files with 119 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
importAllErrors,
importCoreProxy,
importDebtRepayer,
importMulticall3,
importTrustedMulticallForwarder,
importSpotMarketProxy,
} from '@snx-v3/contracts';
import { parseContractError } from '@snx-v3/parseContractError';
Expand All @@ -25,7 +25,10 @@ export async function delegateCollateralAndromeda({

const AllErrors = await importAllErrors(Cypress.env('chainId'), Cypress.env('preset'));

const Multicall3 = await importMulticall3(Cypress.env('chainId'), Cypress.env('preset'));
const Multicall3 = await importTrustedMulticallForwarder(
Cypress.env('chainId'),
Cypress.env('preset')
);
const Multicall3Contract = new ethers.Contract(Multicall3.address, Multicall3.abi, signer);

const CoreProxy = await importCoreProxy(Cypress.env('chainId'), Cypress.env('preset'));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,4 @@
const abi = [
'function aggregate(tuple(address target, bytes callData)[] calls) payable returns (uint256 blockNumber, bytes[] returnData)',
'function blockAndAggregate(tuple(address target, bytes callData)[] calls) payable returns (uint256 blockNumber, bytes32 blockHash, tuple(bool success, bytes returnData)[] returnData)',
'function getBasefee() view returns (uint256 basefee)',
'function getBlockHash(uint256 blockNumber) view returns (bytes32 blockHash)',
'function getBlockNumber() view returns (uint256 blockNumber)',
'function getChainId() view returns (uint256 chainid)',
'function getCurrentBlockCoinbase() view returns (address coinbase)',
'function getCurrentBlockDifficulty() view returns (uint256 difficulty)',
'function getCurrentBlockGasLimit() view returns (uint256 gaslimit)',
'function getCurrentBlockTimestamp() view returns (uint256 timestamp)',
'function getEthBalance(address addr) view returns (uint256 balance)',
'function getLastBlockHash() view returns (bytes32 blockHash)',
'function tryAggregate(bool requireSuccess, tuple(address target, bytes callData)[] calls) payable returns (tuple(bool success, bytes returnData)[] returnData)',
'function tryBlockAndAggregate(bool requireSuccess, tuple(address target, bytes callData)[] calls) payable returns (uint256 blockNumber, bytes32 blockHash, tuple(bool success, bytes returnData)[] returnData)',
'function aggregate3(tuple(address target, bool allowFailure, bytes callData)[] calls) payable returns (tuple(bool success, bytes returnData)[] returnData)',
'function aggregate3Value(tuple(address target, bool allowFailure, uint256 value, bytes callData)[] calls) payable returns (tuple(bool success, bytes returnData)[] returnData)',
];

export async function importMulticall3(
export async function importTrustedMulticallForwarder(
chainId?: number,
preset?: string
): Promise<{ address: string; abi: string[] }> {
Expand All @@ -38,8 +19,11 @@ export async function importMulticall3(
return { address: meta.contracts.TrustedMulticallForwarder, abi };
}
case '10-main': {
const { optimism } = await import('viem/chains');
return { address: optimism.contracts.multicall3.address, abi };
const [{ default: meta }, { default: abi }] = await Promise.all([
import('@synthetixio/v3-contracts/10-main/meta.json'),
import('@synthetixio/v3-contracts/10-main/TrustedMulticallForwarder.readable.json'),
]);
return { address: meta.contracts.TrustedMulticallForwarder, abi };
}
case '8453-andromeda': {
const [{ default: meta }, { default: abi }] = await Promise.all([
Expand Down
2 changes: 1 addition & 1 deletion liquidity/lib/contracts/importers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export * from './importExtras';
export * from './importLegacyMarket';
export * from './importMeta';
export * from './importMintableTokens';
export * from './importMulticall3';
export * from './importOracleManagerProxy';
export * from './importPerpsAccountProxy';
export * from './importPerpsMarketProxy';
Expand All @@ -22,6 +21,7 @@ export * from './importSpotMarketProxy';
export * from './importStaticAaveUSDC';
export * from './importSynthTokens';
export * from './importSystemToken';
export * from './importTrustedMulticallForwarder';
export * from './importUSDC';
export * from './importUSDProxy';
export * from './importV2x';
Expand Down
3 changes: 1 addition & 2 deletions liquidity/lib/contracts/importers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"main": "index.ts",
"types": "index.d.ts",
"dependencies": {
"@synthetixio/v3-contracts": "^7.1.0",
"viem": "^2.13.5"
"@synthetixio/v3-contracts": "^7.1.0"
}
}
1 change: 0 additions & 1 deletion liquidity/lib/contracts/useMulticall3/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useTrustedMulticallForwarder';
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@snx-v3/useMulticall3",
"name": "@snx-v3/useTrustedMulticallForwarder",
"private": true,
"main": "index.ts",
"version": "0.0.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { importMulticall3 } from '@snx-v3/contracts';
import { importTrustedMulticallForwarder } from '@snx-v3/contracts';
import { Network, useNetwork } from '@snx-v3/useBlockchain';
import { useQuery } from '@tanstack/react-query';

export function useMulticall3(customNetwork?: Network) {
export function useTrustedMulticallForwarder(customNetwork?: Network) {
const { network } = useNetwork();
const targetNetwork = customNetwork || network;

Expand All @@ -11,7 +11,7 @@ export function useMulticall3(customNetwork?: Network) {
enabled: Boolean(targetNetwork),
queryFn: async function () {
if (!targetNetwork) throw new Error('OMFG');
return importMulticall3(targetNetwork.id, targetNetwork.preset);
return importTrustedMulticallForwarder(targetNetwork.id, targetNetwork.preset);
},
staleTime: Infinity,
// On some chains this is not available, and that is expected
Expand Down
2 changes: 1 addition & 1 deletion liquidity/lib/useAccounts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"@snx-v3/tsHelpers": "workspace:*",
"@snx-v3/useAccountProxy": "workspace:*",
"@snx-v3/useBlockchain": "workspace:*",
"@snx-v3/useMulticall3": "workspace:*",
"@snx-v3/useTrustedMulticallForwarder": "workspace:*",
"@tanstack/react-query": "^5.8.3",
"debug": "^4.3.7",
"ethers": "^5.7.2"
Expand Down
4 changes: 2 additions & 2 deletions liquidity/lib/useAccounts/useAccounts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { contractsHash } from '@snx-v3/tsHelpers';
import { useAccountProxy } from '@snx-v3/useAccountProxy';
import { useNetwork, useProvider, useWallet } from '@snx-v3/useBlockchain';
import { useMulticall3 } from '@snx-v3/useMulticall3';
import { useTrustedMulticallForwarder } from '@snx-v3/useTrustedMulticallForwarder';
import { useQuery } from '@tanstack/react-query';
import debug from 'debug';
import { ethers } from 'ethers';
Expand All @@ -13,7 +13,7 @@ export function useAccounts() {
const { network } = useNetwork();
const provider = useProvider();
const { data: AccountProxy } = useAccountProxy();
const { data: Multicall3 } = useMulticall3();
const { data: Multicall3 } = useTrustedMulticallForwarder();
const walletAddress = activeWallet?.address;

return useQuery({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { EvmPriceServiceConnection } from '@pythnetwork/pyth-evm-js';
import { offchainMainnetEndpoint } from '@snx-v3/constants';
import {
importExtras,
importMulticall3,
importTrustedMulticallForwarder,
importPythERC7412Wrapper,
importPythFeeds,
importPythVerfier,
Expand Down Expand Up @@ -168,10 +168,8 @@ export const useCollateralPriceUpdates = (customNetwork?: Network) => {
}

try {
const { address: multicallAddress, abi: multiCallAbi } = await importMulticall3(
network.id,
network.preset
);
const { address: multicallAddress, abi: multiCallAbi } =
await importTrustedMulticallForwarder(network.id, network.preset);

const multicallInterface = new ethers.utils.Interface(multiCallAbi);
const pythInterface = new ethers.utils.Interface([
Expand Down
2 changes: 1 addition & 1 deletion liquidity/lib/useManagePermissions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"dependencies": {
"@snx-v3/useBlockchain": "workspace:*",
"@snx-v3/useCoreProxy": "workspace:*",
"@snx-v3/useMulticall3": "workspace:*",
"@snx-v3/useTrustedMulticallForwarder": "workspace:*",
"@tanstack/react-query": "^5.8.3",
"debug": "^4.3.7",
"ethers": "^5.7.2"
Expand Down
4 changes: 2 additions & 2 deletions liquidity/lib/useManagePermissions/useManagePermissions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useProvider, useSigner } from '@snx-v3/useBlockchain';
import { useCoreProxy } from '@snx-v3/useCoreProxy';
import { useMulticall3 } from '@snx-v3/useMulticall3';
import { useTrustedMulticallForwarder } from '@snx-v3/useTrustedMulticallForwarder';
import { useMutation } from '@tanstack/react-query';
import debug from 'debug';
import { ethers } from 'ethers';
Expand Down Expand Up @@ -40,7 +40,7 @@ export const useManagePermissions = ({
selected: Permissions;
}) => {
const { data: CoreProxy } = useCoreProxy();
const { data: Multicall3 } = useMulticall3();
const { data: Multicall3 } = useTrustedMulticallForwarder();
const signer = useSigner();
const provider = useProvider();

Expand Down
2 changes: 1 addition & 1 deletion liquidity/lib/useRewards/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"@snx-v3/useBlockchain": "workspace:*",
"@snx-v3/useCollateralTypes": "workspace:*",
"@snx-v3/useCoreProxy": "workspace:*",
"@snx-v3/useMulticall3": "workspace:*",
"@snx-v3/useRewardsDistributors": "workspace:*",
"@snx-v3/useSynthTokens": "workspace:*",
"@snx-v3/useTrustedMulticallForwarder": "workspace:*",
"@synthetixio/wei": "^2.74.4",
"@tanstack/react-query": "^5.8.3",
"debug": "^4.3.7",
Expand Down
4 changes: 2 additions & 2 deletions liquidity/lib/useRewards/useRewards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { contractsHash } from '@snx-v3/tsHelpers';
import { useNetwork, useProvider } from '@snx-v3/useBlockchain';
import { useCollateralTypes } from '@snx-v3/useCollateralTypes';
import { useCoreProxy } from '@snx-v3/useCoreProxy';
import { useMulticall3 } from '@snx-v3/useMulticall3';
import { useTrustedMulticallForwarder } from '@snx-v3/useTrustedMulticallForwarder';
import { useRewardsDistributors } from '@snx-v3/useRewardsDistributors';
import { useSynthTokens } from '@snx-v3/useSynthTokens';
import { Wei, wei } from '@synthetixio/wei';
Expand Down Expand Up @@ -72,7 +72,7 @@ export function useRewards({ accountId }: { accountId?: string }) {
const provider = useProvider();
const { data: synthTokens } = useSynthTokens();
const { data: collateralTypes } = useCollateralTypes();
const { data: Multicall3 } = useMulticall3(network);
const { data: Multicall3 } = useTrustedMulticallForwarder(network);
const { data: CoreProxy } = useCoreProxy(network);
// const { data: AllErrors } = useAllErrors(network);
const { data: rewardsDistributors } = useRewardsDistributors(network);
Expand Down
2 changes: 1 addition & 1 deletion liquidity/lib/useSynthBalances/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"dependencies": {
"@snx-v3/tsHelpers": "workspace:*",
"@snx-v3/useBlockchain": "workspace:*",
"@snx-v3/useMulticall3": "workspace:*",
"@snx-v3/useSynthTokens": "workspace:*",
"@snx-v3/useTrustedMulticallForwarder": "workspace:*",
"@synthetixio/wei": "^2.74.4",
"@tanstack/react-query": "^5.8.3",
"debug": "^4.3.7",
Expand Down
4 changes: 2 additions & 2 deletions liquidity/lib/useSynthBalances/useSynthBalances.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { contractsHash } from '@snx-v3/tsHelpers';
import { useNetwork, useProvider, useWallet } from '@snx-v3/useBlockchain';
import { useMulticall3 } from '@snx-v3/useMulticall3';
import { useTrustedMulticallForwarder } from '@snx-v3/useTrustedMulticallForwarder';
import { useSynthTokens } from '@snx-v3/useSynthTokens';
import { wei } from '@synthetixio/wei';
import { useQuery } from '@tanstack/react-query';
Expand All @@ -16,7 +16,7 @@ export function useSynthBalances() {
const walletAddress = activeWallet?.address;

const { data: synthTokens } = useSynthTokens();
const { data: Multicall3 } = useMulticall3();
const { data: Multicall3 } = useTrustedMulticallForwarder();

return useQuery({
enabled: Boolean(network && Multicall3 && synthTokens && walletAddress),
Expand Down
2 changes: 1 addition & 1 deletion liquidity/lib/useV2Position/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"dependencies": {
"@snx-v3/tsHelpers": "workspace:*",
"@snx-v3/useBlockchain": "workspace:*",
"@snx-v3/useMulticall3": "workspace:*",
"@snx-v3/useTrustedMulticallForwarder": "workspace:*",
"@snx-v3/useV2xSynthetix": "workspace:*",
"@synthetixio/wei": "^2.74.4",
"@tanstack/react-query": "^5.8.3",
Expand Down
4 changes: 2 additions & 2 deletions liquidity/lib/useV2Position/useV2Position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { useQuery } from '@tanstack/react-query';
import { Network, useWallet, useProviderForChain } from '@snx-v3/useBlockchain';
import { useV2xSynthetix } from '@snx-v3/useV2xSynthetix';
import { wei } from '@synthetixio/wei';
import { useMulticall3 } from '@snx-v3/useMulticall3';
import { useTrustedMulticallForwarder } from '@snx-v3/useTrustedMulticallForwarder';
import { contractsHash } from '@snx-v3/tsHelpers';
import { ethers } from 'ethers';

export function useV2Position(network: Network) {
const { activeWallet } = useWallet();
const provider = useProviderForChain(network);
const { data: Multicall3 } = useMulticall3(network);
const { data: Multicall3 } = useTrustedMulticallForwarder(network);
const { data: V2xSynthetix } = useV2xSynthetix(network);
const walletAddress = activeWallet?.address;

Expand Down
13 changes: 8 additions & 5 deletions liquidity/lib/withERC7412/withERC7412.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
importAllErrors,
importClosePosition,
importCoreProxy,
importMulticall3,
importTrustedMulticallForwarder,
importPythERC7412Wrapper,
importPythVerfier,
importSpotMarketProxy,
Expand Down Expand Up @@ -128,7 +128,9 @@ export async function logMulticall({
const ClosePositionContract = await importClosePosition(network.id, network.preset).catch(
() => undefined
);
const PythERC7412Wrapper = await importPythERC7412Wrapper(network.id, network.preset);
const PythERC7412Wrapper = await importPythERC7412Wrapper(network.id, network.preset).catch(
() => undefined
);
const PythVerfier = await importPythVerfier(network.id, network.preset);
const AllInterface = new ethers.utils.Interface(
dedupedAbi([
Expand All @@ -137,7 +139,7 @@ export async function logMulticall({
...AccountProxyContract.abi,
...USDProxyContract.abi,
...(ClosePositionContract ? ClosePositionContract.abi : []),
...PythERC7412Wrapper.abi,
...(PythERC7412Wrapper ? PythERC7412Wrapper.abi : []),
...PythVerfier.abi,
])
);
Expand Down Expand Up @@ -184,7 +186,7 @@ async function getMulticallTransaction(
from: string,
provider: ethers.providers.BaseProvider
) {
const Multicall3Contract = await importMulticall3(network.id, network.preset);
const Multicall3Contract = await importTrustedMulticallForwarder(network.id, network.preset);
const Multicall3Interface = new ethers.utils.Interface(Multicall3Contract.abi);

const multicallTxn = {
Expand Down Expand Up @@ -220,6 +222,7 @@ export const withERC7412 = async (
const log = debug(`snx:withERC7412:${label}`);

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

Expand Down Expand Up @@ -322,7 +325,7 @@ export async function erc7412Call<T>(
) {
const log = debug(`snx:withERC7412:${label}`);

const Multicall3Contract = await importMulticall3(network.id, network.preset);
const Multicall3Contract = await importTrustedMulticallForwarder(network.id, network.preset);

const from = getDefaultFromAddress(network.name);

Expand Down
2 changes: 1 addition & 1 deletion liquidity/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
"@snx-v3/useManagePermissions": "workspace:*",
"@snx-v3/useMigrate": "workspace:*",
"@snx-v3/useMigrateUSD": "workspace:*",
"@snx-v3/useMulticall3": "workspace:*",
"@snx-v3/useOraclePrice": "workspace:*",
"@snx-v3/useParams": "workspace:*",
"@snx-v3/usePoolConfiguration": "workspace:*",
Expand All @@ -88,6 +87,7 @@
"@snx-v3/useTokenPrice": "workspace:*",
"@snx-v3/useTransferAccountId": "workspace:*",
"@snx-v3/useTransferableSynthetix": "workspace:*",
"@snx-v3/useTrustedMulticallForwarder": "workspace:*",
"@snx-v3/useUSDC": "workspace:*",
"@snx-v3/useUSDProxy": "workspace:*",
"@snx-v3/useUndelegate": "workspace:*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { formatGasPriceForTransaction } from '@snx-v3/useGasOptions';
import { getGasPrice } from '@snx-v3/useGasPrice';
import { useGasSpeed } from '@snx-v3/useGasSpeed';
import { useLiquidityPosition } from '@snx-v3/useLiquidityPosition';
import { useMulticall3 } from '@snx-v3/useMulticall3';
import { useTrustedMulticallForwarder } from '@snx-v3/useTrustedMulticallForwarder';
import { type PositionPageSchemaType, useParams } from '@snx-v3/useParams';
import { usePythFeeds } from '@snx-v3/usePythFeeds';
import { usePythVerifier } from '@snx-v3/usePythVerifier';
Expand Down Expand Up @@ -54,7 +54,7 @@ export function ClosePositionOneStep({
const errorParser = useContractErrorParser();

const { data: CoreProxy } = useCoreProxy();
const { data: Multicall3 } = useMulticall3();
const { data: Multicall3 } = useTrustedMulticallForwarder();
const { data: AccountProxy } = useAccountProxy();
const { data: ClosePosition } = useClosePosition();
const { data: PythVerfier } = usePythVerifier();
Expand Down
Loading

0 comments on commit 4ba1256

Please sign in to comment.