-
Notifications
You must be signed in to change notification settings - Fork 574
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
wallet:transactions:delete
command (#5276)
* Add `wallet:transactions:delete` command * remove remoteflags
- Loading branch information
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* 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 { NodeUtils, TransactionStatus } from '@ironfish/sdk' | ||
import { Args, ux } from '@oclif/core' | ||
import { IronfishCommand } from '../../../command' | ||
|
||
export default class TransactionsDelete extends IronfishCommand { | ||
static description = 'delete an expired or pending transaction from the wallet' | ||
|
||
static args = { | ||
transaction: Args.string({ | ||
required: true, | ||
description: 'Hash of the transaction to delete from the wallet', | ||
}), | ||
} | ||
|
||
async start(): Promise<void> { | ||
const { args } = await this.parse(TransactionsDelete) | ||
const { transaction } = args | ||
|
||
ux.action.start('Opening node') | ||
const node = await this.sdk.node() | ||
await NodeUtils.waitForOpen(node) | ||
ux.action.stop('Done.') | ||
|
||
const accounts = node.wallet.accounts | ||
const transactionHash = Buffer.from(transaction, 'hex') | ||
let deleted = false | ||
|
||
for (const account of accounts) { | ||
const transactionValue = await account.getTransaction(transactionHash) | ||
|
||
if (transactionValue == null) { | ||
continue | ||
} | ||
|
||
const transactionStatus = await node.wallet.getTransactionStatus( | ||
account, | ||
transactionValue, | ||
) | ||
|
||
if ( | ||
transactionStatus === TransactionStatus.CONFIRMED || | ||
transactionStatus === TransactionStatus.UNCONFIRMED | ||
) { | ||
this.error(`Transaction ${transaction} is already on a block, so it cannot be deleted`) | ||
} | ||
|
||
if ( | ||
transactionStatus === TransactionStatus.EXPIRED || | ||
transactionStatus === TransactionStatus.PENDING | ||
) { | ||
await account.deleteTransaction(transactionValue.transaction) | ||
deleted = true | ||
} | ||
} | ||
|
||
if (deleted) { | ||
this.log(`Transaction ${transaction} deleted from wallet`) | ||
} else { | ||
this.log(`No transaction with hash ${transaction} found in wallet`) | ||
} | ||
} | ||
} |