forked from hiero-ledger/hiero-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount_records_query.go
331 lines (276 loc) · 8.99 KB
/
account_records_query.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package hedera
/*-
*
* Hedera Go SDK
*
* Copyright (C) 2020 - 2022 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import (
"fmt"
"time"
"github.com/hashgraph/hedera-protobufs-go/services"
)
// AccountRecordsQuery gets all of the records for an account for any transfers into it and out of
// it, that were above the threshold, during the last 25 hours.
type AccountRecordsQuery struct {
Query
accountID *AccountID
}
// NewAccountRecordsQuery creates an AccountRecordsQuery query which can be used to construct and execute
// an AccountRecordsQuery.
//
// It is recommended that you use this for creating new instances of an AccountRecordQuery
// instead of manually creating an instance of the struct.
func NewAccountRecordsQuery() *AccountRecordsQuery {
header := services.QueryHeader{}
return &AccountRecordsQuery{
Query: _NewQuery(true, &header),
}
}
func (query *AccountRecordsQuery) SetGrpcDeadline(deadline *time.Duration) *AccountRecordsQuery {
query.Query.SetGrpcDeadline(deadline)
return query
}
// SetAccountID sets the account ID for which the records should be retrieved.
func (query *AccountRecordsQuery) SetAccountID(accountID AccountID) *AccountRecordsQuery {
query.accountID = &accountID
return query
}
func (query *AccountRecordsQuery) GetAccountID() AccountID {
if query.accountID == nil {
return AccountID{}
}
return *query.accountID
}
func (query *AccountRecordsQuery) _ValidateNetworkOnIDs(client *Client) error {
if client == nil || !client.autoValidateChecksums {
return nil
}
if query.accountID != nil {
if err := query.accountID.ValidateChecksum(client); err != nil {
return err
}
}
return nil
}
func (query *AccountRecordsQuery) _Build() *services.Query_CryptoGetAccountRecords {
pb := services.Query_CryptoGetAccountRecords{
CryptoGetAccountRecords: &services.CryptoGetAccountRecordsQuery{
Header: &services.QueryHeader{},
},
}
if query.accountID != nil {
pb.CryptoGetAccountRecords.AccountID = query.accountID._ToProtobuf()
}
return &pb
}
// GetCost Get the cost of the query
func (query *AccountRecordsQuery) GetCost(client *Client) (Hbar, error) {
if client == nil || client.operator == nil {
return Hbar{}, errNoClientProvided
}
var err error
err = query._ValidateNetworkOnIDs(client)
if err != nil {
return Hbar{}, err
}
for range query.nodeAccountIDs.slice {
paymentTransaction, err := _QueryMakePaymentTransaction(TransactionID{}, AccountID{}, client.operator, Hbar{})
if err != nil {
return Hbar{}, err
}
query.paymentTransactions = append(query.paymentTransactions, paymentTransaction)
}
pb := query._Build()
pb.CryptoGetAccountRecords.Header = query.pbHeader
query.pb = &services.Query{
Query: pb,
}
resp, err := _Execute(
client,
&query.Query,
_AccountRecordsQueryShouldRetry,
_CostQueryMakeRequest,
_CostQueryAdvanceRequest,
_QueryGetNodeAccountID,
_AccountRecordsQueryGetMethod,
_AccountRecordsQueryMapStatusError,
_QueryMapResponse,
query._GetLogID(),
query.grpcDeadline,
query.maxBackoff,
query.minBackoff,
query.maxRetry,
)
if err != nil {
return Hbar{}, err
}
cost := int64(resp.(*services.Response).GetCryptoGetAccountRecords().Header.Cost)
return HbarFromTinybar(cost), nil
}
func _AccountRecordsQueryShouldRetry(logID string, _ interface{}, response interface{}) _ExecutionState {
return _QueryShouldRetry(logID, Status(response.(*services.Response).GetCryptoGetAccountRecords().Header.NodeTransactionPrecheckCode))
}
func _AccountRecordsQueryMapStatusError(_ interface{}, response interface{}) error {
return ErrHederaPreCheckStatus{
Status: Status(response.(*services.Response).GetCryptoGetAccountRecords().Header.NodeTransactionPrecheckCode),
}
}
func _AccountRecordsQueryGetMethod(_ interface{}, channel *_Channel) _Method {
return _Method{
query: channel._GetCrypto().GetAccountRecords,
}
}
func (query *AccountRecordsQuery) Execute(client *Client) ([]TransactionRecord, error) {
if client == nil || client.operator == nil {
return []TransactionRecord{}, errNoClientProvided
}
var err error
err = query._ValidateNetworkOnIDs(client)
if err != nil {
return []TransactionRecord{}, err
}
if !query.paymentTransactionIDs.locked {
query.paymentTransactionIDs._Clear()._Push(TransactionIDGenerate(client.operator.accountID))
}
var cost Hbar
if query.queryPayment.tinybar != 0 {
cost = query.queryPayment
} else {
if query.maxQueryPayment.tinybar == 0 {
cost = client.maxQueryPayment
} else {
cost = query.maxQueryPayment
}
actualCost, err := query.GetCost(client)
if err != nil {
return []TransactionRecord{}, err
}
if cost.tinybar < actualCost.tinybar {
return []TransactionRecord{}, ErrMaxQueryPaymentExceeded{
QueryCost: actualCost,
MaxQueryPayment: cost,
query: "AccountRecordsQuery",
}
}
cost = actualCost
}
query.paymentTransactions = make([]*services.Transaction, 0)
if query.nodeAccountIDs.locked {
err = _QueryGeneratePayments(&query.Query, client, cost)
if err != nil {
return []TransactionRecord{}, err
}
} else {
paymentTransaction, err := _QueryMakePaymentTransaction(query.paymentTransactionIDs._GetCurrent().(TransactionID), AccountID{}, client.operator, cost)
if err != nil {
if err != nil {
return []TransactionRecord{}, err
}
}
query.paymentTransactions = append(query.paymentTransactions, paymentTransaction)
}
pb := query._Build()
pb.CryptoGetAccountRecords.Header = query.pbHeader
query.pb = &services.Query{
Query: pb,
}
records := make([]TransactionRecord, 0)
resp, err := _Execute(
client,
&query.Query,
_AccountRecordsQueryShouldRetry,
_QueryMakeRequest,
_QueryAdvanceRequest,
_QueryGetNodeAccountID,
_AccountRecordsQueryGetMethod,
_AccountRecordsQueryMapStatusError,
_QueryMapResponse,
query._GetLogID(),
query.grpcDeadline,
query.maxBackoff,
query.minBackoff,
query.maxRetry,
)
if err != nil {
return []TransactionRecord{}, err
}
for _, element := range resp.(*services.Response).GetCryptoGetAccountRecords().Records {
record := _TransactionRecordFromProtobuf(&services.TransactionGetRecordResponse{TransactionRecord: element})
records = append(records, record)
}
return records, err
}
// SetMaxQueryPayment sets the maximum payment allowed for this Query.
func (query *AccountRecordsQuery) SetMaxQueryPayment(maxPayment Hbar) *AccountRecordsQuery {
query.Query.SetMaxQueryPayment(maxPayment)
return query
}
// SetQueryPayment sets the payment amount for this Query.
func (query *AccountRecordsQuery) SetQueryPayment(paymentAmount Hbar) *AccountRecordsQuery {
query.Query.SetQueryPayment(paymentAmount)
return query
}
// SetNodeAccountIDs sets the _Node AccountID for this AccountRecordsQuery.
func (query *AccountRecordsQuery) SetNodeAccountIDs(accountID []AccountID) *AccountRecordsQuery {
query.Query.SetNodeAccountIDs(accountID)
return query
}
func (query *AccountRecordsQuery) SetMaxRetry(count int) *AccountRecordsQuery {
query.Query.SetMaxRetry(count)
return query
}
func (query *AccountRecordsQuery) SetMaxBackoff(max time.Duration) *AccountRecordsQuery {
if max.Nanoseconds() < 0 {
panic("maxBackoff must be a positive duration")
} else if max.Nanoseconds() < query.minBackoff.Nanoseconds() {
panic("maxBackoff must be greater than or equal to minBackoff")
}
query.maxBackoff = &max
return query
}
func (query *AccountRecordsQuery) GetMaxBackoff() time.Duration {
if query.maxBackoff != nil {
return *query.maxBackoff
}
return 8 * time.Second
}
func (query *AccountRecordsQuery) SetMinBackoff(min time.Duration) *AccountRecordsQuery {
if min.Nanoseconds() < 0 {
panic("minBackoff must be a positive duration")
} else if query.maxBackoff.Nanoseconds() < min.Nanoseconds() {
panic("minBackoff must be less than or equal to maxBackoff")
}
query.minBackoff = &min
return query
}
func (query *AccountRecordsQuery) GetMinBackoff() time.Duration {
if query.minBackoff != nil {
return *query.minBackoff
}
return 250 * time.Millisecond
}
func (query *AccountRecordsQuery) _GetLogID() string {
timestamp := query.timestamp.UnixNano()
if query.paymentTransactionIDs._Length() > 0 && query.paymentTransactionIDs._GetCurrent().(TransactionID).ValidStart != nil {
timestamp = query.paymentTransactionIDs._GetCurrent().(TransactionID).ValidStart.UnixNano()
}
return fmt.Sprintf("AccountRecordsQuery:%d", timestamp)
}
func (query *AccountRecordsQuery) SetPaymentTransactionID(transactionID TransactionID) *AccountRecordsQuery {
query.paymentTransactionIDs._Clear()._Push(transactionID)._SetLocked(true)
return query
}