mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-06 14:16:43 +00:00

* [NOD-303] Implement get transaction by id for api server * [NOD-303] Make routeParamTxID a constant * [NOD-303] Change database is not current error. * [NOD-303] Add ID to TransactionInput and TransactionOutput models * [NOD-303] Change transactions_outputs table name to transaction_outputs and transactions_inputs to transaction_inputs * [NOD-303] Add json annotations to transaction response types * [NOD-303] Split server package * [NOD-303] Add GetTransactionByHashHandler * [NOD-303] Add comments to exported functions and variables * [NOD-303] Put response types in a separate file * [NOD-303] Rename functions
47 lines
1.7 KiB
Go
47 lines
1.7 KiB
Go
package controllers
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/daglabs/btcd/apiserver/database"
|
|
"github.com/daglabs/btcd/apiserver/models"
|
|
"github.com/daglabs/btcd/apiserver/utils"
|
|
"github.com/daglabs/btcd/util/daghash"
|
|
"github.com/jinzhu/gorm"
|
|
"net/http"
|
|
)
|
|
|
|
// GetTransactionByIDHandler returns a transaction by a given transaction ID.
|
|
func GetTransactionByIDHandler(txID string) (interface{}, *utils.HandlerError) {
|
|
if len(txID) != daghash.TxIDSize*2 {
|
|
return nil, utils.NewHandlerError(http.StatusUnprocessableEntity, fmt.Sprintf("The given txid is not a hex-encoded %d-byte hash.", daghash.TxIDSize))
|
|
}
|
|
tx := &models.Transaction{}
|
|
db := database.DB.Where("transaction_id = ?", txID)
|
|
addTxPreloadedFields(db).First(&tx)
|
|
if tx.ID == 0 {
|
|
return nil, utils.NewHandlerError(http.StatusNotFound, "No transaction with the given txid was found.")
|
|
}
|
|
return convertTxModelToTxResponse(tx), nil
|
|
}
|
|
|
|
// GetTransactionByHashHandler returns a transaction by a given transaction hash.
|
|
func GetTransactionByHashHandler(txHash string) (interface{}, *utils.HandlerError) {
|
|
if len(txHash) != daghash.HashSize*2 {
|
|
return nil, utils.NewHandlerError(http.StatusUnprocessableEntity, fmt.Sprintf("The given txhash is not a hex-encoded %d-byte hash.", daghash.HashSize))
|
|
}
|
|
tx := &models.Transaction{}
|
|
db := database.DB.Where("transaction_hash = ?", txHash)
|
|
addTxPreloadedFields(db).First(&tx)
|
|
if tx.ID == 0 {
|
|
return nil, utils.NewHandlerError(http.StatusNotFound, "No transaction with the given txhash was found.")
|
|
}
|
|
return convertTxModelToTxResponse(tx), nil
|
|
}
|
|
|
|
func addTxPreloadedFields(db *gorm.DB) *gorm.DB {
|
|
return db.Preload("AcceptingBlock").
|
|
Preload("Subnetwork").
|
|
Preload("TransactionOutputs").
|
|
Preload("TransactionInputs.TransactionOutput.Transaction")
|
|
}
|