Skip to content

Commit 4da7f4a

Browse files
authored
fix: zksync gas estimation from safe account (#2371)
1 parent a1a2ad1 commit 4da7f4a

File tree

6 files changed

+56
-14
lines changed

6 files changed

+56
-14
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
"test:coverage": "jest --coverage"
3232
},
3333
"dependencies": {
34-
"@aave/contract-helpers": "1.33.0",
35-
"@aave/math-utils": "1.33.0",
34+
"@aave/contract-helpers": "1.33.1",
35+
"@aave/math-utils": "1.33.1",
3636
"@bgd-labs/aave-address-book": "^4.17.1",
3737
"@emotion/cache": "11.10.3",
3838
"@emotion/react": "11.10.4",

src/hooks/useIsContractAddress.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export const useIsContractAddress = (address: string, chainId?: number) => {
99
return useQuery({
1010
queryFn: () => provider.getCode(address),
1111
queryKey: ['isContractAddress', address],
12-
enabled: true,
12+
enabled: address !== '',
1313
staleTime: Infinity,
1414
select: (data) => data !== '0x',
1515
});

src/libs/web3-data-provider/Web3Provider.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import { SignatureLike } from '@ethersproject/bytes';
33
import { JsonRpcProvider, TransactionResponse } from '@ethersproject/providers';
44
import { BigNumber, PopulatedTransaction, utils } from 'ethers';
55
import React, { ReactElement, useEffect, useState } from 'react';
6+
import { useIsContractAddress } from 'src/hooks/useIsContractAddress';
67
import { useRootStore } from 'src/store/root';
78
import { wagmiConfig } from 'src/ui-config/wagmiConfig';
89
import { hexToAscii } from 'src/utils/utils';
910
import { UserRejectedRequestError } from 'viem';
1011
import { useAccount, useConnect, useSwitchChain, useWatchAsset } from 'wagmi';
12+
import { useShallow } from 'zustand/shallow';
1113

1214
import { Web3Context } from '../hooks/useWeb3Context';
1315
import { getEthersProvider } from './adapters/EthersAdapter';
@@ -47,7 +49,9 @@ export const Web3ContextProvider: React.FC<{ children: ReactElement }> = ({ chil
4749

4850
const [readOnlyModeAddress, setReadOnlyModeAddress] = useState<string | undefined>();
4951
const [switchNetworkError, setSwitchNetworkError] = useState<Error>();
50-
const setAccount = useRootStore((store) => store.setAccount);
52+
const [setAccount, setConnectedAccountIsContract] = useRootStore(
53+
useShallow((store) => [store.setAccount, store.setConnectedAccountIsContract])
54+
);
5155

5256
const account = address;
5357
const readOnlyMode = utils.isAddress(readOnlyModeAddress || '');
@@ -56,6 +60,8 @@ export const Web3ContextProvider: React.FC<{ children: ReactElement }> = ({ chil
5660
currentAccount = readOnlyModeAddress;
5761
}
5862

63+
const { data: isContractAddress } = useIsContractAddress(account || '', chainId);
64+
5965
useEffect(() => {
6066
if (didInit) {
6167
return;
@@ -179,6 +185,17 @@ export const Web3ContextProvider: React.FC<{ children: ReactElement }> = ({ chil
179185
}
180186
}, [readOnlyModeAddress, setAccount]);
181187

188+
useEffect(() => {
189+
if (!account) {
190+
setConnectedAccountIsContract(false);
191+
return;
192+
}
193+
194+
if (isContractAddress) {
195+
setConnectedAccountIsContract(true);
196+
}
197+
}, [isContractAddress, setConnectedAccountIsContract, account]);
198+
182199
return (
183200
<Web3Context.Provider
184201
value={{

src/store/poolSlice.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
ApproveDelegationType,
44
ApproveType,
55
BaseDebtToken,
6+
ChainId,
67
DebtSwitchAdapterService,
78
ERC20_2612Service,
89
ERC20Service,
@@ -701,7 +702,11 @@ export const createPoolSlice: StateCreator<
701702
poolComputed: {
702703
get minRemainingBaseTokenBalance() {
703704
if (!get()) return '0.001';
704-
const { currentNetworkConfig, currentChainId } = { ...get() };
705+
706+
const { currentNetworkConfig, currentChainId, connectedAccountIsContract } = { ...get() };
707+
708+
if (connectedAccountIsContract) return '0';
709+
705710
const chainId = currentNetworkConfig.underlyingChainId || currentChainId;
706711
const min = minBaseTokenRemainingByNetwork[chainId];
707712
return min || '0.001';
@@ -766,7 +771,21 @@ export const createPoolSlice: StateCreator<
766771
return JSON.stringify(typeData);
767772
},
768773
estimateGasLimit: async (tx: PopulatedTransaction, chainId?: number) => {
769-
const provider = get().jsonRpcProvider(chainId);
774+
const { currentChainId, connectedAccountIsContract, jsonRpcProvider } = get();
775+
776+
const effectiveChainId = chainId ?? currentChainId;
777+
778+
/**
779+
* Trying to estimate gas on zkSync when connected with a smart contract address
780+
* will fail. In that case, we'll just return the default value for all transactions.
781+
*
782+
* See here for more details: https://github.com/zkSync-Community-Hub/zksync-developers/discussions/144
783+
*/
784+
if (effectiveChainId === ChainId.zksync && connectedAccountIsContract) {
785+
return tx;
786+
}
787+
788+
const provider = jsonRpcProvider(chainId);
770789
const defaultGasLimit: BigNumber = tx.gasLimit ? tx.gasLimit : BigNumber.from('0');
771790
delete tx.gasLimit;
772791
let estimatedGas = await provider.estimateGas(tx);

src/store/walletSlice.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ export interface WalletSlice {
1717
walletApprovalMethodPreference: ApprovalMethod;
1818
setWalletApprovalMethodPreference: (method: ApprovalMethod) => void;
1919
refreshWalletApprovalMethod: () => void;
20+
connectedAccountIsContract: boolean;
21+
setConnectedAccountIsContract: (isContract: boolean) => void;
2022
}
2123

2224
const getWalletPreferences = () => {
@@ -73,4 +75,8 @@ export const createWalletSlice: StateCreator<
7375
}));
7476
}
7577
},
78+
connectedAccountIsContract: false,
79+
setConnectedAccountIsContract(isContract) {
80+
set({ connectedAccountIsContract: isContract });
81+
},
7682
});

yarn.lock

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
# yarn lockfile v1
33

44

5-
6-
version "1.33.0"
7-
resolved "https://registry.yarnpkg.com/@aave/contract-helpers/-/contract-helpers-1.33.0.tgz#d58f44c83737dfe3ab04b3776f1133064e08ebc6"
8-
integrity sha512-hwOG+B6LYv8/oZIW3OYPZqr2OwEVYLWXO230h3LfUpG9P4+MhknZeyuJHRA7uGNpj2dpP/KYuLrYYfx4G9D0aQ==
5+
6+
version "1.33.1"
7+
resolved "https://registry.yarnpkg.com/@aave/contract-helpers/-/contract-helpers-1.33.1.tgz#2156c179e802c40c137381d337c5f93ebee1abeb"
8+
integrity sha512-Zu/zwJiX3k2xBRAlNfmIJMIo19YWVJAL2uRPHbvCVxOUgzlhVyglIF98FgcZ+4aLwcjvSAw10wnIzGgJcPTNVw==
99
dependencies:
1010
isomorphic-unfetch "^3.1.0"
1111

12-
13-
version "1.33.0"
14-
resolved "https://registry.yarnpkg.com/@aave/math-utils/-/math-utils-1.33.0.tgz#2386f7353dfdc7bdd88079e4fd7351275b1cdbf6"
15-
integrity sha512-guDUruuQTmp8QLUNRZJFxzu/VyJPs0Mieebk+tJDplVNq+TzaSZHU3YtAxp/u6tGw166v47ALFrt7yA/oaX1TA==
12+
13+
version "1.33.1"
14+
resolved "https://registry.yarnpkg.com/@aave/math-utils/-/math-utils-1.33.1.tgz#1dddf06e48cca13251db94e2f24afb0aa1b270ed"
15+
integrity sha512-+e7ThIRLxyrcLB507T3khdE6DqKn2vTR5hfnQd1rHetEzLObA6bKDElX7YEtGmuSzBwDTP4inpK2VtgiQAqIWQ==
1616

1717
"@adobe/css-tools@^4.0.1":
1818
version "4.4.1"

0 commit comments

Comments
 (0)