-
Notifications
You must be signed in to change notification settings - Fork 113
/
token721.go
411 lines (362 loc) · 11.7 KB
/
token721.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package native
import (
"encoding/json"
"errors"
"strconv"
"fmt"
"github.com/iost-official/go-iost/core/contract"
"github.com/iost-official/go-iost/vm/host"
)
var token721ABIs *abiSet
// const prefix
const (
Token721InfoMapPrefix = "T721I"
Token721BalanceMapPrefix = "T721B"
Token721IssuerMapField = "T721issuer"
Token721MetadataMapPrefix = "T721M"
Token721MetadataKeySeparator = "#"
)
func init() {
token721ABIs = newAbiSet()
token721ABIs.Register(initToken721ABI, true)
token721ABIs.Register(createToken721ABI)
token721ABIs.Register(issueToken721ABI)
token721ABIs.Register(transferToken721ABI)
token721ABIs.Register(balanceOfToken721ABI)
token721ABIs.Register(ownerOfToken721ABI)
token721ABIs.Register(tokenOfOwnerByIndexToken721ABI)
token721ABIs.Register(tokenMetadataToken721ABI)
}
func checkToken721Exists(h *host.Host, tokenSym string) (ok bool, cost contract.Cost) {
exists, cost0 := h.MapHas(Token721InfoMapPrefix+tokenSym, Token721IssuerMapField)
return exists, cost0
}
func getToken721Balance(h *host.Host, tokenSym string, from string) (balance int64, cost contract.Cost, err error) {
balance = int64(0)
cost = contract.Cost0()
ok, cost0 := h.MapHas(Token721BalanceMapPrefix+from, tokenSym)
cost.AddAssign(cost0)
if ok {
tmp, cost0 := h.MapGet(Token721BalanceMapPrefix+from, tokenSym)
cost.AddAssign(cost0)
balance = tmp.(int64)
}
return balance, cost, nil
}
func setToken721Balance(h *host.Host, tokenSym string, from string, balance int64, ramPayer string) (cost contract.Cost) {
cost, _ = h.MapPut(Token721BalanceMapPrefix+from, tokenSym, balance, ramPayer)
return cost
}
var (
initToken721ABI = &abi{
name: "init",
args: []string{},
do: func(h *host.Host, args ...interface{}) (rtn []interface{}, cost contract.Cost, err error) {
return []interface{}{}, host.CommonErrorCost(1), nil
},
}
createToken721ABI = &abi{
name: "create",
args: []string{"string", "string", "number"},
do: func(h *host.Host, args ...interface{}) (rtn []interface{}, cost contract.Cost, err error) {
cost = contract.Cost0()
cost.AddAssign(host.CommonOpCost(1))
tokenSym := args[0].(string)
issuer := args[1].(string)
totalSupply := args[2].(int64)
cost.AddAssign(host.CommonOpCost(1))
err = checkTokenSymValid(tokenSym)
if err != nil {
return nil, cost, err
}
// check auth
ok, cost0 := h.RequireAuth(issuer, TokenPermission)
cost.AddAssign(cost0)
if !ok {
return nil, cost, host.ErrPermissionLost
}
if !CheckCost(h, cost) {
return nil, cost, host.ErrOutOfGas
}
// check exists
ok, cost0 = checkToken721Exists(h, tokenSym)
cost.AddAssign(cost0)
if ok {
return nil, cost, host.ErrTokenExists
}
publisher := h.Context().Value("publisher").(string)
// put info
cost0, _ = h.MapPut(Token721InfoMapPrefix+tokenSym, Token721IssuerMapField, issuer, publisher)
cost.AddAssign(cost0)
cost0, _ = h.MapPut(Token721InfoMapPrefix+tokenSym, TotalSupplyMapField, totalSupply, publisher)
cost.AddAssign(cost0)
cost0, _ = h.MapPut(Token721InfoMapPrefix+tokenSym, SupplyMapField, int64(0), publisher)
cost.AddAssign(cost0)
// generate receipt
message, err := json.Marshal(args)
cost.AddAssign(host.CommonOpCost(1))
if err != nil {
return nil, cost, err
}
cost0 = h.Receipt(string(message))
cost.AddAssign(cost0)
return []interface{}{}, cost, nil
},
}
issueToken721ABI = &abi{
name: "issue",
args: []string{"string", "string", "string"},
do: func(h *host.Host, args ...interface{}) (rtn []interface{}, cost contract.Cost, err error) {
cost = contract.Cost0()
cost.AddAssign(host.CommonOpCost(1))
tokenSym := args[0].(string)
to := args[1].(string)
metaDataJSON := args[2].(string)
// get token info
ok, cost0 := checkToken721Exists(h, tokenSym)
cost.AddAssign(cost0)
if !ok {
return nil, cost, host.ErrTokenNotExists
}
issuer, cost0 := h.MapGet(Token721InfoMapPrefix+tokenSym, Token721IssuerMapField)
cost.AddAssign(cost0)
supply, cost0 := h.MapGet(Token721InfoMapPrefix+tokenSym, SupplyMapField)
cost.AddAssign(cost0)
totalSupply, cost0 := h.MapGet(Token721InfoMapPrefix+tokenSym, TotalSupplyMapField)
cost.AddAssign(cost0)
if !CheckCost(h, cost) {
return nil, cost, host.ErrOutOfGas
}
// check supply
if totalSupply.(int64)-supply.(int64) <= 0 {
return nil, cost, errors.New("supply too much")
}
tokenID := strconv.FormatInt(supply.(int64), 10)
// check auth
ok, cost0 = h.RequireAuth(issuer.(string), TokenPermission)
cost.AddAssign(cost0)
if !ok {
return nil, cost, host.ErrPermissionLost
}
if !CheckCost(h, cost) {
return nil, cost, host.ErrOutOfGas
}
publisher := h.Context().Value("publisher").(string)
// set supply, set balance
cost0, err = h.MapPut(Token721InfoMapPrefix+tokenSym, SupplyMapField, supply.(int64)+1, publisher)
cost.AddAssign(cost0)
if err != nil {
return nil, cost, err
}
cost0, err = h.MapPut(Token721InfoMapPrefix+tokenSym, tokenID, to, publisher)
cost.AddAssign(cost0)
if err != nil {
return nil, cost, err
}
tbalance, cost0, err := getToken721Balance(h, tokenSym, to)
cost.AddAssign(cost0)
if err != nil {
return nil, cost, err
}
tbalance++
cost0 = setToken721Balance(h, tokenSym, to, tbalance, publisher)
cost.AddAssign(cost0)
cost0, err = h.MapPut(Token721MetadataMapPrefix+tokenSym+Token721MetadataKeySeparator+to, tokenID, metaDataJSON, publisher)
cost.AddAssign(cost0)
if err != nil {
return nil, cost, err
}
// generate receipt
message, err := json.Marshal(args)
cost.AddAssign(host.CommonOpCost(1))
if err != nil {
return nil, cost, err
}
cost0 = h.Receipt(string(message))
cost.AddAssign(cost0)
return []interface{}{tokenID}, cost, nil
},
}
transferToken721ABI = &abi{
name: "transfer",
args: []string{"string", "string", "string", "string"},
do: func(h *host.Host, args ...interface{}) (rtn []interface{}, cost contract.Cost, err error) {
cost = contract.Cost0()
cost.AddAssign(host.CommonOpCost(1))
tokenSym := args[0].(string)
from := args[1].(string)
to := args[2].(string)
tokenID := args[3].(string)
if from == to {
return []interface{}{}, cost, nil
}
// get token info
ok, cost0 := checkToken721Exists(h, tokenSym)
cost.AddAssign(cost0)
if !ok {
return nil, cost, host.ErrTokenNotExists
}
// check auth
ok, cost0 = h.RequireAuth(from, TransferPermission)
cost.AddAssign(cost0)
if !ok {
return nil, cost, host.ErrPermissionLost
}
if !CheckCost(h, cost) {
return nil, cost, host.ErrOutOfGas
}
tmp, cost0 := h.MapGet(Token721InfoMapPrefix+tokenSym, tokenID)
cost.AddAssign(cost0)
if tmp == nil {
return nil, cost, fmt.Errorf("error tokenID not exists. %v %v", tokenSym, tokenID)
}
owner := tmp.(string)
if owner != from {
return nil, cost, fmt.Errorf("error token owner isn't from. owner: %v, from: %v", owner, from)
}
publisher := h.Context().Value("publisher").(string)
cost0, err = h.MapPut(Token721InfoMapPrefix+tokenSym, tokenID, to, publisher)
cost.AddAssign(cost0)
if err != nil {
return nil, cost, err
}
fbalance, cost0, err := getToken721Balance(h, tokenSym, from)
cost.AddAssign(cost0)
if err != nil {
return nil, cost, err
}
tbalance, cost0, err := getToken721Balance(h, tokenSym, to)
cost.AddAssign(cost0)
if err != nil {
return nil, cost, err
}
fbalance--
tbalance++
cost0 = setToken721Balance(h, tokenSym, from, fbalance, publisher)
cost.AddAssign(cost0)
cost0 = setToken721Balance(h, tokenSym, to, tbalance, publisher)
cost.AddAssign(cost0)
metaDataJSON, cost0 := h.MapGet(Token721MetadataMapPrefix+tokenSym+Token721MetadataKeySeparator+from, tokenID)
cost.AddAssign(cost0)
cost0, err = h.MapDel(Token721MetadataMapPrefix+tokenSym+Token721MetadataKeySeparator+from, tokenID)
cost.AddAssign(cost0)
if err != nil {
return nil, cost, err
}
cost0, err = h.MapPut(Token721MetadataMapPrefix+tokenSym+Token721MetadataKeySeparator+to, tokenID, metaDataJSON, publisher)
cost.AddAssign(cost0)
if err != nil {
return nil, cost, err
}
// generate receipt
message, err := json.Marshal(args)
cost.AddAssign(host.CommonOpCost(1))
if err != nil {
return nil, cost, err
}
cost0 = h.Receipt(string(message))
cost.AddAssign(cost0)
return []interface{}{}, cost, nil
},
}
balanceOfToken721ABI = &abi{
name: "balanceOf",
args: []string{"string", "string"},
do: func(h *host.Host, args ...interface{}) (rtn []interface{}, cost contract.Cost, err error) {
cost = contract.Cost0()
cost.AddAssign(host.CommonOpCost(1))
tokenSym := args[0].(string)
to := args[1].(string)
// check token info
ok, cost0 := checkToken721Exists(h, tokenSym)
cost.AddAssign(cost0)
if !ok {
return nil, cost, host.ErrTokenNotExists
}
tbalance, cost0, err := getToken721Balance(h, tokenSym, to)
cost.AddAssign(cost0)
if err != nil {
return nil, cost, err
}
return []interface{}{tbalance}, cost, nil
},
}
ownerOfToken721ABI = &abi{
name: "ownerOf",
args: []string{"string", "string"},
do: func(h *host.Host, args ...interface{}) (rtn []interface{}, cost contract.Cost, err error) {
cost = contract.Cost0()
cost.AddAssign(host.CommonOpCost(1))
tokenSym := args[0].(string)
tokenID := args[1].(string)
// check token info
ok, cost0 := checkToken721Exists(h, tokenSym)
cost.AddAssign(cost0)
if !ok {
return nil, cost, host.ErrTokenNotExists
}
ok, cost0 = h.MapHas(Token721InfoMapPrefix+tokenSym, tokenID)
cost.AddAssign(cost0)
if !ok {
return nil, cost, host.ErrTokenExists
}
tmp, cost0 := h.MapGet(Token721InfoMapPrefix+tokenSym, tokenID)
cost.AddAssign(cost0)
owner := tmp.(string)
return []interface{}{owner}, cost, nil
},
}
tokenOfOwnerByIndexToken721ABI = &abi{
name: "tokenOfOwnerByIndex",
args: []string{"string", "string", "number"},
do: func(h *host.Host, args ...interface{}) (rtn []interface{}, cost contract.Cost, err error) {
cost = contract.Cost0()
cost.AddAssign(host.CommonOpCost(1))
tokenSym := args[0].(string)
owner := args[1].(string)
index := args[2].(int64)
ok, cost0 := checkToken721Exists(h, tokenSym)
cost.AddAssign(cost0)
if !ok {
return nil, cost, host.ErrTokenNotExists
}
tokens, cost0 := h.MapKeys(Token721MetadataMapPrefix + tokenSym + Token721MetadataKeySeparator + owner)
cost.AddAssign(cost0)
if int(index) >= len(tokens) {
return nil, cost, errors.New("out of range")
}
return []interface{}{tokens[index]}, cost, nil
},
}
tokenMetadataToken721ABI = &abi{
name: "tokenMetadata",
args: []string{"string", "string"},
do: func(h *host.Host, args ...interface{}) (rtn []interface{}, cost contract.Cost, err error) {
cost = contract.Cost0()
cost.AddAssign(host.CommonOpCost(1))
tokenSym := args[0].(string)
tokenID := args[1].(string)
ok, cost0 := checkToken721Exists(h, tokenSym)
cost.AddAssign(cost0)
if !ok {
return nil, cost, host.ErrTokenNotExists
}
ok, cost0 = h.MapHas(Token721InfoMapPrefix+tokenSym, tokenID)
cost.AddAssign(cost0)
if !ok {
return nil, cost, host.ErrTokenExists
}
tmp, cost0 := h.MapGet(Token721InfoMapPrefix+tokenSym, tokenID)
cost.AddAssign(cost0)
owner := tmp.(string)
ok, cost0 = h.MapHas(Token721MetadataMapPrefix+tokenSym+Token721MetadataKeySeparator+owner, tokenID)
cost.AddAssign(cost0)
if !ok {
return nil, cost, host.ErrTokenExists
}
metaDataJSON, cost0 := h.MapGet(Token721MetadataMapPrefix+tokenSym+Token721MetadataKeySeparator+owner, tokenID)
cost.AddAssign(cost0)
return []interface{}{metaDataJSON.(string)}, cost, nil
},
}
)