Skip to content
Merged
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
4 changes: 2 additions & 2 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.33.0",
"@aave/math-utils": "1.33.0",
"@aave/contract-helpers": "1.33.1",
"@aave/math-utils": "1.33.1",
"@bgd-labs/aave-address-book": "^4.17.1",
"@emotion/cache": "11.10.3",
"@emotion/react": "11.10.4",
Expand Down
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
19 changes: 18 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 All @@ -56,6 +60,8 @@ export const Web3ContextProvider: React.FC<{ children: ReactElement }> = ({ chil
currentAccount = readOnlyModeAddress;
}

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

useEffect(() => {
if (didInit) {
return;
Expand Down Expand Up @@ -179,6 +185,17 @@ export const Web3ContextProvider: React.FC<{ children: ReactElement }> = ({ chil
}
}, [readOnlyModeAddress, setAccount]);

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

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].0":
version "1.33.0"
resolved "https://registry.yarnpkg.com/@aave/contract-helpers/-/contract-helpers-1.33.0.tgz#d58f44c83737dfe3ab04b3776f1133064e08ebc6"
integrity sha512-hwOG+B6LYv8/oZIW3OYPZqr2OwEVYLWXO230h3LfUpG9P4+MhknZeyuJHRA7uGNpj2dpP/KYuLrYYfx4G9D0aQ==
"@aave/[email protected].1":
version "1.33.1"
resolved "https://registry.yarnpkg.com/@aave/contract-helpers/-/contract-helpers-1.33.1.tgz#2156c179e802c40c137381d337c5f93ebee1abeb"
integrity sha512-Zu/zwJiX3k2xBRAlNfmIJMIo19YWVJAL2uRPHbvCVxOUgzlhVyglIF98FgcZ+4aLwcjvSAw10wnIzGgJcPTNVw==
dependencies:
isomorphic-unfetch "^3.1.0"

"@aave/[email protected].0":
version "1.33.0"
resolved "https://registry.yarnpkg.com/@aave/math-utils/-/math-utils-1.33.0.tgz#2386f7353dfdc7bdd88079e4fd7351275b1cdbf6"
integrity sha512-guDUruuQTmp8QLUNRZJFxzu/VyJPs0Mieebk+tJDplVNq+TzaSZHU3YtAxp/u6tGw166v47ALFrt7yA/oaX1TA==
"@aave/[email protected].1":
version "1.33.1"
resolved "https://registry.yarnpkg.com/@aave/math-utils/-/math-utils-1.33.1.tgz#1dddf06e48cca13251db94e2f24afb0aa1b270ed"
integrity sha512-+e7ThIRLxyrcLB507T3khdE6DqKn2vTR5hfnQd1rHetEzLObA6bKDElX7YEtGmuSzBwDTP4inpK2VtgiQAqIWQ==

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