This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
action_utxo.go
152 lines (127 loc) · 4.1 KB
/
action_utxo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package bux
import (
"context"
"github.com/BuxOrg/bux/utils"
"github.com/mrz1836/go-datastore"
)
// GetUtxos will get all the utxos from the Datastore
func (c *Client) GetUtxos(ctx context.Context, metadataConditions *Metadata,
conditions *map[string]interface{}, queryParams *datastore.QueryParams, opts ...ModelOps,
) ([]*Utxo, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "get_utxos")
// Get the utxos
utxos, err := getUtxos(
ctx, metadataConditions, conditions, queryParams,
c.DefaultModelOptions(opts...)...,
)
if err != nil {
return nil, err
}
// add the transaction linked to the utxos
c.enrichUtxoTransactions(ctx, utxos)
return utxos, nil
}
// GetUtxosCount will get a count of all the utxos from the Datastore
func (c *Client) GetUtxosCount(ctx context.Context, metadataConditions *Metadata,
conditions *map[string]interface{}, opts ...ModelOps,
) (int64, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "get_utxos_count")
// Get the utxos count
count, err := getUtxosCount(
ctx, metadataConditions, conditions,
c.DefaultModelOptions(opts...)...,
)
if err != nil {
return 0, err
}
return count, nil
}
// GetUtxosByXpubID will get utxos based on an xPub
func (c *Client) GetUtxosByXpubID(ctx context.Context, xPubID string, metadata *Metadata, conditions *map[string]interface{},
queryParams *datastore.QueryParams,
) ([]*Utxo, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "get_utxos")
// Get the utxos
utxos, err := getUtxosByXpubID(
ctx,
xPubID,
metadata,
conditions,
queryParams,
c.DefaultModelOptions()...,
)
if err != nil {
return nil, err
}
// add the transaction linked to the utxos
c.enrichUtxoTransactions(ctx, utxos)
return utxos, nil
}
// GetUtxo will get a single utxo based on an xPub, the tx ID and the outputIndex
func (c *Client) GetUtxo(ctx context.Context, xPubKey, txID string, outputIndex uint32) (*Utxo, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "get_utxo")
// Get the utxos
utxo, err := getUtxo(
ctx, txID, outputIndex, c.DefaultModelOptions()...,
)
if err != nil {
return nil, err
} else if utxo == nil {
return nil, ErrMissingUtxo
}
// Check that the id matches
if utxo.XpubID != utils.Hash(xPubKey) {
return nil, ErrXpubIDMisMatch
}
var tx *Transaction
tx, err = getTransactionByID(ctx, "", utxo.TransactionID, c.DefaultModelOptions()...)
if err != nil {
c.Logger().Error().Str("utxoID", utxo.ID).Msg("failed finding transaction related to utxo")
} else {
utxo.Transaction = tx
}
return utxo, nil
}
// GetUtxoByTransactionID will get a single utxo based on the tx ID and the outputIndex
func (c *Client) GetUtxoByTransactionID(ctx context.Context, txID string, outputIndex uint32) (*Utxo, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "get_utxo_by_transaction_id")
// Get the utxo
utxo, err := getUtxo(
ctx, txID, outputIndex, c.DefaultModelOptions()...,
)
if err != nil {
return nil, err
} else if utxo == nil {
return nil, ErrMissingUtxo
}
var tx *Transaction
tx, err = getTransactionByID(ctx, "", utxo.TransactionID, c.DefaultModelOptions()...)
if err != nil {
c.Logger().Error().Str("utxoID", utxo.ID).Msg("failed finding transaction related to utxo")
} else {
utxo.Transaction = tx
}
return utxo, nil
}
// UnReserveUtxos remove the reservation on the utxos for the given draft ID
func (c *Client) UnReserveUtxos(ctx context.Context, xPubID, draftID string) error {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "unreserve_uxtos_by_draft_id")
return unReserveUtxos(ctx, xPubID, draftID, c.DefaultModelOptions()...)
}
// should this be optional in the results?
func (c *Client) enrichUtxoTransactions(ctx context.Context, utxos []*Utxo) {
for index, utxo := range utxos {
tx, err := getTransactionByID(ctx, "", utxo.TransactionID, c.DefaultModelOptions()...)
if err != nil {
c.Logger().Error().Str("utxoID", utxo.ID).Msg("failed finding transaction related to utxo")
} else {
utxos[index].Transaction = tx
}
}
}