/
rpcClient.SmartContract.go
206 lines (182 loc) · 6.49 KB
/
rpcClient.SmartContract.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
package rpc
import (
"fmt"
"github.com/joeqian10/neo3-gogogo/crypto"
"github.com/joeqian10/neo3-gogogo/helper"
"github.com/joeqian10/neo3-gogogo/rpc/models"
"github.com/joeqian10/neo3-gogogo/sc"
)
type InvokeResultResponse struct {
RpcResponse
ErrorResponse
Result models.InvokeResult `json:"result"`
}
type TraverseIteratorResponse struct {
RpcResponse
ErrorResponse
Result []models.InvokeStack `json:"result"`
}
type TerminateSessionResponse struct {
RpcResponse
ErrorResponse
Result bool `json:"result"`
}
type GetUnclaimedGasResponse struct {
RpcResponse
ErrorResponse
Result models.UnclaimedGas `json:"result"`
}
// InvokeFunction params: scriptHash and method are necessary, set args = nil, signersOrWitnesses = nil, useDiagnostic = false if not necessary
func (n *RpcClient) InvokeFunction(scriptHash string, method string, args []models.RpcContractParameter,
signersOrWitnesses interface{}, useDiagnostic bool) InvokeResultResponse {
u, err := helper.UInt160FromString(scriptHash)
if err != nil {
return InvokeResultResponse{
ErrorResponse: ErrorResponse{Error: RpcError{Code: -1, Message: "invalid contract script hash: " + err.Error()}},
}
}
callArgs := make([]interface{}, len(args))
for i, _ := range args {
t, err := sc.NewContractParameterTypeFromString(args[i].Type)
if err != nil {
return InvokeResultResponse{
ErrorResponse: ErrorResponse{Error: RpcError{Code: -1, Message: err.Error()}},
}
}
callArgs[i] = &sc.ContractParameter{
Type: t,
Value: args[i].Value,
}
}
script, err := sc.MakeScript(u, method, callArgs)
if err != nil {
return InvokeResultResponse{
ErrorResponse: ErrorResponse{Error: RpcError{Code: -1, Message: err.Error()}},
}
}
return n.InvokeScript(crypto.Base64Encode(script), signersOrWitnesses, useDiagnostic)
//response := InvokeResultResponse{}
//params := []interface{}{scriptHash, method}
//if args != nil {
// params = append(params, args) // params[2]
//}
//
//if signers, ok := signersOrWitnesses.([]models.RpcSigner); ok {
// if len(params) == 2 {
// params = append(params, []models.RpcContractParameter{}) // params[2]
// }
// params = append(params, signers) // params[3]
//} else if witnesses, ok := signersOrWitnesses.([]models.RpcWitness); ok {
// if len(params) == 2 {
// params = append(params, []models.RpcContractParameter{}) // params[2]
// }
// params = append(params, witnesses) // params[3]
//}
//
//if useDiagnostic {
// if len(params) == 2 {
// params = append(params, []models.RpcContractParameter{}) // params[2]
// params = append(params, []models.RpcSigner{}) // params[3]
// } else if len(params) == 3 {
// params = append(params, []models.RpcSigner{}) // params[3]
// }
// params = append(params, useDiagnostic) // params[4]
//}
//
//_ = n.makeRequest("invokefunction", params, &response)
//return response
}
// InvokeScript params: scriptInBase64 is necessary, set signersOrWitnesses = nil, useDiagnostic = false if not necessary
func (n *RpcClient) InvokeScript(scriptInBase64 string, signersOrWitnesses interface{}, useDiagnostic bool) InvokeResultResponse {
response := InvokeResultResponse{}
params := []interface{}{scriptInBase64}
if signers, ok := signersOrWitnesses.([]models.RpcSigner); ok {
params = append(params, signers) // params[1]
} else if witnesses, ok := signersOrWitnesses.([]models.RpcWitness); ok {
params = append(params, witnesses) // params[1]
}
if useDiagnostic {
if len(params) == 1 {
params = append(params, []models.RpcSigner{}) // params[1]
}
params = append(params, useDiagnostic) // params[2]
}
_ = n.makeRequest("invokescript", params, &response)
return response
}
func (n *RpcClient) TraverseIterator(sessionId string, iteratorId string, count int32) TraverseIteratorResponse {
response := TraverseIteratorResponse{}
params := []interface{}{sessionId, iteratorId, count}
_ = n.makeRequest("traverseiterator", params, &response)
return response
}
func (n *RpcClient) TerminateSession(sessionId string) TerminateSessionResponse {
response := TerminateSessionResponse{}
params := []interface{}{sessionId}
_ = n.makeRequest("terminatesession", params, &response)
return response
}
func (n *RpcClient) GetUnclaimedGas(address string) GetUnclaimedGasResponse {
response := GetUnclaimedGasResponse{}
params := []interface{}{address}
_ = n.makeRequest("getunclaimedgas", params, &response)
return response
}
func (n *RpcClient) InvokeFunctionAndIterate(scriptHash string, method string, args []models.RpcContractParameter,
signersOrWitnesses interface{}, useDiagnostic bool, count int32) ([][]models.InvokeStack, error) {
u, err := helper.UInt160FromString(scriptHash)
if err != nil {
return nil, err
}
callArgs := make([]interface{}, len(args))
for i, _ := range args {
t, err := sc.NewContractParameterTypeFromString(args[i].Type)
if err != nil {
return nil, err
}
callArgs[i] = sc.ContractParameter{Type: t, Value: args[i].Value}
}
script, err := sc.MakeScript(u, method, callArgs)
if err != nil {
return nil, err
}
return n.InvokeScriptAndIterate(crypto.Base64Encode(script), signersOrWitnesses, useDiagnostic, count)
}
func (n *RpcClient) InvokeScriptAndIterate(scriptInBase64 string, signersOrWitnesses interface{}, useDiagnostic bool,
count int32) ([][]models.InvokeStack, error) {
if count > 10000 {
return nil, fmt.Errorf("max count exceeded, count should not exceed 10000")
}
if count < 0 {
return nil, fmt.Errorf("count should not be less than zero")
}
response := n.InvokeScript(scriptInBase64, signersOrWitnesses, useDiagnostic)
invokeStacks, err := PopInvokeStacks(response)
if err != nil {
return nil, err
}
sessionId := response.Result.Session
if sessionId == "" {
return nil, nil
}
var iterateStacks [][]models.InvokeStack // this array is for all the iterators in this session
for _, invokeStack := range invokeStacks {
if invokeStack.Type == "InteropInterface" &&
invokeStack.Interface == "IIterator" &&
invokeStack.Id != "" {
var iteStacks []models.InvokeStack // this array is for one unique iterator id
for ; count > 0; count -= 100 {
iterateResponse := n.TraverseIterator(sessionId, invokeStack.Id, 100) // max iterate count is 100 for one request
if iterateResponse.HasError() {
return nil, fmt.Errorf(iterateResponse.GetErrorInfo())
}
if len(iterateResponse.Result) == 0 { // the iterator reaches the end
break
}
iteStacks = append(iteStacks, iterateResponse.Result...)
}
iterateStacks = append(iterateStacks, iteStacks)
}
}
return iterateStacks, nil
}