Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: zksync gas estimation from safe account #2371

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
"test:coverage": "jest --coverage"
},
"dependencies": {
"@aave/contract-helpers": "1.32.1",
"@aave/math-utils": "1.32.1",
"@aave/contract-helpers": "1.32.2-51a91f59a8ffb3609b3e1d811454d42b056e09c0.0+0e8fe1e",
"@aave/math-utils": "1.32.2-51a91f59a8ffb3609b3e1d811454d42b056e09c0.0+0e8fe1e",
"@bgd-labs/aave-address-book": "4.10.0",
"@emotion/cache": "11.10.3",
"@emotion/react": "11.10.4",
Expand Down Expand Up @@ -151,5 +151,6 @@
"budget": null,
"budgetPercentIncreaseRed": 20,
"showDetails": true
}
},
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
}
2 changes: 1 addition & 1 deletion src/hooks/useIsContractAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const useIsContractAddress = (address: string, chainId?: number) => {
return useQuery({
queryFn: () => provider.getCode(address),
queryKey: ['isContractAddress', address],
enabled: true,
enabled: address !== '',
staleTime: Infinity,
select: (data) => data !== '0x',
});
Expand Down
21 changes: 20 additions & 1 deletion src/libs/web3-data-provider/Web3Provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { SignatureLike } from '@ethersproject/bytes';
import { JsonRpcProvider, TransactionResponse } from '@ethersproject/providers';
import { BigNumber, PopulatedTransaction, utils } from 'ethers';
import React, { ReactElement, useEffect, useState } from 'react';
import { useIsContractAddress } from 'src/hooks/useIsContractAddress';
import { useRootStore } from 'src/store/root';
import { wagmiConfig } from 'src/ui-config/wagmiConfig';
import { hexToAscii } from 'src/utils/utils';
import { UserRejectedRequestError } from 'viem';
import { useAccount, useConnect, useSwitchChain, useWatchAsset } from 'wagmi';
import { useShallow } from 'zustand/shallow';

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

const [readOnlyModeAddress, setReadOnlyModeAddress] = useState<string | undefined>();
const [switchNetworkError, setSwitchNetworkError] = useState<Error>();
const setAccount = useRootStore((store) => store.setAccount);
const [setAccount, setConnectedAccountIsContract] = useRootStore(
useShallow((store) => [store.setAccount, store.setConnectedAccountIsContract])
);

const account = address;
const readOnlyMode = utils.isAddress(readOnlyModeAddress || '');
Expand Down Expand Up @@ -179,6 +183,21 @@ export const Web3ContextProvider: React.FC<{ children: ReactElement }> = ({ chil
}
}, [readOnlyModeAddress, setAccount]);

console.log('account', account);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can remove the logs

const { data: isContractAddress } = useIsContractAddress(account || '', chainId);

useEffect(() => {
if (!account) {
setConnectedAccountIsContract(false);
return;
}

console.log('isContractAddress', isContractAddress);
if (isContractAddress) {
setConnectedAccountIsContract(true);
}
}, [isContractAddress, setConnectedAccountIsContract, account]);

return (
<Web3Context.Provider
value={{
Expand Down
23 changes: 21 additions & 2 deletions src/store/poolSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ApproveDelegationType,
ApproveType,
BaseDebtToken,
ChainId,
DebtSwitchAdapterService,
ERC20_2612Service,
ERC20Service,
Expand Down Expand Up @@ -701,7 +702,11 @@ export const createPoolSlice: StateCreator<
poolComputed: {
get minRemainingBaseTokenBalance() {
if (!get()) return '0.001';
const { currentNetworkConfig, currentChainId } = { ...get() };

const { currentNetworkConfig, currentChainId, connectedAccountIsContract } = { ...get() };

if (connectedAccountIsContract) return '0';

const chainId = currentNetworkConfig.underlyingChainId || currentChainId;
const min = minBaseTokenRemainingByNetwork[chainId];
return min || '0.001';
Expand Down Expand Up @@ -766,7 +771,21 @@ export const createPoolSlice: StateCreator<
return JSON.stringify(typeData);
},
estimateGasLimit: async (tx: PopulatedTransaction, chainId?: number) => {
const provider = get().jsonRpcProvider(chainId);
const { currentChainId, connectedAccountIsContract, jsonRpcProvider } = get();

const effectiveChainId = chainId ?? currentChainId;

/**
* Trying to estimate gas on zkSync when connected with a smart contract address
* will fail. In that case, we'll just return the default value for all transactions.
*
* See here for more details: https://github.com/zkSync-Community-Hub/zksync-developers/discussions/144
*/
if (effectiveChainId === ChainId.zksync && connectedAccountIsContract) {
return tx;
}

const provider = jsonRpcProvider(chainId);
const defaultGasLimit: BigNumber = tx.gasLimit ? tx.gasLimit : BigNumber.from('0');
delete tx.gasLimit;
let estimatedGas = await provider.estimateGas(tx);
Expand Down
6 changes: 6 additions & 0 deletions src/store/walletSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export interface WalletSlice {
walletApprovalMethodPreference: ApprovalMethod;
setWalletApprovalMethodPreference: (method: ApprovalMethod) => void;
refreshWalletApprovalMethod: () => void;
connectedAccountIsContract: boolean;
setConnectedAccountIsContract: (isContract: boolean) => void;
}

const getWalletPreferences = () => {
Expand Down Expand Up @@ -73,4 +75,8 @@ export const createWalletSlice: StateCreator<
}));
}
},
connectedAccountIsContract: false,
setConnectedAccountIsContract(isContract) {
set({ connectedAccountIsContract: isContract });
},
});
16 changes: 8 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
# yarn lockfile v1


"@aave/[email protected].1":
version "1.32.1"
resolved "https://registry.yarnpkg.com/@aave/contract-helpers/-/contract-helpers-1.32.1.tgz#ad3e216118f282f450b7ab160bd270930ccf5fa4"
integrity sha512-iYkopRnzbfnW7Pxa/qNmqVJVSCWKqF14FNK9SKpmLAkndH8P0UZqSkIdcnvW/ekNx6cyGIc506gKc00j5OnegQ==
"@aave/[email protected].2-51a91f59a8ffb3609b3e1d811454d42b056e09c0.0+0e8fe1e":
version "1.32.2-51a91f59a8ffb3609b3e1d811454d42b056e09c0.0"
resolved "https://registry.yarnpkg.com/@aave/contract-helpers/-/contract-helpers-1.32.2-51a91f59a8ffb3609b3e1d811454d42b056e09c0.0.tgz#763822a3e0f7bd0df87684c87135f7401b287db5"
integrity sha512-82MysayYNdHv4XCTqGZJTPTaf59cc0MN95b4CYMrDRnwnQHdpXPf59kZcVVfgK04X9j96uaXWXlA4IpPwcCvuA==
dependencies:
isomorphic-unfetch "^3.1.0"

"@aave/[email protected].1":
version "1.32.1"
resolved "https://registry.yarnpkg.com/@aave/math-utils/-/math-utils-1.32.1.tgz#9cd4bb9343f2fabb078b6ca61b5845c7044fe643"
integrity sha512-uLjwGNEOilWWguW7CX6Dwsk9uP4aT9HmOCzXpHqDrPbR2oGSLV8BlvHWx/8wzY9Cdt5Jr4ofP0vm/Xs4lrgDNg==
"@aave/[email protected].2-51a91f59a8ffb3609b3e1d811454d42b056e09c0.0+0e8fe1e":
version "1.32.2-51a91f59a8ffb3609b3e1d811454d42b056e09c0.0"
resolved "https://registry.yarnpkg.com/@aave/math-utils/-/math-utils-1.32.2-51a91f59a8ffb3609b3e1d811454d42b056e09c0.0.tgz#e1f2bd05df263def0278eb2ceb4c356c69e7d7fe"
integrity sha512-ZIZCAvmbtxdiBuStCgZEUuEki+CAxkTc6ivpKQByV5/jaIKWLpy3si7ksw7/UKfL5i3nxmi2wUiCKMqcam6Ymw==

"@adobe/css-tools@^4.0.1":
version "4.4.1"
Expand Down
Loading