Skip to content

Commit

Permalink
Revert "Fix crash due to failed estimated gas limit (#6055)"
Browse files Browse the repository at this point in the history
This reverts commit 56e9585.
  • Loading branch information
ibrahimtaveras00 committed Sep 20, 2024
1 parent 85fd901 commit b886ba3
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 89 deletions.
18 changes: 2 additions & 16 deletions src/__swaps__/screens/Swap/hooks/useEstimatedGasFee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,9 @@ function safeBigInt(value: string) {
}
}

const isFeeNaN = (value: string | undefined) => isNaN(Number(value)) || typeof value === 'undefined';

export function calculateGasFee(gasSettings: GasSettings, gasLimit: string) {
if (gasSettings.isEIP1559) {
if (isFeeNaN(gasSettings.maxBaseFee) || isFeeNaN(gasSettings.maxPriorityFee)) {
return null;
}

return add(gasSettings.maxBaseFee, gasSettings.maxPriorityFee);
}

if (isFeeNaN(gasSettings.gasPrice)) {
return null;
}

return multiply(gasLimit, gasSettings.gasPrice);
const amount = gasSettings.isEIP1559 ? add(gasSettings.maxBaseFee, gasSettings.maxPriorityFee || '0') : gasSettings.gasPrice;
return multiply(gasLimit, amount);
}

export function useEstimatedGasFee({
Expand All @@ -53,7 +40,6 @@ export function useEstimatedGasFee({
if (!gasLimit || !gasSettings || !nativeNetworkAsset?.price) return;

const fee = calculateGasFee(gasSettings, gasLimit);
if (!fee) return;

const networkAssetPrice = nativeNetworkAsset.price.value?.toString();
if (!networkAssetPrice) return `${formatNumber(weiToGwei(fee))} Gwei`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,28 +79,10 @@ export const SyncQuoteSharedValuesToState = () => {
return null;
};

const isFeeNaNWorklet = (value: string | undefined) => {
'worklet';

return isNaN(Number(value)) || typeof value === 'undefined';
};

export function calculateGasFeeWorklet(gasSettings: GasSettings, gasLimit: string) {
'worklet';

if (gasSettings.isEIP1559) {
if (isFeeNaNWorklet(gasSettings.maxBaseFee) || isFeeNaNWorklet(gasSettings.maxPriorityFee)) {
return null;
}

return sumWorklet(gasSettings.maxBaseFee || '0', gasSettings.maxPriorityFee || '0');
}

if (isFeeNaNWorklet(gasSettings.gasPrice)) {
return null;
}

return mulWorklet(gasLimit, gasSettings.gasPrice);
const amount = gasSettings.isEIP1559 ? sumWorklet(gasSettings.maxBaseFee, gasSettings.maxPriorityFee || '0') : gasSettings.gasPrice;
return mulWorklet(gasLimit, amount);
}

export function formatUnitsWorklet(value: string, decimals: number) {
Expand Down Expand Up @@ -184,23 +166,12 @@ export function SyncGasStateToSharedValues() {
hasEnoughFundsForGas.value = undefined;
if (!gasSettings || !estimatedGasLimit || !quote || 'error' in quote || isLoadingNativeNetworkAsset) return;

// NOTE: if we don't have a gas price or max base fee or max priority fee, we can't calculate the gas fee
if (
(gasSettings.isEIP1559 && !(gasSettings.maxBaseFee || gasSettings.maxPriorityFee)) ||
(!gasSettings.isEIP1559 && !gasSettings.gasPrice)
) {
return;
}

if (!userNativeNetworkAsset) {
hasEnoughFundsForGas.value = false;
return;
}

const gasFee = calculateGasFeeWorklet(gasSettings, estimatedGasLimit);
if (gasFee === null || isNaN(Number(gasFee))) {
return;
}

const nativeGasFee = divWorklet(gasFee, powWorklet(10, userNativeNetworkAsset.decimals));

Expand Down
4 changes: 0 additions & 4 deletions src/handlers/web3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,10 +329,6 @@ export async function estimateGasWithPadding(
? contractCallEstimateGas(...(callArguments ?? []), txPayloadToEstimate)
: p.estimateGas(cleanTxPayload));

if (!BigNumber.isBigNumber(estimatedGas)) {
throw new Error('Invalid gas limit type');
}

const lastBlockGasLimit = addBuffer(gasLimit.toString(), 0.9);
const paddedGas = addBuffer(estimatedGas.toString(), paddingFactor.toString());
logger.debug('[web3]: ⛽ GAS CALCULATIONS!', {
Expand Down
8 changes: 2 additions & 6 deletions src/raps/actions/crosschainSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,9 @@ export const estimateCrosschainSwapGasLimit = async ({
SWAP_GAS_PADDING
);

if (gasLimit === null || gasLimit === undefined || isNaN(Number(gasLimit))) {
return getCrosschainSwapDefaultGasLimit(quote) || getDefaultGasLimitForTrade(quote, chainId);
}

return gasLimit;
return gasLimit || getCrosschainSwapDefaultGasLimit(quote);
} catch (error) {
return getCrosschainSwapDefaultGasLimit(quote) || getDefaultGasLimitForTrade(quote, chainId);
return getCrosschainSwapDefaultGasLimit(quote);
}
};

Expand Down
13 changes: 3 additions & 10 deletions src/raps/actions/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,9 @@ export const estimateSwapGasLimit = async ({
WRAP_GAS_PADDING
);

if (gasLimit === null || gasLimit === undefined || isNaN(Number(gasLimit))) {
return quote?.defaultGasLimit || default_estimate;
}

return gasLimit;
return gasLimit || String(quote?.defaultGasLimit) || String(default_estimate);
} catch (e) {
return quote?.defaultGasLimit || default_estimate;
return String(quote?.defaultGasLimit) || String(default_estimate);
}
// Swap
} else {
Expand All @@ -120,11 +116,8 @@ export const estimateSwapGasLimit = async ({
}

const gasLimit = await estimateGasWithPadding(params, method, methodArgs, provider, SWAP_GAS_PADDING);
if (gasLimit === null || gasLimit === undefined || isNaN(Number(gasLimit))) {
return getDefaultGasLimitForTrade(quote, chainId);
}

return gasLimit;
return gasLimit || getDefaultGasLimitForTrade(quote, chainId);
} catch (error) {
return getDefaultGasLimitForTrade(quote, chainId);
}
Expand Down
7 changes: 1 addition & 6 deletions src/raps/actions/unlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,7 @@ export const estimateApprove = async ({
const gasLimit = await tokenContract.estimateGas.approve(spender, MaxUint256, {
from: owner,
});

if (gasLimit === null || gasLimit === undefined || isNaN(Number(gasLimit.toString()))) {
return `${gasUnits.basic_approval}`;
}

return gasLimit.toString();
return gasLimit ? gasLimit.toString() : `${gasUnits.basic_approval}`;
} catch (error) {
logger.error(new RainbowError('[raps/unlock]: error estimateApprove'), {
message: (error as Error)?.message,
Expand Down
11 changes: 3 additions & 8 deletions src/raps/unlockAndCrosschainSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ export const estimateUnlockAndCrosschainSwap = async ({
});
}

let unlockGasLimit;

if (swapAssetNeedsUnlocking) {
const unlockGasLimit = await estimateApprove({
unlockGasLimit = await estimateApprove({
owner: accountAddress,
tokenAddress: sellTokenAddress,
spender: allowanceTarget,
Expand All @@ -69,14 +71,7 @@ export const estimateUnlockAndCrosschainSwap = async ({
quote,
});

if (swapGasLimit === null || swapGasLimit === undefined || isNaN(Number(swapGasLimit))) {
return null;
}

const gasLimit = gasLimits.concat(swapGasLimit).reduce((acc, limit) => add(acc, limit), '0');
if (isNaN(Number(gasLimit))) {
return null;
}

return gasLimit.toString();
};
Expand Down
14 changes: 6 additions & 8 deletions src/raps/unlockAndSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const estimateUnlockAndSwap = async ({
let swapAssetNeedsUnlocking = false;

const nativeAsset = isLowerCaseMatch(ETH_ADDRESS_AGGREGATOR, sellTokenAddress) || isNativeAsset(sellTokenAddress, chainId);

if (!isNativeAssetUnwrapping && !nativeAsset) {
swapAssetNeedsUnlocking = await assetNeedsUnlocking({
owner: accountAddress,
Expand All @@ -64,8 +65,12 @@ export const estimateUnlockAndSwap = async ({
if (gasLimitFromMetadata) {
return gasLimitFromMetadata;
}
}

const unlockGasLimit = await estimateApprove({
let unlockGasLimit;

if (swapAssetNeedsUnlocking) {
unlockGasLimit = await estimateApprove({
owner: accountAddress,
tokenAddress: sellTokenAddress,
spender: getRainbowRouterContractAddress(chainId),
Expand All @@ -80,14 +85,7 @@ export const estimateUnlockAndSwap = async ({
quote,
});

if (swapGasLimit === null || swapGasLimit === undefined || isNaN(Number(swapGasLimit))) {
return null;
}

const gasLimit = gasLimits.concat(swapGasLimit).reduce((acc, limit) => add(acc, limit), '0');
if (isNaN(Number(gasLimit))) {
return null;
}

return gasLimit.toString();
};
Expand Down

0 comments on commit b886ba3

Please sign in to comment.