[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:
Ori Newman 2019-09-08 16:58:28 +03:00 committed by stasatdaglabs
parent 75a8c6459a
commit 1ddae35277
3 changed files with 37 additions and 4 deletions

View File

@ -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 {

View File

@ -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").

View File

@ -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])
}