mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-03-30 15:08:33 +00:00
[NOD-305] Implement API-Server GET /utxos/{address} (#398)
* [NOD-305] Implement API-Server GET /utxos/{address} * [NOD-305] Add accepting block blue score to the resulted utxo
This commit is contained in:
parent
75a8c6459a
commit
1ddae35277
@ -22,10 +22,12 @@ type transactionResponse struct {
|
||||
}
|
||||
|
||||
type transactionOutputResponse struct {
|
||||
TransactionID string `json:"transactionId,omitempty"`
|
||||
Value uint64 `json:"value"`
|
||||
PkScript string `json:"pkScript"`
|
||||
Address string `json:"address"`
|
||||
TransactionID string `json:"transactionId,omitempty"`
|
||||
Value uint64 `json:"value"`
|
||||
PkScript string `json:"pkScript"`
|
||||
Address string `json:"address,omitempty"`
|
||||
AcceptingBlockHash string `json:"acceptingBlockHash,omitempty"`
|
||||
AcceptingBlockBlueScore uint64 `json:"acceptingBlockBlueScore,omitempty"`
|
||||
}
|
||||
|
||||
type transactionInputResponse struct {
|
||||
|
@ -68,6 +68,28 @@ func GetTransactionsByAddressHandler(address string, skip uint64, limit uint64)
|
||||
return txResponses, nil
|
||||
}
|
||||
|
||||
// GetUTXOsByAddressHandler searches for all UTXOs that belong to a certain address.
|
||||
func GetUTXOsByAddressHandler(address string) (interface{}, *utils.HandlerError) {
|
||||
utxos := []*models.UTXO{}
|
||||
database.DB.
|
||||
Joins("LEFT JOIN `transaction_outputs` ON `transaction_outputs`.`id` = `utxos`.`transaction_output_id`").
|
||||
Joins("LEFT JOIN `addresses` ON `addresses`.`id` = `transaction_outputs`.`address_id`").
|
||||
Where("`addresses`.`address` = ?", address).
|
||||
Preload("AcceptingBlock").
|
||||
Preload("TransactionOutput").
|
||||
Find(&utxos)
|
||||
UTXOsResponses := make([]*transactionOutputResponse, len(utxos))
|
||||
for i, utxo := range utxos {
|
||||
UTXOsResponses[i] = &transactionOutputResponse{
|
||||
Value: utxo.TransactionOutput.Value,
|
||||
PkScript: hex.EncodeToString(utxo.TransactionOutput.PkScript),
|
||||
AcceptingBlockHash: utxo.AcceptingBlock.BlockHash,
|
||||
AcceptingBlockBlueScore: utxo.AcceptingBlock.BlueScore,
|
||||
}
|
||||
}
|
||||
return UTXOsResponses, nil
|
||||
}
|
||||
|
||||
func addTxPreloadedFields(query *gorm.DB) *gorm.DB {
|
||||
return query.Preload("AcceptingBlock").
|
||||
Preload("Subnetwork").
|
||||
|
@ -76,6 +76,11 @@ func addRoutes(router *mux.Router) {
|
||||
makeHandler(getTransactionsByAddressHandler)).
|
||||
Methods("GET")
|
||||
|
||||
router.HandleFunc(
|
||||
fmt.Sprintf("/utxos/address/{%s}", routeParamAddress),
|
||||
makeHandler(getUTXOsByAddressHandler)).
|
||||
Methods("GET")
|
||||
|
||||
router.HandleFunc(
|
||||
fmt.Sprintf("/block/{%s}", routeParamBlockHash),
|
||||
makeHandler(getBlockByHashHandler)).
|
||||
@ -118,6 +123,10 @@ func getTransactionsByAddressHandler(routeParams map[string]string, queryParams
|
||||
return controllers.GetTransactionsByAddressHandler(routeParams[routeParamAddress], uint64(skip), uint64(limit))
|
||||
}
|
||||
|
||||
func getUTXOsByAddressHandler(routeParams map[string]string, _ map[string][]string, _ *utils.APIServerContext) (interface{}, *utils.HandlerError) {
|
||||
return controllers.GetUTXOsByAddressHandler(routeParams[routeParamAddress])
|
||||
}
|
||||
|
||||
func getBlockByHashHandler(routeParams map[string]string, _ map[string][]string, _ *utils.APIServerContext) (interface{}, *utils.HandlerError) {
|
||||
return controllers.GetBlockByHashHandler(routeParams[routeParamBlockHash])
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user