Skip to content

Commit

Permalink
wallet: add gettransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikEk committed Sep 26, 2023
1 parent 07be54b commit 6f11990
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2464,6 +2464,66 @@ func (w *Wallet) GetTransactions(startBlock, endBlock *BlockIdentifier,
return &res, err
}

// GetTransactionResult returns a transaction in the UnminedTransaction
// field if it is unmined, or in the MinedTransaction field as a Block
// structure for a mined transaction.
type GetTransactionResult struct {
MinedTransaction Block
UnminedTransaction TransactionSummary
}

// GetTransaction returns data of any transaction given its id. A mined
// transaction is returned in a Block structure which records properties about
// the block.
func (w *Wallet) GetTransaction(txHash chainhash.Hash) (*GetTransactionResult,
error) {

var res GetTransactionResult
err := walletdb.View(w.db, func(dbtx walletdb.ReadTx) error {
txmgrNs := dbtx.ReadBucket(wtxmgrNamespaceKey)

txDetail, err := w.TxStore.TxDetails(txmgrNs, &txHash)
if err != nil {
return err
}

// If the transaction was not found we return an error.
if txDetail == nil {
return fmt.Errorf("can not find the transaction")
}

// Otherwise, we create a summary of the transaction details.
summary := makeTxSummary(dbtx, w, txDetail)

var bestHeight int32
var blockHash *chainhash.Hash
// If it is a confirmed transaction we set the corresponding
// block height and hash.
if txDetail.Block.Height != -1 {
bestHeight = txDetail.Block.Height
blockHash = &txDetail.Block.Hash
}

// Add the transaction details either as confirmed or
// unconfirmed to the response.
switch blockHash {
case nil:
res.UnminedTransaction = summary
default:
res.MinedTransaction = Block{
Hash: blockHash,
Height: bestHeight,
Timestamp: summary.Timestamp,
Transactions: []TransactionSummary{summary},
}
}

return nil
})

return &res, err
}

// AccountResult is a single account result for the AccountsResult type.
type AccountResult struct {
waddrmgr.AccountProperties
Expand Down

0 comments on commit 6f11990

Please sign in to comment.