-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNFT.mo
1023 lines (941 loc) · 35 KB
/
NFT.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import Account "../../Module/Account";
import AccountTypes "../../Module/AccountTypes";
import Array "mo:base/Array";
import Blob "mo:base/Blob";
import Cycles "mo:base/ExperimentalCycles";
import Debug "mo:base/Debug";
import Error "mo:base/Error";
import Hash "mo:base/Hash";
import HashMap "mo:base/HashMap";
import Int "mo:base/Int";
import Interface "../../Module/Interface";
import Iter "mo:base/Iter";
import List "mo:base/List";
import Nat "mo:base/Nat";
import Nat32 "mo:base/Nat32";
import Nat64 "mo:base/Nat64";
import Option "mo:base/Option";
import Prelude "mo:base/Prelude";
import Prim "mo:⛔";
import Principal "mo:base/Principal";
import Result "mo:base/Result";
import SHA256 "../../Module/SHA256";
import SM "mo:base/ExperimentalStableMemory";
import Text "mo:base/Text";
import Time "mo:base/Time";
import TimeBase "mo:base/Time";
import TrieMap "mo:base/TrieMap";
import TrieSet "mo:base/TrieSet";
import Types "Types";
shared(msg) actor class NFToken(
_logo: Text,
_name: Text,
_symbol: Text,
_desc: Text,
_owner: Principal
) = this {
type Metadata = Types.Metadata;
type Location = Types.Location;
type Attribute = Types.Attribute;
type TokenMetadata = Types.TokenMetadata;
type Record = Types.Record;
type TxRecord = Types.TxRecord;
type Operation = Types.Operation;
type TokenInfo = Types.TokenInfo;
type TokenInfoExt = Types.TokenInfoExt;
type UserInfo = Types.UserInfo;
type UserInfoExt = Types.UserInfoExt;
type LedgerActor = Interface.LedgerActor;
type AccountIdentifier = AccountTypes.AccountIdentifier;
type SendArgs = AccountTypes.SendArgs;
public type Error = {
#Unauthorized;
#TokenNotExist;
#InvalidOperator;
};
public type TxReceipt = Result.Result<Nat, Error>;
public type MintResult = Result.Result<(Nat, Nat), Error>; // token index, txid
private stable var logo_ : Text = _logo; // base64 encoded image
private stable var name_ : Text = _name;
private stable var symbol_ : Text = _symbol;
private stable var desc_ : Text = _desc;
private stable var owner_: Principal = _owner;
private stable var totalSupply_: Nat = 0;
private stable var blackhole: Principal = Principal.fromText("aaaaa-aa");
private stable let ICPFEE : Nat64 = 10000; // 0.0001 ICP
private stable var tokensEntries : [(Nat, TokenInfo)] = [];
private stable var usersEntries : [(Principal, UserInfo)] = [];
private var tokens = HashMap.HashMap<Nat, TokenInfo>(1, Nat.equal, Hash.hash); // suply_index -> tokeninfo
private var users = HashMap.HashMap<Principal, UserInfo>(1, Principal.equal, Principal.hash);
private stable var txs: [TxRecord] = [];
private stable var txIndex: Nat = 0;
private func addTxRecord(
caller: Principal, op: Operation, tokenIndex: ?Nat,
from: Record, to: Record, timestamp: Time.Time
): Nat {
let record: TxRecord = {
caller = caller;
op = op;
index = txIndex;
tokenIndex = tokenIndex;
from = from;
to = to;
timestamp = timestamp;
};
txs := Array.append(txs, [record]);
txIndex += 1;
return txIndex - 1;
};
private func _unwrap<T>(x : ?T) : T =
switch x {
case null { Prelude.unreachable() };
case (?x_) { x_ };
};
private func _exists(tokenId: Nat) : Bool {
switch (tokens.get(tokenId)) {
case (?info) { return true; };
case _ { return false; };
}
};
private func _ownerOf(tokenId: Nat) : ?Principal {
switch (tokens.get(tokenId)) {
case (?info) { return ?info.owner; };
case (_) { return null; };
}
};
private func _isOwner(who: Principal, tokenId: Nat) : Bool {
switch (tokens.get(tokenId)) {
case (?info) { return info.owner == who; };
case _ { return false; };
};
};
private func _isApproved(who: Principal, tokenId: Nat) : Bool {
switch (tokens.get(tokenId)) {
case (?info) { return info.operator == ?who; };
case _ { return false; };
}
};
private func _balanceOf(who: Principal) : Nat {
switch (users.get(who)) {
case (?user) { return TrieSet.size(user.tokens); };
case (_) { return 0; };
}
};
private func _newUser() : UserInfo {
{
var operators = TrieSet.empty<Principal>();
var allowedBy = TrieSet.empty<Principal>();
var allowedTokens = TrieSet.empty<Nat>();
var tokens = TrieSet.empty<Nat>();
}
};
private func _tokenInfotoExt(info: TokenInfo) : TokenInfoExt {
return {
index = info.index;
owner = info.owner;
metadata = info.metadata;
timestamp = info.timestamp;
operator = info.operator;
};
};
private func _userInfotoExt(info: UserInfo) : UserInfoExt {
return {
operators = TrieSet.toArray(info.operators);
allowedBy = TrieSet.toArray(info.allowedBy);
allowedTokens = TrieSet.toArray(info.allowedTokens);
tokens = TrieSet.toArray(info.tokens);
};
};
private func _isApprovedOrOwner(spender: Principal, tokenId: Nat) : Bool {
switch (_ownerOf(tokenId)) {
case (?owner) {
return spender == owner or _isApproved(spender, tokenId) or _isApprovedForAll(owner, spender);
};
case _ {
return false;
};
};
};
private func _getApproved(tokenId: Nat) : ?Principal {
switch (tokens.get(tokenId)) {
case (?info) {
return info.operator;
};
case (_) {
return null;
};
}
};
private func _isApprovedForAll(owner: Principal, operator: Principal) : Bool {
switch (users.get(owner)) {
case (?user) {
return TrieSet.mem(user.operators, operator, Principal.hash(operator), Principal.equal);
};
case _ { return false; };
};
};
private func _addTokenTo(to: Principal, tokenId: Nat) { //将token放到用户的set中
switch(users.get(to)) {
case (?user) {
user.tokens := TrieSet.put(user.tokens, tokenId, Hash.hash(tokenId), Nat.equal);
users.put(to, user);
};
case _ {
let user = _newUser();
user.tokens := TrieSet.put(user.tokens, tokenId, Hash.hash(tokenId), Nat.equal);
users.put(to, user);
};
}
};
private func _removeTokenFrom(owner: Principal, tokenId: Nat) {
assert(_exists(tokenId) and _isOwner(owner, tokenId));
switch(users.get(owner)) {
case (?user) {
user.tokens := TrieSet.delete(user.tokens, tokenId, Hash.hash(tokenId), Nat.equal);
users.put(owner, user);
};
case _ {
assert(false);
};
}
};
private func _clearApproval(owner: Principal, tokenId: Nat) {
assert(_exists(tokenId) and _isOwner(owner, tokenId));
switch (tokens.get(tokenId)) {
case (?info) {
if (info.operator != null) {
let op = _unwrap(info.operator);
let opInfo = _unwrap(users.get(op));
opInfo.allowedTokens := TrieSet.delete(opInfo.allowedTokens, tokenId, Hash.hash(tokenId), Nat.equal);
users.put(op, opInfo);
info.operator := null;
tokens.put(tokenId, info);
}
};
case _ {
assert(false);
};
}
};
private func _transfer(to: Principal, tokenId: Nat) {
assert(_exists(tokenId));
switch(tokens.get(tokenId)) {
case (?info) {
_removeTokenFrom(info.owner, tokenId);
_addTokenTo(to, tokenId);
info.owner := to;
tokens.put(tokenId, info);
};
case (_) {
assert(false);
};
};
};
private func _burn(owner: Principal, tokenId: Nat) {
_clearApproval(owner, tokenId);
_transfer(blackhole, tokenId);
};
/*
public shared(msg) func send(amount: Nat64, to: AccountIdentifier): async Bool {
if(msg.caller != owner_ ) { return false; };
let ledger: LedgerActor = actor("ryjl3-tyaaa-aaaaa-aaaba-cai");
let now = Nat64.fromIntWrap(TimeBase.now());
let to_ = Account.accountIdentifierToBlob(to);
let args: SendArgs = {
memo = 1: Nat64;
amount = { e8s = amount; };
created_at_time = ?{ timestamp_nanos = now; };
from_subaccount = null;
to = to_;
fee = { e8s = ICPFEE; };
};
ignore await ledger.send_dfx(args);
return true;
};*/
// public update calls
public shared(msg) func mint(to: Principal, metadata: ?TokenMetadata): async MintResult {
if(msg.caller != owner_) {
return #err(#Unauthorized);
};
let token: TokenInfo = {
index = totalSupply_;
var owner = to;
var metadata = metadata;
var operator = null;
timestamp = Time.now();
};
tokens.put(totalSupply_, token);
_addTokenTo(to, totalSupply_);
totalSupply_ += 1;
let txid = addTxRecord(msg.caller, #mint(metadata), ?token.index, #user(blackhole), #user(to), Time.now());
return #ok((token.index, txid));
};
public shared(msg) func batchMint(to: Principal, arr: [?TokenMetadata]): async MintResult {
if(msg.caller != owner_) {
return #err(#Unauthorized);
};
let startIndex = totalSupply_;
for(metadata in Iter.fromArray(arr)) {
let token: TokenInfo = {
index = totalSupply_;
var owner = to;
var metadata = metadata;
var operator = null;
timestamp = Time.now();
};
tokens.put(totalSupply_, token);
_addTokenTo(to, totalSupply_);
totalSupply_ += 1;
ignore addTxRecord(msg.caller, #mint(metadata), ?token.index, #user(blackhole), #user(to), Time.now());
};
return #ok((startIndex, txs.size() - arr.size()));
};
public shared(msg) func burn(tokenId: Nat): async TxReceipt {
if(_exists(tokenId) == false) {
return #err(#TokenNotExist)
};
if(_isOwner(msg.caller, tokenId) == false) {
return #err(#Unauthorized);
};
_burn(msg.caller, tokenId); //not delete tokenId from tokens temporarily. (consider storage limited, it should be delete.)
let txid = addTxRecord(msg.caller, #burn, ?tokenId, #user(msg.caller), #user(blackhole), Time.now());
return #ok(txid);
};
public shared(msg) func setTokenMetadata(tokenId: Nat, new_metadata: TokenMetadata) : async TxReceipt {
// only canister owner can set
if(msg.caller != owner_) {
return #err(#Unauthorized);
};
if(_exists(tokenId) == false) {
return #err(#TokenNotExist)
};
let token = _unwrap(tokens.get(tokenId));
let old_metadate = token.metadata;
token.metadata := ?new_metadata;
tokens.put(tokenId, token);
let txid = addTxRecord(msg.caller, #setMetadata, ?token.index, #metadata(old_metadate), #metadata(?new_metadata), Time.now());
return #ok(txid);
};
public shared(msg) func approve(tokenId: Nat, operator: Principal) : async TxReceipt {
var owner: Principal = switch (_ownerOf(tokenId)) {
case (?own) {
own;
};
case (_) {
return #err(#TokenNotExist)
}
};
if(Principal.equal(msg.caller, owner) == false)
if(_isApprovedForAll(owner, msg.caller) == false)
return #err(#Unauthorized);
if(owner == operator) {
return #err(#InvalidOperator);
};
switch (tokens.get(tokenId)) {
case (?info) {
info.operator := ?operator;
tokens.put(tokenId, info);
};
case _ {
return #err(#TokenNotExist);
};
};
switch (users.get(operator)) {
case (?user) {
user.allowedTokens := TrieSet.put(user.allowedTokens, tokenId, Hash.hash(tokenId), Nat.equal);
users.put(operator, user);
};
case _ {
let user = _newUser();
user.allowedTokens := TrieSet.put(user.allowedTokens, tokenId, Hash.hash(tokenId), Nat.equal);
users.put(operator, user);
};
};
let txid = addTxRecord(msg.caller, #approve, ?tokenId, #user(msg.caller), #user(operator), Time.now());
return #ok(txid);
};
public shared(msg) func setApprovalForAll(operator: Principal, value: Bool): async TxReceipt {
if(msg.caller == operator) { //?
return #err(#Unauthorized);
};
var txid = 0;
if value {
let caller = switch (users.get(msg.caller)) {
case (?user) { user };
case _ { _newUser() };
};
caller.operators := TrieSet.put(caller.operators, operator, Principal.hash(operator), Principal.equal);
users.put(msg.caller, caller);
let user = switch (users.get(operator)) {
case (?user) { user };
case _ { _newUser() };
};
user.allowedBy := TrieSet.put(user.allowedBy, msg.caller, Principal.hash(msg.caller), Principal.equal);
users.put(operator, user);
txid := addTxRecord(msg.caller, #approveAll, null, #user(msg.caller), #user(operator), Time.now());
} else {
switch (users.get(msg.caller)) {
case (?user) {
user.operators := TrieSet.delete(user.operators, operator, Principal.hash(operator), Principal.equal);
users.put(msg.caller, user);
};
case _ { };
};
switch (users.get(operator)) {
case (?user) {
user.allowedBy := TrieSet.delete(user.allowedBy, msg.caller, Principal.hash(msg.caller), Principal.equal);
users.put(operator, user);
};
case _ { };
};
txid := addTxRecord(msg.caller, #revokeAll, null, #user(msg.caller), #user(operator), Time.now());
};
return #ok(txid);
};
public shared(msg) func transfer(to: Principal, tokenId: Nat): async TxReceipt {
var owner: Principal = switch (_ownerOf(tokenId)) {
case (?own) {
own;
};
case (_) {
return #err(#TokenNotExist)
}
};
if (owner != msg.caller) {
return #err(#Unauthorized);
};
_transfer(to, tokenId);
let txid = addTxRecord(msg.caller, #transfer, ?tokenId, #user(msg.caller), #user(to), Time.now());
return #ok(txid);
};
public shared(msg) func transferFrom(from: Principal, to: Principal, tokenId: Nat): async TxReceipt {
if(_exists(tokenId) == false) {
return #err(#TokenNotExist)
};
if(_isApprovedOrOwner(msg.caller, tokenId) == false) {
return #err(#Unauthorized);
};
_clearApproval(from, tokenId);
_transfer(to, tokenId);
let txid = addTxRecord(msg.caller, #transferFrom, ?tokenId, #user(from), #user(to), Time.now());
return #ok(txid);
};
public shared(msg) func batchTransferFrom(from: Principal, to: Principal, tokenIds: [Nat]): async TxReceipt {
var num: Nat = 0;
label l for(tokenId in Iter.fromArray(tokenIds)) {
if(_exists(tokenId) == false) {
continue l;
};
if(_isApprovedOrOwner(msg.caller, tokenId) == false) {
continue l;
};
_clearApproval(from, tokenId);
_transfer(to, tokenId);
num += 1;
ignore addTxRecord(msg.caller, #transferFrom, ?tokenId, #user(from), #user(to), Time.now());
};
return #ok(txs.size() - num);
};
// public query function
public query func logo(): async Text {
return logo_;
};
public query func name(): async Text {
return name_;
};
public query func symbol(): async Text {
return symbol_;
};
public query func desc(): async Text {
return desc_;
};
public query func balanceOf(who: Principal): async Nat {
return _balanceOf(who);
};
public query func totalSupply(): async Nat {
return totalSupply_;
};
// get metadata about this NFT collection
public query func getMetadata(): async Metadata {
{
logo = logo_;
name = name_;
symbol = symbol_;
desc = desc_;
totalSupply = totalSupply_;
owner = owner_;
cycles = Cycles.balance();
}
};
public query func isApprovedForAll(owner: Principal, operator: Principal) : async Bool {
return _isApprovedForAll(owner, operator);
};
public query func getOperator(tokenId: Nat) : async Principal {
switch (_exists(tokenId)) {
case true {
switch (_getApproved(tokenId)) {
case (?who) {
return who;
};
case (_) {
return Principal.fromText("aaaaa-aa");
};
}
};
case (_) {
throw Error.reject("token not exist")
};
}
};
public query func getUserInfo(who: Principal) : async UserInfoExt {
switch (users.get(who)) {
case (?user) {
return _userInfotoExt(user)
};
case _ {
throw Error.reject("unauthorized");
};
};
};
public query func getUserTokens(owner: Principal) : async [TokenInfoExt] {
let tokenIds = switch (users.get(owner)) {
case (?user) {
TrieSet.toArray(user.tokens)
};
case _ {
[]
};
};
var ret: [TokenInfoExt] = [];
for(id in Iter.fromArray(tokenIds)) {
ret := Array.append(ret, [_tokenInfotoExt(_unwrap(tokens.get(id)))]);
};
return ret;
};
public query func ownerOf(tokenId: Nat): async Principal {
switch (_ownerOf(tokenId)) {
case (?owner) {
return owner;
};
case _ {
throw Error.reject("token not exist")
};
}
};
public query func getTokenInfo(tokenId: Nat) : async TokenInfoExt {
switch(tokens.get(tokenId)){
case(?tokeninfo) {
return _tokenInfotoExt(tokeninfo);
};
case(_) {
throw Error.reject("token not exist");
};
};
};
// Optional
public query func getAllTokens() : async [TokenInfoExt] {
Iter.toArray(Iter.map(tokens.entries(), func (i: (Nat, TokenInfo)): TokenInfoExt {_tokenInfotoExt(i.1)}))
};
// transaction history related
public query func historySize(): async Nat {
return txs.size();
};
public query func getTransaction(index: Nat): async TxRecord {
return txs[index];
};
public query func getTransactions(start: Nat, limit: Nat): async [TxRecord] {
var res: [TxRecord] = [];
var i = start;
while (i < start + limit and i < txs.size()) {
res := Array.append(res, [txs[i]]);
i += 1;
};
return res;
};
public query func getUserTransactionAmount(user: Principal): async Nat {
var res: Nat = 0;
for (i in txs.vals()) {
if (i.caller == user or i.from == #user(user) or i.to == #user(user)) {
res += 1;
};
};
return res;
};
public query func getUserTransactions(user: Principal, start: Nat, limit: Nat): async [TxRecord] {
var res: [TxRecord] = [];
var idx = 0;
label l for (i in txs.vals()) {
if (i.caller == user or i.from == #user(user) or i.to == #user(user)) {
if(idx < start) {
idx += 1;
continue l;
};
if(idx >= start + limit) {
break l;
};
res := Array.append<TxRecord>(res, [i]);
idx += 1;
};
};
return res;
};
// upgrade functions
system func preupgrade() {
usersEntries := Iter.toArray(users.entries());
tokensEntries := Iter.toArray(tokens.entries());
};
system func postupgrade() {
type TokenInfo = Types.TokenInfo;
type UserInfo = Types.UserInfo;
users := HashMap.fromIter<Principal, UserInfo>(usersEntries.vals(), 1, Principal.equal, Principal.hash);
tokens := HashMap.fromIter<Nat, TokenInfo>(tokensEntries.vals(), 1, Nat.equal, Hash.hash);
usersEntries := [];
tokensEntries := [];
};
//-------------storage----------------------------------------------------
private type Asset = Types.Asset;
private type AssetExt = Types.AssetExt;
private type Chunk = Types.Chunk;
private type PUT = Types.PUT;
private type GET = Types.GET;
private type State = Types.State;
private type Extension = Types.FileExtension;
private type BufferAsset = Types.BufferAsset;
private type ThumbNail = Types.ThumbNail;
private let cycle_limit = 20_000_000_000_000;
private let MAX_PAGE_NUMBER : Nat32 = 65535;
private let PAGE_SIZE = 65536; // Byte
private let THRESHOLD = 4294901760; // 65535 * 65536
private let MAX_UPDATE_SIZE = 1992295;
private let MAX_QUERY_SIZE = 3144728; // 3M - 1KB
private let UPGRADE_SLICE = 6000; // 暂定
private var offset = 4; // [0, 65535*65536-1]
private var buffer_canister_id = ""; // buffer canister id text
private var thumbnail_map = TrieMap.TrieMap<Blob, ThumbNail>(Blob.equal, Blob.hash);
private var assets = TrieMap.TrieMap<Blob, Asset>(Blob.equal, Blob.hash); // file_key ase map
private var buffer = HashMap.HashMap<Blob, BufferAsset>(10, Blob.equal, Blob.hash);
public query({caller}) func canisterState() : async State {
{
head_size = Prim.rts_heap_size();
memory_size = Prim.rts_memory_size();
balance = Cycles.balance();
}
};
public query({caller}) func cycleBalance() : async Nat {
Cycles.balance()
};
public query({caller}) func avlSM() : async Nat {
_avlSM()
};
public query({caller}) func getThumbnail(file_key : Blob) : async Result.Result<ThumbNail, Text> {
switch(_getThumbnail(file_key)) {
case(#ok(tb)) { return #ok(tb)};
case(#err(info)) {return #err(info)};
};
};
public query({caller}) func getAssetextkey(file_key : Blob) : async Result.Result<AssetExt, Text>{
switch(_getAssetextkey(file_key)) {
case(#ok(aseext)) { #ok(aseext) };
case(#err(info)) { #err(info) };
}
};
// data : [flag, offset + size - 1]
public query({caller}) func get(
g : GET
) : async Result.Result<[Blob], Text>{
switch(assets.get(g.file_key)){
case(null){ #err("wrong file_key") };
case(?ase){
// 安全检测
if(g.flag > ase.page_field.size()){
#err("wrong flag")
}else{
Debug.print("page field : " # debug_show(ase.page_field));
let field = ase.page_field[g.flag];
Debug.print("bucket get field : " # debug_show(field));
#ok(_getSM(field))
}
};
}
};
public shared({caller}) func put(
put : PUT
) : async Result.Result<AssetExt, Text>{
switch(put) {
case(#thumb_nail(tb)) {
switch(assets.get(tb.file_key)){
case(?ase) { return #ok(_assetExt(ase, true)); };
case(null) {};
};
let thumb_nail : ThumbNail = {
image = tb.image;
file_extension = tb.file_extension;
};
return _putThumbnail(tb.file_key, thumb_nail);
};
case(#segment(seg)) {
switch(assets.get(seg.file_key)){
case(?ase) { return #ok(_assetExt(ase, true)); };
case(null) {};
};
switch(_inspectSize(seg.chunk.data)){
case(#ok(size)){ _upload(seg.file_key, seg.chunk, seg.chunk_number, seg.file_extension, seg.order, size) };
case(#err(info)){ #err(info) }
}
};
}
};
public shared({caller}) func preUpgrade() : async() {
SM.storeNat32(0, Nat32.fromNat(offset));
};
public shared({caller}) func postUpgrade() : async() {
offset := Nat32.toNat(SM.loadNat32(0));
};
public shared({caller}) func setBufferCanister(p : Text) : async (){
buffer_canister_id := p;
};
public shared({caller}) func wallet_receive() : async Nat {
Cycles.accept(Cycles.available())
};
private func _appendBuffer(buffer : [var (Nat, Nat)], arr : [var (Nat, Nat)]) : [var (Nat, Nat)]{
switch(buffer.size(), arr.size()) {
case (0, 0) { [var] };
case (0, _) { arr };
case (_, 0) { buffer };
case (xsSize, ysSize) {
let res = Array.init<(Nat, Nat)>(buffer.size() + arr.size(), (0, 0));
var i = 0;
for(e in buffer.vals()){
res[i] := buffer[i];
i += 1;
};
for(e in arr.vals()){
res[i] := arr[i - buffer.size()];
i += 1;
};
res
};
}
};
private func _getThumbnail(file_key : Blob) : Result.Result<ThumbNail, Text>{
switch(thumbnail_map.get(file_key)){
case null { #err("wrong file_key") };
case(?tb){ #ok(tb) };
}
};
private func _putThumbnail(file_key : Blob, thumb_nail : ThumbNail) : Result.Result<AssetExt, Text>{
thumbnail_map.put(file_key, thumb_nail);
return #ok(_assetExt({
file_key = file_key;
page_field = [[(0, 0)]];
total_size = 0;
file_extension = thumb_nail.file_extension;
}, false));
};
private func _getAssetextkey(file_key : Blob) : Result.Result<AssetExt, Text>{
switch(assets.get(file_key)){
case null { #err("wrong file_key") };
case(?ase){
#ok(_assetExt(ase, true))
};
}
};
// inspect file_name file_key exist or not
private func _inspectSize(data : Blob) : Result.Result<Nat, Text>{
var size = data.size();
if(size <= _avlSM()){
#ok(size)
}else{
#err("insufficient memory")
}
};
private func _digest(pred : [var Nat8], nd : [Nat8], received : Nat){
var i = received * 32;
for(e in nd.vals()){
pred[i] := e;
i := i + 1;
};
};
// wirte page field -> query page field
private func _pageField(buffer_page_field : [(Nat, Nat)], total_size : Nat) : [[(Nat, Nat)]]{
var arrSize = 0;
if(total_size % MAX_QUERY_SIZE == 0){
arrSize := total_size / MAX_QUERY_SIZE;
}else{
arrSize := total_size / MAX_QUERY_SIZE + 1;
};
var res = Array.init<[(Nat, Nat)]>(arrSize, []);
var i : Nat = 0;
var rowSize : Nat = 0;
var pre_start : Nat = 0;
var pre_size : Nat = 0;
var buffer : [var (Nat, Nat)] = [var];
// merge query page field
for((start, size) in buffer_page_field.vals()){
if(rowSize + size <= MAX_QUERY_SIZE){
if(start != 0 and pre_start + pre_size == start){
let li : Nat = buffer.size() - 1;
buffer[li] := (pre_start, pre_size + size);
}else{
buffer := _appendBuffer(buffer, [var (start, size)]);
pre_start := start;
pre_size := size;
};
rowSize += size;
}else if(rowSize == MAX_QUERY_SIZE){
res[i] := Array.freeze(buffer);
i := i + 1;
buffer := [var (start, size)];
pre_start := start;
pre_size := size;
rowSize := size;
}else{
assert(MAX_QUERY_SIZE > rowSize);
if(start != 0 and pre_start + pre_size == start){
let li : Nat = buffer.size() - 1;
buffer[li] := (pre_start, pre_size + MAX_QUERY_SIZE - rowSize);
}else{
buffer := _appendBuffer(buffer, [var (start, MAX_QUERY_SIZE - rowSize)])
};
res[i] := Array.freeze(buffer);
i += 1;
buffer := [var (start + MAX_QUERY_SIZE - rowSize, size - (MAX_QUERY_SIZE - rowSize))];
pre_start := start + MAX_QUERY_SIZE - rowSize;
pre_size := size - (MAX_QUERY_SIZE - rowSize);
rowSize := size + rowSize - MAX_QUERY_SIZE;
};
};
res[i] := Array.freeze(buffer);
Array.freeze<[(Nat, Nat)]>(res)
};
// available stable wasm memory
private func _avlSM() : Nat{
THRESHOLD - offset + 1
};
// return page field
private func _putSM(data : Blob, size : Nat) : (Nat, Nat){
// 看本页的内存还剩多少
let page_left : Nat = if(offset == 4){
ignore SM.grow(1);
PAGE_SIZE - offset % PAGE_SIZE
}else{
PAGE_SIZE - offset % PAGE_SIZE
};
let res = (offset, size);
// 如果够则记录到本页, 如果不够就grow
if(size <= page_left){
//本页够
SM.storeBlob(Nat32.fromNat(offset), data);
offset += data.size();
}else {
assert(SM.size() <= MAX_PAGE_NUMBER);
ignore SM.grow(Nat32.fromNat((size - page_left) / PAGE_SIZE + 1));
SM.storeBlob(Nat32.fromNat(offset), data);
offset += data.size();
};
res
};
private func _getSM(field : [(Nat, Nat)]) : [Blob]{
let res = Array.init<Blob>(field.size(), "" : Blob);
var i = 0;
for((start, size) in field.vals()){
res[i] := SM.loadBlob(Nat32.fromNat(start), size);
i := i + 1;
};
Array.freeze<Blob>(res)
};
private func _assetExt(ase : Asset, upload_status : Bool) : AssetExt{
{
bucket_id = Principal.fromActor(this);
upload_status = upload_status;
file_key = ase.file_key;
total_size = ase.total_size;
file_extension = ase.file_extension;
need_query_times = ase.page_field.size();
}
};
private func _key(digests : [Nat8]) : Blob {
Blob.fromArray(SHA256.sha256(digests))
};
/**
* inspect file format and file size
* put file data into stable wasm memory
* put file ase into assets
*/
private func _upload(file_key : Blob, chunk : Chunk, chunk_num : Nat, extension : Extension, order : Nat, size : Nat) : Result.Result<AssetExt, Text> {
var size_ = size;
var field = (0, 0);
if (chunk_num == 1) {
field := _putSM(chunk.data, size_);
let ase = {
file_key = _key(chunk.digest);
total_size = size_;
page_field = [[field]];
file_extension = extension;
};
assets.put(ase.file_key, ase);
Debug.print("final asset" # debug_show(ase));
return #ok(_assetExt(ase, true));
};
switch(buffer.get(file_key)){
case (null) {
var digest = Array.init<Nat8>(chunk_num*32, 0);
var page_field = Array.init<(Nat, Nat)>(chunk_num, (0,0));
field := _putSM(chunk.data, size_);
page_field[order] := field;
_digest(digest, chunk.digest, order);
let buffer_asset = {
digest = digest;
chunk_number = chunk_num;
var page_field = page_field;
var total_size = size_;
var received = 1;
};
buffer.put(file_key, buffer_asset);
#ok(_assetExt(
{
file_key = file_key;
page_field = [Array.freeze<(Nat, Nat)>(page_field)];
total_size = size_;
file_extension = extension;
}, false
))
};
case (?a){
if(a.received + 1 == a.chunk_number){
_digest(a.digest, chunk.digest, order);
a.received += 1;
field := _putSM(chunk.data, size_);
a.page_field[order] := field;
let total_size = a.total_size + size_;
let digest = Array.freeze(a.digest);
let page_field = Array.freeze(a.page_field);
let ase = {
file_key = _key(digest);
page_field = _pageField(page_field, total_size);
total_size = total_size;
file_extension = extension;
};
assets.put(ase.file_key, ase);
buffer.delete(file_key);