-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbidder.py
61 lines (51 loc) · 1.92 KB
/
bidder.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
#!/usr/bin/env python
import os
import sys
import json
import zlib
def log(message: str):
assert isinstance(message, str)
print(message, flush=True, file=sys.stderr)
class Config:
GWEI = 1000000000
def __init__(self, filename="config.json"):
cwd = os.path.dirname(os.path.realpath(__file__))
self.filename = os.path.join(cwd, filename)
self.prices_by_auction_id = {}
self.default_price = None
self.our_addresses = set()
self.last_checksum = None
def check(self):
with open(self.filename) as data_file:
content_file = data_file.read()
new_checksum = zlib.crc32(content_file.encode('utf-8'))
if new_checksum != self.last_checksum:
try:
result = json.loads(content_file)
self.our_addresses = set(result["ourAddresses"])
self.prices_by_auction_id = result["pricesByAuctionId"]
if "defaultPrice" in result:
self.default_price = result["defaultPrice"]
if self.last_checksum:
log("Reloaded configuration file")
except json.JSONDecodeError as ex:
log(f"Ignored bad configuration file: {ex}")
finally:
self.last_checksum = new_checksum
config = Config()
for line in sys.stdin:
config.check()
signal = json.loads(line)
id = signal['id']
guy = signal['guy'].lower()
if guy in map(str.lower, config.our_addresses):
log(f"Not outbidding {guy} on {id}")
continue
if id in config.prices_by_auction_id:
stance = {'price': config.prices_by_auction_id[id]}
print(json.dumps(stance), flush=True)
elif config.default_price:
stance = {'price': config.default_price}
print(json.dumps(stance), flush=True)
else:
log(f"Not bidding on auction {id}")