From 6fd79da8ac008b1df95df4e5bbc0c31815b352d1 Mon Sep 17 00:00:00 2001 From: Rahul Patni Date: Thu, 21 Mar 2024 14:27:16 -0700 Subject: [PATCH] accounting for 0 spendposttime --- .../src/commands/wallet/notes/combine.ts | 19 +++-- ironfish-cli/src/utils/timer.ts | 85 ------------------- ironfish-cli/src/utils/transaction.ts | 14 ++- 3 files changed, 25 insertions(+), 93 deletions(-) delete mode 100644 ironfish-cli/src/utils/timer.ts diff --git a/ironfish-cli/src/commands/wallet/notes/combine.ts b/ironfish-cli/src/commands/wallet/notes/combine.ts index a3fd8824a9..b94f2fd89f 100644 --- a/ironfish-cli/src/commands/wallet/notes/combine.ts +++ b/ironfish-cli/src/commands/wallet/notes/combine.ts @@ -19,8 +19,11 @@ import { getExplorer } from '../../../utils/explorer' import { selectFee } from '../../../utils/fees' import { fetchNotes } from '../../../utils/note' import { getSpendPostTimeInMs } from '../../../utils/spendPostTime' -import { TransactionTimer } from '../../../utils/timer' -import { displayTransactionSummary, watchTransaction } from '../../../utils/transaction' +import { + displayTransactionSummary, + TransactionTimer, + watchTransaction, +} from '../../../utils/transaction' export class CombineNotesCommand extends IronfishCommand { static description = `Combine notes into a single note` @@ -300,11 +303,13 @@ export class CombineNotesCommand extends IronfishCommand { const transactionTimer = new TransactionTimer(spendPostTime, raw) - this.log( - `Time to combine: ${TimeUtils.renderSpan(transactionTimer.getEstimateInMs(), { - hideMilliseconds: true, - })}`, - ) + if (spendPostTime > 0) { + this.log( + `Time to combine: ${TimeUtils.renderSpan(transactionTimer.getEstimateInMs(), { + hideMilliseconds: true, + })}`, + ) + } if (!flags.confirm) { const confirmed = await CliUx.ux.confirm('Do you confirm (Y/N)?') diff --git a/ironfish-cli/src/utils/timer.ts b/ironfish-cli/src/utils/timer.ts deleted file mode 100644 index b916a16707..0000000000 --- a/ironfish-cli/src/utils/timer.ts +++ /dev/null @@ -1,85 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -import { createRootLogger, Logger, RawTransaction, TimeUtils } from '@ironfish/sdk' -import { CliUx } from '@oclif/core' -import { ProgressBar } from '../types' - -export class TransactionTimer { - private logger: Logger - private progressBar: ProgressBar | undefined - private startTime: number | undefined - private estimateInMs: number - private timer: NodeJS.Timer | undefined - - constructor(spendPostTime: number, raw: RawTransaction, logger?: Logger) { - this.logger = logger ?? createRootLogger() - - // if spendPostTime is 0, there means that there was an issue measuring the spendPostTime - // we will not show the progress bar in this case - - this.estimateInMs = - spendPostTime > 0 ? Math.max(Math.ceil(spendPostTime * raw.spends.length), 1000) : 0 - } - - displayEstimate() { - if (this.estimateInMs === 0) { - return - } - this.logger.log( - `Time to send: ${TimeUtils.renderSpan(this.estimateInMs, { - hideMilliseconds: true, - })}`, - ) - } - - start() { - if (this.estimateInMs === 0) { - return - } - - this.progressBar = CliUx.ux.progress({ - format: '{title}: [{bar}] {percentage}% | {estimate}', - }) as ProgressBar - - this.startTime = Date.now() - - this.progressBar.start(100, 0, { - title: 'Sending the transaction', - estimate: TimeUtils.renderSpan(this.estimateInMs, { hideMilliseconds: true }), - }) - - this.timer = setInterval(() => { - if (!this.progressBar || !this.startTime) { - return - } - const durationInMs = Date.now() - this.startTime - const timeRemaining = this.estimateInMs - durationInMs - const progress = Math.round((durationInMs / this.estimateInMs) * 100) - - this.progressBar.update(progress, { - estimate: TimeUtils.renderSpan(timeRemaining, { hideMilliseconds: true }), - }) - }, 1000) - } - - end() { - if (this.estimateInMs === 0) { - return - } - - if (!this.progressBar || !this.startTime || !this.timer) { - return - } - - clearInterval(this.timer) - this.progressBar.update(100) - this.progressBar.stop() - - this.logger.log( - `Sending took ${TimeUtils.renderSpan(Date.now() - this.startTime, { - hideMilliseconds: true, - })}`, - ) - } -} diff --git a/ironfish-cli/src/utils/transaction.ts b/ironfish-cli/src/utils/transaction.ts index 2af15d4e26..3a249cde9f 100644 --- a/ironfish-cli/src/utils/transaction.ts +++ b/ironfish-cli/src/utils/transaction.ts @@ -24,7 +24,11 @@ export class TransactionTimer { private timer: NodeJS.Timer | undefined constructor(spendPostTime: number, raw: RawTransaction) { - this.estimateInMs = Math.max(Math.ceil(spendPostTime * raw.spends.length), 1000) + // if spendPostTime is 0, there means that there was an issue measuring the spendPostTime + // we will not show the progress bar in this case + + this.estimateInMs = + spendPostTime > 0 ? Math.max(Math.ceil(spendPostTime * raw.spends.length), 1000) : -1 } getEstimateInMs(): number { @@ -46,6 +50,10 @@ export class TransactionTimer { } start() { + if (this.estimateInMs <= 0) { + return + } + this.progressBar = CliUx.ux.progress({ format: '{title}: [{bar}] {percentage}% | {estimate}', }) as ProgressBar @@ -72,6 +80,10 @@ export class TransactionTimer { } end() { + if (this.estimateInMs <= 0) { + return + } + if (!this.progressBar || !this.startTime || !this.timer) { return }