Skip to content

Commit

Permalink
Merge pull request #165 from BuxOrg/feature/utxo_functions
Browse files Browse the repository at this point in the history
feat(BUX-184): add GetUtxosCount function
  • Loading branch information
mergify[bot] authored Oct 5, 2023
2 parents 26c0b68 + 74a7bb6 commit c7df2a4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
27 changes: 27 additions & 0 deletions transports/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,33 @@ func (g *TransportGraphQL) GetUtxos(
return respData.Utxos, nil
}

// GetUtxosCount will get the count of utxos filtered by conditions and metadata
func (g *TransportGraphQL) GetUtxosCount(
ctx context.Context,
conditions map[string]interface{},
metadata *buxmodels.Metadata,
) (int64, ResponseError) {
reqBody := `
query ($conditions: Map, $metadata: Metadata) {
utxos_count (
conditions: $conditions
metadata: $metadata
)
}`

variables := map[string]interface{}{
"conditions": conditions,
"metadata": metadata,
}

var respData int64
if err := g.doGraphQLQuery(ctx, reqBody, variables, &respData); err != nil {
return 0, err
}

return respData, nil
}

// UnreserveUtxos will unreserve utxos from draft transaction
func (g *TransportGraphQL) UnreserveUtxos(ctx context.Context, referenceID string) ResponseError {
reqBody := `
Expand Down
23 changes: 23 additions & 0 deletions transports/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,29 @@ func (h *TransportHTTP) GetUtxos(ctx context.Context, conditions map[string]inte
return utxos, nil
}

// GetUtxosCount will get the count of utxos filtered by conditions and metadata
func (h *TransportHTTP) GetUtxosCount(ctx context.Context, conditions map[string]interface{}, metadata *buxmodels.Metadata) (int64, ResponseError) {
jsonStr, err := json.Marshal(map[string]interface{}{
FieldConditions: conditions,
FieldMetadata: processMetadata(metadata),
})
if err != nil {
return 0, WrapError(err)
}

var count int64
if err := h.doHTTPRequest(
ctx, http.MethodPost, "/utxo/count", jsonStr, h.xPriv, h.signRequest, &count,
); err != nil {
return 0, err
}
if h.debug {
log.Printf("utxos count: %v\n", count)
}

return count, nil
}

// UnreserveUtxos will unreserve utxos from draft transaction
func (h *TransportHTTP) UnreserveUtxos(ctx context.Context, referenceID string) ResponseError {
jsonStr, err := json.Marshal(map[string]interface{}{
Expand Down
1 change: 1 addition & 0 deletions transports/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type TransactionService interface {
UpdateTransactionMetadata(ctx context.Context, txID string, metadata *buxmodels.Metadata) (*buxmodels.Transaction, ResponseError)
GetUtxo(ctx context.Context, txID string, outputIndex uint32) (*buxmodels.Utxo, ResponseError)
GetUtxos(ctx context.Context, conditions map[string]interface{}, metadata *buxmodels.Metadata, queryParams *QueryParams) ([]*buxmodels.Utxo, ResponseError)
GetUtxosCount(ctx context.Context, conditions map[string]interface{}, metadata *buxmodels.Metadata) (int64, ResponseError)
UnreserveUtxos(ctx context.Context, referenceID string) ResponseError
}

Expand Down

0 comments on commit c7df2a4

Please sign in to comment.