-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathcontract.go
318 lines (280 loc) · 8.42 KB
/
contract.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
/*
* Copyright (C) 2019 Zilliqa
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package contract
import (
"errors"
"github.com/Zilliqa/gozilliqa-sdk/v3/account"
"github.com/Zilliqa/gozilliqa-sdk/v3/core"
"github.com/Zilliqa/gozilliqa-sdk/v3/provider"
"github.com/Zilliqa/gozilliqa-sdk/v3/transaction"
"github.com/Zilliqa/gozilliqa-sdk/v3/util"
"strconv"
"strings"
)
type ContractStatus int
const MainNet = "mainnet"
const TestNet = "testnet"
const Isolated = "isolated"
const TestNetHost = "https://dev-api.zilliqa.com/"
const MainNetHost = "https://api.zilliqa.com/"
const IsolatedHost = "https://zilliqa-isolated-server.zilliqa.com/"
const (
Deployed ContractStatus = iota
Rejected
Initialised
)
type Contract struct {
Init []core.ContractValue `json:"init"`
Abi string `json:"abi"`
State State `json:"state"`
Address string `json:"address"`
Code string `json:"code"`
ContractStatus ContractStatus `json:"contractStatus"`
Signer *account.Wallet
Provider *provider.Provider
}
type State struct {
core.ContractValue
}
// This a shortcut function to deploy smart contract, all following parameters would remain default:
// network id, message version nonce, gasfee, gaslimit
// take a note that for gas limit, 40k is safe and recommend setting number
// value of network can be testnet, mainnet or isolated
func (c *Contract) DeployTo(network string) (*transaction.Transaction, error) {
if network == TestNet {
c.Provider = provider.NewProvider(TestNetHost)
gasPrice, err := c.Provider.GetMinimumGasPrice()
if err != nil {
return nil, err
}
parameter := DeployParams{
Version: strconv.FormatInt(int64(util.Pack(333, 1)), 10),
Nonce: "",
GasPrice: gasPrice,
GasLimit: "40000",
SenderPubKey: "",
}
return c.Deploy(parameter)
} else if network == MainNet {
c.Provider = provider.NewProvider(MainNetHost)
gasPrice, err := c.Provider.GetMinimumGasPrice()
if err != nil {
return nil, err
}
parameter := DeployParams{
Version: strconv.FormatInt(int64(util.Pack(1, 1)), 10),
Nonce: "",
GasPrice: gasPrice,
GasLimit: "40000",
SenderPubKey: "",
}
return c.Deploy(parameter)
} else if network == Isolated {
c.Provider = provider.NewProvider(IsolatedHost)
gasPrice, err := c.Provider.GetMinimumGasPrice()
if err != nil {
return nil, err
}
parameter := DeployParams{
Version: strconv.FormatInt(int64(util.Pack(222, 1)), 10),
Nonce: "",
GasPrice: gasPrice,
GasLimit: "40000",
SenderPubKey: "",
}
return c.Deploy(parameter)
} else {
return nil, errors.New("unsupported network, please use testnet, mainnet or isolated")
}
}
func (c *Contract) Deploy(params DeployParams) (*transaction.Transaction, error) {
if c.Code == "" || c.Init == nil || len(c.Init) == 0 {
return nil, errors.New("Cannot deploy without code or initialisation parameters.")
}
tx := &transaction.Transaction{
ID: params.ID,
Version: params.Version,
Nonce: params.Nonce,
Amount: "0",
GasPrice: params.GasPrice,
GasLimit: params.GasLimit,
Signature: "",
Receipt: core.TransactionReceipt{},
SenderPubKey: params.SenderPubKey,
ToAddr: "0000000000000000000000000000000000000000",
Code: strings.ReplaceAll(c.Code, "/\\", ""),
Data: c.Init,
Status: 0,
Priority: params.Priority,
}
err2 := c.Signer.Sign(tx, *c.Provider)
if err2 != nil {
return nil, err2
}
rsp, err := c.Provider.CreateTransaction(tx.ToTransactionPayload())
if err != nil {
return nil, err
}
if rsp == nil {
return nil, errors.New("rpc response is nil")
}
if rsp.Error != nil {
return nil, errors.New(rsp.Error.Message)
}
result := rsp.Result.(map[string]interface{})
hash := result["TranID"].(string)
contractAddress := result["ContractAddress"].(string)
tx.ID = hash
tx.ContractAddress = contractAddress
return tx, nil
}
func (c *Contract) Sign(transition string, args []core.ContractValue, params CallParams, priority bool) (error, *transaction.Transaction) {
if c.Address == "" {
_ = errors.New("Contract has not been deployed!")
}
data := Data{
Tag: transition,
Params: args,
}
tx := &transaction.Transaction{
ID: params.ID,
Version: params.Version,
Nonce: params.Nonce,
Amount: params.Amount,
GasPrice: params.GasPrice,
GasLimit: params.GasLimit,
Signature: "",
Receipt: core.TransactionReceipt{},
SenderPubKey: params.SenderPubKey,
ToAddr: c.Address,
Code: strings.ReplaceAll(c.Code, "/\\", ""),
Data: data,
Status: 0,
Priority: priority,
}
err2 := c.Signer.Sign(tx, *c.Provider)
if err2 != nil {
return err2, nil
}
return nil, tx
}
func (c *Contract) CallFor(transition string, args []core.ContractValue, priority bool, amount string, network string) (*transaction.Transaction, error) {
if network == TestNet {
c.Provider = provider.NewProvider(TestNetHost)
gasPrice, err := c.Provider.GetMinimumGasPrice()
if err != nil {
return nil, err
}
params := CallParams{
Version: strconv.FormatInt(int64(util.Pack(333, 1)), 10),
Nonce: "",
GasPrice: gasPrice,
GasLimit: "40000",
Amount: amount,
SenderPubKey: "",
}
return c.Call(transition, args, params, priority)
} else if network == MainNet {
c.Provider = provider.NewProvider(MainNetHost)
gasPrice, err := c.Provider.GetMinimumGasPrice()
if err != nil {
return nil, err
}
params := CallParams{
Version: strconv.FormatInt(int64(util.Pack(1, 1)), 10),
Nonce: "",
GasPrice: gasPrice,
GasLimit: "40000",
Amount: amount,
SenderPubKey: "",
}
return c.Call(transition, args, params, priority)
} else if network == Isolated {
c.Provider = provider.NewProvider(IsolatedHost)
gasPrice, err := c.Provider.GetMinimumGasPrice()
if err != nil {
return nil, err
}
params := CallParams{
Version: strconv.FormatInt(int64(util.Pack(222, 1)), 10),
Nonce: "",
GasPrice: gasPrice,
GasLimit: "40000",
Amount: amount,
SenderPubKey: "",
}
return c.Call(transition, args, params, priority)
} else {
return nil, errors.New("unsupported network, please use testnet, mainnet or isolated")
}
}
func (c *Contract) Call(transition string, args []core.ContractValue, params CallParams, priority bool) (*transaction.Transaction, error) {
if c.Address == "" {
_ = errors.New("Contract has not been deployed!")
}
data := Data{
Tag: transition,
Params: args,
}
tx := &transaction.Transaction{
ID: params.ID,
Version: params.Version,
Nonce: params.Nonce,
Amount: params.Amount,
GasPrice: params.GasPrice,
GasLimit: params.GasLimit,
Signature: "",
Receipt: core.TransactionReceipt{},
SenderPubKey: params.SenderPubKey,
ToAddr: c.Address,
Code: strings.ReplaceAll(c.Code, "/\\", ""),
Data: data,
Status: 0,
Priority: priority,
}
err2 := c.Signer.Sign(tx, *c.Provider)
if err2 != nil {
return tx, err2
}
rsp, err := c.Provider.CreateTransaction(tx.ToTransactionPayload())
if err != nil {
return tx, err
}
if rsp == nil {
return tx, errors.New("rpc response is nil")
}
if rsp.Error != nil {
return tx, errors.New(rsp.Error.Message)
}
result := rsp.Result.(map[string]interface{})
hash := result["TranID"].(string)
tx.ID = hash
if tx.Status == core.Rejected {
c.ContractStatus = Rejected
return tx, nil
}
return tx, nil
}
func (c *Contract) IsInitialised() bool {
return c.ContractStatus == Initialised
}
func (c *Contract) IsDeployed() bool {
return c.ContractStatus == Deployed
}
func (c *Contract) IsRejected() bool {
return c.ContractStatus == Rejected
}