forked from hiero-ledger/hiero-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
account_records_query.go
189 lines (157 loc) · 5.11 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
package hedera
import (
"github.com/hashgraph/hedera-sdk-go/v2/proto"
)
// 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
pb *proto.CryptoGetAccountRecordsQuery
}
// 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 := proto.QueryHeader{}
query := newQuery(true, &header)
pb := proto.CryptoGetAccountRecordsQuery{Header: &header}
query.pb.Query = &proto.Query_CryptoGetAccountRecords{
CryptoGetAccountRecords: &pb,
}
return &AccountRecordsQuery{
Query: query,
pb: &pb,
}
}
// SetAccountID sets the account ID for which the records should be retrieved.
func (query *AccountRecordsQuery) SetAccountID(id AccountID) *AccountRecordsQuery {
query.pb.AccountID = id.toProtobuf()
return query
}
func (query *AccountRecordsQuery) GetAccountID() AccountID {
if query.pb.AccountID != nil {
return AccountID{}
} else {
return accountIDFromProtobuf(query.pb.AccountID)
}
}
func (query *AccountRecordsQuery) GetCost(client *Client) (Hbar, error) {
if client == nil || client.operator == nil {
return Hbar{}, errNoClientProvided
}
paymentTransaction, err := query_makePaymentTransaction(TransactionID{}, AccountID{}, client.operator, Hbar{})
if err != nil {
return Hbar{}, err
}
query.pbHeader.Payment = paymentTransaction
query.pbHeader.ResponseType = proto.ResponseType_COST_ANSWER
query.nodeIDs = client.network.getNodeAccountIDsForExecute()
resp, err := execute(
client,
request{
query: &query.Query,
},
accountRecordsQuery_shouldRetry,
costQuery_makeRequest,
costQuery_advanceRequest,
costQuery_getNodeAccountID,
accountRecordsQuery_getMethod,
accountRecordsQuery_mapStatusError,
query_mapResponse,
)
if err != nil {
return Hbar{}, err
}
cost := int64(resp.query.GetCryptoGetAccountRecords().Header.Cost)
return HbarFromTinybar(cost), nil
}
func accountRecordsQuery_shouldRetry(_ request, response response) executionState {
return query_shouldRetry(Status(response.query.GetCryptoGetAccountRecords().Header.NodeTransactionPrecheckCode))
}
func accountRecordsQuery_mapStatusError(_ request, response response) error {
return ErrHederaPreCheckStatus{
Status: Status(response.query.GetCryptoGetAccountRecords().Header.NodeTransactionPrecheckCode),
}
}
func accountRecordsQuery_getMethod(_ request, 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
}
if len(query.Query.GetNodeAccountIDs()) == 0 {
query.SetNodeAccountIDs(client.network.getNodeAccountIDsForExecute())
}
records := make([]TransactionRecord, 0)
query.paymentTransactionID = 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
}
err := query_generatePayments(&query.Query, client, cost)
if err != nil {
return []TransactionRecord{}, err
}
resp, err := execute(
client,
request{
query: &query.Query,
},
accountRecordsQuery_shouldRetry,
query_makeRequest,
query_advanceRequest,
query_getNodeAccountID,
accountRecordsQuery_getMethod,
accountRecordsQuery_mapStatusError,
query_mapResponse,
)
if err != nil {
return []TransactionRecord{}, err
}
for _, element := range resp.query.GetCryptoGetAccountRecords().Records {
records = append(records, transactionRecordFromProtobuf(element))
}
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
}