-
Notifications
You must be signed in to change notification settings - Fork 13
/
update_lottery.py
98 lines (86 loc) · 2.77 KB
/
update_lottery.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
from typing import Tuple
from classes.LotteryClass import Lottery
import os
import csv
import json
from web3 import Web3
from web3.middleware import geth_poa_middleware
from collections import deque
import datetime as dt
import subprocess
from dataclasses import dataclass
@dataclass
class UpdateConfig:
rpl: str
contract: str
lottery_file_path: str
def __init__(self, rpl: str, contract: str, lottery_file_path: str) -> None:
self.rpl = rpl
self.contract = contract
self.lottery_file_path = lottery_file_path
base_path = os.path.dirname(os.path.realpath(__file__))
mainnet_config = UpdateConfig(
rpl="https://bsc-dataseed.binance.org/",
contract="0x5aF6D33DE2ccEC94efb1bDF8f92Bd58085432d2c",
lottery_file_path=f"{base_path}/data/v2/main/lottery.csv",
)
def lottery_to_tuple(id: int, lottery: Lottery) -> Tuple:
return (
id,
lottery.status.value,
lottery.startTime,
lottery.endTime,
lottery.priceTicketInCake,
lottery.discountDivisor,
lottery.rewardsBreakdown[0],
lottery.rewardsBreakdown[1],
lottery.rewardsBreakdown[2],
lottery.rewardsBreakdown[3],
lottery.rewardsBreakdown[4],
lottery.rewardsBreakdown[5],
lottery.treasuryFee,
lottery.cakePerBracket[0],
lottery.cakePerBracket[1],
lottery.cakePerBracket[2],
lottery.cakePerBracket[3],
lottery.cakePerBracket[4],
lottery.cakePerBracket[5],
lottery.countWinnersPerBracket[0],
lottery.countWinnersPerBracket[1],
lottery.countWinnersPerBracket[2],
lottery.countWinnersPerBracket[3],
lottery.countWinnersPerBracket[4],
lottery.countWinnersPerBracket[5],
lottery.firstTicketId,
lottery.firstTicketIdNextLottery,
lottery.amountCollectedInCake,
lottery.finalNumber
)
def update_lottery(update_all: bool):
config = mainnet_config
web3 = Web3(Web3.HTTPProvider(config.rpl))
web3.middleware_onion.inject(geth_poa_middleware, layer=0)
with open(f'{base_path}/lottery_abi.json', 'r') as f:
abi = json.load(f)
contract = web3.eth.contract(config.contract, abi=abi)
max_id = 0
already_processed = set()
with open(config.lottery_file_path, "r") as f:
reader = csv.reader(f)
first = True
for r in reader:
if not first:
max_id = max(max_id, int(r[0]))
already_processed.add(int(r[0]))
first = False
cur = contract.functions.viewCurrentLotteryId().call()
with open(config.lottery_file_path, "a") as f:
writer = csv.writer(f)
start_epoch = 0 if update_all else max_id + 1
for r in range(start_epoch, cur):
if r in already_processed:
continue
lottery = Lottery(contract.functions.viewLottery(r).call())
# check if oracle has been called or round cancelled
if lottery.finalNumber > 0:
writer.writerow(lottery_to_tuple(r, lottery))