Skip to content

Commit

Permalink
Moves responsibility of logging estimate out of the timer (#4857)
Browse files Browse the repository at this point in the history
* Moves responsibility of logging estimate out of the timer

* name change

* changing date.now to performance.now
  • Loading branch information
patnir authored Mar 21, 2024
1 parent a5acf9a commit ed5a157
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
18 changes: 16 additions & 2 deletions ironfish-cli/src/commands/wallet/notes/combine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,13 @@ export class CombineNotesCommand extends IronfishCommand {

displayTransactionSummary(raw, Asset.nativeId().toString('hex'), amount, from, to, memo)

const transactionTimer = new TransactionTimer(spendPostTime, raw, this.logger)
transactionTimer.displayEstimate()
const transactionTimer = new TransactionTimer(spendPostTime, raw)

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)?')
Expand All @@ -487,6 +492,15 @@ export class CombineNotesCommand extends IronfishCommand {

transactionTimer.end()

this.log(
`Combining took ${TimeUtils.renderSpan(
transactionTimer.getEndTime() - transactionTimer.getStartTime(),
{
hideMilliseconds: true,
},
)}`,
)

if (response.content.accepted === false) {
this.warn(
`Transaction '${transaction.hash().toString('hex')}' was not accepted into the mempool`,
Expand Down
38 changes: 21 additions & 17 deletions ironfish-cli/src/utils/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,40 @@ import { CliUx } from '@oclif/core'
import { ProgressBar } from '../types'

export class TransactionTimer {
private logger: Logger
private progressBar: ProgressBar | undefined
private startTime: number | undefined
private endTime: number | undefined
private estimateInMs: number
private timer: NodeJS.Timer | undefined

constructor(spendPostTime: number, raw: RawTransaction, logger?: Logger) {
this.logger = logger ?? createRootLogger()
constructor(spendPostTime: number, raw: RawTransaction) {
this.estimateInMs = Math.max(Math.ceil(spendPostTime * raw.spends.length), 1000)
}

displayEstimate() {
this.logger.log(
`Time to send: ${TimeUtils.renderSpan(this.estimateInMs, {
hideMilliseconds: true,
})}`,
)
getEstimateInMs(): number {
return this.estimateInMs
}

getStartTime(): number {
if (!this.startTime) {
throw new Error('TransactionTimer not started')
}
return this.startTime
}

getEndTime(): number {
if (!this.endTime) {
throw new Error('TransactionTimer not ended')
}
return this.endTime
}

start() {
this.progressBar = CliUx.ux.progress({
format: '{title}: [{bar}] {percentage}% | {estimate}',
}) as ProgressBar

this.startTime = Date.now()
this.startTime = performance.now()

this.progressBar.start(100, 0, {
title: 'Sending the transaction',
Expand All @@ -52,7 +61,7 @@ export class TransactionTimer {
if (!this.progressBar || !this.startTime) {
return
}
const durationInMs = Date.now() - this.startTime
const durationInMs = performance.now() - this.startTime
const timeRemaining = this.estimateInMs - durationInMs
const progress = Math.round((durationInMs / this.estimateInMs) * 100)

Expand All @@ -70,12 +79,7 @@ export class TransactionTimer {
clearInterval(this.timer)
this.progressBar.update(100)
this.progressBar.stop()

this.logger.log(
`Sending took ${TimeUtils.renderSpan(Date.now() - this.startTime, {
hideMilliseconds: true,
})}`,
)
this.endTime = performance.now()
}
}

Expand Down

0 comments on commit ed5a157

Please sign in to comment.