Skip to content

Commit

Permalink
Add new date flag and use it in transactions cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
NullSoldier committed Nov 13, 2024
1 parent a5afca5 commit d2c416a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
15 changes: 5 additions & 10 deletions ironfish-cli/src/commands/wallet/transactions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from '@ironfish/sdk'
import { Flags } from '@oclif/core'
import { IronfishCommand } from '../../../command'
import { RemoteFlags } from '../../../flags'
import { DateFlag, RemoteFlags } from '../../../flags'
import * as ui from '../../../ui'
import { getAssetsByIDs, useAccount } from '../../../utils'
import { extractChainportDataFromTransaction } from '../../../utils/chainport'
Expand Down Expand Up @@ -75,13 +75,11 @@ export class TransactionsCommand extends IronfishCommand {
options: ['notes', 'transactions', 'transfers'],
helpGroup: 'OUTPUT',
}),
'filter.start': Flags.string({
'filter.start': DateFlag({
description: 'include transactions after this date (inclusive). Example: 2023-04-01',
parse: (input) => Promise.resolve(new Date(input).toISOString()),
}),
'filter.end': Flags.string({
'filter.end': DateFlag({
description: 'include transactions before this date (exclusive). Example: 2023-05-01',
parse: (input) => Promise.resolve(new Date(input).toISOString()),
}),
}

Expand Down Expand Up @@ -142,19 +140,16 @@ export class TransactionsCommand extends IronfishCommand {
let hasTransactions = false
let transactionRows: PartialRecursive<TransactionRow>[] = []

const filterStart = flags['filter.start'] && new Date(flags['filter.start']).valueOf()
const filterEnd = flags['filter.end'] && new Date(flags['filter.end']).valueOf()

for await (const { account, transaction } of transactions) {
if (transactionRows.length >= flags.limit) {
break
}

if (filterStart && transaction.timestamp < filterStart.valueOf()) {
if (flags['filter.start'] && transaction.timestamp < flags['filter.start'].valueOf()) {
continue
}

if (filterEnd && transaction.timestamp >= filterEnd.valueOf()) {
if (flags['filter.end'] && transaction.timestamp >= flags['filter.end'].valueOf()) {
continue
}

Expand Down
14 changes: 14 additions & 0 deletions ironfish-cli/src/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,17 @@ export const EnumLanguageKeyFlag = Flags.custom<LanguageKey, { choices: Array<La
}
},
})

export const DateFlag = Flags.custom<Date>({
parse: async (input, _ctx, opts) => {
const formatted = new Date(input)

if (Number.isNaN(formatted.valueOf())) {
throw new Error(
`The value provided for ${opts.name} is an invalid format. It must be a valid date.`,
)
}

return Promise.resolve(formatted)
},
})

0 comments on commit d2c416a

Please sign in to comment.