-
Notifications
You must be signed in to change notification settings - Fork 1
/
BLCKBOOK.py
3794 lines (3140 loc) · 179 KB
/
BLCKBOOK.py
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 smartpy as sp
class BatchTransfer():
"""
Class for the transfer endpoint of the FA2-Token contract
"""
def get_transfer_type():
tx_type = sp.TRecord(to_ = sp.TAddress,
token_id = sp.TNat,
amount = sp.TNat)
tx_type = tx_type.layout(
("to_", ("token_id", "amount"))
)
transfer_type = sp.TRecord(from_ = sp.TAddress,
txs = sp.TList(tx_type)).layout(
("from_", "txs"))
return transfer_type
def get_type():
return sp.TList(BatchTransfer.get_transfer_type())
def item(from_, txs):
return sp.set_type_expr(sp.record(from_ = from_, txs = txs), BatchTransfer.get_transfer_type())
class Operator:
def get_type():
t = sp.TRecord(
owner = sp.TAddress,
operator = sp.TAddress,
token_id = sp.TNat).layout(("owner", ("operator", "token_id")))
return t
def make(owner, operator, token_id):
r = sp.record(owner = owner,
operator = operator,
token_id = token_id)
return sp.set_type_expr(r, Operator.get_type())
class TokensContract(sp.Contract):
def __init__(self, administrator):
sp.set_type_expr(administrator, sp.TAddress)
list_of_views = [
self.get_balance
, self.does_token_exist
, self.count_tokens
, self.all_tokens
, self.is_operator
]
metadata = {
"name": "BLCKBOOK",
"description": "BLCKBOOK beta implementation. Uses the didactic reference implementation of FA2,"
+ " a.k.a. TZIP-012, using SmartPy.\n\n",
"version": "FA2",
"views": list_of_views,
"interfaces": ["TZIP-012", "TZIP-016"],
"authors": ["Niels Hanselmann", "Simon Schiebler"],
"homepage": "https://blckbook.vote",
"source": {"tools": ["SmartPy"], "location": "https://github.com/BLCKBOOK/BLCKBOOK-contract"},
"permissions": {
"operator": "owner-or-operator-transfer",
"receiver": "owner-no-hook",
"sender": "owner-no-hook"
},
}
# Helper method that builds the metadata and produces the JSON representation as an artifact.
self.init_metadata("BLCKBOOK-FA2", metadata) #the string is just for the output of the online-IDE
self.init_type(sp.TRecord(
administrator = sp.TAddress,
all_tokens = sp.TNat,
ledger = sp.TBigMap(sp.TPair(sp.TAddress, sp.TNat), sp.TRecord(balance = sp.TNat).layout("balance")),
metadata = sp.TBigMap(sp.TString, sp.TBytes),
operators = sp.TBigMap(Operator.get_type(), sp.TUnit),
paused = sp.TBool,
token_metadata = sp.TBigMap(sp.TNat, sp.TRecord(token_id = sp.TNat, token_info = sp.TMap(sp.TString, sp.TBytes))
.layout(("token_id", "token_info")))
)
.layout((("administrator", ("all_tokens", ("ledger", ("metadata", ("operators", ("paused", "token_metadata")))))))))
self.init(
administrator = administrator,
all_tokens = 0,
ledger = sp.big_map(tkey = sp.TPair(sp.TAddress, sp.TNat), tvalue = sp.TRecord(balance = sp.TNat)),
metadata = sp.big_map(tkey = sp.TString, tvalue = sp.TBytes),
operators = sp.big_map(tkey = Operator.get_type(), tvalue = sp.TUnit),
paused = False,
token_metadata = sp.big_map(
tkey = sp.TNat,
tvalue = sp.TRecord(token_id = sp.TNat, token_info = sp.TMap(sp.TString, sp.TBytes))
)
)
@sp.entry_point
def balance_of(self, params):
sp.verify(~ self.data.paused, 'FA2_PAUSED')
sp.set_type(params, sp.TRecord(callback = sp.TContract(sp.TList(sp.TRecord(balance = sp.TNat, request = sp.TRecord(owner = sp.TAddress, token_id = sp.TNat)
.layout(("owner", "token_id"))).layout(("request", "balance")))),
requests = sp.TList(sp.TRecord(owner = sp.TAddress, token_id = sp.TNat).layout(("owner", "token_id")))).layout(("requests", "callback")))
def f_x0(_x0):
sp.verify(self.data.token_metadata.contains(_x0.token_id), 'FA2_TOKEN_UNDEFINED')
sp.if self.data.ledger.contains((sp.set_type_expr(_x0.owner, sp.TAddress), sp.set_type_expr(_x0.token_id, sp.TNat))):
sp.result(sp.record(request = sp.record(owner = sp.set_type_expr(_x0.owner, sp.TAddress), token_id =
sp.set_type_expr(_x0.token_id, sp.TNat)), balance = self.data.ledger[(sp.set_type_expr(_x0.owner, sp.TAddress), sp.set_type_expr(_x0.token_id, sp.TNat))].balance))
sp.else:
sp.result(sp.record(request = sp.record(owner = sp.set_type_expr(_x0.owner, sp.TAddress), token_id =
sp.set_type_expr(_x0.token_id, sp.TNat)), balance = 0))
responses = sp.local("responses", params.requests.map(sp.build_lambda(f_x0)))
sp.transfer(responses.value, sp.tez(0), sp.set_type_expr(params.callback,
sp.TContract(sp.TList(sp.TRecord(balance = sp.TNat, request = sp.TRecord(owner = sp.TAddress, token_id = sp.TNat)
.layout(("owner", "token_id"))).layout(("request", "balance"))))))
@sp.entry_point
def mint(self, params):
sp.verify(sp.sender == self.data.administrator, 'FA2_NOT_ADMIN')
sp.verify(params.amount == 1, 'NFT-asset: amount <> 1')
sp.verify(~ (params.token_id < self.data.all_tokens), 'NFT-asset: cannot mint the same token twice')
self.data.ledger[(sp.set_type_expr(params.address, sp.TAddress),
sp.set_type_expr(params.token_id, sp.TNat))] = sp.record(balance = params.amount)
sp.if ~ (params.token_id < self.data.all_tokens):
sp.verify(self.data.all_tokens == params.token_id, 'Token-IDs should be consecutive')
self.data.all_tokens = params.token_id + 1
self.data.token_metadata[params.token_id] = sp.record(token_id = params.token_id, token_info = params.metadata)
@sp.entry_point
def set_administrator(self, params):
sp.verify(sp.sender == self.data.administrator, 'FA2_NOT_ADMIN')
self.data.administrator = params
@sp.entry_point
def set_metadata(self, params):
sp.verify(sp.sender == self.data.administrator, 'FA2_NOT_ADMIN')
self.data.metadata[params.k] = params.v
@sp.entry_point
def set_pause(self, params):
sp.verify(sp.sender == self.data.administrator, 'FA2_NOT_ADMIN')
self.data.paused = params
@sp.entry_point
def transfer(self, params):
sp.verify(~ self.data.paused, 'FA2_PAUSED')
sp.set_type(params, BatchTransfer.get_type())
sp.for transfer in params:
sp.for tx in transfer.txs:
sp.verify(((sp.sender == self.data.administrator) | (transfer.from_ == sp.sender)) | (self.data.operators.contains(sp.set_type_expr(sp.record(owner = transfer.from_, operator = sp.sender, token_id = tx.token_id), sp.TRecord(operator = sp.TAddress, owner = sp.TAddress, token_id = sp.TNat).layout(("owner", ("operator", "token_id")))))), 'FA2_NOT_OPERATOR')
sp.verify(self.data.token_metadata.contains(tx.token_id), 'FA2_TOKEN_UNDEFINED')
sp.if tx.amount > 0:
sp.verify(self.data.ledger[(sp.set_type_expr(transfer.from_, sp.TAddress), sp.set_type_expr(tx.token_id, sp.TNat))]
.balance >= tx.amount, 'FA2_INSUFFICIENT_BALANCE')
self.data.ledger[(sp.set_type_expr(transfer.from_, sp.TAddress),
sp.set_type_expr(tx.token_id, sp.TNat))].balance = sp.as_nat(self.data.ledger[
(sp.set_type_expr(transfer.from_, sp.TAddress), sp.set_type_expr(tx.token_id, sp.TNat))].balance - tx.amount)
sp.if self.data.ledger.contains((sp.set_type_expr(tx.to_, sp.TAddress), sp.set_type_expr(tx.token_id, sp.TNat))):
self.data.ledger[(sp.set_type_expr(tx.to_, sp.TAddress), sp.set_type_expr(tx.token_id, sp.TNat))].balance += tx.amount
sp.else:
self.data.ledger[(sp.set_type_expr(tx.to_, sp.TAddress),
sp.set_type_expr(tx.token_id, sp.TNat))] = sp.record(balance = tx.amount)
@sp.entry_point
def update_operators(self, params):
sp.set_type(params, sp.TList(sp.TVariant(add_operator = sp.TRecord(operator = sp.TAddress, owner = sp.TAddress, token_id = sp.TNat)
.layout(("owner", ("operator", "token_id"))), remove_operator = sp.TRecord(operator = sp.TAddress, owner = sp.TAddress, token_id = sp.TNat)
.layout(("owner", ("operator", "token_id")))).layout(("add_operator", "remove_operator"))))
sp.for update in params:
with update.match_cases() as arg:
with arg.match('add_operator') as add_operator:
sp.verify((add_operator.owner == sp.sender) | (sp.sender == self.data.administrator), 'FA2_NOT_ADMIN_OR_OPERATOR')
self.data.operators[sp.set_type_expr(sp.record(owner = add_operator.owner, operator = add_operator.operator, token_id =
add_operator.token_id), sp.TRecord(operator = sp.TAddress, owner = sp.TAddress, token_id = sp.TNat)
.layout(("owner", ("operator", "token_id"))))] = sp.unit
with arg.match('remove_operator') as remove_operator:
sp.verify((remove_operator.owner == sp.sender) | (sp.sender == self.data.administrator), 'FA2_NOT_ADMIN_OR_OPERATOR')
del self.data.operators[sp.set_type_expr(sp.record(owner = remove_operator.owner, operator =
remove_operator.operator, token_id = remove_operator.token_id),
sp.TRecord(operator = sp.TAddress, owner = sp.TAddress, token_id = sp.TNat).layout(("owner", ("operator", "token_id"))))]
@sp.entry_point
def burn(self, address, token_id):
"""
Burn tokens (destroy existing tokens)
Args:
address : sp.TAddress - Token holder address
token_id : sp.TNat - Id of the token
"""
# We don't check for pauseness because we're the admin.
sp.verify(sp.sender == self.data.administrator, 'FA2_NOT_ADMIN')
sp.verify(self.data.token_metadata.contains(token_id), 'FA2_TOKEN_UNDEFINED')
user = (sp.set_type_expr(address, sp.TAddress), sp.set_type_expr(token_id, sp.TNat))
sp.verify(self.data.ledger.contains(user), 'FA2_WRONG_ADDRESS_FOR_BURN')
sp.verify(self.data.ledger[user].balance == sp.nat(1), 'FA2_ADDRESS_DOES_NOT_HAVE_TOKEN_FOR_BURN')
self.data.ledger[user].balance = sp.nat(0)
@sp.onchain_view(pure = True)
def get_balance(self, req):
"""This is the `get_balance` view defined in TZIP-12."""
sp.set_type(
req, sp.TRecord(
owner = sp.TAddress,
token_id = sp.TNat
).layout(("owner", "token_id")))
user = sp.set_type_expr(req.owner, sp.TAddress)
token = sp.set_type_expr(req.token_id, sp.TNat)
ledger_key = sp.pair(user, token)
sp.verify(self.data.token_metadata.contains(req.token_id), message = 'FA2_TOKEN_UNDEFINED')
sp.result(self.data.ledger[ledger_key].balance)
@sp.onchain_view(pure = True)
def count_tokens(self):
"""Get how many tokens are in this FA2 contract."""
sp.result(self.data.all_tokens)
@sp.onchain_view(pure = True)
def does_token_exist(self, tok):
"Ask whether a token ID is exists."
sp.set_type(tok, sp.TNat)
sp.result(self.data.token_metadata.contains(tok))
@sp.onchain_view(pure = True)
def all_tokens(self):
sp.result(sp.range(0, self.data.all_tokens))
@sp.onchain_view(pure = True)
def is_operator(self, query):
sp.set_type(query,
sp.TRecord(token_id = sp.TNat,
owner = sp.TAddress,
operator = sp.TAddress).layout(
("owner", ("operator", "token_id"))))
sp.result(
self.data.operators.contains(sp.record(owner = query.owner,
operator = query.operator,
token_id = query.token_id))
)
class AuctionErrorMessage:
PREFIX = "AUC_"
ID_ALREADY_IN_USE = "{}ID_ALREADY_IN_USE".format(PREFIX)
UPLOADER_CANNOT_BID = "{}UPLOADER_CANNOT_BID".format(PREFIX)
BID_AMOUNT_TOO_LOW = "{}BID_AMOUNT_TOO_LOW".format(PREFIX)
AUCTION_IS_OVER = "{}AUCTION_IS_OVER".format(PREFIX)
AUCTION_IS_ONGOING = "{}AUCTION_IS_ONGOING".format(PREFIX)
SENDER_NOT_BIDDER = "{}SENDER_NOT_BIDDER".format(PREFIX)
END_DATE_TOO_SOON = "{}END_DATE_TOO_SOON".format(PREFIX)
END_DATE_TOO_LATE = "{}END_DATE_TOO_LATE".format(PREFIX)
NOT_ADMIN = "{}NOT_ADMIN".format(PREFIX)
CAN_NOT_CREATE_AN_AUCTION_TWICE = "{}CAN_NOT_CREATE_AN_AUCTION_TWICE".format(PREFIX)
AUCTION_ID_SHOULD_BE_CONSECUTIVE = "{}AUCTION_ID_SHOULD_BE_CONSECUTIVE".format(PREFIX)
NOT_100 = "{}SHARES_MUST_SUM_UP_TO_100".format(PREFIX)
AUCTION_DOES_NOT_EXIST = "{}DOES_NOT_EXIST".format(PREFIX)
INITIAL_BID = sp.mutez(900000)
MINIMAL_BID = sp.mutez(100000)
MINIMAL_AUCTION_DURATION = sp.int(1) # 1 minute
MAXIMAL_AUCTION_DURATION = sp.int(24*14) # 14 days
AUCTION_EXTENSION_THRESHOLD = sp.int(60*5) # 5 minutes. Check whether we actually want this
BID_STEP_THRESHOLD = sp.mutez(100000)
class AuctionCreationParams():
"""
The data-type class for creating a new auction
"""
def get_type():
return sp.TRecord(
auction_and_token_id=sp.TNat,
end_timestamp=sp.TTimestamp,
voter_amount=sp.TNat,
uploader=sp.TAddress,
bid_amount=sp.TMutez,
).layout(("auction_and_token",("end_timestamp",("voter_amount",("uploader","bid_amount")))))
class Auction():
"""
The data-type class for a single auction contained in the auction-house-contract
"""
def get_type():
return sp.TRecord(
end_timestamp=sp.TTimestamp,
voter_amount=sp.TNat,
uploader=sp.TAddress,
bid_amount=sp.TMutez, #holds the current bid (at the start the minimal bid)
bidder=sp.TAddress,
).layout(("end_timestamp",("voter_amount",("uploader",("bid_amount","bidder")))))
class AuctionHouseContract(sp.Contract):
"""
The smart contract for the actual Auction-House
"""
def __init__(self, administrator, blckbook_collector, voter_money_pool, tokens_contract_address):
list_of_views = [
self.get_expired_auctions
]
metadata = {
"name": "BLCKBOOK-Auction-House",
"description": "BLCKBOOK beta implementation of the Auction-House",
"views": list_of_views,
"authors": ["Niels Hanselmann", "Simon Schiebler"],
"homepage": "https://blckbook.vote",
"source": {"tools": ["SmartPy"], "location": "https://github.com/BLCKBOOK/BLCKBOOK-contract"},
}
self.init_metadata("AuctionHouseContract", metadata)
sp.set_type_expr(administrator, sp.TAddress)
sp.set_type_expr(blckbook_collector, sp.TAddress)
sp.set_type_expr(voter_money_pool, sp.TAddress)
sp.set_type_expr(tokens_contract_address, sp.TAddress)
self.init_type(sp.TRecord(
administrator = sp.TAddress,
blckbook_collector = sp.TAddress,
voter_money_pool = sp.TAddress,
tokens_contract_address = sp.TAddress,
blckbook_share=sp.TNat,
uploader_share=sp.TNat,
voter_share=sp.TNat,
auctions = sp.TBigMap(sp.TNat, Auction.get_type()),
all_auctions = sp.TNat,
metadata = sp.TBigMap(sp.TString, sp.TBytes),
).layout(("administrator", ("blckbook_collector", ("voter_money_pool", ("tokens_contract_address", ("blckbook_share", ("uploader_share", ("voter_share", ("all_auctions", ("auctions", "metadata")))))))))))
self.init(blckbook_share = sp.nat(25),
voter_share = sp.nat(15),
uploader_share = sp.nat(60),
auctions=sp.big_map(tkey=sp.TNat, tvalue = Auction.get_type()),
blckbook_collector = blckbook_collector,
administrator = administrator,
tokens_contract_address = tokens_contract_address,
voter_money_pool = voter_money_pool,
metadata = sp.big_map(tkey = sp.TString, tvalue = sp.TBytes),
all_auctions= sp.nat(0))
@sp.entry_point
def set_administrator(self, params):
sp.verify(sp.sender == self.data.administrator, AuctionErrorMessage.NOT_ADMIN)
self.data.administrator = params
@sp.entry_point
def set_tokens_contract_address(self, params):
"""
Entry-Point for setting the FA2-Contract Address
"""
sp.verify(sp.sender == self.data.administrator, AuctionErrorMessage.NOT_ADMIN)
self.data.tokens_contract_address = params
@sp.entry_point
def set_blckbook_collector(self, params):
"""
Entry-Point for setting the address of the blckbook_collector which will get the blckbook share of the auction-prices
"""
sp.verify(sp.sender == self.data.administrator, AuctionErrorMessage.NOT_ADMIN)
self.data.blckbook_collector = params
@sp.entry_point
def set_voter_money_pool_address(self, params):
"""
Entry-Point for setting the address of the voter_money_pool which will get the shares for the voters and will get called with the info how much every voter gets
"""
sp.verify(sp.sender == self.data.administrator, AuctionErrorMessage.NOT_ADMIN)
self.data.voter_money_pool = params
@sp.entry_point
def set_shares(self, blckbook_share, uploader_share, voter_share):
"""
Entry-Point for setting the share percentages of the auction-price
"""
sp.verify(sp.sender == self.data.administrator, AuctionErrorMessage.NOT_ADMIN)
sp.set_type_expr(blckbook_share, sp.TNat)
sp.set_type_expr(uploader_share, sp.TNat)
sp.set_type_expr(voter_share, sp.TNat)
sp.verify(blckbook_share + uploader_share + voter_share == sp.nat(100), AuctionErrorMessage.NOT_100)
self.data.blckbook_share = blckbook_share
self.data.uploader_share = uploader_share
self.data.voter_share = voter_share
@sp.entry_point
def create_auction(self, create_auction_request):
"""
Entry-Point for creating a new auction
"""
sp.verify(sp.sender == self.data.administrator, AuctionErrorMessage.NOT_ADMIN) # only admin can create auction (nft needs to be minted for auction-contract)
sp.set_type_expr(create_auction_request, AuctionCreationParams.get_type())
sp.verify(~(create_auction_request.auction_and_token_id < self.data.all_auctions), message=AuctionErrorMessage.CAN_NOT_CREATE_AN_AUCTION_TWICE)
sp.verify(self.data.all_auctions == create_auction_request.auction_and_token_id, message=AuctionErrorMessage.AUCTION_ID_SHOULD_BE_CONSECUTIVE)
sp.verify(create_auction_request.end_timestamp >= sp.now.add_minutes(MINIMAL_AUCTION_DURATION), message=AuctionErrorMessage.END_DATE_TOO_SOON)
sp.verify(create_auction_request.end_timestamp <= sp.now.add_hours(MAXIMAL_AUCTION_DURATION), message=AuctionErrorMessage.END_DATE_TOO_LATE)
sp.verify(create_auction_request.bid_amount >= MINIMAL_BID, message=AuctionErrorMessage.BID_AMOUNT_TOO_LOW)
sp.verify(~self.data.auctions.contains(create_auction_request.auction_and_token_id), message=AuctionErrorMessage.ID_ALREADY_IN_USE)
#set the actual auction in the auctions
self.data.auctions[create_auction_request.auction_and_token_id] = sp.record(
end_timestamp=create_auction_request.end_timestamp,
uploader=create_auction_request.uploader,
bid_amount=create_auction_request.bid_amount,
voter_amount=create_auction_request.voter_amount,
bidder=create_auction_request.uploader)
#and increase the auction_and_token_id counter
self.data.all_auctions = create_auction_request.auction_and_token_id + 1
@sp.entry_point
def bid(self, auction_and_token_id):
"""
Entry-Point for bidding on an auction (will be called by the users)
"""
sp.set_type_expr(auction_and_token_id, sp.TNat)
sp.verify(self.data.auctions.contains(auction_and_token_id), message = AuctionErrorMessage.AUCTION_DOES_NOT_EXIST)
auction = self.data.auctions[auction_and_token_id] #find the auction the user wants to bid on
sp.verify(sp.sender != auction.uploader, message = AuctionErrorMessage.UPLOADER_CANNOT_BID)
sp.verify(sp.amount >= auction.bid_amount + BID_STEP_THRESHOLD, message=AuctionErrorMessage.BID_AMOUNT_TOO_LOW)
sp.verify(sp.now < auction.end_timestamp, message = AuctionErrorMessage.AUCTION_IS_OVER)
#do not send the initial amount to the uploader because we just use this as a minimal amount for the auction
sp.if auction.bidder != auction.uploader:
sp.send(auction.bidder, auction.bid_amount)
# otherwise we transfer the previous bid_amount to the previous highest bidder
auction.bidder = sp.sender
auction.bid_amount = sp.amount
#This will extend an auction-timeframe if an auction is bid on in the last 5 minutes. Which is common practice in tezos auctions
sp.if auction.end_timestamp-sp.now < AUCTION_EXTENSION_THRESHOLD:
auction.end_timestamp = sp.now.add_seconds(AUCTION_EXTENSION_THRESHOLD)
self.data.auctions[auction_and_token_id] = auction
@sp.entry_point
def set_metadata(self, params):
sp.verify(sp.sender == self.data.administrator, AuctionErrorMessage.NOT_ADMIN)
self.data.metadata[params.k] = params.v
@sp.entry_point
def end_auction(self, auction_and_token_id):
"""
Entry-Point for ending an auction. Can be called by anyone.
"""
sp.set_type_expr(auction_and_token_id, sp.TNat)
sp.verify(self.data.auctions.contains(auction_and_token_id), message = AuctionErrorMessage.AUCTION_DOES_NOT_EXIST)
auction = self.data.auctions[auction_and_token_id]
sp.verify(sp.now > auction.end_timestamp, message=AuctionErrorMessage.AUCTION_IS_ONGOING)
# initialize voter_reward and voter_transaction to 0
# because we transmit it if no one bid on the auction, so it gets resolved in the data-structure of the VoterMoneyPool
voter_reward = sp.local("voter_reward", sp.nat(0))
voter_transaction = sp.local("voter_transaction", sp.nat(0))
# somebody bid who isn't the uploader => we actually got value
sp.if auction.bidder != auction.uploader:
# calculation of the shares
bid_amount = sp.local("bid_amount", sp.utils.mutez_to_nat(auction.bid_amount))
percentage = sp.local("percentage", bid_amount.value // sp.nat(100))
percentage_remainder = sp.local("percentage_remainder", bid_amount.value % sp.nat(100))
uploader_reward = sp.local("uploader_reward", percentage.value * self.data.uploader_share)
# initialize remainder2 with the total edge-case of voter_amount being 0 for a minted artwork, that was bid on
# set the remainder2 to the actual total amount for the edge case of 0 voters
remainder2 = sp.local("remainder2", percentage.value * self.data.voter_share)
sp.if auction.voter_amount > 0:
# and in the normal case overwrite the values. Can't be done in if-else because of scope
voter_reward.value = (percentage.value * self.data.voter_share) // auction.voter_amount
remainder2.value = (percentage.value * self.data.voter_share) % auction.voter_amount
voter_transaction.value = voter_reward.value * auction.voter_amount
blckbook_reward = sp.local("blckbook_reward", self.data.blckbook_share * percentage.value + percentage_remainder.value + remainder2.value)
sp.send(auction.uploader, sp.utils.nat_to_mutez(uploader_reward.value))
sp.send(self.data.blckbook_collector, sp.utils.nat_to_mutez(blckbook_reward.value))
voter_money_pool_contract = sp.contract(SetAuctionRewardParams.get_type(), self.data.voter_money_pool, entry_point = "set_auction_rewards").open_some()
sp.transfer(
sp.record(auction_and_token_id=auction_and_token_id, reward=sp.utils.nat_to_mutez(voter_reward.value)),
sp.utils.nat_to_mutez(voter_transaction.value),
voter_money_pool_contract,
)
token_contract = sp.contract(BatchTransfer.get_type(), self.data.tokens_contract_address, entry_point = "transfer").open_some()
# we always transfer to the highest-bidder which could be the uploader (if no-one bid on the auction)
sp.transfer([BatchTransfer.item(sp.self_address, [sp.record(to_=auction.bidder, token_id=auction_and_token_id, amount=sp.nat(1))])],
sp.mutez(0), token_contract)
del self.data.auctions[auction_and_token_id] #this will delete the auction-entry (so we reduce the storage-diff)- otherwise make it so an auction can not be ended twice
@sp.onchain_view(pure = True)
def get_expired_auctions(self, timestamp):
sp.set_type_expr(timestamp, sp.TTimestamp)
i = sp.local('i', sp.nat(0))
expired_auctions = sp.local('expired_auctions', sp.list([], t = sp.TNat))
sp.while i.value < self.data.all_auctions:
sp.if self.data.auctions.contains(i.value) & (timestamp > self.data.auctions[i.value].end_timestamp):
expired_auctions.value.push(i.value)
i.value += 1
sp.result(expired_auctions.value)
class AddVotesParams():
"""
The data-type class for adding votes to a single auction (and its corresponding token)
"""
def get_type():
return sp.TRecord(
voter_addresses=sp.TList(sp.TAddress),
auction_and_token_id=sp.TNat,
).layout(("voter_addresses","auction_and_token_id"))
class VoterMoneyPoolErrorMessage:
PREFIX = "VOTER_MONEY_POOL_"
NOT_ADMIN = "{}NOT_ADMIN".format(PREFIX)
AUCTION_ALREADY_RESOLVED = "{}AUCTION_ALREADY_RESOLVED".format(PREFIX)
NOT_A_VOTER = "{}NOT_A_VOTER".format(PREFIX)
NOT_AUCTION_HOUSE = "{}NOT_THE_AUCTION_HOUSE".format(PREFIX)
ALL_VOTES_ALREADY_PAYED_OUT = "{}ALL_VOTES_ALREADY_PAYED_OUT".format(PREFIX)
class SetAuctionRewardParams():
"""
The data-type class for setting the voter_rewards for a specific auction (and it's corresponding token)
"""
def get_type():
return sp.TRecord(
auction_and_token_id=sp.TNat,
reward=sp.TMutez)
class VoterMoneyPoolContract(sp.Contract):
def __init__(self, administrator):
list_of_views = [
self.get_balance
]
metadata = {
"name": "BLCKBOOK-VoterMoneyPool",
"description": "BLCKBOOK beta implementation of a VoterMoneyPool",
"views": list_of_views,
"authors": ["Niels Hanselmann", "Simon Schiebler"],
"homepage": "https://blckbook.vote",
"source": {"tools": ["SmartPy"], "location": "https://github.com/BLCKBOOK/BLCKBOOK-contract"},
}
# Helper method that builds the metadata and produces the JSON representation as an artifact.
self.init_metadata("VoterMoneyPoolContract", metadata) #the string is just for the output of the online-IDE
self.init_type(sp.TRecord(
administrator = sp.TAddress,
resolved_auctions = sp.TBigMap(sp.TNat, sp.TMutez),
vote_map = sp.TBigMap(sp.TAddress, sp.TList(sp.TNat)),
metadata = sp.TBigMap(sp.TString, sp.TBytes),
auction_house_address = sp.TVariant(address=sp.TAddress, none=sp.TUnit),
))
self.init(
administrator = administrator,
resolved_auctions=sp.big_map(tkey=sp.TNat, tvalue = sp.TMutez),
vote_map = sp.big_map(tkey=sp.TAddress, tvalue=sp.TList(sp.TNat)),
metadata = sp.big_map(tkey = sp.TString, tvalue = sp.TBytes),
auction_house_address = sp.variant("none", sp.unit)
)
@sp.entry_point
def set_administrator(self, params):
sp.verify(sp.sender == self.data.administrator, VoterMoneyPoolErrorMessage.NOT_ADMIN)
self.data.administrator = params
@sp.entry_point
def set_auction_house_address(self, params):
sp.verify(sp.sender == self.data.administrator, VoterMoneyPoolErrorMessage.NOT_ADMIN)
sp.set_type(params, sp.TAddress)
self.data.auction_house_address = sp.variant("address", params)
@sp.entry_point
def set_auction_rewards(self, params):
# maybe change this so a user can resolve the auction to check for sender = AuctionHouseContract
sp.if self.data.auction_house_address.is_variant("address"):
sp.verify(sp.sender == self.data.auction_house_address.open_variant("address"), VoterMoneyPoolErrorMessage.NOT_AUCTION_HOUSE)
sp.else:
sp.verify(sp.source == self.data.administrator, VoterMoneyPoolErrorMessage.NOT_ADMIN)
sp.set_type(params, SetAuctionRewardParams.get_type())
sp.verify(~self.data.resolved_auctions.contains(params.auction_and_token_id), VoterMoneyPoolErrorMessage.AUCTION_ALREADY_RESOLVED)
self.data.resolved_auctions[params.auction_and_token_id] = params.reward
@sp.entry_point
def add_votes(self, votes):
sp.verify(sp.sender == self.data.administrator, VoterMoneyPoolErrorMessage.NOT_ADMIN) # only admin can create auction (nft needs to be minted for auction-contract)
sp.set_type_expr(votes, AddVotesParams.get_type())
sp.for vote in votes.voter_addresses:
self.data.vote_map[vote] = sp.cons(votes.auction_and_token_id, self.data.vote_map.get(vote, default_value = []))
@sp.entry_point
def withdraw(self):
sp.verify(self.data.vote_map.contains(sp.sender), VoterMoneyPoolErrorMessage.NOT_A_VOTER)
sum = sp.local("sum", sp.mutez(0))
not_resolved_yet = sp.local('not_resolved_yet', sp.list([], t = sp.TNat))
already_resolved = sp.local('already_resolved', sp.set({}, t = sp.TNat))
sp.for auction in self.data.vote_map[sp.sender]:
# check that the auction is not in the already_resolved set so we do not add to the sum twice
# this helps prevent errors in the data (when a voter somehow voted twice for the same auction)
sp.if ~(already_resolved.value.contains(auction)):
sp.if self.data.resolved_auctions.contains(auction):
sum.value = sum.value + self.data.resolved_auctions[auction]
already_resolved.value.add(auction)
sp.else:
not_resolved_yet.value.push(auction)
self.data.vote_map[sp.sender] = not_resolved_yet.value
sp.if sum.value > sp.mutez(0):
sp.send(sp.sender, sum.value)
sp.if sp.len(already_resolved.value.elements()) == 0:
sp.failwith(VoterMoneyPoolErrorMessage.ALL_VOTES_ALREADY_PAYED_OUT)
@sp.entry_point
def set_metadata(self, params):
sp.verify(sp.sender == self.data.administrator, VoterMoneyPoolErrorMessage.NOT_ADMIN)
self.data.metadata[params.k] = params.v
@sp.onchain_view(pure = True)
def get_balance(self, address):
"""This view calculates how much a voter will get from withdrawing"""
sp.set_type(address, sp.TAddress)
sum = sp.local("sum", sp.mutez(0))
already_resolved = sp.local('already_resolved', sp.set({}, t = sp.TNat))
sp.if self.data.vote_map.contains(address):
sp.for auction in self.data.vote_map[address]:
sp.if self.data.resolved_auctions.contains(auction) & ~(already_resolved.value.contains(auction)):
sum.value = sum.value + self.data.resolved_auctions[auction]
already_resolved.value.add(auction)
sp.result(sum.value)
class FA2Spray(sp.Contract):
"""Minimal FA2 contract for fungible tokens.
TODO: fix this comment
This is a minimal example showing how to implement an NFT following
the FA2 standard in SmartPy. It is for illustrative purposes only.
For a more flexible toolbox aimed at real world applications please
refer to FA2_lib.
"""
def __init__(self, administrator, the_vote):
list_of_views = [
self.get_balance
, self.does_token_exist
, self.count_tokens
, self.all_tokens
, self.is_operator
]
metadata_base = {
"name": "BLCKBOOK $PRAY",
"version": "1.0.0",
"views": list_of_views,
"description": "This is an adapted minimal implementation of FA2 (TZIP-012) using SmartPy. It is used for the $PRAY-Token",
"interfaces": ["TZIP-012", "TZIP-016"],
"authors": ["Niels Hanselmann", "SmartPy <https://smartpy.io/#contact>"],
"homepage": "https://blckbook.vote",
"source": {
"tools": ["SmartPy"],
"location": "https://github.com/BLCKBOOK/BLCKBOOK-contract",
},
"permissions": {
"operator": "owner-or-operator-transfer",
"receiver": "owner-no-hook",
"sender": "owner-no-hook",
},
}
self.init(
administrator=administrator,
the_vote=the_vote,
ledger=sp.big_map(tkey=sp.TPair(sp.TAddress, sp.TNat), tvalue=sp.TNat),
metadata=sp.big_map(tkey=sp.TString, tvalue=sp.TBytes),
next_token_id=sp.nat(0),
operators=sp.big_map(
tkey=sp.TRecord(
owner=sp.TAddress, operator=sp.TAddress, token_id=sp.TNat
).layout(("owner", ("operator", "token_id"))),
tvalue=sp.TUnit,
),
supply=sp.big_map(tkey=sp.TNat, tvalue=sp.TNat),
token_metadata=sp.big_map(
tkey=sp.TNat,
tvalue=sp.TRecord(
token_id=sp.TNat, token_info=sp.TMap(sp.TString, sp.TBytes)
),
),
)
# Helper method that builds the metadata and produces the JSON representation as an artifact.
self.init_metadata("BLCKBOOK-$PRAY", metadata_base) # the string is just for the output of the online-IDE
@sp.entry_point
def set_metadata(self, params):
sp.verify(sp.sender == self.data.administrator, 'FA2_NOT_ADMIN')
self.data.metadata[params.k] = params.v
@sp.entry_point
def transfer(self, batch):
"""Accept a list of transfer operations.
Each transfer operation specifies a source: `from_` and a list
of transactions. Each transaction specifies the destination: `to_`,
the `token_id` and the `amount` to be transferred.
Args:
batch: List of transfer operations.
Raises:
`FA2_TOKEN_UNDEFINED`, `FA2_NOT_OPERATOR`, `FA2_INSUFFICIENT_BALANCE`
"""
sp.set_type(batch, BatchTransfer.get_type())
with sp.for_("transfer", batch) as transfer:
with sp.for_("tx", transfer.txs) as tx:
sp.verify(tx.token_id < self.data.next_token_id, "FA2_TOKEN_UNDEFINED")
from_ = (transfer.from_, tx.token_id)
to_ = (tx.to_, tx.token_id)
sp.verify((sp.sender == transfer.from_)
| self.data.operators.contains(sp.record(owner=transfer.from_, operator=sp.sender, token_id=tx.token_id))
# We allow the_vote to transmit all tokens
| (sp.sender == self.data.the_vote), message="FA2_NOT_OPERATOR")
# reduce the amount and see if it is still >= 0
self.data.ledger[from_] = sp.as_nat(
self.data.ledger.get(from_, 0) - tx.amount,
message="FA2_INSUFFICIENT_BALANCE",
)
# add the amount to the "to"
self.data.ledger[to_] = self.data.ledger.get(to_, 0) + tx.amount
@sp.entry_point
def update_operators(self, actions):
"""Accept a list of variants to add or remove operators.
Operators can perform transfer on behalf of the owner.
Owner is a Tezos address which can hold tokens.
Only the owner can change its operators.
Args:
actions: List of operator update actions.
Raises:
`FA2_NOT_OWNER`
"""
with sp.for_("update", actions) as action:
with action.match_cases() as arg:
with arg.match("add_operator") as operator:
sp.verify(operator.owner == sp.sender, "FA2_NOT_OWNER")
self.data.operators[operator] = sp.unit
with arg.match("remove_operator") as operator:
sp.verify(operator.owner == sp.sender, "FA2_NOT_OWNER")
del self.data.operators[operator]
@sp.entry_point
def balance_of(self, callback, requests):
"""Send the balance of multiple account / token pairs to a
callback address.
transfer 0 mutez to `callback` with corresponding response.
Args:
callback (contract): Where we callback the answer.
requests: List of requested balances.
Raises:
`FA2_TOKEN_UNDEFINED`, `FA2_CALLBACK_NOT_FOUND`
"""
def f_process_request(req):
sp.verify(req.token_id < self.data.next_token_id, "FA2_TOKEN_UNDEFINED")
sp.result(
sp.record(
request=sp.record(owner=req.owner, token_id=req.token_id),
balance=self.data.ledger.get((req.owner, req.token_id), 0),
)
)
t_request = sp.TRecord(owner=sp.TAddress, token_id=sp.TNat)
sp.set_type(requests, sp.TList(t_request))
sp.set_type(
callback,
sp.TContract(sp.TList(sp.TRecord(request=t_request, balance=sp.TNat))),
)
sp.transfer(requests.map(f_process_request), sp.mutez(0), callback)
@sp.entry_point
def set_administrator(self, params):
sp.verify(sp.sender == self.data.administrator, "FA2_NOT_ADMIN")
self.data.administrator = params
@sp.entry_point
def set_the_vote(self, params):
sp.verify(sp.sender == self.data.administrator, "FA2_NOT_ADMIN")
self.data.the_vote = params
@sp.entry_point
def mint(self, to_, amount, token):
"""(Admin only) Create new tokens from scratch and assign
them to `to_`.
If `token` is "existing": increase the supply of the `token_id`.
If `token` is "new": create a new token and assign the `metadata`.
Args:
to_ (address): Receiver of the tokens.
amount (nat): Amount of token to be minted.
token (variant): "_new_": id of the token, "_existing_": metadata of the token.
Raises:
`FA2_NOT_ADMIN`, `FA2_TOKEN_UNDEFINED`
"""
sp.verify(sp.sender == self.data.administrator, "FA2_NOT_ADMIN")
with token.match_cases() as arg:
with arg.match("new") as metadata:
sp.verify(self.data.next_token_id == 0, "FA2_NOT_SINGLE_ASSET")
token_id = sp.compute(self.data.next_token_id) # only can mint one token and then create more of it
self.data.token_metadata[token_id] = sp.record(
token_id=token_id, token_info=metadata
)
self.data.supply[token_id] = amount
self.data.ledger[(to_, token_id)] = amount
self.data.next_token_id += 1
with arg.match("existing") as token_id:
sp.verify(token_id < self.data.next_token_id, "FA2_TOKEN_UNDEFINED")
self.data.supply[token_id] += amount
self.data.ledger[(to_, token_id)] = (
self.data.ledger.get((to_, token_id), 0) + amount
)
@sp.onchain_view(pure=True)
def all_tokens(self):
"""(Onchain view) Return the list of all the `token_id` known to the contract."""
sp.result(sp.range(0, self.data.next_token_id))
@sp.onchain_view(pure = True)
def count_tokens(self):
"""Get how many tokens are in this FA2 contract."""
sp.result(self.data.next_token_id)
@sp.onchain_view(pure = True)
def does_token_exist(self, tok):
"Ask whether a token ID is exists."
sp.set_type(tok, sp.TNat)
sp.result(self.data.token_metadata.contains(tok))
@sp.onchain_view(pure=True)
def get_balance(self, params):
"""(Onchain view) Return the balance of an address for the specified `token_id`."""
sp.set_type_expr(
params,
sp.TRecord(owner=sp.TAddress, token_id=sp.TNat).layout(
("owner", "token_id")
),
)
sp.verify(params.token_id < self.data.next_token_id, "FA2_TOKEN_UNDEFINED")
sp.result(self.data.ledger.get((params.owner, params.token_id), 0))
@sp.onchain_view(pure=True)
def total_supply(self, params):
"""(Onchain view) Return the total number of tokens for the given `token_id` if known or
fail if not."""
sp.verify(params.token_id < self.data.next_token_id, "FA2_TOKEN_UNDEFINED")
sp.result(self.data.supply.get(params.token_id, 0))
@sp.onchain_view(pure=True)
def is_operator(self, params):
"""(Onchain view) Return whether `operator` is allowed to transfer `token_id` tokens
owned by `owner`."""
sp.result(self.data.operators.contains(params))
class SprayBank(sp.Contract):
def __init__(self, administrator, spray_address, the_vote_address):
self.init_type(sp.TRecord(
administrator=sp.TAddress,
spray_address=sp.TAddress,
the_vote_address=sp.TAddress,
withdrawls=sp.TBigMap(sp.TAddress, sp.TNat),
withdraw_limit=sp.TNat,
withdraw_period=sp.TNat,
))
self.init(
administrator=administrator,
spray_address=spray_address,
the_vote_address=the_vote_address,
withdrawls=sp.big_map(
tkey=sp.TAddress,
tvalue=sp.TNat
),
withdraw_limit=sp.nat(5),
withdraw_period=sp.nat(1),
# we start with 1, so we can have 0 as a default value for someone who has not withdrawn
)
@sp.entry_point
def set_administrator(self, params):
sp.verify(sp.sender == self.data.administrator, '$PRAY_BANK_NOT_ADMIN')
sp.set_type(params, sp.TAddress)
self.data.administrator = params
@sp.entry_point
def set_spray_address(self, params):
sp.verify(sp.sender == self.data.administrator, '$PRAY_BANK_NOT_ADMIN')
sp.set_type(params, sp.TAddress)
self.data.spray_address = params
@sp.entry_point
def set_the_vote_address(self, params):
sp.verify(sp.sender == self.data.administrator, '$PRAY_BANK_NOT_ADMIN')
sp.set_type(params, sp.TAddress)
self.data.the_vote_address = params
@sp.entry_point
def set_withdraw_limit(self, params):
sp.set_type(params, sp.TNat)
sp.verify(sp.sender == self.data.administrator, '$PRAY_BANK_NOT_ADMIN')
sp.verify(params > sp.nat(0), '$PRAY_BANK_WITHDRAW_0')
self.data.withdraw_limit = params
@sp.entry_point
def set_new_period(self):
sp.if sp.sender != self.data.administrator:
sp.verify(sp.sender == self.data.the_vote_address, '$PRAY_BANK_NOT_ADMIN_NOR_THE_VOTE')
self.data.withdraw_period += 1
@sp.entry_point
def register_user(self, params):
sp.verify(sp.sender == self.data.administrator, '$PRAY_BANK_NOT_ADMIN')
sp.set_type(params, sp.TAddress)
# maybe this checking step can be omitted to save gas because this method has to be called for every registered user
sp.verify(~self.data.withdrawls.contains(params), '$PRAY_BANK_USER_ALREADY_REGISTERED')
self.data.withdrawls[params] = sp.nat(0)
@sp.entry_point
def withdraw(self):
"""
withdraw spray tokens for SOURCE (not sender!) if they haven't withdrawn already (throws error otherwise)
Will only withdraw to the withdraw_limit and not further (sitting on unspent tokens between voting cycles does not work)
"""
# check that the voter is registered and is allowed to withdraw in the current_period
sp.verify(self.data.withdraw_period > self.data.withdrawls.get(sp.source, None, '$PRAY_BANK_NOT_REGISTERED'), '$PRAY_BANK_ALREADY_WITHDRAWN')
current_amount = sp.local("current_amount", sp.view("get_balance", self.data.spray_address, sp.record(owner=sp.source, token_id=sp.nat(0)), sp.TNat).open_some("$PRAY_BANK_INVALID_VIEW"))
withdraw_amount = sp.local("withdraw_amount", self.data.withdraw_limit - current_amount.value)
sp.if withdraw_amount.value > 0:
spray_contract = sp.contract(BatchTransfer.get_type(), self.data.spray_address,
entry_point="transfer").open_some('$PRAY_BANK_SPRAY_CONTRACT_ERROR')
# we now transfer the $PRAY tokens to the sender
sp.transfer([BatchTransfer.item(sp.self_address, [
sp.record(to_=sp.source, token_id=0, amount=sp.as_nat(withdraw_amount.value, message="$PRAY_BANK_NAT_CAST_ERROR"))])],
sp.mutez(0), spray_contract)
self.data.withdrawls[sp.source] = self.data.withdraw_period
@sp.onchain_view(pure=True)
def can_withdraw(self):
sp.if self.data.withdrawls.contains(sp.source):
sp.result(self.data.withdraw_period > self.data.withdrawls.get(sp.source))
sp.else:
sp.result(sp.bool(False))
class TheVote(sp.Contract):
def __init__(self, administrator, tokens_contract_address, auction_house_address, voter_money_pool_address, spray_bank_address, spray_contract_address, deadline):
list_of_views = [
]
# this has to be added because of an error in SmartPy as of Version 0.10.1 Maybe this can be removed later on
self.add_flag("initial-cast")
metadata = {
"name": "BLCKBOOK The Vote",
"description": "BLCKBOOK The Vote beta implementation. Using SmartPy.\n\n",
"version": "0.1",
"views": list_of_views,
"interfaces": [],
"authors": ["Niels Hanselmann"],
"homepage": "https://blckbook.vote",
"source": {"tools": ["SmartPy"], "location": "https://github.com/BLCKBOOK/BLCKBOOK-contract"},
"permissions": {},
}
# Helper method that builds the metadata and produces the JSON representation as an artifact.
self.init_metadata("BLCKBOOK-THE-VOTE", metadata) # the string is just for the output of the online-IDE
self.init_type(sp.TRecord(
administrator=sp.TAddress,
tokens_contract_address=sp.TAddress,
spray_contract_address=sp.TAddress,
auction_house_address=sp.TAddress,
voter_money_pool_address=sp.TAddress,
spray_bank_address=sp.TAddress,
votes=sp.TBigMap(sp.TNat, sp.TRecord(
vote_amount=sp.TNat,
next=sp.TVariant(index=sp.TNat, end=sp.TUnit),
previous=sp.TVariant(index=sp.TNat, end=sp.TUnit),
artwork_id=sp.TNat)),
all_artworks=sp.TNat,