Skip to content

Commit

Permalink
fix(network): cancel tx request if gas estimation failed
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs committed Sep 26, 2022
1 parent 204945e commit 565b37f
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions packages/network/src/createTxQueue.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { BaseContract, BigNumberish, CallOverrides, Overrides } from "ethers";
import { autorun, computed, IComputedValue, IObservableValue, observable, runInAction } from "mobx";
import { action, autorun, computed, IComputedValue, IObservableValue, observable, runInAction } from "mobx";
import { mapObject, deferred, uuid, awaitValue, cacheUntilReady, extractEncodedArguments } from "@latticexyz/utils";
import { Mutex } from "async-mutex";
import { TransactionResponse } from "@ethersproject/providers";
Expand Down Expand Up @@ -143,11 +143,19 @@ export function createTxQueue<C extends Contracts>(
let error: any;
const stateMutability = txRequest.stateMutability;

// Await gas estimation to avoid increasing nonce before tx is actually sent
let gasLimit: BigNumberish;
try {
gasLimit = await txRequest.estimateGas();
} catch (e) {
console.error("GAS ESTIMATION ERROR", e);
return txRequest.cancel();
}

// Wait if nonce is not ready
const { nonce } = await awaitValue(readyState);

try {
// Wait if nonce is not ready
const { nonce } = await awaitValue(readyState);
// Await gas estimation to avoid increasing nonce before tx is actually sent
const gasLimit = await txRequest.estimateGas();
return await txRequest.execute(nonce, gasLimit);
} catch (e: any) {
console.warn("TXQUEUE EXECUTION FAILED", e);
Expand Down

0 comments on commit 565b37f

Please sign in to comment.