Skip to content

Commit

Permalink
Revert "improve gas estimates (#4922)"
Browse files Browse the repository at this point in the history
This reverts commit d7c5eb8.
  • Loading branch information
Ibrahim Taveras committed Jul 7, 2023
1 parent ace1312 commit 5622bbf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 45 deletions.
12 changes: 5 additions & 7 deletions src/handlers/web3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,6 @@ export async function estimateGasWithPadding(
provider: StaticJsonRpcProvider | null = null,
paddingFactor: number = 1.1
): Promise<string | null> {
let estimatedGasLimit = ethUnits.basic_tx.toString();
try {
const p = provider || web3Provider;
if (!p) {
Expand Down Expand Up @@ -368,8 +367,6 @@ export async function estimateGasWithPadding(

logger.info('⛽ Calculating safer gas limit for last block');
// 3 - If it is a contract, call the RPC method `estimateGas` with a safe value
const lastBlockGasLimit = addBuffer(gasLimit.toString(), 0.9);
estimatedGasLimit = lastBlockGasLimit.toString();
const saferGasLimit = fraction(gasLimit.toString(), 19, 20);
logger.info('⛽ safer gas limit for last block is', { saferGasLimit });

Expand All @@ -381,6 +378,7 @@ export async function estimateGasWithPadding(
? contractCallEstimateGas(...(callArguments ?? []), txPayloadToEstimate)
: p.estimateGas(txPayloadToEstimate));

const lastBlockGasLimit = addBuffer(gasLimit.toString(), 0.9);
const paddedGas = addBuffer(
estimatedGas.toString(),
paddingFactor.toString()
Expand All @@ -397,24 +395,24 @@ export async function estimateGasWithPadding(
logger.info('⛽ returning orginal gas estimation', {
esimatedGas: estimatedGas.toString(),
});
estimatedGasLimit = estimatedGas.toString();
return estimatedGas.toString();
}
// If the estimation is below the last block gas limit, use the padded estimate
if (greaterThan(lastBlockGasLimit, paddedGas)) {
logger.info('⛽ returning padded gas estimation', { paddedGas });
estimatedGasLimit = paddedGas;
return paddedGas;
}
// otherwise default to the last block gas limit
logger.info('⛽ returning last block gas limit', { lastBlockGasLimit });
return estimatedGasLimit;
return lastBlockGasLimit;
} catch (e: any) {
/*
* Reported ~400x per day, but if it's not actionable it might as well be a warning.
*/
logger.warn('Error calculating gas limit with padding', {
message: e.message,
});
return estimatedGasLimit;
return null;
}
}

Expand Down
63 changes: 25 additions & 38 deletions src/screens/TransactionConfirmationScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,12 +538,7 @@ export default function TransactionConfirmationScreen() {
{ gas },
logger.DebugContext.walletconnect
);
const rawGasLimit = await estimateGasWithPadding(
txPayload,
null,
null,
provider
);
const rawGasLimit = await estimateGas(txPayload, provider);
logger.debug(
'WC: Estimated gas limit',
{ rawGasLimit },
Expand Down Expand Up @@ -593,7 +588,6 @@ export default function TransactionConfirmationScreen() {
params,
provider,
updateTxFee,
currentNetwork,
]);

const walletBalance = useMemo(() => {
Expand Down Expand Up @@ -677,28 +671,30 @@ export default function TransactionConfirmationScreen() {
logger.DebugContext.walletconnect
);

// Estimate the tx with gas limit padding before sending
const rawGasLimit = await estimateGasWithPadding(
txPayload,
null,
null,
provider
);

// If the estimation with padding is higher or gas limit was missing,
// let's use the higher value
if (
(isNil(gas) && isNil(gasLimitFromPayload)) ||
(!isNil(gas) && greaterThan(rawGasLimit, convertHexToString(gas))) ||
(!isNil(gasLimitFromPayload) &&
greaterThan(rawGasLimit, convertHexToString(gasLimitFromPayload)))
) {
logger.debug(
'WC: using padded estimation!',
{ gas: rawGasLimit.toString() },
logger.DebugContext.walletconnect
if (currentNetwork === networkTypes.mainnet) {
// Estimate the tx with gas limit padding before sending
const rawGasLimit = await estimateGasWithPadding(
txPayload,
null,
null,
provider
);
gas = toHex(rawGasLimit);

// If the estimation with padding is higher or gas limit was missing,
// let's use the higher value
if (
(isNil(gas) && isNil(gasLimitFromPayload)) ||
(!isNil(gas) && greaterThan(rawGasLimit, convertHexToString(gas))) ||
(!isNil(gasLimitFromPayload) &&
greaterThan(rawGasLimit, convertHexToString(gasLimitFromPayload)))
) {
logger.debug(
'WC: using padded estimation!',
{ gas: rawGasLimit.toString() },
logger.DebugContext.walletconnect
);
gas = toHex(rawGasLimit);
}
}
} catch (error) {
logger.error(new RainbowError('WC: error estimating gas'), { error });
Expand All @@ -710,16 +706,7 @@ export default function TransactionConfirmationScreen() {
'maxPriorityFeePerGas',
]);
const gasParams = parseGasParamsForTransaction(selectedGasFee);

// we us whichever gasLimit is larger so we dont have any failed txs
let calculatedGasLimit = gas || 0;
if (greaterThan(gasLimit, calculatedGasLimit)) {
calculatedGasLimit = gasLimit;
}
if (greaterThan(gasLimitFromPayload, calculateGasLimit)) {
calculatedGasLimit = gasLimitFromPayload;
}

const calculatedGasLimit = gas || gasLimitFromPayload || gasLimit;
const nonce = await getNextNonce();
let txPayloadUpdated = {
...cleanTxPayload,
Expand Down

0 comments on commit 5622bbf

Please sign in to comment.