forked from grzracz/OrangeCLI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
284 lines (243 loc) · 9.08 KB
/
main.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import click
import algosdk
from dotenv import load_dotenv
import os
import math
import base64
import time
from algosdk.atomic_transaction_composer import (
AtomicTransactionComposer,
AtomicTransactionComposerStatus,
AccountTransactionSigner,
)
from threading import Thread, Lock
from datetime import datetime
load_dotenv()
miner_mnemonic = os.getenv("MINER_MNEMONIC")
try:
miner_sk = algosdk.mnemonic.to_private_key(miner_mnemonic)
miner_signer = AccountTransactionSigner(miner_sk)
miner_address = algosdk.account.address_from_private_key(miner_sk)
except Exception:
click.secho(f"Miner mnemonic is malformed.", fg="red")
exit(1)
deposit_mnemonic = os.getenv("DEPOSIT_MNEMONIC")
try:
deposit_pk = algosdk.mnemonic.to_private_key(deposit_mnemonic)
deposit_address = algosdk.account.address_from_private_key(deposit_pk)
except Exception:
deposit_pk = None
deposit_address = os.getenv("DEPOSIT_ADDRESS")
if not algosdk.encoding.is_valid_address(deposit_address):
click.secho(f"Deposit address not set or mnemonic is malformed.", fg="red")
exit(1)
click.echo(f"Deposit address: {click.style(deposit_address, bold=True)}")
click.echo(f"Miner address: {click.style(miner_address, bold=True)}")
with open("./abi.json") as f:
contract = algosdk.abi.Contract.from_json(f.read())
def get_client(network):
if network == "mainnet":
return algosdk.v2client.algod.AlgodClient(
os.getenv("ALGOD_MAINNET_TOKEN"),
f'{os.getenv("ALGOD_MAINNET_SERVER")}:{os.getenv("ALGOD_MAINNET_PORT")}',
)
else:
return algosdk.v2client.algod.AlgodClient(
os.getenv("ALGOD_TESTNET_TOKEN"),
f'{os.getenv("ALGOD_TESTNET_SERVER")}:{os.getenv("ALGOD_TESTNET_PORT")}',
)
def check_node_connection(network):
client = get_client(network)
try:
status = client.status()
click.secho(
f"Node connected successfully. Block {status['last-round']}",
fg="green",
)
except Exception as e:
click.secho(
"Node connection failed. Please update your node connectivity settings.",
fg="red",
)
exit()
def get_state_value(state, key):
bkey = base64.b64encode(bytes(key, "utf-8")).decode()
for kv in state:
if kv["key"] == bkey:
return kv["value"]
return None
def get_state_number(state, key):
return get_state_value(state, key)["uint"]
def get_state_address(state, key):
value = get_state_value(state, key)
return algosdk.encoding.encode_address(base64.b64decode(value["bytes"]))
def get_application_data(network):
app_id = os.getenv("APP_MAINNET" if network == "mainnet" else "APP_TESTNET")
client = get_client(network)
app_info = client.application_info(app_id)
state = app_info["params"]["global-state"]
return {
"id": int(app_id),
"asset": get_state_number(state, "token"),
"block": get_state_number(state, "block"),
"total_effort": get_state_number(state, "total_effort"),
"total_transcations": get_state_number(state, "total_transactions"),
"halving": get_state_number(state, "halving"),
"halving_supply": get_state_number(state, "halving_supply"),
"mined_supply": get_state_number(state, "mined_supply"),
"miner_reward": get_state_number(state, "miner_reward"),
"last_miner": get_state_address(state, "last_miner"),
"last_miner_effort": get_state_number(state, "last_miner_effort"),
"current_miner": get_state_address(state, "current_miner"),
"current_miner_effort": get_state_number(state, "current_miner_effort"),
"start_timestamp": get_state_number(state, "start_timestamp"),
}
def check_miner(network, tpm, fee):
client = get_client(network)
miner_info = client.account_info(miner_address)
miner_balance = max(0, miner_info["amount"] - miner_info["min-balance"])
if miner_balance > 1000000:
cost = tpm * fee
click.echo(
f"Miner will send {tpm} transactions per minute with {fee / pow(10, 6)} fee ({cost / pow(10, 6)} ALGO cost per minute)."
)
click.echo(
f"Miner will spend {click.style(miner_balance / pow(10, 6), bold=True)} ALGO"
)
miner_seconds = math.floor(miner_balance / (cost / 60))
miner_hours = math.floor(miner_seconds / 3600)
miner_minutes = math.floor((miner_seconds % 3600) / 60)
duration = click.style(
f"{miner_hours} {'hour' if miner_hours == 1 else 'hours'} and {miner_minutes} {'minute' if miner_minutes == 1 else 'minutes'}",
bold=True,
)
click.echo(f"Miner will run for approximately {duration}")
else:
click.secho(
f"Miner has low balance ({miner_balance / pow(10, 6)} ALGO), please fund before mining.",
fg="red",
)
exit(1)
def find(array, condition):
return next(iter([item for item in array if condition(item)]), None)
def check_deposit_opted_in(network):
client = get_client(network)
deposit_info = client.account_info(deposit_address)
app_info = get_application_data(network)
app_opted_in = any(
[app["id"] == app_info["id"] for app in deposit_info["apps-local-state"]]
)
if not app_opted_in:
click.secho(f"Deposit address not opted in to app {app_info['id']}.", fg="red")
exit(1)
asset_data = find(
deposit_info["assets"], lambda asset: asset["asset-id"] == app_info["asset"]
)
if not asset_data:
click.secho(
f"Deposit address not opted in to asset {app_info['asset']}.", fg="red"
)
exit(1)
def send_mining_group(client, sp, app_info, amount, total_txs, finish):
try:
composer = AtomicTransactionComposer()
for i in range(amount):
txid = total_txs + i
composer.add_method_call(
app_info["id"],
contract.get_method_by_name("mine"),
miner_address,
sp,
miner_signer,
[algosdk.encoding.decode_address(deposit_address)],
accounts=[app_info["last_miner"], deposit_address],
foreign_assets=[app_info["asset"]],
note=txid.to_bytes(math.ceil(txid / 255), "big"),
)
composer.execute(client, 5)
except Exception as e:
click.secho(f"Transactions failed: {e}", fg="red")
finish(amount)
pending_txs = 0
mutex = Lock()
def finish_transactions(amount):
global pending_txs
with mutex:
pending_txs -= amount
def log_mining_stats(network, total_txs):
global pending_txs
click.echo(
f"[{datetime.now().strftime('%H:%M:%S')}] "
+ f"{click.style(network.upper(), fg='red' if network == 'testnet' else 'yellow', bold=True)}: "
+ f"Sent {total_txs} transactions, {pending_txs} currently pending."
)
def mine(network, tpm, fee):
global pending_txs
client = get_client(network)
started = int(time.time())
started = started - started % 60
transactions_to_send = tpm
tps = math.ceil(tpm / 30)
app_info = get_application_data(network)
sp = None
loops = 0
starttime = time.monotonic()
total_txs = 0
now = int(time.time())
while now < (app_info["start_timestamp"]):
click.echo(
f"Waiting for mining to begin... {app_info['start_timestamp'] - now} seconds left"
)
time.sleep(5)
now = int(time.time())
click.echo("Mining starts...")
while True:
now = int(time.time())
now = now - now % 60
if started != now:
transactions_to_send = tpm
started = now
if loops % 5 == 0:
app_info = get_application_data(network)
sp = client.suggested_params()
log_mining_stats(network, total_txs)
sp.flat_fee = True
sp.fee = fee
total = min(tps, transactions_to_send)
while total > 0:
amount = min(16, total)
task = Thread(
target=send_mining_group,
args=(client, sp, app_info, amount, total_txs, finish_transactions),
)
task.start()
total -= amount
total_txs += amount
with mutex:
pending_txs += amount
transactions_to_send -= total
loops += 1
time.sleep(2.0 - ((time.monotonic() - starttime) % 2.0))
@click.command()
@click.option("--tpm", default=1, help="Transactions per minute.")
@click.option("--fee", default=2000, help="Fee per transaction (micro algos).")
@click.argument(
"network", type=click.Choice(["testnet", "mainnet"], case_sensitive=False)
)
def main(network, tpm, fee):
click.echo(
f"Network: {click.style(network.upper(), fg='red' if network == 'testnet' else 'yellow', bold=True)}"
)
check_node_connection(network)
check_deposit_opted_in(network)
check_miner(network, tpm, fee)
click.confirm("Do you want to continue?", abort=True)
mine(network, tpm, fee)
# TODO
def opt_in():
pass
# TODO
def withdraw():
pass
if __name__ == "__main__":
main()