Skip to content
This repository has been archived by the owner on Dec 27, 2022. It is now read-only.

Commit

Permalink
Merge pull request #660 from connext/one-confirmation
Browse files Browse the repository at this point in the history
After one confirmation, double timeout
  • Loading branch information
jakekidd authored Jun 18, 2021
2 parents 0a3e3b9 + bdee8c1 commit 1957338
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions modules/contracts/src.ts/services/ethService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import { parseUnits } from "ethers/lib/utils";
// The amount of time (ms) to wait before a confirmation polling period times out,
// indiciating we should resubmit tx with higher gas if the tx is not confirmed.
export const CONFIRMATION_TIMEOUT = 45_000;
// Multiplier for timeout once we get at least 1 confirmation.
const CONFIRMATION_MULTIPLIER = 4;
// The min percentage to bump gas.
export const GAS_BUMP_PERCENT = 20;
// 1M gas should cover all Connext txs. Gas won't exceed this amount.
Expand Down Expand Up @@ -592,8 +594,8 @@ export class EthereumChainService extends EthereumChainReader implements IVector
/**
* Will wait for any of the given TransactionResponses to return
* a receipt. Once a receipt is returned by any of the responses,
* it will wait for 10 confirmations of the given receipt against
* a timeout. If within the timeout there are *not* 10 confirmations,
* it will wait for X confirmations of the given receipt against
* a timeout. If within the timeout there are *not* X confirmations,
* the tx will be resubmitted at the same nonce.
*/
public async waitForConfirmation(chainId: number, responses: TransactionResponse[]): Promise<TransactionReceipt> {
Expand All @@ -602,6 +604,9 @@ export class EthereumChainService extends EthereumChainReader implements IVector
throw new ChainError(ChainError.reasons.ProviderNotFound);
}
const numConfirmations = getConfirmationsForChain(chainId);
// A flag for marking when we have received at least 1 confirmation. We'll extend the wait period by 2x
// if this is the case.
let receivedConfirmation: boolean = false;

// An anon fn to get the tx receipts for all responses.
// We must check for confirmation in all previous transactions. Although it's most likely
Expand All @@ -620,6 +625,8 @@ export class EthereumChainService extends EthereumChainReader implements IVector
reverted.push(r);
} else if (r.confirmations >= numConfirmations) {
return resolve(r);
} else if (r.confirmations >= 1) {
receivedConfirmation = true;
}
}
});
Expand All @@ -645,7 +652,10 @@ export class EthereumChainService extends EthereumChainReader implements IVector
// NOTE: This loop won't execute if receipt is valid (not undefined).
let timeElapsed: number = 0;
const startMark = new Date().getTime();
while (!receipt && timeElapsed < CONFIRMATION_TIMEOUT) {
while (
!receipt &&
timeElapsed < (receivedConfirmation ? CONFIRMATION_TIMEOUT * CONFIRMATION_MULTIPLIER : CONFIRMATION_TIMEOUT)
) {
receipt = await pollForReceipt();
// Update elapsed time.
timeElapsed = new Date().getTime() - startMark;
Expand Down

0 comments on commit 1957338

Please sign in to comment.