Skip to content

Commit

Permalink
Added new utxo endpoints, including the original transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
icellan committed Jun 30, 2022
1 parent 4e18cfb commit 90f9b44
Show file tree
Hide file tree
Showing 8 changed files with 714 additions and 44 deletions.
45 changes: 45 additions & 0 deletions actions/utxos/count.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package utxos

import (
"net/http"

"github.com/BuxOrg/bux"
"github.com/BuxOrg/bux-server/actions"
"github.com/julienschmidt/httprouter"
apirouter "github.com/mrz1836/go-api-router"
)

// count will count all the utxos fulfilling the given conditions
func (a *Action) count(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {

reqXPubID, _ := bux.GetXpubIDFromRequest(req)

// Parse the params
params := apirouter.GetParams(req)
_, metadata, conditions, err := actions.GetQueryParameters(params)
if err != nil {
apirouter.ReturnResponse(w, req, http.StatusExpectationFailed, err.Error())
return
}

var dbConditions = map[string]interface{}{}
if conditions != nil {
dbConditions = *conditions
}
// force the xpub_id of the current user on query
dbConditions["xpub_id"] = reqXPubID

// Get a utxo using a xPub
var count int64
if count, err = a.Services.Bux.GetUtxosCount(
req.Context(),
metadata,
&dbConditions,
); err != nil {
apirouter.ReturnResponse(w, req, http.StatusExpectationFailed, err.Error())
return
}

// Return response
apirouter.ReturnResponse(w, req, http.StatusOK, count)
}
24 changes: 13 additions & 11 deletions actions/utxos/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,32 @@ import (
"net/http"

"github.com/BuxOrg/bux"
"github.com/BuxOrg/bux/utils"
"github.com/julienschmidt/httprouter"
apirouter "github.com/mrz1836/go-api-router"
)

// get will fetch a model
// get will fetch a utxo according to conditions
func (a *Action) get(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {

reqXPubID, _ := bux.GetXpubIDFromRequest(req)

// Parse the params
params := apirouter.GetParams(req)
txID := params.GetString("tx_id")
outputIndex := uint32(params.GetUint64("output_index"))

// Get a utxo using a xPub
var err error
var utxos []*bux.Utxo
if utxos, err = a.Services.Bux.GetUtxosByXpubID(
utxo, err := a.Services.Bux.GetUtxo(
req.Context(),
utils.Hash(params.GetString("key")),
nil,
nil,
nil,
); err != nil {
reqXPubID,
txID,
outputIndex,
)
if err != nil {
apirouter.ReturnResponse(w, req, http.StatusExpectationFailed, err.Error())
return
}

// Return response
apirouter.ReturnResponse(w, req, http.StatusOK, bux.DisplayModels(utxos))
apirouter.ReturnResponse(w, req, http.StatusOK, bux.DisplayModels(utxo))
}
1 change: 1 addition & 0 deletions actions/utxos/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ func RegisterRoutes(router *apirouter.Router, appConfig *config.AppConfig, servi

// V1 Requests
router.HTTPRouter.GET("/"+config.CurrentMajorVersion+"/utxo", action.Request(router, require.Wrap(action.get)))
router.HTTPRouter.POST("/"+config.CurrentMajorVersion+"/utxo/count", action.Request(router, require.Wrap(action.count)))
router.HTTPRouter.POST("/"+config.CurrentMajorVersion+"/utxo/search", action.Request(router, require.Wrap(action.search)))
}
10 changes: 5 additions & 5 deletions go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 16 additions & 28 deletions go.sum

Large diffs are not rendered by default.

Loading

0 comments on commit 90f9b44

Please sign in to comment.