Contract con_uw_master_auction_2


Contract Code


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

Byte Code

e3000000000000000000000000070000004000000073f8000000640064016c005a00640064016c015a0165025a03650464016402640364048d035a05650464006402640564048d035a066406640784005a076508640283016509650a64089c026409640a840483015a0b650864028301650965096509640b9c03640c640d840483015a0c6508640283016509650d640e9c02640f6410840483015a0e650964119c016412641384045a0f65086402830165096509650d65106a1065106a1064149c0564156416840483015a1165086402830165096512650964179c0364186419840483015a13641a641b84005a14641c641d84005a156508640283016509650d641e9c02641f6420840483015a16640153002921e9000000004eda17636f6e5f75775f6d61737465725f61756374696f6e5f32da01532903da0d64656661756c745f76616c7565da08636f6e7472616374da046e616d65da086d65746164617461630000000000000000000000000300000043000000730e00000074006a01740264013c006400530029024eda086f70657261746f722903da03637478da0663616c6c6572da0a5f5f6d65746164617461a900720c000000720c000000da00da045f5f5f5f0900000073020000000001720e0000002902da036b6579da0576616c7565630200000000000000020000000300000043000000732200000074006a017402640119006b02731674036402830182017c0174027c003c006400530029034e72080000007a274f6e6c792061756374696f6e206f70657261746f722063616e20736574206d657461646174612129047209000000720a000000720b000000da0e417373657274696f6e4572726f722902720f0000007210000000720c000000720c000000720d000000da0f6368616e67655f6d657461646174610d000000730600000000021001060172120000002903da03756964da096e65775f6f776e65727205000000630300000000000000040000000500000043000000733c00000074006a017402640119006b027316740364028301820174046a057c0283017d037c036a067c007c017c0264038d030100640474077c003c006400530029054e72080000007a384f6e6c792061756374696f6e206f70657261746f722063616e207472616e73666572207468696e67732066726f6d20636f6e74726163742e29037213000000721400000072050000004629087209000000720a000000720b0000007211000000da0149da0d696d706f72745f6d6f64756c65da087472616e73666572da035f5f532904721300000072140000007205000000da157468696e675f6d61737465725f636f6e7472616374720c000000720c000000720d000000da176f70657261746f725f7472616e736665725f7468696e6714000000730c0000000002100106010a0108010801721a0000002902da06616d6f756e74da02746f630200000000000000020000000400000043000000732800000074006a017402640119006b027316740364028301820174046a057c007c0164038d0201006400530029044e72080000007a3a4f6e6c792061756374696f6e206f70657261746f722063616e207472616e736665722063757272656e63792066726f6d20636f6e74726163742e2902721b000000721c00000029067209000000720a000000720b0000007211000000da14636f6e5f7577617272696f72735f6c737430303172170000002902721b000000721c000000720c000000720c000000720d000000da1a6f70657261746f725f7472616e736665725f63757272656e63791e0000007306000000000210010601721e00000029017213000000630100000000000000020000000900000043000000736400000074007c0019007d017c0164006b097318740164018301820174007c0064026602190074007c0064036602190074007c0064046602190074007c0064056602190074007c0064066602190074007c0064076602190074007c0064086602190064099c075300290a4e7a164c697374696e6720646f65736e277420657869737421da0a73746172745f64617465da08656e645f64617465da0d63757272656e745f6f776e65727213000000da0d726573657276655f7072696365da0b63757272656e745f626964da0e63757272656e745f77696e6e65722907721f00000072200000007221000000721300000072220000007223000000722400000029027218000000721100000029027213000000da0c6c697374696e675f696e666f720c000000720c000000720d000000da125f5f6765745f6c697374696e675f696e666f25000000730e0000000001080110010e01120112010e0172260000002905720500000072130000007222000000721f000000722000000063050000000000000006000000050000004300000073b200000074006a017c0083017d057c056a027c0174036a0474036a057c008304010074067c0119000c00733074076401830182017c0474086b04734074076402830182017c0264036b05735074076404830182017c0374067c01640566023c007c0474067c01640666023c0074036a0574067c01640766023c007c0174067c01640866023c007c0274067c01640966023c00640074067c01640a66023c00640b74067c01640c66023c00640d74067c013c0064005300290e4e7a1c41756374696f6e2068617320616c72656164792073746172746564217a17656e645f6461746520697320696e20746865207061737472010000007a23726573657276655f70726963652063616e6e6f74206265206c657373207468616e2030721f00000072200000007221000000721300000072220000007223000000720d000000722400000054290972150000007216000000da0d7472616e736665725f66726f6d7209000000da0474686973720a00000072180000007211000000da036e6f772906720500000072130000007222000000721f00000072200000007219000000720c000000720c000000720d000000da0d61756374696f6e5f7468696e672f000000731a00000000030a0114011201100110010c010c010e010c010c010c010c01722a00000029037213000000da09656e645f6561726c79720500000063030000000000000004000000030000004300000073c200000074007c0064018d017d037c0172787c036402190074016a026b02733274036403190074016a026b027332740464048301820174057c03640519006b00724a74067c037c028302010071be7c03640619007054640d7c03640819006b00726a74067c037c028302010071be640973be7404640a830182016e4674057c03640b19006b04738c7404640c830182017c036406190064006b0273a87c03640619007c03640819006b0072b474067c037c02830201006e0a74077c037c028302010064005300290e4e29017213000000722100000072080000007a3f4f6e6c79207468696e67206f776e6572206f722061756374696f6e206f70657261746f722063616e20656e64207468652061756374696f6e206561726c7921721f0000007223000000e9010000007222000000467a3a43616e6e6f7420656e64206561726c792e2041756374696f6e2073746172746564206f72207265736572766520686173206265656e206d65742e72200000007a1941756374696f6e206973207374696c6c2070656e64696e6721e9ffffffff290872260000007209000000720a000000720b00000072110000007229000000da225f5f70726f636573735f61756374696f6e5f726573756c745f6e6f5f77696e6e6572da185f5f70726f636573735f61756374696f6e5f726573756c7429047213000000722b00000072050000007225000000720c000000720c000000720d000000da0b656e645f61756374696f6e41000000731e00000000020a0104011001040110010c010c0114020c020e02140112010a010c027230000000630200000000000000030000000500000043000000735400000074006a017c0183017d027c026a027c00640119007c00640219007c0164038d0301007c006404190064056b03724474036a027c00640419007c006406190064078d020100640874047c00640119003c006400530029094e7213000000722100000029037213000000721400000072050000007224000000720d00000072230000002902721c000000721b000000462905721500000072160000007217000000721d00000072180000002903722500000072050000007219000000720c000000720c000000720d000000722e00000058000000730e00000000010a010a010e010c010a010c01722e000000630200000000000000040000000500000043000000734c00000074006a017c0183017d027c026a027c00640119007c00640219007c0164038d0301007c00640419007d0374036a027c00640519007c0364068d020100640774047c00640119003c006400530029084e721300000072240000002903721300000072140000007205000000722300000072210000002902721c000000721b000000462905721500000072160000007217000000721d00000072180000002904722500000072050000007219000000da0a6e65745f616d6f756e74720c000000720c000000720d000000722f00000062000000730e00000000010a010a010e0108010a010801722f00000029027213000000da0a6269645f616d6f756e7463020000000000000004000000050000004300000073b200000074007c0064018d017d027c0264021900701464037d0374017c02640419006b00732a740264058301820174017c02640619006b04733e74026407830182017c0164036b04734e74026408830182017c017c036b04735e740264098301820174036a0474056a0674056a077c01640a8d0301007c02640b1900640c6b03729474036a087c02640b19007c0264021900640d8d0201007c0174097c00640266023c0074056a0674097c00640b66023c0064005300290e4e290172130000007223000000720100000072200000007a1241756374696f6e2068617320656e6465642e721f0000007a1741756374696f6e20686173206e6f74207374617265642e7a1e426964206d7573742062652067726561746572207468616e207a65726f2e7a2743757272656e7420626964206f66207b63757272656e745f6269647d20697320686967686572212903da0c6d61696e5f6163636f756e74721c000000721b0000007224000000720d0000002902721c000000721b000000290a722600000072290000007211000000721d00000072270000007209000000720a00000072280000007217000000721800000029047213000000723200000072250000007223000000720c000000720c000000720d000000da036269646c000000731a00000000020a010c0114011401100110010c0108010c010a010c010c0172340000002917da0863757272656e6379721d000000da09696d706f72746c69627215000000da04486173687218000000720b000000720e000000da085f5f6578706f7274da03737472da03416e797212000000721a000000da05666c6f6174721e0000007226000000da086461746574696d65722a000000da04626f6f6c7230000000722e000000722f0000007234000000720c000000720c000000720c000000720d000000da083c6d6f64756c653e01000000732c0000000801080104010e010601080308040601120606011409060112060e0a06010601161006011416080a080a0601