|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ |
| 4 | +import { NodeUtils, TransactionStatus } from '@ironfish/sdk' |
| 5 | +import { Args, ux } from '@oclif/core' |
| 6 | +import { IronfishCommand } from '../../../command' |
| 7 | + |
| 8 | +export default class TransactionsDelete extends IronfishCommand { |
| 9 | + static description = 'delete an expired or pending transaction from the wallet' |
| 10 | + |
| 11 | + static args = { |
| 12 | + transaction: Args.string({ |
| 13 | + required: true, |
| 14 | + description: 'Hash of the transaction to delete from the wallet', |
| 15 | + }), |
| 16 | + } |
| 17 | + |
| 18 | + async start(): Promise<void> { |
| 19 | + const { args } = await this.parse(TransactionsDelete) |
| 20 | + const { transaction } = args |
| 21 | + |
| 22 | + ux.action.start('Opening node') |
| 23 | + const node = await this.sdk.node() |
| 24 | + await NodeUtils.waitForOpen(node) |
| 25 | + ux.action.stop('Done.') |
| 26 | + |
| 27 | + const accounts = node.wallet.accounts |
| 28 | + const transactionHash = Buffer.from(transaction, 'hex') |
| 29 | + let deleted = false |
| 30 | + |
| 31 | + for (const account of accounts) { |
| 32 | + const transactionValue = await account.getTransaction(transactionHash) |
| 33 | + |
| 34 | + if (transactionValue == null) { |
| 35 | + continue |
| 36 | + } |
| 37 | + |
| 38 | + const transactionStatus = await node.wallet.getTransactionStatus( |
| 39 | + account, |
| 40 | + transactionValue, |
| 41 | + ) |
| 42 | + |
| 43 | + if ( |
| 44 | + transactionStatus === TransactionStatus.CONFIRMED || |
| 45 | + transactionStatus === TransactionStatus.UNCONFIRMED |
| 46 | + ) { |
| 47 | + this.error(`Transaction ${transaction} is already on a block, so it cannot be deleted`) |
| 48 | + } |
| 49 | + |
| 50 | + if ( |
| 51 | + transactionStatus === TransactionStatus.EXPIRED || |
| 52 | + transactionStatus === TransactionStatus.PENDING |
| 53 | + ) { |
| 54 | + await account.deleteTransaction(transactionValue.transaction) |
| 55 | + deleted = true |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + if (deleted) { |
| 60 | + this.log(`Transaction ${transaction} deleted from wallet`) |
| 61 | + } else { |
| 62 | + this.log(`No transaction with hash ${transaction} found in wallet`) |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments