forked from Toniq-Labs/extendable-token
-
Notifications
You must be signed in to change notification settings - Fork 0
/
erc721.mo
408 lines (374 loc) · 12.8 KB
/
erc721.mo
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
/*
Cronics
*/
import Cycles "mo:base/ExperimentalCycles";
import HashMap "mo:base/HashMap";
import Principal "mo:base/Principal";
import Result "mo:base/Result";
import Array "mo:base/Array";
import Iter "mo:base/Iter";
import AID "../motoko/util/AccountIdentifier";
import ExtCore "../motoko/ext/Core";
import ExtCommon "../motoko/ext/Common";
import ExtAllowance "../motoko/ext/Allowance";
import ExtNonFungible "../motoko/ext/NonFungible";
import Option "mo:base/Option";
import Blob "mo:base/Blob";
import SVG "./svg";
import Text "mo:base/Text";
shared (install) actor class erc721_token(init_minter: Principal) = this {
// Types
type AccountIdentifier = ExtCore.AccountIdentifier;
type SubAccount = ExtCore.SubAccount;
type User = ExtCore.User;
type Balance = ExtCore.Balance;
type TokenIdentifier = ExtCore.TokenIdentifier;
type TokenIndex = ExtCore.TokenIndex ;
type Extension = ExtCore.Extension;
type CommonError = ExtCore.CommonError;
type BalanceRequest = ExtCore.BalanceRequest;
type BalanceResponse = ExtCore.BalanceResponse;
type TransferRequest = ExtCore.TransferRequest;
type TransferResponse = ExtCore.TransferResponse;
type AllowanceRequest = ExtAllowance.AllowanceRequest;
type ApproveRequest = ExtAllowance.ApproveRequest;
type Metadata = ExtCommon.Metadata;
type MintRequest = ExtNonFungible.MintRequest ;
//HTTP
type HeaderField = (Text, Text);
type HttpResponse = {
status_code: Nat16;
headers: [HeaderField];
body: Blob;
};
type HttpRequest = {
method : Text;
url : Text;
headers : [HeaderField];
body : Blob;
};
private let EXTENSIONS : [Extension] = ["@ext/common", "@ext/allowance", "@ext/nonfungible"];
//State work
private stable var _registryState : [(TokenIndex, AccountIdentifier)] = [];
private var _registry : HashMap.HashMap<TokenIndex, AccountIdentifier> = HashMap.fromIter(_registryState.vals(), 0, ExtCore.TokenIndex.equal, ExtCore.TokenIndex.hash);
private stable var _buyersState : [(AccountIdentifier, [TokenIndex])] = [];
private var _buyers : HashMap.HashMap<AccountIdentifier, [TokenIndex]> = HashMap.fromIter(_buyersState.vals(), 0, AID.equal, AID.hash);
private stable var _allowancesState : [(TokenIndex, Principal)] = [];
private var _allowances : HashMap.HashMap<TokenIndex, Principal> = HashMap.fromIter(_allowancesState.vals(), 0, ExtCore.TokenIndex.equal, ExtCore.TokenIndex.hash);
private stable var _tokenMetadataState : [(TokenIndex, Metadata)] = [];
private var _tokenMetadata : HashMap.HashMap<TokenIndex, Metadata> = HashMap.fromIter(_tokenMetadataState.vals(), 0, ExtCore.TokenIndex.equal, ExtCore.TokenIndex.hash);
private stable var _supply : Balance = 0;
private stable var _minter : Principal = init_minter;
private stable var _gifter : Principal = Principal.fromText("nraos-xaaaa-aaaah-qadqa-cai");
private stable var _nextTokenId : TokenIndex = 0;
private stable var _nextToSell : TokenIndex = 0;
//State functions
system func preupgrade() {
_registryState := Iter.toArray(_registry.entries());
_buyersState := Iter.toArray(_buyers.entries());
_allowancesState := Iter.toArray(_allowances.entries());
_tokenMetadataState := Iter.toArray(_tokenMetadata.entries());
};
system func postupgrade() {
_registryState := [];
_buyersState := [];
_allowancesState := [];
_tokenMetadataState := [];
};
public shared(msg) func disribute(user : User) : async () {
assert(msg.caller == _minter);
assert(_nextToSell < _nextTokenId);
let bearer = ExtCore.User.toAID(user);
_registry.put(_nextToSell, bearer);
switch (_buyers.get(bearer)) {
case (?nfts) {
_buyers.put(bearer, Array.append(nfts, [_nextToSell]));
};
case (_) {
_buyers.put(bearer, [_nextToSell]);
};
};
_nextToSell := _nextToSell + 1;
};
public shared(msg) func setMinter(minter : Principal) : async () {
assert(msg.caller == _minter);
_minter := minter;
};
public shared(msg) func freeGift(bearer : AccountIdentifier) : async ?TokenIndex {
assert(msg.caller == _gifter);
assert(_nextToSell < _nextTokenId);
if (_nextToSell < 5000) {
let tokenid = _nextToSell + 1000;
_registry.put(tokenid, bearer);
switch (_buyers.get(bearer)) {
case (?nfts) {
_buyers.put(bearer, Array.append(nfts, [tokenid]));
};
case (_) {
_buyers.put(bearer, [tokenid]);
};
};
_nextToSell := _nextToSell + 1;
return ?tokenid;
} else {
return null;
}
};
public shared(msg) func mintNFT(request : MintRequest) : async TokenIndex {
assert(msg.caller == _minter);
let receiver = ExtCore.User.toAID(request.to);
let token = _nextTokenId;
let md : Metadata = #nonfungible({
metadata = request.metadata;
});
_registry.put(token, receiver);
_tokenMetadata.put(token, md);
_supply := _supply + 1;
_nextTokenId := _nextTokenId + 1;
token;
};
public shared(msg) func transfer(request: TransferRequest) : async TransferResponse {
if (request.amount != 1) {
return #err(#Other("Must use amount of 1"));
};
if (ExtCore.TokenIdentifier.isPrincipal(request.token, Principal.fromActor(this)) == false) {
return #err(#InvalidToken(request.token));
};
let token = ExtCore.TokenIdentifier.getIndex(request.token);
let owner = ExtCore.User.toAID(request.from);
let spender = AID.fromPrincipal(msg.caller, request.subaccount);
let receiver = ExtCore.User.toAID(request.to);
switch (_registry.get(token)) {
case (?token_owner) {
if(AID.equal(owner, token_owner) == false) {
return #err(#Unauthorized(owner));
};
if (AID.equal(owner, spender) == false) {
switch (_allowances.get(token)) {
case (?token_spender) {
if(Principal.equal(msg.caller, token_spender) == false) {
return #err(#Unauthorized(spender));
};
};
case (_) {
return #err(#Unauthorized(spender));
};
};
};
_allowances.delete(token);
_registry.put(token, receiver);
return #ok(request.amount);
};
case (_) {
return #err(#InvalidToken(request.token));
};
};
};
public shared(msg) func approve(request: ApproveRequest) : async () {
if (ExtCore.TokenIdentifier.isPrincipal(request.token, Principal.fromActor(this)) == false) {
return;
};
let token = ExtCore.TokenIdentifier.getIndex(request.token);
let owner = AID.fromPrincipal(msg.caller, request.subaccount);
switch (_registry.get(token)) {
case (?token_owner) {
if(AID.equal(owner, token_owner) == false) {
return;
};
_allowances.put(token, request.spender);
return;
};
case (_) {
return;
};
};
};
public query func getSold() : async TokenIndex {
_nextToSell;
};
public query func getMinted() : async TokenIndex {
_nextTokenId;
};
public query func getMinter() : async Principal {
_minter;
};
public query func extensions() : async [Extension] {
EXTENSIONS;
};
public query func balance(request : BalanceRequest) : async BalanceResponse {
if (ExtCore.TokenIdentifier.isPrincipal(request.token, Principal.fromActor(this)) == false) {
return #err(#InvalidToken(request.token));
};
let token = ExtCore.TokenIdentifier.getIndex(request.token);
let aid = ExtCore.User.toAID(request.user);
switch (_registry.get(token)) {
case (?token_owner) {
if (AID.equal(aid, token_owner) == true) {
return #ok(1);
} else {
return #ok(0);
};
};
case (_) {
return #err(#InvalidToken(request.token));
};
};
};
public query func allowance(request : AllowanceRequest) : async Result.Result<Balance, CommonError> {
if (ExtCore.TokenIdentifier.isPrincipal(request.token, Principal.fromActor(this)) == false) {
return #err(#InvalidToken(request.token));
};
let token = ExtCore.TokenIdentifier.getIndex(request.token);
let owner = ExtCore.User.toAID(request.owner);
switch (_registry.get(token)) {
case (?token_owner) {
if (AID.equal(owner, token_owner) == false) {
return #err(#Other("Invalid owner"));
};
switch (_allowances.get(token)) {
case (?token_spender) {
if (Principal.equal(request.spender, token_spender) == true) {
return #ok(1);
} else {
return #ok(0);
};
};
case (_) {
return #ok(0);
};
};
};
case (_) {
return #err(#InvalidToken(request.token));
};
};
};
public query func index(token : TokenIdentifier) : async Result.Result<TokenIndex, CommonError> {
if (ExtCore.TokenIdentifier.isPrincipal(token, Principal.fromActor(this)) == false) {
return #err(#InvalidToken(token));
};
#ok(ExtCore.TokenIdentifier.getIndex(token));
};
public query func bearer(token : TokenIdentifier) : async Result.Result<AccountIdentifier, CommonError> {
if (ExtCore.TokenIdentifier.isPrincipal(token, Principal.fromActor(this)) == false) {
return #err(#InvalidToken(token));
};
let tokenind = ExtCore.TokenIdentifier.getIndex(token);
switch (_registry.get(tokenind)) {
case (?token_owner) {
return #ok(token_owner);
};
case (_) {
return #err(#InvalidToken(token));
};
};
};
public query func supply(token : TokenIdentifier) : async Result.Result<Balance, CommonError> {
#ok(_supply);
};
public query func getBuyers() : async [(AccountIdentifier, [TokenIndex])] {
Iter.toArray(_buyers.entries());
};
public query func getRegistry() : async [(TokenIndex, AccountIdentifier)] {
Iter.toArray(_registry.entries());
};
public query func getAllowances() : async [(TokenIndex, Principal)] {
Iter.toArray(_allowances.entries());
};
public query func getTokens() : async [(TokenIndex, Metadata)] {
Iter.toArray(_tokenMetadata.entries());
};
public query func metadata(token : TokenIdentifier) : async Result.Result<Metadata, CommonError> {
if (ExtCore.TokenIdentifier.isPrincipal(token, Principal.fromActor(this)) == false) {
return #err(#InvalidToken(token));
};
let tokenind = ExtCore.TokenIdentifier.getIndex(token);
switch (_tokenMetadata.get(tokenind)) {
case (?token_metadata) {
return #ok(token_metadata);
};
case (_) {
return #err(#InvalidToken(token));
};
};
};
//Frontend
public query func http_request(request : HttpRequest) : async HttpResponse {
switch(getTokenData(getParam(request.url, "tokenid"))) {
case (?svgdata) {
return {
status_code = 200;
headers = [("content-type", "image/svg+xml")];
body = Text.encodeUtf8 (
SVG.make(svgdata)
)
}
};
case (_) {
return {
status_code = 200;
headers = [("content-type", "text/plain")];
body = Text.encodeUtf8 (
"My current cycle balance: " # debug_show (Cycles.balance()) # "\n" #
"Minted NFTs: " # debug_show (_nextTokenId) # "\n" #
"Distributed NFTs: " # debug_show (_nextToSell) # "\n" #
"Admin: " # debug_show (_minter) # "\n"
)
}
}
};
};
func getTokenData(tokenid : ?Text) : ?[Nat8] {
switch (tokenid) {
case (?token) {
if (ExtCore.TokenIdentifier.isPrincipal(token, Principal.fromActor(this)) == false) {
return null;
};
let tokenind = ExtCore.TokenIdentifier.getIndex(token);
switch (_tokenMetadata.get(tokenind)) {
case (?token_metadata) {
switch(token_metadata) {
case (#fungible data) return null;
case (#nonfungible data) return ?Blob.toArray(Option.unwrap(data.metadata));
};
};
case (_) {
return null;
};
};
return null;
};
case (_) {
return null;
};
};
};
func getParam(url : Text, param : Text) : ?Text {
var _s : Text = url;
Iter.iterate<Text>(Text.split(_s, #text("/")), func(x, _i) {
_s := x;
});
Iter.iterate<Text>(Text.split(_s, #text("?")), func(x, _i) {
if (_i == 1) _s := x;
});
var t : ?Text = null;
var found : Bool = false;
Iter.iterate<Text>(Text.split(_s, #text("&")), func(x, _i) {
Iter.iterate<Text>(Text.split(x, #text("=")), func(y, _ii) {
if (_ii == 0) {
if (Text.equal(y, param)) found := true;
} else if (found == true) t := ?y;
});
});
return t;
};
//Internal cycle management - good general case
public func acceptCycles() : async () {
let available = Cycles.available();
let accepted = Cycles.accept(available);
assert (accepted == available);
};
public query func availableCycles() : async Nat {
return Cycles.balance();
};
}