-
Notifications
You must be signed in to change notification settings - Fork 1
/
mint_manager.py
132 lines (92 loc) · 4.46 KB
/
mint_manager.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import importlib
import json
from threading import Thread
from time import sleep
from dotenv import dotenv_values, set_key
from web3 import Web3
from helpers import is_valid_url, create_web3_instance
from account_manager import AccountManager
class MinterClassNotSetException(Exception):
pass
class MinterClassNotRightException(Exception):
pass
class Web3ProviderURLNotFoundException(Exception):
pass
class ContractABINotRightException(Exception):
pass
class ContractABINotProvidedException(Exception):
pass
class ContractAddressNotProvided(Exception):
pass
class Web3ProviderURLNotValid(Exception):
pass
class MintManager():
def __init__(self):
config = dotenv_values(".env")
self.mint_file = config.get("MINT_FILE", "")
if self.mint_file != "":
self.mint_module = importlib.import_module(self.mint_file)
else:
self.mint_module = None
self.web3_provider_url = config.get("WEB3_PROVIDER_URL", "")
self.contract_abi_path = config.get("CONTRACT_ABI_PATH", "")
self.contract_address = config.get("CONTRACT_ADDRESS", "")
if self.contract_abi_path != "" and self.contract_address != "":
abi = json.load(open(self.contract_abi_path, "r"))
self.contract = create_web3_instance(self.web3_provider_url).eth.contract(address=Web3.toChecksumAddress(self.contract_address), abi=abi)
def set_mint_file(self, mint_file):
module = importlib.import_module(mint_file)
try:
test_obj = module.ChildMinter(None, None, None, None)
except:
raise MinterClassNotRightException("ChildMinter class is not written correctly!")
else:
self.mint_file = mint_file
self.mint_module = importlib.import_module(self.mint_file)
set_key(".env", "MINT_FILE", mint_file)
def set_web3_provider_url(self, url, auto_set_key=False):
if is_valid_url(url):
self.web3_provider_url = url
if auto_set_key:
set_key(".env", "WEB3_PROVIDER_URL", url)
else:
raise Web3ProviderURLNotValid()
def set_contract_abi_path(self, path:str):
if path.endswith(".json"):
self.contract_abi_path = path
set_key(".env", "CONTRACT_ABI_PATH", path)
if self.contract_abi_path != "" and self.contract_address != "":
abi = json.load(open(self.contract_abi_path, "r"))
self.contract = create_web3_instance(self.web3_provider_url).eth.contract(address=Web3.toChecksumAddress(self.contract_address), abi=abi)
else:
raise ContractABINotRightException("ABI files should have extension .json")
def set_contract_address(self, address):
self.contract_address = address
set_key(".env", "CONTRACT_ADDRESS", address)
if self.contract_abi_path != "" and self.contract_address != "":
abi = json.load(open(self.contract_abi_path, "r"))
self.contract = create_web3_instance(self.web3_provider_url).eth.contract(address=Web3.toChecksumAddress(self.contract_address), abi=abi)
def start_minting(self, account_manager:AccountManager):
self._check_is_config_ready()
num_threads = account_manager.get_no_of_accounts_to_use()
accounts = account_manager.get_account_list() # TODO: change to accs_to_use_in_mint when it's implemented.
if num_threads > 0:
for i in range(num_threads):
minter = self.mint_module.ChildMinter(accounts[i], account_manager.get_address(accounts[i]), create_web3_instance(self.web3_provider_url), self.contract)
Thread(target=self._mint_target, args=(minter,)).start()
def _mint_target(self, minter):
minter.pre_everything()
while not minter.is_mint_ready():
sleep(0.00000001)
minter.pre_mint()
minter.mint()
minter.post_mint()
def _check_is_config_ready(self) -> bool:
if self.mint_module is None:
raise MinterClassNotSetException("Minter class is not set up correctly!")
if self.web3_provider_url == "":
raise Web3ProviderURLNotFoundException("Web3 provider url not set.")
if self.contract_address == "":
raise ContractAddressNotProvided("Contract adddress not set!")
if self.contract_abi_path == "":
raise ContractABINotProvidedException("Contract ABI not set!")