Skip to content

Commit

Permalink
fix: base multicall issue (#299)
Browse files Browse the repository at this point in the history
* fix: base multicall issue

* fix: persist account id
  • Loading branch information
Rickk137 authored Jun 10, 2024
1 parent 0086ddd commit c29d918
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 25 deletions.
37 changes: 21 additions & 16 deletions contracts/importers/importMulticall3.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const abi = [
'function aggregate(tuple(address target, bytes callData)[] calls) payable returns (uint256 blockNumber, bytes[] 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)',
'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)',
Expand All @@ -17,37 +15,44 @@ const abi = [
'function tryBlockAndAggregate(bool requireSuccess, tuple(address target, bytes callData)[] calls) payable returns (uint256 blockNumber, bytes32 blockHash, tuple(bool success, bytes returnData)[] returnData)',
];

const multicall3Abi = [
...abi,
'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)',
];

const trustedMulticallAbi = [
...abi,
'function aggregate3(tuple(address target, bool requireSuccess, bytes callData)[] calls) payable returns (tuple(bool success, bytes returnData)[] returnData)',
'function aggregate3Value(tuple(address target, bool requireSuccess, uint256 value, bytes callData)[] calls) payable returns (tuple(bool success, bytes returnData)[] returnData)',
];

const TrustedMulticallForwarder = '0xE2C5658cC5C448B48141168f3e475dF8f65A1e3e';

export async function importMulticall3(chainId, preset = 'main') {
const deployment = `${Number(chainId).toFixed(0)}-${preset}`;
switch (deployment) {
case '1-main': {
const { mainnet } = await import('viem/chains');
return { address: mainnet.contracts.multicall3.address, abi };
return { address: mainnet.contracts.multicall3.address, abi: multicall3Abi };
}
case '11155111-main': {
const { sepolia } = await import('viem/chains');
return { address: sepolia.contracts.multicall3.address, abi };
return { address: sepolia.contracts.multicall3.address, abi: multicall3Abi };
}
case '10-main': {
const { optimism } = await import('viem/chains');
return { address: optimism.contracts.multicall3.address, abi };
}
case '8453-andromeda': {
const { base } = await import('viem/chains');
return { address: base.contracts.multicall3.address, abi };
}
case '84532-andromeda': {
const { baseSepolia } = await import('viem/chains');
return { address: baseSepolia.contracts.multicall3.address, abi };
return { address: optimism.contracts.multicall3.address, abi: multicall3Abi };
}
case '8453-andromeda':
case '84532-andromeda':
case '42161-main':
case '42161-arbthetix': {
const { arbitrum } = await import('viem/chains');
return { address: arbitrum.contracts.multicall3.address, abi };
return { address: TrustedMulticallForwarder, abi: trustedMulticallAbi };
}
case '421614-main': {
const { arbitrumSepolia } = await import('viem/chains');
return { address: arbitrumSepolia.contracts.multicall3.address, abi };
return { address: arbitrumSepolia.contracts.multicall3.address, abi: multicall3Abi };
}
default: {
throw new Error(`Unsupported deployment ${deployment} for Multicall3`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ export const useLiquidityPositions = ({ accountId }: { accountId?: string }) =>
const prices = priceDecoder(encoded.slice(0, priceCalls.length));
const pricesByAddress = keyBy(
'address',

Array.isArray(prices)
? prices.map((price, i) => ({
price,
Expand Down
3 changes: 2 additions & 1 deletion liquidity/lib/withERC7412/withERC7412.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ export async function erc7412Call<T>(

const res = await provider.call(newCall);

if (newCall.to === multicallAddress) {
if (newCall.to?.toLowerCase() === multicallAddress.toLowerCase()) {
// If this was a multicall, decode and remove price updates.
const decodedMultiCall: { returnData: string }[] = new ethers.utils.Interface(
multicallAbi
Expand All @@ -323,6 +323,7 @@ export async function erc7412Call<T>(
const responseWithoutPriceUpdates = decodedMultiCall.filter(
({ returnData }) => returnData !== '0x' // price updates have 0x as return data
);

return decode(responseWithoutPriceUpdates.map(({ returnData }) => returnData));
}

Expand Down
23 changes: 19 additions & 4 deletions liquidity/ui/src/layouts/Default/NetworkController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ import {
Text,
} from '@chakra-ui/react';
import { WalletIcon } from '@snx-v3/icons';
import { NetworkIcon, useNetwork, useWallet, NETWORKS } from '@snx-v3/useBlockchain';
import { NetworkIcon, useNetwork, useWallet } from '@snx-v3/useBlockchain';
import { prettyString } from '@snx-v3/format';
import { useLocalStorage } from '@snx-v3/useLocalStorage';
import { LOCAL_STORAGE_KEYS } from '../../utils/constants';
import { CopyIcon, SettingsIcon } from '@chakra-ui/icons';
import { useAccounts, useCreateAccount } from '@snx-v3/useAccounts';
import { useLocation, useNavigate, useSearchParams } from 'react-router-dom';
import { Tooltip } from '@snx-v3/Tooltip';
import { supportedNetworks } from '../../utils/onboard';

export function NetworkController() {
const [toolTipLabel, setTooltipLabel] = useState('Copy');
Expand All @@ -39,7 +40,7 @@ export function NetworkController() {
const { pathname } = useLocation();

useEffect(() => {
if (!isAccountsLoading && !isAccountsFetching && accounts) {
if (!isAccountsLoading && !isAccountsFetching && !!accounts?.length) {
const accountId = queryParams.get('accountId');

if (accountId && !accounts?.includes(accountId)) {
Expand Down Expand Up @@ -69,12 +70,26 @@ export function NetworkController() {

useEffect(() => {
const accountId = queryParams.get('accountId');

if (!accountId && !!accounts?.length) {
queryParams.set('accountId', accounts[0]);
const lastUsedAccount = localStorage.getItem('accountId');
if (lastUsedAccount && accounts.find((account) => String(account) === lastUsedAccount)) {
queryParams.set('accountId', lastUsedAccount);
} else {
queryParams.set('accountId', accounts[0]);
}
navigate({ pathname, search: queryParams.toString() });
}
}, [accounts, navigate, pathname, queryParams]);

useEffect(() => {
const accountId = queryParams.get('accountId');

if (accountId) {
localStorage.setItem('accountId', accountId);
}
}, [queryParams]);

const onDisconnect = () => {
if (walletsInfo) {
disconnect(walletsInfo);
Expand All @@ -100,7 +115,7 @@ export function NetworkController() {
<NetworkIcon networkId={notConnected ? 8453 : notSupported ? 0 : activeNetwork?.id} />
</MenuButton>
<MenuList>
{NETWORKS.map(({ id, preset, label, isTestnet }) => {
{supportedNetworks.map(({ id, preset, label, isTestnet }) => {
if (isTestnet && !showTestnets) return null;
return (
<MenuItem key={`${id}-${preset}`} onClick={() => setNetwork(id)}>
Expand Down
8 changes: 5 additions & 3 deletions liquidity/ui/src/utils/onboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ const supportedDeployments = [
'421614-main',
];

// Filter networks to only supported ones
export const chains = NETWORKS.filter(({ id, preset }) =>
export const supportedNetworks = NETWORKS.filter(({ id, preset }) =>
supportedDeployments.includes(`${id}-${preset}`)
).map((n) => ({
);

// Filter networks to only supported ones
export const chains = supportedNetworks.map((n) => ({
id: n.id,
token: n.token,
label: n.label,
Expand Down

0 comments on commit c29d918

Please sign in to comment.