Skip to content

Commit

Permalink
TW-1622 Implement estimation retry if 'gas_exhausted' error is received
Browse files Browse the repository at this point in the history
  • Loading branch information
keshan3262 committed Dec 26, 2024
1 parent 316ddb9 commit 958ca58
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export const searchHotkey = ` (${isMacOS ? '⌘' : 'Ctrl + '}K)`;

export const LIQUIDITY_BAKING_DEX_ADDRESS = 'KT1TxqZ8QtKvLu3V3JH7Gx58n7Co8pgtpQU5';

export const FEE_PER_GAS_UNIT = 0.1;

export const THEME_COLOR_SEARCH_PARAM_NAME = 'tc';
export const FONT_SIZE_SEARCH_PARAM_NAME = 'fs';
export const LINE_HEIGHT_SEARCH_PARAM_NAME = 'lh';
43 changes: 40 additions & 3 deletions src/lib/temple/back/dryrun.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { localForger } from '@taquito/local-forging';
import { ForgeOperationsParams } from '@taquito/rpc';
import { Estimate, TezosToolkit } from '@taquito/taquito';
import { Estimate, TezosOperationError, TezosToolkit } from '@taquito/taquito';

import { FEE_PER_GAS_UNIT } from 'lib/constants';
import { formatOpParamsBeforeSend, michelEncoder, loadFastRpcClient } from 'lib/temple/helpers';
import { ReadOnlySigner } from 'lib/temple/read-only-signer';

Expand All @@ -10,6 +11,8 @@ type DryRunParams = {
networkRpc: string;
sourcePkh: string;
sourcePublicKey: string;
attemptCounter?: number;
prevFailedOperationIndex?: number;
};

export interface DryRunResult {
Expand All @@ -26,7 +29,9 @@ export async function dryRunOpParams({
opParams,
networkRpc,
sourcePkh,
sourcePublicKey
sourcePublicKey,
attemptCounter = 0,
prevFailedOperationIndex = -1
}: DryRunParams): Promise<DryRunResult | null> {
try {
const tezos = new TezosToolkit(loadFastRpcClient(networkRpc));
Expand All @@ -46,6 +51,36 @@ export async function dryRunOpParams({
const [estimationResult] = await Promise.allSettled([tezos.estimate.batch(formatted)]);
const [contractBatchResult] = await Promise.allSettled([tezos.contract.batch(formatted).send()]);
if (estimationResult.status === 'rejected' && contractBatchResult.status === 'rejected') {
if (
estimationResult.reason instanceof TezosOperationError &&
estimationResult.reason.errors.some(error => error.id.includes('gas_exhausted'))
) {
const { operationsWithResults } = estimationResult.reason;
const firstSkippedOperationIndex = operationsWithResults.findIndex(
op =>
'metadata' in op && 'operation_result' in op.metadata && op.metadata.operation_result.status === 'skipped'
);
// An internal operation of this operation may be marked as failed but this one as backtracked
const failedOperationIndex =
firstSkippedOperationIndex === -1 ? operationsWithResults.length - 1 : firstSkippedOperationIndex - 1;
const failedOperationWithResult = operationsWithResults[failedOperationIndex];
if ('gas_limit' in failedOperationWithResult) {
const newOpParams = Array.from(opParams);
newOpParams[failedOperationIndex].gasLimit =
Math.max(opParams[failedOperationIndex].gasLimit ?? 0, Number(failedOperationWithResult.gas_limit)) * 2;

if (attemptCounter < 3) {
return dryRunOpParams({
opParams: newOpParams,
networkRpc,
sourcePkh,
sourcePublicKey,
attemptCounter: failedOperationIndex > prevFailedOperationIndex ? 0 : attemptCounter + 1,
prevFailedOperationIndex: Math.max(failedOperationIndex, prevFailedOperationIndex)
});
}
}
}
error = [
{ ...estimationResult.reason, isError: true },
{ ...contractBatchResult.reason, isError: true }
Expand All @@ -61,7 +96,9 @@ export async function dryRunOpParams({
consumedMilligas: e.consumedMilligas,
gasLimit: e.gasLimit,
minimalFeeMutez: e.minimalFeeMutez,
suggestedFeeMutez: e.suggestedFeeMutez,
suggestedFeeMutez:
e.suggestedFeeMutez +
(opParams[i]?.gasLimit ? Math.ceil((opParams[i].gasLimit - e.gasLimit) * FEE_PER_GAS_UNIT) : 0),
storageLimit: opParams[i]?.storageLimit ? +opParams[i].storageLimit : e.storageLimit,
totalCost: e.totalCost,
usingBaseFeeMutez: e.usingBaseFeeMutez
Expand Down

0 comments on commit 958ca58

Please sign in to comment.