-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
857 lines (745 loc) · 25.8 KB
/
script.js
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
var ContractAddress;
var ContractObject;
var ContractState;
function checkWallet(){
if(window.zilPay){
return true;
}else{
return false;
}
}
async function connectWallet(){
return (await window.zilPay.wallet.connect());
}
function loadContract(contractAddr){
try{
return window.zilPay.contracts.at(contractAddr);
}catch(err){
console.log(err.message);
return false;
}
}
function onloadInit(){
connectAppToWallet();
observer();
console.clear();
}
function loadGallery(flag){
if(!ContractObject){
alert("Please load contract first");
return;
}
var gallery = document.querySelector("#gallery-container");
gallery.innerHTML = "";
tokenOwners = ContractState.token_owners;
tokenUris = ContractState.token_uris;
galleryCode = "<h2 style='width:100%' class='HVCenter'>" + ContractObject.init[1].value + "</h2>";
var currentAccountAddress = window.zilPay.wallet.defaultAccount.base16;
for(i in tokenOwners){
if(flag){
if(tokenOwners[i].toUpperCase() !== currentAccountAddress.toUpperCase()){
continue;
}
}
var transferBtn = "";
if(tokenOwners[i].toUpperCase() == currentAccountAddress.toUpperCase()){
transferBtn = "<button onclick='transferNFT(" + i + ")'>Transfer</button>"
}
galleryCode += `
<div id="nft-${i}" class="nft-card">
<div class="nft-card-id HVCenter">NFT ID: ${i} ${transferBtn}</div>
<div class="nft-card-img-holder HVCenter">
<img src="${tokenUris[i]}"></div>
<div class="nft-card-owner">Owner: ${tokenOwners[i]}</div>
</div>
`;
}
gallery.innerHTML = galleryCode;
}
async function connectAppToWallet(){
check1 = checkWallet();
check2 = await connectWallet();
if(check1 && check2){
//if successful hide button and show net and address
document.querySelector("#wallet-address-container").style.display = "inline-block";
document.querySelector("#connect-button-container").style.display = "none";
//get and set network and address
let networkName = window.zilPay.wallet.net;
let currentAddress = window.zilPay.wallet.defaultAccount.bech32;
document.querySelector("#wallet-network-span").innerHTML = networkName;
document.querySelector("#wallet-address-span").innerHTML = currentAddress;
}else{
//if connection failed
alert("Something went wrong connecting wallet, try again later.");
}
}
function observer(){
window.zilPay.wallet.observableAccount().subscribe(function (acc){
if(acc) connectAppToWallet();
});
window.zilPay.wallet.observableNetwork().subscribe(function (net){
if(net) connectAppToWallet();
});
}
function loadNFTContract(){
var contractAddress = prompt("Please enter contract address");
ContractObject = loadContract(contractAddress);
if(ContractObject){
ContractObject.getState().then(function(stateData){
ContractState = stateData;
ContractAddress = contractAddress;
alert("Contract State Loaded Successfully!");
ContractObject.getInit().then(function(x){
loadGallery(false);
});
});
}else{
ContractObject = undefined;
}
}
function showMintbox() {
document.querySelector("#mintbox-container").style.display = "flex";
}
function hideMintbox() {
document.querySelector("#mintbox-container").style.display = "none";
}
function mintNFT(){
if(!ContractObject){
alert("Please load contract first");
return;
}
/*check if the user is a minter*/
flag = false;
var currentAccountAddress = window.zilPay.wallet.defaultAccount.base16;
for(i in ContractState.minters){
if(i.toUpperCase() == currentAccountAddress.toUpperCase()){
flag = true;
break;
}
}
if(!flag){
alert("You don't have a mint access");
return;
}
/*check if the user is a minter*/
//geting inputs
var NftTo = document.querySelector("#mintnft_to").value;
var NftUri = document.querySelector("#mintnft_uri").value;
/* Code for transaction call */
const gasPrice = window.zilPay.utils.units.toQa('2000', window.zilPay.utils.units.Units.Li);
var tx = ContractObject.call('Mint',[{
vname: "to",
type: "ByStr20",
value: NftTo
},{
vname: "token_uri",
type: "String",
value: NftUri
}],
{
gasPrice: gasPrice,
gasLimit: window.zilPay.utils.Long.fromNumber(10000)
});
/* Code for transaction call */
//handle the promise accordingly
tx.then(function(a){
console.log(a);
});
console.log(t);
}
function transferNFT(nftid){
var receiverAddress = prompt("Please enter the address you want to send NFT ID:" + nftid + " to");
/* Code for transaction call */
const gasPrice = window.zilPay.utils.units.toQa('2000', window.zilPay.utils.units.Units.Li);
var tx = ContractObject.call('Transfer',[{
vname: "to",
type: "ByStr20",
value: receiverAddress+""
},{
vname: "token_id",
type: "Uint256",
value: nftid+""
}],
{
gasPrice: gasPrice,
gasLimit: window.zilPay.utils.Long.fromNumber(10000)
});
/* Code for transaction call */
//handle the promise accordingly
tx.then(function(a){
console.log(a);
});
console.log(t);
}
function showCndbox() {
document.querySelector("#cndbox-container").style.display = "flex";
}
function hideCndbox() {
document.querySelector("#cndbox-container").style.display = "none";
}
function DeployNFT(){
//geting inputs
var NFTName = document.querySelector("#cndnft_name").value;
var NFTSymbol = document.querySelector("#cndnft_symbol").value;
/* NFT contract Inits */
var NftContractInits = [{
vname: 'contract_owner',
type: 'ByStr20',
value: '',
},{
vname: 'name',
type: 'String',
value: '',
},{
vname: 'symbol',
type: 'String',
value: '',
},{
vname: '_scilla_version',
type: 'Uint32',
value: '0'
}];
NftContractInits[0].value = window.zilPay.wallet.defaultAccount.base16;
NftContractInits[1].value = NFTName;
NftContractInits[2].value = NFTSymbol;
/* NFT contract Inits */
/* NFT contract Code */
let NftContractCode = `
scilla_version 0
(***************************************************)
(* Associated library *)
(***************************************************)
import BoolUtils PairUtils ListUtils
library NonfungibleToken
(* User-defined ADTs *)
type Dummy =
| Dummy
type Operation =
| Add
| Sub
(* Global variables *)
let zero = Uint256 0
let one = Uint256 1
let verdad = Dummy
let add_operation = Add
let sub_operation = Sub
(* Library functions *)
let one_msg =
fun (msg : Message) =>
let nil_msg = Nil {Message} in
Cons {Message} msg nil_msg
let two_msgs =
fun (msg1 : Message) =>
fun (msg2 : Message) =>
let msgs_tmp = one_msg msg2 in
Cons {Message} msg1 msgs_tmp
let get_bal =
fun (some_bal: Option Uint256) =>
match some_bal with
| Some bal => bal
| None => zero
end
let build_pair =
fun (input_to_address : ByStr20) =>
fun (input_token_uri : String) =>
Pair {ByStr20 String} input_to_address input_token_uri
(* Error exception *)
type Error =
| CodeNotContractOwner
| CodeIsSelf
| CodeTokenExists
| CodeIsNotMinter
| CodeNotApproved
| CodeNotTokenOwner
| CodeNotFound
| CodeNotApprovedForAll
| CodeNotOwnerOrOperator
| CodeNotApprovedSpenderOrOperator
let make_error =
fun (result : Error) =>
let result_code =
match result with
| CodeNotContractOwner => Int32 -1
| CodeIsSelf => Int32 -2
| CodeTokenExists => Int32 -3
| CodeIsNotMinter => Int32 -4
| CodeNotApproved => Int32 -5
| CodeNotTokenOwner => Int32 -6
| CodeNotFound => Int32 -7
| CodeNotApprovedForAll => Int32 -8
| CodeNotOwnerOrOperator => Int32 -9
| CodeNotApprovedSpenderOrOperator => Int32 -10
end
in
{ _exception : "Error"; code : result_code }
(***************************************************)
(* The contract definition *)
(***************************************************)
contract NonfungibleToken
(
contract_owner: ByStr20,
name : String,
symbol: String
)
(* Mutable fields *)
(* Mapping of minters available *)
field minters: Map ByStr20 Dummy =
let emp_map = Emp ByStr20 Dummy in
builtin put emp_map contract_owner verdad
(* Mapping between token_id to token_owner *)
field token_owners: Map Uint256 ByStr20 = Emp Uint256 ByStr20
(* Mapping from owner to number of owned tokens *)
field owned_token_count: Map ByStr20 Uint256 = Emp ByStr20 Uint256
(* Mapping between token_id to approved address *)
(* @dev: There can only be one approved address per token at any given time. *)
field token_approvals: Map Uint256 ByStr20 = Emp Uint256 ByStr20
(* Mapping of token_owner to operator *)
field operator_approvals: Map ByStr20 (Map ByStr20 Dummy)
= Emp ByStr20 (Map ByStr20 Dummy)
(* Mapping from token_id to token_uri *)
field token_uris: Map Uint256 String = Emp Uint256 String
(* Total token count *)
field total_supply: Uint256 = Uint256 0
(* Used token id *)
field token_id_count: Uint256 = Uint256 0
(* Emit Errors *)
procedure ThrowError(err : Error)
e = make_error err;
throw e
end
procedure IsContractOwner()
is_contract_owner = builtin eq contract_owner _sender;
match is_contract_owner with
| True =>
| False =>
err = CodeNotContractOwner;
ThrowError err
end
end
procedure IsSelf(address_a: ByStr20, address_b: ByStr20)
is_self = builtin eq address_a address_b;
match is_self with
| False =>
| True =>
err = CodeIsSelf;
ThrowError err
end
end
procedure IsTokenExists(token_id: Uint256)
token_exist <- exists token_owners[token_id];
match token_exist with
| False =>
| True =>
err = CodeTokenExists;
ThrowError err
end
end
procedure IsMinter(address: ByStr20)
is_minter <- exists minters[address];
match is_minter with
| True =>
| False =>
err = CodeIsNotMinter;
ThrowError err
end
end
procedure IsTokenOwner(token_id: Uint256, address: ByStr20)
some_token_owner <- token_owners[token_id];
match some_token_owner with
| Some addr =>
is_token_owner = builtin eq addr address;
match is_token_owner with
| True =>
| False =>
err = CodeNotTokenOwner;
ThrowError err
end
| None =>
err = CodeNotFound;
ThrowError err
end
end
procedure IsApprovedForAll(token_owner: ByStr20, operator: ByStr20)
is_operator_approved <- exists operator_approvals[token_owner][operator];
match is_operator_approved with
| True =>
| False =>
err = CodeNotApprovedForAll;
ThrowError err
end
end
procedure IsOwnerOrOperator(token_owner: ByStr20)
is_owner = builtin eq _sender token_owner;
is_approved_for_all <- exists operator_approvals[token_owner][_sender];
is_authorised = orb is_owner is_approved_for_all;
match is_authorised with
| True =>
| False =>
err = CodeNotOwnerOrOperator;
ThrowError err
end
end
procedure IsApprovedSpenderOrOperator(token_id: Uint256, token_owner: ByStr20)
some_token_approval <- token_approvals[token_id];
is_approved = match some_token_approval with
| None => False
| Some approved_address =>
builtin eq _sender approved_address
end;
is_operator <- exists operator_approvals[token_owner][_sender];
is_authorised = orb is_approved is_operator;
match is_authorised with
| True =>
| False =>
err = CodeNotApprovedSpenderOrOperator;
ThrowError err
end
end
procedure UpdateTokenCount(operation: Operation, address: ByStr20)
match operation with
| Add =>
some_to_count <- owned_token_count[address];
new_to_count =
let current_count = get_bal some_to_count in
builtin add current_count one;
owned_token_count[address] := new_to_count
| Sub =>
some_from_count <- owned_token_count[address];
new_from_count =
let current_count = get_bal some_from_count in
let is_zero = builtin eq current_count zero in
match is_zero with
| True => zero
| False => builtin sub current_count one
end;
owned_token_count[address] := new_from_count
end
end
(* Getter transitions *)
(* @dev: Get number of NFTs assigned to a token_owner *)
transition BalanceOf(address: ByStr20)
some_bal <- owned_token_count[address];
balance = get_bal some_bal;
msg_to_sender = { _tag : "BalanceOfCallBack"; _recipient : _sender; _amount : Uint128 0;
balance : balance};
msgs = one_msg msg_to_sender;
send msgs
end
(* @dev: Get total supply of NFTs minted *)
transition TotalSupply()
current_supply <- total_supply;
msg_to_sender = { _tag : "TotalSupplyCallBack"; _recipient : _sender; _amount : Uint128 0;
total_supply : current_supply};
msgs = one_msg msg_to_sender;
send msgs
end
(* @dev: Get name of the NFTs *)
transition Name()
msg_to_sender = { _tag : "NameCallBack"; _recipient : _sender; _amount : Uint128 0;
name : name};
msgs = one_msg msg_to_sender;
send msgs
end
(* @dev: Get name of the NFTs *)
transition Symbol()
msg_to_sender = { _tag : "SymbolCallBack"; _recipient : _sender; _amount : Uint128 0;
symbol : symbol};
msgs = one_msg msg_to_sender;
send msgs
end
(* @dev: Get approved_addr for token_id *)
transition GetApproved(token_id: Uint256)
some_token_approval <- token_approvals[token_id];
match some_token_approval with
| Some addr =>
msg_to_sender = { _tag : "GetApprovedCallBack"; _recipient : _sender; _amount : Uint128 0;
approved_addr : addr; token_id : token_id};
msgs = one_msg msg_to_sender;
send msgs
| None =>
err = CodeNotApproved;
ThrowError err
end
end
(* @dev: Get the token_uri of a certain token_id *)
transition GetTokenURI(token_id: Uint256)
some_token_uri <- token_uris[token_id];
match some_token_uri with
| Some token_uri =>
msg_to_sender = { _tag : "GetTokenURICallBack"; _recipient : _sender; _amount : Uint128 0;
token_uri : token_uri};
msgs = one_msg msg_to_sender;
send msgs
| None =>
err = CodeNotFound;
ThrowError err
end
end
(* @dev: Check if a token_id is owned by a token_owner *)
transition CheckTokenOwner(token_id: Uint256, address: ByStr20)
IsTokenOwner token_id address;
msg_to_sender = { _tag : "IsTokenOwnerCallBack"; _recipient : _sender; _amount : Uint128 0};
msgs = one_msg msg_to_sender;
send msgs
end
(* @dev: Check if address is operator for token_owner *)
transition CheckApprovedForAll(token_owner: ByStr20, operator: ByStr20)
IsApprovedForAll token_owner operator;
msg_to_sender = { _tag : "IsApprovedForAllCallBack"; _recipient : _sender; _amount : Uint128 0;
token_owner : token_owner; operator : operator};
msgs = one_msg msg_to_sender;
send msgs
end
(* Interface transitions *)
(* @dev: Add or remove approved minters. Only contract_owner can approve minters. *)
(* @param: minter - Address of the minter to be approved or removed *)
transition ConfigureMinter(minter: ByStr20)
IsContractOwner;
some_minter <- minters[minter];
match some_minter with
| Some Dummy =>
(* Remove minter *)
delete minters[minter];
e = {_eventname: "RemovedMinterSuccess"; minter: minter};
event e
| None =>
(* Add minter *)
minters[minter] := verdad;
e = {_eventname: "AddMinterSuccess"; minter: minter};
event e
end
end
procedure Minting(input_pair: Pair ByStr20 String)
to = let fst_string_bystr20 = @fst ByStr20 String in
fst_string_bystr20 input_pair;
token_uri = let snd_string_bystr20 = @snd ByStr20 String in
snd_string_bystr20 input_pair;
IsMinter _sender;
(* Add to owner count *)
UpdateTokenCount add_operation to;
(* Add to total_supply *)
current_supply <- total_supply;
new_supply = builtin add current_supply one;
total_supply := new_supply;
(* Initiate token_id and check if exists *)
current_token_id_count <- token_id_count;
new_token_id_count = builtin add current_token_id_count one;
token_id_count := new_token_id_count;
token_id = new_token_id_count;
IsTokenExists token_id;
(* Mint new non-fungible token *)
token_owners[token_id] := to;
(* Add token_uri for token_id *)
token_uris[token_id] := token_uri;
e = {_eventname: "MintSuccess"; by: _sender; recipient: to;
token_id: token_id; token_uri: token_uri};
event e
end
(* @dev: Mint new tokens. Only minters can mint. *)
(* @param: to - Address of the token recipient *)
(* @param: token_uri - URI of the the new token to be minted *)
transition Mint(to: ByStr20, token_uri: String)
input_pair = build_pair to token_uri;
Minting input_pair;
token_id <- token_id_count;
msg_to_recipient = { _tag : "RecipientAcceptMint"; _recipient : to; _amount : Uint128 0 };
msg_to_sender = { _tag : "MintCallBack"; _recipient : _sender; _amount : Uint128 0;
recipient : _sender; token_id : token_id; token_uri : token_uri };
msgs = two_msgs msg_to_recipient msg_to_sender;
send msgs
end
transition BatchMint(to: ByStr20, token_uris_list: List String)
pair_token_uri_list = @list_map String (Pair ByStr20 String);
tmp_fun = build_pair to;
input_list = pair_token_uri_list tmp_fun token_uris_list;
forall input_list Minting;
msg_to_recipient = { _tag : "RecipientAcceptBatchMint"; _recipient : to; _amount : Uint128 0 };
msg_to_sender = { _tag : "BatchMintCallBack"; _recipient : _sender; _amount : Uint128 0 };
msgs = two_msgs msg_to_recipient msg_to_sender;
send msgs
end
(* @dev: Burn existing tokens. Only token_owner or an operator can burn a NFT. *)
(* @param: token_id - Unique ID of the NFT to be destroyed *)
transition Burn(token_id: Uint256)
(* Check if token exists *)
some_token_owner <- token_owners[token_id];
match some_token_owner with
| None =>
err = CodeNotFound;
ThrowError err
| Some token_owner =>
IsOwnerOrOperator token_owner;
(* Destroy existing token *)
delete token_owners[token_id];
delete token_approvals[token_id];
delete token_uris[token_id];
(* Deduct from owned_token_count *)
UpdateTokenCount sub_operation token_owner;
(* Deduct from total_supply *)
current_supply <- total_supply;
new_supply = builtin sub current_supply one;
total_supply := new_supply;
e = {_eventname: "BurnSuccess"; initiator: _sender; burn_address: token_owner; token_id: token_id};
event e;
msg_to_sender = { _tag : "BurnCallBack"; _recipient : _sender; _amount : Uint128 0;
initiator : _sender; burn_address : token_owner; token_id : token_id };
msgs = one_msg msg_to_sender;
send msgs
end
end
(* @dev: Approves OR remove an address ability to transfer a given token_id *)
(* There can only be one approved_spender per token at any given time *)
(* param: to - Address to be approved for the given token_id *)
(* param: token_id - Unique ID of the NFT to be approved *)
transition SetApprove(to: ByStr20, token_id: Uint256)
some_token_owner <- token_owners[token_id];
match some_token_owner with
| None =>
err = CodeNotFound;
ThrowError err
| Some token_owner =>
IsOwnerOrOperator token_owner;
is_approved <- token_approvals[token_id];
match is_approved with
| Some approved_spender =>
(* Remove approved_spender *)
is_approved_spender = builtin eq to approved_spender;
match is_approved_spender with
| True =>
delete token_approvals[token_id];
e = {_eventname: "RemoveApprovalSuccess"; initiator: _sender; removed_spender: to; token_id: token_id};
event e;
msg_to_sender = { _tag : "RemoveApprovalSuccessCallBack"; _recipient : _sender; _amount : Uint128 0;
removed_spender : to; token_id : token_id };
msgs = one_msg msg_to_sender;
send msgs
| False =>
err = CodeNotApproved;
ThrowError err
end
| None =>
(* Add approved_spender *)
token_approvals[token_id] := to;
e = {_eventname: "AddApprovalSuccess"; initiator: _sender; approved_spender: to; token_id: token_id};
event e;
msg_to_sender = { _tag : "AddApprovalSuccessCallBack"; _recipient : _sender; _amount : Uint128 0;
approved_spender : to; token_id : token_id };
msgs = one_msg msg_to_sender;
send msgs
end
end
end
(* @dev: Sets or unsets an operator for the _sender *)
(* @param: to - Address to be set or unset as an operator *)
transition SetApprovalForAll(to: ByStr20)
IsSelf to _sender;
is_operator <- exists operator_approvals[_sender][to];
match is_operator with
| False =>
(* Add operator *)
operator_approvals[_sender][to] := verdad;
e = {_eventname: "AddApprovalForAllSuccess"; initiator: _sender; operator: to};
event e
| True =>
(* Remove operator *)
delete operator_approvals[_sender][to];
e = {_eventname: "RemoveApprovalForAllSuccess"; initiator: _sender; operator: to};
event e
end;
new_status = negb is_operator;
msg_to_sender = { _tag : "SetApprovalForAllSuccessCallBack"; _recipient : _sender; _amount : Uint128 0;
operator : to; status : new_status};
msgs = one_msg msg_to_sender;
send msgs
end
(* @dev: Transfer the ownership of a given token_id to another address. token_owner only transition. *)
(* @param: to - Recipient address for the token *)
(* @param: token_id - Unique ID of the NFT to be transferred *)
transition Transfer(to: ByStr20, token_id: Uint256)
IsSelf to _sender;
IsTokenOwner token_id _sender;
(* Change token_owner for that token_id *)
token_owners[token_id] := to;
(* Delete tokenApproval entry for that token_id *)
delete token_approvals[token_id];
(* Subtract one from previous token owner count *)
UpdateTokenCount sub_operation _sender;
(* Add one to the new token owner count *)
UpdateTokenCount add_operation to;
e = {_eventname: "TransferSuccess"; from: _sender; recipient: to; token_id: token_id};
event e;
msg_to_recipient = { _tag : "RecipientAcceptTransfer"; _recipient : to; _amount : Uint128 0;
from : _sender; recipient : to; token_id : token_id };
msg_to_sender = { _tag : "TransferSuccessCallBack"; _recipient : _sender; _amount : Uint128 0;
from : _sender; recipient : to; token_id : token_id };
msgs = two_msgs msg_to_recipient msg_to_sender;
send msgs
end
(* @dev: Transfer the ownership of a given token_id to another address. approved_spender or operator only transition. *)
(* @param: to - Recipient address for the NFT *)
(* @param: token_id - Unique ID of the NFT to be transferred *)
transition TransferFrom(to: ByStr20, token_id: Uint256)
some_token_owner <- token_owners[token_id];
match some_token_owner with
| None =>
err = CodeNotFound;
ThrowError err
| Some token_owner =>
IsSelf to token_owner;
IsApprovedSpenderOrOperator token_id token_owner;
(* Change token_owner for that token_id *)
token_owners[token_id] := to;
(* Delete tokenApproval entry for that token_id *)
delete token_approvals[token_id];
(* Subtract one from previous token owner count *)
UpdateTokenCount sub_operation token_owner;
(* Add one to the new token owner count *)
UpdateTokenCount add_operation to;
e = {_eventname: "TransferFromSuccess"; from: token_owner; recipient: to; token_id: token_id};
event e;
msg_to_recipient = { _tag : "RecipientAcceptTransferFrom"; _recipient : to; _amount : Uint128 0;
from : token_owner; recipient : to; token_id : token_id };
msg_to_sender = { _tag : "TransferFromSuccessCallBack"; _recipient : _sender; _amount : Uint128 0;
from : token_owner; recipient : to; token_id : token_id };
msgs = two_msgs msg_to_recipient msg_to_sender;
send msgs
end
end
`;
/* NFT contract Code */
const msgVersion = 1;
let currentNetwork = window.zilPay.wallet.net;
let ChainId;
if(currentNetwork == "mainnet"){
ChainId = 1;
}else if(currentNetwork == "testnet"){
ChainId = 333;
}else {
ChainID = prompt("You are not on testnet or mainnet. Please enter your chain ID:");
}
VERSION = window.zilPay.utils.bytes.pack(ChainId, msgVersion);
myGasPrice = window.zilPay.utils.units.toQa('2000', window.zilPay.utils.units.Units.Li);
//Create new contract object
let NewContract = window.zilPay.contracts.new(NftContractCode,NftContractInits);
//call deploy method on the new contract object along with the required arguments
NewContract.deploy(
{
version: VERSION,
gasPrice: myGasPrice,
gasLimit: window.zilPay.utils.Long.fromNumber(25000),
},
33, //attempts
1000, //interval
false, //toDS or shard
).then(function(resp){
console.log("NFT Contract Deploy Transaction Sent.")
console.log(resp);
alert("Transaction Id: 0x" + resp[0].TranID);
});
}
function showCLbox() {
document.querySelector("#clbox-container").style.display = "flex";
}
function hideCLbox() {
document.querySelector("#clbox-container").style.display = "none";
}