forked from gnolang/gno
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_grc1155_token.gno
347 lines (274 loc) · 8.93 KB
/
basic_grc1155_token.gno
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
package grc1155
import (
"std"
"gno.land/p/demo/avl"
"gno.land/p/demo/ufmt"
)
type basicGRC1155Token struct {
uri string
balances avl.Tree // "TokenId:Address" -> uint64
operatorApprovals avl.Tree // "OwnerAddress:OperatorAddress" -> bool
}
var _ IGRC1155 = (*basicGRC1155Token)(nil)
// Returns new basic GRC1155 token
func NewBasicGRC1155Token(uri string) *basicGRC1155Token {
return &basicGRC1155Token{
uri: uri,
balances: avl.Tree{},
operatorApprovals: avl.Tree{},
}
}
func (s *basicGRC1155Token) Uri() string { return s.uri }
// BalanceOf returns the input address's balance of the token type requested
func (s *basicGRC1155Token) BalanceOf(addr std.Address, tid TokenID) (uint64, error) {
if !isValidAddress(addr) {
return 0, ErrInvalidAddress
}
key := string(tid) + ":" + addr.String()
balance, found := s.balances.Get(key)
if !found {
return 0, nil
}
return balance.(uint64), nil
}
// BalanceOfBatch returns the balance of multiple account/token pairs
func (s *basicGRC1155Token) BalanceOfBatch(owners []std.Address, batch []TokenID) ([]uint64, error) {
if len(owners) != len(batch) {
return nil, ErrMismatchLength
}
balanceOfBatch := make([]uint64, len(owners))
for i := 0; i < len(owners); i++ {
balanceOfBatch[i], _ = s.BalanceOf(owners[i], batch[i])
}
return balanceOfBatch, nil
}
// SetApprovalForAll can approve the operator to operate on all tokens
func (s *basicGRC1155Token) SetApprovalForAll(operator std.Address, approved bool) error {
if !isValidAddress(operator) {
return ErrInvalidAddress
}
caller := std.OrigCaller()
return s.setApprovalForAll(caller, operator, approved)
}
// IsApprovedForAll returns true if operator is the owner or is approved for all by the owner.
// Otherwise, returns false
func (s *basicGRC1155Token) IsApprovedForAll(owner, operator std.Address) bool {
if operator == owner {
return true
}
key := owner.String() + ":" + operator.String()
_, found := s.operatorApprovals.Get(key)
if !found {
return false
}
return true
}
// Safely transfers `tokenId` token from `from` to `to`, checking that
// contract recipients are aware of the GRC1155 protocol to prevent
// tokens from being forever locked.
func (s *basicGRC1155Token) SafeTransferFrom(from, to std.Address, tid TokenID, amount uint64) error {
caller := std.OrigCaller()
if !s.IsApprovedForAll(caller, from) {
return ErrCallerIsNotOwnerOrApproved
}
err := s.safeBatchTransferFrom(from, to, []TokenID{tid}, []uint64{amount})
if err != nil {
return err
}
if !s.doSafeTransferAcceptanceCheck(caller, from, to, tid, amount) {
return ErrTransferToRejectedOrNonGRC1155Receiver
}
emit(&TransferSingleEvent{caller, from, to, tid, amount})
return nil
}
// Safely transfers a `batch` of tokens from `from` to `to`, checking that
// contract recipients are aware of the GRC1155 protocol to prevent
// tokens from being forever locked.
func (s *basicGRC1155Token) SafeBatchTransferFrom(from, to std.Address, batch []TokenID, amounts []uint64) error {
caller := std.OrigCaller()
if !s.IsApprovedForAll(caller, from) {
return ErrCallerIsNotOwnerOrApproved
}
err := s.safeBatchTransferFrom(from, to, batch, amounts)
if err != nil {
return err
}
if !s.doSafeBatchTransferAcceptanceCheck(caller, from, to, batch, amounts) {
return ErrTransferToRejectedOrNonGRC1155Receiver
}
emit(&TransferBatchEvent{caller, from, to, batch, amounts})
return nil
}
// Creates `amount` tokens of token type `id`, and assigns them to `to`. Also checks that
// contract recipients are using GRC1155 protocol.
func (s *basicGRC1155Token) SafeMint(to std.Address, tid TokenID, amount uint64) error {
caller := std.OrigCaller()
err := s.mintBatch(to, []TokenID{tid}, []uint64{amount})
if err != nil {
return err
}
if !s.doSafeTransferAcceptanceCheck(caller, zeroAddress, to, tid, amount) {
return ErrTransferToRejectedOrNonGRC1155Receiver
}
emit(&TransferSingleEvent{caller, zeroAddress, to, tid, amount})
return nil
}
// Batch version of `SafeMint()`. Also checks that
// contract recipients are using GRC1155 protocol.
func (s *basicGRC1155Token) SafeBatchMint(to std.Address, batch []TokenID, amounts []uint64) error {
caller := std.OrigCaller()
err := s.mintBatch(to, batch, amounts)
if err != nil {
return err
}
if !s.doSafeBatchTransferAcceptanceCheck(caller, zeroAddress, to, batch, amounts) {
return ErrTransferToRejectedOrNonGRC1155Receiver
}
emit(&TransferBatchEvent{caller, zeroAddress, to, batch, amounts})
return nil
}
// Destroys `amount` tokens of token type `id` from `from`.
func (s *basicGRC1155Token) Burn(from std.Address, tid TokenID, amount uint64) error {
caller := std.OrigCaller()
err := s.burnBatch(from, []TokenID{tid}, []uint64{amount})
if err != nil {
return err
}
emit(&TransferSingleEvent{caller, from, zeroAddress, tid, amount})
return nil
}
// Batch version of `Burn()`
func (s *basicGRC1155Token) BatchBurn(from std.Address, batch []TokenID, amounts []uint64) error {
caller := std.OrigCaller()
err := s.burnBatch(from, batch, amounts)
if err != nil {
return err
}
emit(&TransferBatchEvent{caller, from, zeroAddress, batch, amounts})
return nil
}
/* Helper methods */
// Helper for SetApprovalForAll(): approve `operator` to operate on all of `owner` tokens
func (s *basicGRC1155Token) setApprovalForAll(owner, operator std.Address, approved bool) error {
if owner == operator {
return nil
}
key := owner.String() + ":" + operator.String()
if approved {
s.operatorApprovals.Set(key, approved)
} else {
s.operatorApprovals.Remove(key)
}
emit(&ApprovalForAllEvent{owner, operator, approved})
return nil
}
// Helper for SafeTransferFrom() and SafeBatchTransferFrom()
func (s *basicGRC1155Token) safeBatchTransferFrom(from, to std.Address, batch []TokenID, amounts []uint64) error {
if len(batch) != len(amounts) {
return ErrMismatchLength
}
if !isValidAddress(from) || !isValidAddress(to) {
return ErrInvalidAddress
}
if from == to {
return ErrCannotTransferToSelf
}
caller := std.OrigCaller()
s.beforeTokenTransfer(caller, from, to, batch, amounts)
for i := 0; i < len(batch); i++ {
tid := batch[i]
amount := amounts[i]
fromBalance, err := s.BalanceOf(from, tid)
if err != nil {
return err
}
if fromBalance < amount {
return ErrInsufficientBalance
}
toBalance, err := s.BalanceOf(to, tid)
if err != nil {
return err
}
fromBalance -= amount
toBalance += amount
fromBalanceKey := string(tid) + ":" + from.String()
toBalanceKey := string(tid) + ":" + to.String()
s.balances.Set(fromBalanceKey, fromBalance)
s.balances.Set(toBalanceKey, toBalance)
}
s.afterTokenTransfer(caller, from, to, batch, amounts)
return nil
}
// Helper for SafeMint() and SafeBatchMint()
func (s *basicGRC1155Token) mintBatch(to std.Address, batch []TokenID, amounts []uint64) error {
if len(batch) != len(amounts) {
return ErrMismatchLength
}
if !isValidAddress(to) {
return ErrInvalidAddress
}
caller := std.OrigCaller()
s.beforeTokenTransfer(caller, zeroAddress, to, batch, amounts)
for i := 0; i < len(batch); i++ {
tid := batch[i]
amount := amounts[i]
toBalance, err := s.BalanceOf(to, tid)
if err != nil {
return err
}
toBalance += amount
toBalanceKey := string(tid) + ":" + to.String()
s.balances.Set(toBalanceKey, toBalance)
}
s.afterTokenTransfer(caller, zeroAddress, to, batch, amounts)
return nil
}
// Helper for Burn() and BurnBatch()
func (s *basicGRC1155Token) burnBatch(from std.Address, batch []TokenID, amounts []uint64) error {
if len(batch) != len(amounts) {
return ErrMismatchLength
}
if !isValidAddress(from) {
return ErrInvalidAddress
}
caller := std.OrigCaller()
s.beforeTokenTransfer(caller, from, zeroAddress, batch, amounts)
for i := 0; i < len(batch); i++ {
tid := batch[i]
amount := amounts[i]
fromBalance, err := s.BalanceOf(from, tid)
if err != nil {
return err
}
if fromBalance < amount {
return ErrBurnAmountExceedsBalance
}
fromBalance -= amount
fromBalanceKey := string(tid) + ":" + from.String()
s.balances.Set(fromBalanceKey, fromBalance)
}
s.afterTokenTransfer(caller, from, zeroAddress, batch, amounts)
return nil
}
func (s *basicGRC1155Token) setUri(newUri string) {
s.uri = newUri
emit(&UpdateURIEvent{newUri})
}
func (s *basicGRC1155Token) beforeTokenTransfer(operator, from, to std.Address, batch []TokenID, amounts []uint64) {
// TODO: Implementation
}
func (s *basicGRC1155Token) afterTokenTransfer(operator, from, to std.Address, batch []TokenID, amounts []uint64) {
// TODO: Implementation
}
func (s *basicGRC1155Token) doSafeTransferAcceptanceCheck(operator, from, to std.Address, tid TokenID, amount uint64) bool {
// TODO: Implementation
return true
}
func (s *basicGRC1155Token) doSafeBatchTransferAcceptanceCheck(operator, from, to std.Address, batch []TokenID, amounts []uint64) bool {
// TODO: Implementation
return true
}
func (s *basicGRC1155Token) RenderHome() (str string) {
str += ufmt.Sprintf("# URI:%s\n", s.uri)
return
}