Skip to content

Commit

Permalink
Merge pull request #7 from lidofinance/feature/ethui-483-fix-usemaxga…
Browse files Browse the repository at this point in the history
…sprice-overcharge

feat: switch to our own getFeeData
  • Loading branch information
itaven authored Aug 15, 2023
2 parents 9f6cfe3 + e8bee81 commit b4c6a74
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 111 deletions.
18 changes: 4 additions & 14 deletions features/home/stake-form/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { AddressZero } from '@ethersproject/constants';
import { useLidoSWR, useSTETHContractRPC } from '@lido-sdk/react';
import {
ESTIMATE_ACCOUNT,
getBackendRPCPath,
STETH_SUBMIT_GAS_LIMIT_DEFAULT,
} from 'config';
import { ESTIMATE_ACCOUNT, STETH_SUBMIT_GAS_LIMIT_DEFAULT } from 'config';
import { parseEther } from '@ethersproject/units';
import { useWeb3 } from 'reef-knot/web3-react';
import { getStaticRpcBatchProvider } from 'utils/rpcProviders';
import { BigNumber } from 'ethers';
import { CHAINS } from 'utils/chains';
import { getFeeData } from 'utils/getFeeData';
import { CHAINS } from '@lido-sdk/constants';

type UseStethSubmitGasLimit = () => number | undefined;

Expand All @@ -24,13 +20,7 @@ export const useStethSubmitGasLimit: UseStethSubmitGasLimit = () => {
return;
}

const provider = getStaticRpcBatchProvider(
chainId as string,
// TODO: add a way to type useWeb3 hook
getBackendRPCPath(chainId as CHAINS),
);

const feeData = await provider.getFeeData();
const feeData = await getFeeData(chainId as CHAINS);
const maxPriorityFeePerGas = feeData.maxPriorityFeePerGas ?? undefined;
const maxFeePerGas = feeData.maxFeePerGas ?? undefined;

Expand Down
26 changes: 10 additions & 16 deletions features/home/stake-form/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { getBackendRPCPath } from 'config';
import { TX_STAGE } from 'shared/components';
import { BigNumber } from 'ethers';
import invariant from 'tiny-invariant';
import { getFeeData } from 'utils/getFeeData';
import type { Web3Provider } from '@ethersproject/providers';

const SUBMIT_EXTRA_GAS_TRANSACTION_RATIO = 1.05;
Expand Down Expand Up @@ -79,15 +80,12 @@ export const stakeProcessing: StakeProcessingProps = async (
refFromQuery,
isMultisig,
) => {
if (!stethContractWeb3 || !chainId) {
return;
}

invariant(providerWeb3, 'must have providerWeb3');

try {
const referralAddress = await getAddress(refFromQuery, chainId);
invariant(stethContractWeb3);
invariant(chainId);
invariant(providerWeb3);

const referralAddress = await getAddress(refFromQuery, chainId);
const callback = async () => {
if (isMultisig) {
const tx = await stethContractWeb3.populateTransaction.submit(
Expand All @@ -98,17 +96,13 @@ export const stakeProcessing: StakeProcessingProps = async (
);
return providerWeb3.getSigner().sendUncheckedTransaction(tx);
} else {
const provider = getStaticRpcBatchProvider(
chainId,
getBackendRPCPath(chainId),
);

const feeData = await provider.getFeeData();

const feeData = await getFeeData(chainId);
const maxPriorityFeePerGas = feeData.maxPriorityFeePerGas ?? undefined;
const maxFeePerGas = feeData.maxFeePerGas ?? undefined;
const overrides = {
value: parseEther(inputValue),
maxPriorityFeePerGas: feeData.maxPriorityFeePerGas ?? undefined,
maxFeePerGas: feeData.maxFeePerGas ?? undefined,
maxPriorityFeePerGas,
maxFeePerGas,
};

const originalGasLimit = await stethContractWeb3.estimateGas.submit(
Expand Down
9 changes: 5 additions & 4 deletions features/withdrawals/hooks/contract/useRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { useWithdrawals } from 'features/withdrawals/contexts/withdrawals-contex

import { useWithdrawalsContract } from './useWithdrawalsContract';
import { useApprove } from 'shared/hooks/useApprove';
import { getFeeData } from 'utils/getFeeData';
import { Zero } from '@ethersproject/constants';
import { TokensWithdrawable } from 'features/withdrawals/types/tokens-withdrawable';

Expand Down Expand Up @@ -58,7 +59,7 @@ const useWithdrawalRequestMethods = () => {
},
] as const;

const feeData = await contractWeb3.provider.getFeeData();
const feeData = await getFeeData(chainId);
const maxFeePerGas = feeData.maxFeePerGas ?? undefined;
const maxPriorityFeePerGas = feeData.maxPriorityFeePerGas ?? undefined;
const gasLimit =
Expand Down Expand Up @@ -114,7 +115,7 @@ const useWithdrawalRequestMethods = () => {
},
] as const;

const feeData = await contractWeb3.provider.getFeeData();
const feeData = await getFeeData(chainId);
const maxFeePerGas = feeData.maxFeePerGas ?? undefined;
const maxPriorityFeePerGas = feeData.maxPriorityFeePerGas ?? undefined;
const gasLimit =
Expand Down Expand Up @@ -169,7 +170,7 @@ const useWithdrawalRequestMethods = () => {
);
return providerWeb3?.getSigner().sendUncheckedTransaction(tx);
} else {
const feeData = await contractWeb3.provider.getFeeData();
const feeData = await getFeeData(chainId);
const maxFeePerGas = feeData.maxFeePerGas ?? undefined;
const maxPriorityFeePerGas =
feeData.maxPriorityFeePerGas ?? undefined;
Expand Down Expand Up @@ -224,7 +225,7 @@ const useWithdrawalRequestMethods = () => {
);
return providerWeb3?.getSigner().sendUncheckedTransaction(tx);
} else {
const feeData = await contractWeb3.provider.getFeeData();
const feeData = await getFeeData(chainId);
const maxFeePerGas = feeData.maxFeePerGas ?? undefined;
const maxPriorityFeePerGas =
feeData.maxPriorityFeePerGas ?? undefined;
Expand Down
13 changes: 4 additions & 9 deletions features/wrap/features/unwrap-form/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { parseEther } from '@ethersproject/units';
import { getStaticRpcBatchProvider } from 'utils/rpcProviders';
import { useLidoSWR, useWSTETHContractRPC } from '@lido-sdk/react';
import { useWeb3 } from 'reef-knot/web3-react';
import { ESTIMATE_ACCOUNT, getBackendRPCPath, UNWRAP_GAS_LIMIT } from 'config';
import { ESTIMATE_ACCOUNT, UNWRAP_GAS_LIMIT } from 'config';
import { BigNumber } from 'ethers';
import { getFeeData } from 'utils/getFeeData';
import { CHAINS } from '@lido-sdk/constants';

export const useUnwrapGasLimit = () => {
const wsteth = useWSTETHContractRPC();
Expand All @@ -16,13 +17,7 @@ export const useUnwrapGasLimit = () => {
return;
}

const provider = getStaticRpcBatchProvider(
// TODO: add a way to type useWeb3 hook
chainId as number,
getBackendRPCPath(chainId as number),
);

const feeData = await provider.getFeeData();
const feeData = await getFeeData(chainId as CHAINS);
const maxPriorityFeePerGas = feeData.maxPriorityFeePerGas ?? undefined;
const maxFeePerGas = feeData.maxFeePerGas ?? undefined;

Expand Down
11 changes: 2 additions & 9 deletions features/wrap/features/wrap-form/hooks.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parseEther } from '@ethersproject/units';
import { CHAINS } from '@lido-sdk/constants';

import { getStaticRpcBatchProvider } from '@lido-sdk/providers';
import {
useLidoSWR,
Expand All @@ -17,6 +17,7 @@ import {
} from 'config';
import { BigNumber } from 'ethers';
import { STRATEGY_IMMUTABLE } from 'utils/swrStrategies';
import { CHAINS } from '@lido-sdk/constants';

export const useApproveGasLimit = () => {
const steth = useSTETHContractRPC();
Expand Down Expand Up @@ -63,18 +64,12 @@ export const useWrapGasLimit = (fromEther: boolean) => {
getBackendRPCPath(chainId as CHAINS),
);

const feeData = await provider.getFeeData();
const maxPriorityFeePerGas = feeData.maxPriorityFeePerGas ?? undefined;
const maxFeePerGas = feeData.maxFeePerGas ?? undefined;

if (fromEther) {
const gasLimit = await provider
.estimateGas({
from: ESTIMATE_ACCOUNT,
to: wsteth.address,
value: parseEther('0.001'),
maxPriorityFeePerGas,
maxFeePerGas,
})
.catch((error) => {
console.warn(error);
Expand All @@ -86,8 +81,6 @@ export const useWrapGasLimit = (fromEther: boolean) => {
const gasLimit = await wsteth.estimateGas
.wrap(parseEther('0.0001'), {
from: ESTIMATE_ACCOUNT,
maxPriorityFeePerGas,
maxFeePerGas,
})
.catch((error) => {
console.warn(error);
Expand Down
41 changes: 15 additions & 26 deletions features/wrap/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { parseEther } from '@ethersproject/units';
import { WstethAbi } from '@lido-sdk/contracts';
import { getTokenAddress, TOKENS } from '@lido-sdk/constants';
import { CHAINS, getTokenAddress, TOKENS } from '@lido-sdk/constants';
import { TX_STAGE } from 'shared/components';
import { getErrorMessage, runWithTransactionLogger } from 'utils';
import { getStaticRpcBatchProvider } from 'utils/rpcProviders';
import { getBackendRPCPath } from 'config';
import { getFeeData } from 'utils/getFeeData';
import invariant from 'tiny-invariant';
import type { Web3Provider } from '@ethersproject/providers';

Expand Down Expand Up @@ -41,10 +40,8 @@ export const unwrapProcessing: UnwrapProcessingProps = async (
resetForm,
isMultisig,
) => {
if (!wstethContractWeb3 || !chainId) {
return;
}

invariant(wstethContractWeb3, 'must have wstethContractWeb3');
invariant(chainId, 'must have chain id');
invariant(providerWeb3, 'must have providerWeb3');

try {
Expand All @@ -55,14 +52,12 @@ export const unwrapProcessing: UnwrapProcessingProps = async (
);
return providerWeb3.getSigner().sendUncheckedTransaction(tx);
} else {
const provider = getStaticRpcBatchProvider(
chainId,
getBackendRPCPath(chainId),
const { maxFeePerGas, maxPriorityFeePerGas } = await getFeeData(
chainId as CHAINS,
);
const feeData = await provider.getFeeData();
return wstethContractWeb3.unwrap(parseEther(inputValue), {
maxPriorityFeePerGas: feeData.maxPriorityFeePerGas ?? undefined,
maxFeePerGas: feeData.maxFeePerGas ?? undefined,
maxPriorityFeePerGas: maxPriorityFeePerGas ?? undefined,
maxFeePerGas: maxFeePerGas ?? undefined,
});
}
};
Expand Down Expand Up @@ -146,33 +141,27 @@ export const wrapProcessingWithApprove: WrapProcessingWithApproveProps = async (
approve,
resetForm,
) => {
if (!chainId || !wstethContractWeb3) {
return;
}

invariant(providerWeb3, 'must have providerWeb3');

const wstethTokenAddress = getTokenAddress(chainId, TOKENS.WSTETH);

const handleEnding = () => {
resetForm();
ethBalanceUpdate();
stethBalanceUpdate();
};

const getGasParameters = async () => {
const provider = getStaticRpcBatchProvider(
chainId,
getBackendRPCPath(chainId),
);
const feeData = await provider.getFeeData();
const feeData = await getFeeData(chainId as CHAINS);
return {
maxPriorityFeePerGas: feeData.maxPriorityFeePerGas ?? undefined,
maxFeePerGas: feeData.maxFeePerGas ?? undefined,
};
};

try {
invariant(providerWeb3, 'must have providerWeb3');
invariant(wstethContractWeb3, 'must have wstethContractWeb3');
invariant(chainId, 'must have chain id');

const wstethTokenAddress = getTokenAddress(chainId, TOKENS.WSTETH);

if (selectedToken === ETH) {
const callback = async () => {
if (isMultisig) {
Expand Down
1 change: 1 addition & 0 deletions pages/api/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const rpc = rpcFactory({
'eth_getCode',
'eth_estimateGas',
'eth_getBlockByNumber',
'eth_feeHistory',
'eth_getBalance',
'eth_blockNumber',
'eth_getTransactionByHash',
Expand Down
11 changes: 2 additions & 9 deletions shared/components/token-to-wallet/token-to-wallet.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ToastInfo, Tooltip } from '@lidofinance/lido-ui';
import { Tooltip } from '@lidofinance/lido-ui';
import { useTokenToWallet } from '@lido-sdk/react';
import { TokenToWalletStyle } from './styles';

Expand All @@ -12,16 +12,9 @@ export const TokenToWallet: TokenToWalletComponent = (props) => {

if (!addToken) return null;

const onClickHandler = async () => {
const result = await addToken();
if (!result) return;

ToastInfo('Tokens were successfully added to your wallet', {});
};

return (
<Tooltip placement="bottomLeft" title="Add tokens to wallet">
<TokenToWalletStyle tabIndex={-1} onClick={onClickHandler} {...rest} />
<TokenToWalletStyle tabIndex={-1} onClick={addToken} {...rest} />
</Tooltip>
);
};
16 changes: 10 additions & 6 deletions shared/hooks/useApprove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { getERC20Contract } from '@lido-sdk/contracts';
import { Zero } from '@ethersproject/constants';
import { useAllowance, useMountedState, useSDK } from '@lido-sdk/react';
import { isContract } from 'utils/isContract';
import { getFeeData } from 'utils/getFeeData';
import { CHAINS } from '@lido-sdk/constants';

type TransactionCallback = () => Promise<ContractTransaction | string>;

Expand Down Expand Up @@ -37,7 +39,7 @@ export const useApprove = (
owner?: string,
wrapper: UseApproveWrapper = defaultWrapper,
): UseApproveResponse => {
const { providerWeb3, account } = useSDK();
const { providerWeb3, account, chainId } = useSDK();
const mergedOwner = owner ?? account;

invariant(token != null, 'Token is required');
Expand All @@ -58,6 +60,7 @@ export const useApprove = (
try {
setApproving(true);
invariant(providerWeb3 != null, 'Web3 provider is required');
invariant(chainId, 'chain id is required');
invariant(account, 'account is required');
const contractWeb3 = getERC20Contract(token, providerWeb3.getSigner());
const isMultisig = await isContract(account, providerWeb3);
Expand All @@ -72,9 +75,9 @@ export const useApprove = (
.sendUncheckedTransaction(tx);
return hash;
} else {
const feeData = await providerWeb3
.getFeeData()
.catch((error) => console.warn(error));
const feeData = await getFeeData(chainId as CHAINS).catch((error) =>
console.warn(error),
);
const maxPriorityFeePerGas =
feeData?.maxPriorityFeePerGas ?? undefined;
const maxFeePerGas = feeData?.maxFeePerGas ?? undefined;
Expand All @@ -90,10 +93,11 @@ export const useApprove = (
setApproving(false);
}
}, [
providerWeb3,
token,
setApproving,
providerWeb3,
chainId,
account,
token,
wrapper,
updateAllowance,
spender,
Expand Down
Loading

0 comments on commit b4c6a74

Please sign in to comment.