Skip to content

Commit

Permalink
Adds a transaction timer class to reuse (#4852)
Browse files Browse the repository at this point in the history
* Adds a transaction timer class to reuse

* lint fix

* shortening function name

* explicitly calling display estimate
  • Loading branch information
patnir committed Mar 19, 2024
1 parent a6a7fa7 commit 84e6771
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 37 deletions.
46 changes: 9 additions & 37 deletions ironfish-cli/src/commands/wallet/notes/combine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ import { CliUx, Flags } from '@oclif/core'
import inquirer from 'inquirer'
import { IronfishCommand } from '../../../command'
import { IronFlag, RemoteFlags } from '../../../flags'
import { ProgressBar } from '../../../types'
import { getExplorer } from '../../../utils/explorer'
import { selectFee } from '../../../utils/fees'
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`
Expand Down Expand Up @@ -462,40 +465,17 @@ export class CombineNotesCommand extends IronfishCommand {

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

const estimateInMs = Math.max(Math.ceil(spendPostTime * raw.spends.length), 1000)
const transactionTimer = new TransactionTimer(spendPostTime, raw, this.logger)
transactionTimer.displayEstimate()

this.log(
`Time to send: ${TimeUtils.renderSpan(estimateInMs, {
hideMilliseconds: true,
})}`,
)
if (!flags.confirm) {
const confirmed = await CliUx.ux.confirm('Do you confirm (Y/N)?')
if (!confirmed) {
this.error('Transaction aborted.')
}
}

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

const startTime = Date.now()

progressBar.start(100, 0, {
title: 'Sending the transaction',
estimate: TimeUtils.renderSpan(estimateInMs, { hideMilliseconds: true }),
})

const timer = setInterval(() => {
const durationInMs = Date.now() - startTime
const timeRemaining = estimateInMs - durationInMs
const progress = Math.round((durationInMs / estimateInMs) * 100)

progressBar.update(progress, {
estimate: TimeUtils.renderSpan(timeRemaining, { hideMilliseconds: true }),
})
}, 1000)
transactionTimer.start()

const response = await client.wallet.postTransaction({
transaction: RawTransactionSerde.serialize(raw).toString('hex'),
Expand All @@ -505,15 +485,7 @@ export class CombineNotesCommand extends IronfishCommand {
const bytes = Buffer.from(response.content.transaction, 'hex')
const transaction = new Transaction(bytes)

clearInterval(timer)
progressBar.update(100)
progressBar.stop()

this.log(
`Sending took ${TimeUtils.renderSpan(Date.now() - startTime, {
hideMilliseconds: true,
})}`,
)
transactionTimer.end()

if (response.content.accepted === false) {
this.warn(
Expand Down
64 changes: 64 additions & 0 deletions ironfish-cli/src/utils/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,70 @@ import {
UnsignedTransaction,
} 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()
this.estimateInMs = Math.max(Math.ceil(spendPostTime * raw.spends.length), 1000)
}

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

start() {
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.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,
})}`,
)
}
}

export async function renderUnsignedTransactionDetails(
client: RpcClient,
Expand Down

0 comments on commit 84e6771

Please sign in to comment.