Contract con_uw_auction


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)
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(
51 uid=uid,
52 to=ctx.this,
53 main_account=ctx.caller
54 )
55
56 assert not S[uid], 'Auction has already started!'
57 assert end_date > now, "end_date is in the past"
58 assert reserve_price >= 0, "reserve_price cannot be less than 0"
59
60 S[uid, 'start_date'] = start_date
61 S[uid, 'end_date'] = end_date
62 S[uid, 'current_owner'] = ctx.caller
63 S[uid, 'uid'] = uid
64 S[uid, 'reserve_price'] = reserve_price
65 S[uid, 'current_bid'] = None
66 S[uid, 'current_winner'] = ""
67
68 # Mark as auction started
69 S[uid] = True
70
71 @export
72 def end_auction(uid: str, end_early: bool, contract: str):
73 # Get listing info
74 listing_info = get_listing_info(uid=uid)
75
76 if end_early:
77 # Only the owner or the operator can end an auction early
78 assert listing_info['current_owner'] == ctx.caller or metadata['operator'] == ctx.caller, \
79 'Only thing owner or auction operator can end the auction early!'
80
81 if now < listing_info['start_date']:
82 process_auction_result_no_winner(listing_info, contract)
83 else:
84 if (listing_info['current_bid'] or -1) < listing_info['reserve_price']:
85 process_auction_result_no_winner(listing_info, contract)
86 else:
87 assert False, "Cannot end early. Auction started or reserve has been met."
88 else:
89 # Else, anyone can call this to complete the auction, assuming the auction is over
90 assert now > listing_info['end_date'], 'Auction is still pending!'
91 if listing_info['current_bid'] == None or listing_info['current_bid'] < listing_info['reserve_price']:
92 process_auction_result_no_winner(listing_info, contract)
93 else:
94 process_auction_result(listing_info, contract)
95
96 def process_auction_result_no_winner(listing_info, contract):
97 thing_master_contract = I.import_module(contract)
98
99 # Send thing back to owner
100 thing_master_contract.transfer(
101 uid=listing_info['uid'],
102 new_owner=listing_info['current_owner'],
103 contract=contract
104
105 )
106
107 # Send money back to last bidder (if there was one)
108 if listing_info['current_winner'] != "":
109 con_uwarriors_lst001.transfer(
110 to=listing_info['current_winner'],
111 amount=listing_info['current_bid']
112 )
113
114 S[listing_info['uid']] = False
115
116 def process_auction_result(listing_info, contract):
117 thing_master_contract = I.import_module(contract)
118
119 # transfer thing to new owner
120 thing_master_contract.transfer(
121 uid=listing_info['uid'],
122 new_owner=listing_info['current_winner'],
123 contract=contract
124 )
125
126 net_amount = listing_info['current_bid']
127
128 con_uwarriors_lst001.transfer(
129 to=listing_info['current_owner'],
130 amount=net_amount
131 )
132
133 S[listing_info['uid']] = False
134
135 @export
136 def bid(uid: str, bid_amount: float):
137 listing_info = get_listing_info(uid=uid)
138
139 current_bid = listing_info['current_bid'] or 0
140
141 assert now < listing_info['end_date'], "Auction has ended."
142 assert now > listing_info['start_date'], "Auction has not stared."
143 assert bid_amount > 0 , "Bid must be greater than zero."
144 assert bid_amount > current_bid, "Current bid of {current_bid} is higher!"
145 # Take the currency bid from the bidder
146 con_uwarriors_lst001.transfer_from(
147 main_account=ctx.caller,
148 to=ctx.this,
149 amount=bid_amount,
150 )
151 # Send the previous bidder's bid back to them
152 if listing_info['current_winner'] != "":
153 con_uwarriors_lst001.transfer(
154 to=listing_info['current_winner'],
155 amount=listing_info['current_bid']
156 )
157 # Set the new bid info
158 S[uid, 'current_bid'] = bid_amount
159 S[uid, 'current_winner'] = ctx.caller

Byte Code

e3000000000000000000000000070000004000000073f8000000640064016c005a00640064016c015a0165025a03650464016402640364048d035a05650464006402640564048d035a066406640784005a076508640283016509650a64089c026409640a840483015a0b650864028301650965096509640b9c03640c640d840483015a0c6508640283016509650d640e9c02640f6410840483015a0e650964119c016412641384045a0f65086402830165096509650d65106a1065106a1064149c0564156416840483015a1165086402830165096512650964179c0364186419840483015a13641a641b84005a14641c641d84005a156508640283016509650d641e9c02641f6420840483015a16640153002921e9000000004eda0e636f6e5f75775f61756374696f6eda01532903da0d64656661756c745f76616c7565da08636f6e7472616374da046e616d65da086d65746164617461630000000000000000000000000300000043000000730e00000074006a01740264013c006400530029024eda086f70657261746f722903da03637478da0663616c6c6572da0a5f5f6d65746164617461a900720c000000720c000000da00da045f5f5f5f0800000073020000000001720e0000002902da036b6579da0576616c7565630200000000000000020000000300000043000000732200000074006a017402640119006b02731674036402830182017c0174027c003c006400530029034e72080000007a274f6e6c792061756374696f6e206f70657261746f722063616e20736574206d657461646174612129047209000000720a000000720b000000da0e417373657274696f6e4572726f722902720f0000007210000000720c000000720c000000720d000000da0f6368616e67655f6d657461646174610c000000730600000000021001060172120000002903da03756964da096e65775f6f776e65727205000000630300000000000000040000000400000043000000733a00000074006a017402640119006b027316740364028301820174046a057c0283017d037c036a067c007c0164038d020100640474077c003c006400530029054e72080000007a384f6e6c792061756374696f6e206f70657261746f722063616e207472616e73666572207468696e67732066726f6d20636f6e74726163742e2902721300000072140000004629087209000000720a000000720b0000007211000000da0149da0d696d706f72745f6d6f64756c65da087472616e73666572da035f5f532904721300000072140000007205000000da157468696e675f6d61737465725f636f6e7472616374720c000000720c000000720d000000da176f70657261746f725f7472616e736665725f7468696e6713000000730a0000000002100106010a010e01721a0000002902da06616d6f756e74da02746f630200000000000000020000000400000043000000732800000074006a017402640119006b027316740364028301820174046a057c007c0164038d0201006400530029044e72080000007a3a4f6e6c792061756374696f6e206f70657261746f722063616e207472616e736665722063757272656e63792066726f6d20636f6e74726163742e2902721b000000721c00000029067209000000720a000000720b0000007211000000da14636f6e5f7577617272696f72735f6c737430303172170000002902721b000000721c000000720c000000720c000000720d000000da1a6f70657261746f725f7472616e736665725f63757272656e63791c0000007306000000000210010601721e00000029017213000000630100000000000000020000000900000043000000736400000074007c0019007d017c0164006b097318740164018301820174007c0064026602190074007c0064036602190074007c0064046602190074007c0064056602190074007c0064066602190074007c0064076602190074007c0064086602190064099c075300290a4e7a164c697374696e6720646f65736e277420657869737421da0a73746172745f64617465da08656e645f64617465da0d63757272656e745f6f776e65727213000000da0d726573657276655f7072696365da0b63757272656e745f626964da0e63757272656e745f77696e6e65722907721f00000072200000007221000000721300000072220000007223000000722400000029027218000000721100000029027213000000da0c6c697374696e675f696e666f720c000000720c000000720d000000da125f5f6765745f6c697374696e675f696e666f23000000730e0000000001080110010e01120112010e0172260000002905720500000072130000007222000000721f000000722000000063050000000000000006000000050000004300000073b200000074006a017c0083017d057c056a027c0174036a0474036a0564018d03010074067c0119000c00733074076402830182017c0474086b04734074076403830182017c0264046b05735074076405830182017c0374067c01640666023c007c0474067c01640766023c0074036a0574067c01640866023c007c0174067c01640966023c007c0274067c01640a66023c00640074067c01640b66023c00640c74067c01640d66023c00640e74067c013c0064005300290f4e29037213000000721c000000da0c6d61696e5f6163636f756e747a1c41756374696f6e2068617320616c72656164792073746172746564217a17656e645f6461746520697320696e20746865207061737472010000007a23726573657276655f70726963652063616e6e6f74206265206c657373207468616e2030721f00000072200000007221000000721300000072220000007223000000720d000000722400000054290972150000007216000000da0d7472616e736665725f66726f6d7209000000da0474686973720a00000072180000007211000000da036e6f772906720500000072130000007222000000721f00000072200000007219000000720c000000720c000000720d000000da0d61756374696f6e5f7468696e672d000000731c00000000030a010a010a011201100110010c010c010e010c010c010c010c01722b00000029037213000000da09656e645f6561726c79720500000063030000000000000004000000030000004300000073c200000074007c0064018d017d037c0172787c036402190074016a026b02733274036403190074016a026b027332740464048301820174057c03640519006b00724a74067c037c028302010071be7c03640619007054640d7c03640819006b00726a74067c037c028302010071be640973be7404640a830182016e4674057c03640b19006b04738c7404640c830182017c036406190064006b0273a87c03640619007c03640819006b0072b474067c037c02830201006e0a74077c037c028302010064005300290e4e29017213000000722100000072080000007a3f4f6e6c79207468696e67206f776e6572206f722061756374696f6e206f70657261746f722063616e20656e64207468652061756374696f6e206561726c7921721f0000007223000000e9010000007222000000467a3a43616e6e6f7420656e64206561726c792e2041756374696f6e2073746172746564206f72207265736572766520686173206265656e206d65742e72200000007a1941756374696f6e206973207374696c6c2070656e64696e6721e9ffffffff290872260000007209000000720a000000720b0000007211000000722a000000da225f5f70726f636573735f61756374696f6e5f726573756c745f6e6f5f77696e6e6572da185f5f70726f636573735f61756374696f6e5f726573756c7429047213000000722c00000072050000007225000000720c000000720c000000720d000000da0b656e645f61756374696f6e40000000731e00000000020a0104011001040110010c010c0114020c020e02140112010a010c027231000000630200000000000000030000000500000043000000735400000074006a017c0183017d027c026a027c00640119007c00640219007c0164038d0301007c006404190064056b03724474036a027c00640419007c006406190064078d020100640874047c00640119003c006400530029094e7213000000722100000029037213000000721400000072050000007224000000720d00000072230000002902721c000000721b000000462905721500000072160000007217000000721d00000072180000002903722500000072050000007219000000720c000000720c000000720d000000722f00000057000000730e00000000010a010a010e010c010a010c01722f000000630200000000000000040000000500000043000000734c00000074006a017c0183017d027c026a027c00640119007c00640219007c0164038d0301007c00640419007d0374036a027c00640519007c0364068d020100640774047c00640119003c006400530029084e721300000072240000002903721300000072140000007205000000722300000072210000002902721c000000721b000000462905721500000072160000007217000000721d00000072180000002904722500000072050000007219000000da0a6e65745f616d6f756e74720c000000720c000000720d000000723000000061000000730e00000000010a010a010e0108010a010801723000000029027213000000da0a6269645f616d6f756e7463020000000000000004000000050000004300000073b200000074007c0064018d017d027c0264021900701464037d0374017c02640419006b00732a740264058301820174017c02640619006b04733e74026407830182017c0164036b04734e74026408830182017c017c036b04735e740264098301820174036a0474056a0674056a077c01640a8d0301007c02640b1900640c6b03729474036a087c02640b19007c0264021900640d8d0201007c0174097c00640266023c0074056a0674097c00640b66023c0064005300290e4e290172130000007223000000720100000072200000007a1241756374696f6e2068617320656e6465642e721f0000007a1741756374696f6e20686173206e6f74207374617265642e7a1e426964206d7573742062652067726561746572207468616e207a65726f2e7a2743757272656e7420626964206f66207b63757272656e745f6269647d206973206869676865722129037227000000721c000000721b0000007224000000720d0000002902721c000000721b000000290a7226000000722a0000007211000000721d00000072280000007209000000720a00000072290000007217000000721800000029047213000000723300000072250000007223000000720c000000720c000000720d000000da036269646b000000731a00000000020a010c0114011401100110010c0108010c010a010c010c0172340000002917da0863757272656e6379721d000000da09696d706f72746c69627215000000da04486173687218000000720b000000720e000000da085f5f6578706f7274da03737472da03416e797212000000721a000000da05666c6f6174721e0000007226000000da086461746574696d65722b000000da04626f6f6c7231000000722f00000072300000007234000000720c000000720c000000720c000000720d000000da083c6d6f64756c653e01000000732a0000000801080104010e010e0308040601120606011408060112060e0a06010601161106011416080a080a0601