forked from hiero-ledger/hiero-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmirror_node_contract_query.go
More file actions
268 lines (225 loc) · 8.26 KB
/
Copy pathmirror_node_contract_query.go
File metadata and controls
268 lines (225 loc) · 8.26 KB
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
package hiero
import (
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"github.com/pkg/errors"
)
// SPDX-License-Identifier: Apache-2.0
// mirrorNodeContractQuery returns a result from EVM execution such as cost-free execution of read-only smart contract
// queries, gas estimation, and transient simulation of read-write operations.
type mirrorNodeContractQuery struct {
// The contract we are sending the transaction to
contractID *ContractID
contractEvmAddress *string
// The account we are sending the transaction from
sender *AccountID
senderEvmAddress *string
// The transaction callData
callData []byte
// The amount we are sending to the contract
value *int64
// The gas limit
gasLimit *int64
// The gas price
gasPrice *int64
// The block number for the simulation
blockNumber *int64
}
// GetContractID returns the contract instance to call
func (mirrorNodeContractQuery *mirrorNodeContractQuery) GetContractID() ContractID {
if mirrorNodeContractQuery.contractID == nil {
return ContractID{}
}
return *mirrorNodeContractQuery.contractID
}
// GetContractEvmAddress returns the 20-byte EVM address of the contract to call.
func (mirrorNodeContractQuery *mirrorNodeContractQuery) GetContractEvmAddress() string {
if mirrorNodeContractQuery.contractEvmAddress == nil {
return ""
}
return *mirrorNodeContractQuery.contractEvmAddress
}
// GetSender returns the sender of the transaction simulation.
func (mirrorNodeContractQuery *mirrorNodeContractQuery) GetSender() AccountID {
if mirrorNodeContractQuery.sender == nil {
return AccountID{}
}
return *mirrorNodeContractQuery.sender
}
// GetSenderEvmAddress returns the 20-byte EVM address of the sender of the transaction simulation.
func (mirrorNodeContractQuery *mirrorNodeContractQuery) GetSenderEvmAddress() string {
if mirrorNodeContractQuery.senderEvmAddress == nil {
return ""
}
return *mirrorNodeContractQuery.senderEvmAddress
}
// setFunction sets which function to call, and the ContractFunctionParams to pass to the function
func (mirrorNodeContractQuery *mirrorNodeContractQuery) setFunction(name string, params *ContractFunctionParameters) {
if params == nil {
params = NewContractFunctionParameters()
}
mirrorNodeContractQuery.callData = params._Build(&name)
}
// GetCallData returns the calldata
func (mirrorNodeContractQuery *mirrorNodeContractQuery) GetCallData() []byte {
return mirrorNodeContractQuery.callData
}
// GetValue returns the amount of value (in tinybars or wei) to be sent to the contract in the transaction.
func (mirrorNodeContractQuery *mirrorNodeContractQuery) GetValue() int64 {
if mirrorNodeContractQuery.value == nil {
return 0
}
return *mirrorNodeContractQuery.value
}
// GetGasLimit returns the gas limit for the contract call. This specifies the maximum amount of gas that the transaction can consume.
func (mirrorNodeContractQuery *mirrorNodeContractQuery) GetGasLimit() int64 {
if mirrorNodeContractQuery.gasLimit == nil {
return 0
}
return *mirrorNodeContractQuery.gasLimit
}
// GetGasPrice returns the gas price to be used for the contract call. This specifies the price of each unit of gas used in the transaction.
func (mirrorNodeContractQuery *mirrorNodeContractQuery) GetGasPrice() int64 {
if mirrorNodeContractQuery.gasPrice == nil {
return 0
}
return *mirrorNodeContractQuery.gasPrice
}
// GetBlockNumber returns the block number for the simulation of the contract call. The block number determines the context of the contract call simulation within the blockchain.
func (mirrorNodeContractQuery *mirrorNodeContractQuery) GetBlockNumber() int64 {
if mirrorNodeContractQuery.blockNumber == nil {
return 0
}
return *mirrorNodeContractQuery.blockNumber
}
// Returns gas estimation for the EVM execution
func (mirrorNodeContractQuery *mirrorNodeContractQuery) estimateGas(client *Client) (uint64, error) {
err := mirrorNodeContractQuery.fillEvmAddresses()
if err != nil {
return 0, err
}
jsonPayload, err := mirrorNodeContractQuery.createJSONPayload(true, "latest")
if err != nil {
return 0, err
}
result, err := mirrorNodeContractQuery.performContractCallToMirrorNode(client, jsonPayload)
if err != nil {
return 0, err
}
hexString, ok := result["result"].(string)
if !ok {
return 0, fmt.Errorf("result is not a string")
}
hexString = strings.TrimPrefix(hexString, "0x")
gas, err := strconv.ParseUint(hexString, 16, 64)
if err != nil {
return 0, fmt.Errorf("failed to parse the result: %w", err)
}
return gas, nil
}
// Does transient simulation of read-write operations and returns the result in hexadecimal string format. The result can be any solidity type.
func (mirrorNodeContractQuery *mirrorNodeContractQuery) call(client *Client) (string, error) {
err := mirrorNodeContractQuery.fillEvmAddresses()
if err != nil {
return "", err
}
var blockNumber string
if mirrorNodeContractQuery.blockNumber == nil {
blockNumber = "latest"
} else {
blockNumber = fmt.Sprintf("%d", *mirrorNodeContractQuery.blockNumber)
}
jsonPayload, err := mirrorNodeContractQuery.createJSONPayload(false, blockNumber)
if err != nil {
return "", err
}
result, err := mirrorNodeContractQuery.performContractCallToMirrorNode(client, jsonPayload)
if err != nil {
return "", err
}
hexString, ok := result["result"].(string)
if !ok {
return "", fmt.Errorf("result is not a string")
}
return hexString, nil
}
// Retrieve and set the evm addresses if necessary
func (mirrorNodeContractQuery *mirrorNodeContractQuery) fillEvmAddresses() error {
// fill contractEvmAddress
if mirrorNodeContractQuery.contractEvmAddress == nil {
if mirrorNodeContractQuery.contractID == nil {
return errors.New("contractID is not set")
}
address := mirrorNodeContractQuery.contractID.ToEvmAddress()
mirrorNodeContractQuery.contractEvmAddress = &address
}
// fill senderEvmAddress
if mirrorNodeContractQuery.senderEvmAddress == nil && mirrorNodeContractQuery.sender != nil {
address := mirrorNodeContractQuery.sender.ToEvmAddress()
mirrorNodeContractQuery.senderEvmAddress = &address
}
return nil
}
func (mirrorNodeContractQuery *mirrorNodeContractQuery) performContractCallToMirrorNode(client *Client, jsonPayload string) (map[string]any, error) {
if client.mirrorNetwork == nil || len(client.GetMirrorNetwork()) == 0 {
return nil, errors.New("mirror node is not set")
}
mirrorUrl, err := client.GetMirrorRestApiBaseUrl()
if err != nil {
return nil, err
}
isLocalHost := strings.Contains(mirrorUrl, "localhost") || strings.Contains(mirrorUrl, "127.0.0.1")
if isLocalHost {
mirrorUrl = "http://localhost:8545/api/v1"
}
mirrorUrl = fmt.Sprintf("%s/contracts/call", mirrorUrl)
resp, err := mirrorNodePostWithRetry(client, mirrorUrl, "application/json", []byte(jsonPayload), mirrorNodeDefaultMaxAttempts, mirrorNodeDefaultTimeout)
if err != nil {
return nil, fmt.Errorf("failed to send request: %w", err)
}
if resp == nil {
return nil, errors.New("received nil response from Mirror Node")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("received non-200 response from Mirror Node: %d, details: %s", resp.StatusCode, body)
}
var result map[string]any
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, err
}
return result, nil
}
func (mirrorNodeContractQuery *mirrorNodeContractQuery) createJSONPayload(estimate bool, blockNumber string) (string, error) {
hexData := hex.EncodeToString(mirrorNodeContractQuery.callData)
payload := map[string]any{
"data": hexData,
"to": mirrorNodeContractQuery.contractEvmAddress,
"estimate": estimate,
"blockNumber": blockNumber,
}
// Conditionally add fields if they are set to non-default values
if mirrorNodeContractQuery.senderEvmAddress != nil {
payload["from"] = mirrorNodeContractQuery.senderEvmAddress
}
if mirrorNodeContractQuery.gasLimit != nil {
payload["gas"] = mirrorNodeContractQuery.gasLimit
}
if mirrorNodeContractQuery.gasPrice != nil {
payload["gasPrice"] = mirrorNodeContractQuery.gasPrice
}
if mirrorNodeContractQuery.value != nil {
payload["value"] = mirrorNodeContractQuery.value
}
jsonBytes, err := json.Marshal(payload)
if err != nil {
return "", fmt.Errorf("failed to marshal JSON: %w", err)
}
return string(jsonBytes), nil
}