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 23, 2023
1 parent 07be54b commit fe351af
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2464,6 +2464,73 @@ 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 (
bestHeight int32
blockHash *chainhash.Hash
summary TransactionSummary
)
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 can exit early.
if txDetail == nil {
return nil
}

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

// 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
}

return nil
})
if err != nil {
return nil, err
}

var res GetTransactionResult

// 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 &res, nil
}

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

0 comments on commit fe351af

Please sign in to comment.