Contract con_pixel_whale_auction


Contract Code


  
1 import currency
2 I = importlib
3
4 # Import the Thing Info contract storage
5 Thing_Info = ForeignHash(foreign_contract='con_pixel_whale_info_v1', foreign_name='S')
6
7 S = Hash(default_value=None)
8 metadata = Hash(default_value=0)
9
10
11 @construct
12 def seed():
13 S['thing_master_contract'] = 'con_pixel_whale_master_v1'
14 # LST002
15 metadata['operator'] = ctx.caller
16
17 # LST002
18 @export
19 def change_metadata(key: str, value: Any):
20 assert ctx.caller == metadata['operator'], 'Only auction operator can set metadata!'
21 metadata[key] = value
22
23 @export
24 def operator_transfer_thing(uid: str, new_owner: str):
25 assert ctx.caller == metadata['operator'], 'Only auction operator can transfer things from contract.'
26 thing_master_contract = I.import_module(S['thing_master_contract'])
27 thing_master_contract.transfer(uid=uid, new_owner=new_owner)
28 S[uid] = False
29
30 @export
31 def operator_transfer_currency(amount: str, to: float):
32 assert ctx.caller == metadata['operator'], 'Only auction operator can transfer currency from contract.'
33 currency.transfer(amount=amount, to=to)
34
35 def get_listing_info(uid: str):
36 # Get listing info
37 listing_info = S[uid]
38 assert listing_info is not None, "Listing doesn't exist!"
39 return {
40 'start_date': S[uid, 'start_date'],
41 'end_date': S[uid, 'end_date'],
42 'current_owner': S[uid, 'current_owner'],
43 'uid': S[uid, 'uid'],
44 'reserve_price': S[uid, 'reserve_price'],
45 'current_bid': S[uid, 'current_bid'],
46 'current_winner': S[uid, 'current_winner'],
47 'royalty_percent': S[uid, 'royalty_percent'],
48 'creator': S[uid, 'creator']
49 }
50
51 @export
52 def auction_thing(uid: str, reserve_price: float, start_date: datetime.datetime, end_date: datetime.datetime):
53 # transfer thing to this auction contract
54 # This will throw an Assertion error if caller does not own the thing and revert the tx
55 thing_master_contract = I.import_module(S['thing_master_contract'])
56 thing_master_contract.transfer_from(
57 uid=uid,
58 to=ctx.this,
59 main_account=ctx.caller
60 )
61
62 assert not S[uid], 'Auction has already started!'
63 assert end_date > now, "end_date is in the past"
64 assert reserve_price >= 0, "reserve_price cannot be less than 0"
65
66 S[uid, 'start_date'] = start_date
67 S[uid, 'end_date'] = end_date
68 S[uid, 'current_owner'] = ctx.caller
69 S[uid, 'uid'] = uid
70 S[uid, 'reserve_price'] = reserve_price
71 S[uid, 'current_bid'] = None
72 S[uid, 'current_winner'] = ""
73 S[uid, "royalty_percent"] = Thing_Info[uid, 'meta', 'royalty_percent']
74 S[uid, "creator"] = Thing_Info[uid, 'creator']
75
76 # Mark as auction started
77 S[uid] = True
78
79 @export
80 def end_auction(uid: str, end_early: bool):
81 # Get listing info
82 listing_info = get_listing_info(uid=uid)
83
84 if end_early:
85 # Only the owner or the operator can end an auction early
86 assert listing_info['current_owner'] == ctx.caller or metadata['operator'] == ctx.caller, \
87 'Only thing owner or auction operator can end the auction early!'
88
89 if now < listing_info['start_date']:
90 process_auction_result_no_winner(listing_info)
91 else:
92 if (listing_info['current_bid'] or -1) < listing_info['reserve_price']:
93 process_auction_result_no_winner(listing_info)
94 else:
95 assert False, "Cannot end early. Auction started or reserve has been met."
96 else:
97 # Else, anyone can call this to complete the auction, assuming the auction is over
98 assert now > listing_info['end_date'], 'Auction is still pending!'
99 if listing_info['current_bid'] == None or listing_info['current_bid'] < listing_info['reserve_price']:
100 process_auction_result_no_winner(listing_info)
101 else:
102 process_auction_result(listing_info)
103
104 def process_auction_result_no_winner(listing_info):
105 thing_master_contract = I.import_module(S['thing_master_contract'])
106
107 # Send thing back to owner
108 thing_master_contract.transfer(
109 uid=listing_info['uid'],
110 new_owner=listing_info['current_owner']
111 )
112
113 # Send money back to last bidder (if there was one)
114 if listing_info['current_winner'] != "":
115 currency.transfer(
116 to=listing_info['current_winner'],
117 amount=listing_info['current_bid']
118 )
119
120 S[listing_info['uid']] = False
121
122 def process_auction_result(listing_info):
123 thing_master_contract = I.import_module(S['thing_master_contract'])
124
125 # transfer thing to new owner
126 thing_master_contract.transfer(
127 uid=listing_info['uid'],
128 new_owner=listing_info['current_winner']
129 )
130
131 royalty_percent = listing_info['royalty_percent']
132
133 if royalty_percent > 0:
134 # calculate the royalty
135 royalty_amount = listing_info['current_bid'] * (royalty_percent / 100)
136
137 # calculate the amount that goes to the seller
138 net_amount = listing_info['current_bid'] - royalty_amount
139
140 # send royalty to creator
141 currency.transfer(
142 to=listing_info['creator'],
143 amount=royalty_amount
144 )
145 else:
146 net_amount = listing_info['current_bid']
147
148 currency.transfer(
149 to=listing_info['current_owner'],
150 amount=net_amount
151 )
152
153 S[listing_info['uid']] = False
154
155 @export
156 def bid(uid: str, bid_amount: float):
157 listing_info = get_listing_info(uid=uid)
158
159 current_bid = listing_info['current_bid'] or 0
160
161 assert now < listing_info['end_date'], "Auction has ended."
162 assert now > listing_info['start_date'], "Auction has not stared."
163 assert bid_amount > 0 , "Bid must be greater than zero."
164 assert bid_amount > current_bid, f"Current bid of {current_bid} is higher!"
165
166 # Take the currency bid from the bidder
167 currency.transfer_from(
168 main_account=ctx.caller,
169 to=ctx.this,
170 amount=bid_amount,
171 )
172
173 # Send the previous bidder's bid back to them
174 if listing_info['current_winner'] != "":
175 currency.transfer(
176 to=listing_info['current_winner'],
177 amount=listing_info['current_bid']
178 )
179
180 # Set the new bid info
181 S[uid, 'current_bid'] = bid_amount
182 S[uid, 'current_winner'] = ctx.caller
183

Byte Code

e3000000000000000000000000060000004000000073fa000000640064016c005a0065015a026503640264036404640564068d045a04650564016404640364078d035a06650564006404640864078d035a076409640a84005a08650964048301650a650b640b9c02640c640d840483015a0c650964048301650a650a640e9c02640f6410840483015a0d650964048301650a650e64119c0264126413840483015a0f650a64149c016415641684045a10650964048301650a650e65116a1165116a1164179c0464186419840483015a12650964048301650a6513641a9c02641b641c840483015a14641d641e84005a15641f642084005a16650964048301650a650e64219c0264226423840483015a17640153002924e9000000004eda17636f6e5f706978656c5f7768616c655f696e666f5f7631da0153da17636f6e5f706978656c5f7768616c655f61756374696f6eda0a5468696e675f496e666f2904da10666f726569676e5f636f6e7472616374da0c666f726569676e5f6e616d65da08636f6e7472616374da046e616d652903da0d64656661756c745f76616c756572080000007209000000da086d6574616461746163000000000000000000000000030000004300000073160000006401740064023c0074016a02740364033c006400530029044eda19636f6e5f706978656c5f7768616c655f6d61737465725f7631da157468696e675f6d61737465725f636f6e7472616374da086f70657261746f722904da035f5f53da03637478da0663616c6c6572da0a5f5f6d65746164617461a90072130000007213000000da00da045f5f5f5f0a00000073040000000001080172150000002902da036b6579da0576616c7565630200000000000000020000000300000043000000732200000074006a017402640119006b02731674036402830182017c0174027c003c006400530029034e720e0000007a274f6e6c792061756374696f6e206f70657261746f722063616e20736574206d65746164617461212904721000000072110000007212000000da0e417373657274696f6e4572726f72290272160000007217000000721300000072130000007214000000da0f6368616e67655f6d657461646174610f000000730600000000021001060172190000002902da03756964da096e65775f6f776e6572630200000000000000030000000400000043000000733e00000074006a017402640119006b027316740364028301820174046a0574066403190083017d027c026a077c007c0164048d020100640574067c003c006400530029064e720e0000007a384f6e6c792061756374696f6e206f70657261746f722063616e207472616e73666572207468696e67732066726f6d20636f6e74726163742e720d0000002902721a000000721b0000004629087210000000721100000072120000007218000000da0149da0d696d706f72745f6d6f64756c65720f000000da087472616e736665722903721a000000721b000000720d000000721300000072130000007214000000da176f70657261746f725f7472616e736665725f7468696e6716000000730a0000000002100106010e010e01721f0000002902da06616d6f756e74da02746f630200000000000000020000000400000043000000732800000074006a017402640119006b027316740364028301820174046a057c007c0164038d0201006400530029044e720e0000007a3a4f6e6c792061756374696f6e206f70657261746f722063616e207472616e736665722063757272656e63792066726f6d20636f6e74726163742e29027220000000722100000029067210000000721100000072120000007218000000da0863757272656e6379721e000000290272200000007221000000721300000072130000007214000000da1a6f70657261746f725f7472616e736665725f63757272656e63791f000000730600000000021001060172230000002901721a000000630100000000000000020000000b00000043000000737800000074007c0019007d017c0164006b097318740164018301820174007c0064026602190074007c0064036602190074007c0064046602190074007c0064056602190074007c0064066602190074007c0064076602190074007c0064086602190074007c0064096602190074007c00640a66021900640b9c095300290c4e7a164c697374696e6720646f65736e277420657869737421da0a73746172745f64617465da08656e645f64617465da0d63757272656e745f6f776e6572721a000000da0d726573657276655f7072696365da0b63757272656e745f626964da0e63757272656e745f77696e6e6572da0f726f79616c74795f70657263656e74da0763726561746f722909722400000072250000007226000000721a000000722700000072280000007229000000722a000000722b0000002902720f00000072180000002902721a000000da0c6c697374696e675f696e666f721300000072130000007214000000da125f5f6765745f6c697374696e675f696e666f2600000073100000000001080110010e01120112010e011001722d0000002904721a00000072270000007224000000722500000063040000000000000005000000050000004300000073e000000074006a0174026401190083017d047c046a037c0074046a0574046a0664028d03010074027c0019000c00733474076403830182017c0374086b04734474076404830182017c0164056b05735474076406830182017c0274027c00640766023c007c0374027c00640866023c0074046a0674027c00640966023c007c0074027c00640a66023c007c0174027c00640b66023c00640074027c00640c66023c00640d74027c00640e66023c0074097c00640f64106603190074027c00641066023c0074097c0064116602190074027c00641166023c00641274027c003c006400530029134e720d0000002903721a0000007221000000da0c6d61696e5f6163636f756e747a1c41756374696f6e2068617320616c72656164792073746172746564217a17656e645f6461746520697320696e20746865207061737472010000007a23726573657276655f70726963652063616e6e6f74206265206c657373207468616e2030722400000072250000007226000000721a0000007227000000722800000072140000007229000000da046d657461722a000000722b00000054290a721c000000721d000000720f000000da0d7472616e736665725f66726f6d7210000000da047468697372110000007218000000da036e6f77da0c5f5f5468696e675f496e666f2905721a000000722700000072240000007225000000720d000000721300000072130000007214000000da0d61756374696f6e5f7468696e6731000000732000000000030e010a010a011201100110010c010c010e010c010c010c010c011601140172340000002902721a000000da09656e645f6561726c7963020000000000000003000000030000004300000073ba00000074007c0064018d017d027c0172747c026402190074016a026b02733274036403190074016a026b027332740464048301820174057c02640519006b00724874067c028301010071b67c02640619007052640d7c02640819006b00726674067c028301010071b6640973b67404640a830182016e4274057c02640b19006b0473887404640c830182017c026406190064006b0273a47c02640619007c02640819006b0072ae74067c02830101006e0874077c028301010064005300290e4e2901721a0000007226000000720e0000007a3f4f6e6c79207468696e67206f776e6572206f722061756374696f6e206f70657261746f722063616e20656e64207468652061756374696f6e206561726c792172240000007228000000e9010000007227000000467a3a43616e6e6f7420656e64206561726c792e2041756374696f6e2073746172746564206f72207265736572766520686173206265656e206d65742e72250000007a1941756374696f6e206973207374696c6c2070656e64696e6721e9ffffffff2908722d00000072100000007211000000721200000072180000007232000000da225f5f70726f636573735f61756374696f6e5f726573756c745f6e6f5f77696e6e6572da185f5f70726f636573735f61756374696f6e5f726573756c742903721a0000007235000000722c000000721300000072130000007214000000da0b656e645f61756374696f6e46000000731e00000000020a0104011001040110010c010a0114020a020e02140112010a010a02723a000000630100000000000000020000000400000043000000735600000074006a0174026401190083017d017c016a037c00640219007c006403190064048d0201007c006405190064066b03724674046a037c00640519007c006407190064088d020100640974027c00640219003c0064005300290a4e720d000000721a00000072260000002902721a000000721b000000722900000072140000007228000000290272210000007220000000462905721c000000721d000000720f000000721e00000072220000002902722c000000720d00000072130000007213000000721400000072380000005d000000730e00000000010e010a010c010c010a010c017238000000630100000000000000050000000400000043000000738e00000074006a0174026401190083017d017c016a037c00640219007c006403190064048d0201007c00640519007d027c0264066b0472647c00640719007c0264081b0014007d037c00640719007c0318007d0474046a037c00640919007c03640a8d0201006e087c00640719007d0474046a037c00640b19007c04640a8d020100640c74027c00640219003c0064005300290d4e720d000000721a00000072290000002902721a000000721b000000722a00000072010000007228000000e964000000722b0000002902722100000072200000007226000000462905721c000000721d000000720f000000721e00000072220000002905722c000000720d000000722a000000da0e726f79616c74795f616d6f756e74da0a6e65745f616d6f756e74721300000072130000007214000000723900000067000000731600000000010e010a010c010801080110010c0114020801120172390000002902721a000000da0a6269645f616d6f756e7463020000000000000004000000050000004300000073ba00000074007c0064018d017d027c0264021900701464037d0374017c02640419006b00732a740264058301820174017c02640619006b04733e74026407830182017c0164036b04734e74026408830182017c017c036b047366740264097c039b00640a9d038301820174036a0474056a0674056a077c01640b8d0301007c02640c1900640d6b03729c74036a087c02640c19007c0264021900640e8d0201007c0174097c00640266023c0074056a0674097c00640c66023c0064005300290f4e2901721a0000007228000000720100000072250000007a1241756374696f6e2068617320656e6465642e72240000007a1741756374696f6e20686173206e6f74207374617265642e7a1e426964206d7573742062652067726561746572207468616e207a65726f2e7a0f43757272656e7420626964206f66207a0b20697320686967686572212903722e0000007221000000722000000072290000007214000000290272210000007220000000290a722d0000007232000000721800000072220000007230000000721000000072110000007231000000721e000000720f0000002904721a000000723e000000722c0000007228000000721300000072130000007214000000da0362696476000000731a00000000020a010c0114011401100118010c0108010c010a010c010c01723f00000029187222000000da09696d706f72746c6962721c000000da0b466f726569676e486173687233000000da0448617368720f00000072120000007215000000da085f5f6578706f7274da03737472da03416e797219000000721f000000da05666c6f61747223000000722d000000da086461746574696d657234000000da04626f6f6c723a00000072380000007239000000723f0000007213000000721300000072130000007214000000da083c6d6f64756c653e01000000732e0000000801040104010c010e010601080308050601120606011208060112060e0b06010801121306011216080a080f0601