-
Notifications
You must be signed in to change notification settings - Fork 66
/
contract_call_query.go
256 lines (211 loc) · 7.55 KB
/
contract_call_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
package hedera
/*-
*
* Hedera Go SDK
*
* Copyright (C) 2020 - 2024 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 (
"time"
"github.com/hashgraph/hedera-sdk-go/v2/proto/services"
)
// ContractCallQuery calls a function of the given smart contract instance, giving it ContractFunctionParameters as its
// inputs. It will consume the entire given amount of gas.
//
// This is performed locally on the particular _Node that the client is communicating with. It cannot change the state of
// the contract instance (and so, cannot spend anything from the instance's Hedera account). It will not have a
// consensus timestamp. It cannot generate a record or a receipt. This is useful for calling getter functions, which
// purely read the state and don't change it. It is faster and cheaper than a ContractExecuteTransaction, because it is
// purely local to a single _Node.
type ContractCallQuery struct {
Query
contractID *ContractID
gas uint64
maxResultSize uint64
functionParameters []byte
senderID *AccountID
}
// NewContractCallQuery creates a ContractCallQuery query which can be used to construct and execute a
// Contract Call Local Query.
func NewContractCallQuery() *ContractCallQuery {
header := services.QueryHeader{}
query := _NewQuery(true, &header)
return &ContractCallQuery{
Query: query,
}
}
// When execution is attempted, a single attempt will timeout when this deadline is reached. (The SDK may subsequently retry the execution.)
func (q *ContractCallQuery) SetGrpcDeadline(deadline *time.Duration) *ContractCallQuery {
q.Query.SetGrpcDeadline(deadline)
return q
}
// SetContractID sets the contract instance to call
func (q *ContractCallQuery) SetContractID(contractID ContractID) *ContractCallQuery {
q.contractID = &contractID
return q
}
// GetContractID returns the contract instance to call
func (q *ContractCallQuery) GetContractID() ContractID {
if q.contractID == nil {
return ContractID{}
}
return *q.contractID
}
// SetSenderID
// The account that is the "sender." If not present it is the accountId from the transactionId.
// Typically a different value than specified in the transactionId requires a valid signature
// over either the hedera transaction or foreign transaction data.
func (q *ContractCallQuery) SetSenderID(id AccountID) *ContractCallQuery {
q.senderID = &id
return q
}
// GetSenderID returns the AccountID that is the "sender."
func (q *ContractCallQuery) GetSenderID() AccountID {
if q.senderID == nil {
return AccountID{}
}
return *q.senderID
}
// SetGas sets the amount of gas to use for the call. All of the gas offered will be charged for.
func (q *ContractCallQuery) SetGas(gas uint64) *ContractCallQuery {
q.gas = gas
return q
}
// GetGas returns the amount of gas to use for the call.
func (q *ContractCallQuery) GetGas() uint64 {
return q.gas
}
// Deprecated
func (q *ContractCallQuery) SetMaxResultSize(size uint64) *ContractCallQuery {
q.maxResultSize = size
return q
}
// SetFunction sets which function to call, and the ContractFunctionParams to pass to the function
func (q *ContractCallQuery) SetFunction(name string, params *ContractFunctionParameters) *ContractCallQuery {
if params == nil {
params = NewContractFunctionParameters()
}
q.functionParameters = params._Build(&name)
return q
}
// SetFunctionParameters sets the function parameters as their raw bytes.
func (q *ContractCallQuery) SetFunctionParameters(byteArray []byte) *ContractCallQuery {
q.functionParameters = byteArray
return q
}
// GetFunctionParameters returns the function parameters as their raw bytes.
func (q *ContractCallQuery) GetFunctionParameters() []byte {
return q.functionParameters
}
func (q *ContractCallQuery) GetCost(client *Client) (Hbar, error) {
return q.Query.getCost(client, q)
}
// Execute executes the Query with the provided client
func (q *ContractCallQuery) Execute(client *Client) (ContractFunctionResult, error) {
resp, err := q.Query.execute(client, q)
if err != nil {
return ContractFunctionResult{}, err
}
return _ContractFunctionResultFromProtobuf(resp.GetContractCallLocal().FunctionResult), nil
}
// SetMaxQueryPayment sets the maximum payment allowed for this Query.
func (q *ContractCallQuery) SetMaxQueryPayment(maxPayment Hbar) *ContractCallQuery {
q.Query.SetMaxQueryPayment(maxPayment)
return q
}
// SetQueryPayment sets the payment amount for this Query.
func (q *ContractCallQuery) SetQueryPayment(paymentAmount Hbar) *ContractCallQuery {
q.Query.SetQueryPayment(paymentAmount)
return q
}
// SetNodeAccountIDs sets the _Node AccountID for this ContractCallQuery.
func (q *ContractCallQuery) SetNodeAccountIDs(accountID []AccountID) *ContractCallQuery {
q.Query.SetNodeAccountIDs(accountID)
return q
}
// SetMaxRetry sets the max number of errors before execution will fail.
func (q *ContractCallQuery) SetMaxRetry(count int) *ContractCallQuery {
q.Query.SetMaxRetry(count)
return q
}
// SetMaxBackoff The maximum amount of time to wait between retries.
// Every retry attempt will increase the wait time exponentially until it reaches this time.
func (q *ContractCallQuery) SetMaxBackoff(max time.Duration) *ContractCallQuery {
q.Query.SetMaxBackoff(max)
return q
}
// SetMinBackoff sets the minimum amount of time to wait between retries.
func (q *ContractCallQuery) SetMinBackoff(min time.Duration) *ContractCallQuery {
q.Query.SetMinBackoff(min)
return q
}
// SetPaymentTransactionID assigns the payment transaction id.
func (q *ContractCallQuery) SetPaymentTransactionID(transactionID TransactionID) *ContractCallQuery {
q.paymentTransactionIDs._Clear()._Push(transactionID)._SetLocked(true)
return q
}
func (q *ContractCallQuery) SetLogLevel(level LogLevel) *ContractCallQuery {
q.Query.SetLogLevel(level)
return q
}
// ---------- Parent functions specific implementation ----------
func (q *ContractCallQuery) getMethod(channel *_Channel) _Method {
return _Method{
query: channel._GetContract().ContractCallLocalMethod,
}
}
func (q *ContractCallQuery) getName() string {
return "ContractCallQuery"
}
func (q *ContractCallQuery) buildQuery() *services.Query {
pb := services.Query_ContractCallLocal{
ContractCallLocal: &services.ContractCallLocalQuery{
Header: q.pbHeader,
Gas: int64(q.gas),
},
}
if q.contractID != nil {
pb.ContractCallLocal.ContractID = q.contractID._ToProtobuf()
}
if q.senderID != nil {
pb.ContractCallLocal.SenderId = q.senderID._ToProtobuf()
}
if len(q.functionParameters) > 0 {
pb.ContractCallLocal.FunctionParameters = q.functionParameters
}
return &services.Query{
Query: &pb,
}
}
func (q *ContractCallQuery) validateNetworkOnIDs(client *Client) error {
if client == nil || !client.autoValidateChecksums {
return nil
}
if q.contractID != nil {
if err := q.contractID.ValidateChecksum(client); err != nil {
return err
}
}
if q.senderID != nil {
if err := q.senderID.ValidateChecksum(client); err != nil {
return err
}
}
return nil
}
func (q *ContractCallQuery) getQueryResponse(response *services.Response) queryResponse {
return response.GetContractCallLocal()
}