-
Notifications
You must be signed in to change notification settings - Fork 6
/
luxor.py
559 lines (450 loc) · 16.4 KB
/
luxor.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
from __future__ import annotations
import json
import logging
import os
from typing import Any
import typer
from dotenv import load_dotenv
from rich import print
from client import GraphQlClient
load_dotenv()
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[logging.StreamHandler(), logging.FileHandler("requests.log")],
)
app = typer.Typer()
HOST = os.getenv("HOST")
API_KEY = os.getenv("API_KEY")
METHOD = os.getenv("METHOD")
env_settings: list[str | None] = [HOST, API_KEY, METHOD]
if None in env_settings:
print("[bold red]Alert![/bold red] It seems you have not setup your .env file.")
print(
"Please copy the [bold].env.example[/bold] with the name of [bold green].env[/bold green] and write your own values",
)
exit(1)
CLIENT = GraphQlClient(host=HOST, key=API_KEY, method=METHOD) # type: ignore
@app.command()
def get_all_transaction_history(
mpn: str,
subaccount: str,
first: int,
) -> dict[str, Any]:
"""
Get all the transaction history of the user associated to the token provided.
mpn (str): mining profile name, refers to the coin ticker
subaccount (str): subaccount username
first (int): limits the number of data points returned.
"""
query = """query getAllTransactionHistory($cid: CurrencyProfileName!, $uname: String!, $first: Int) {
getAllTransactionHistory(cid: $cid, uname: $uname, first: $first, orderBy: CREATED_AT_DESC) {
edges {
node {
transactionId
amount
status
payoutAddress
currency
createdAt
}
}
}
}
"""
params = {"cid": mpn, "uname": f"{subaccount}", "first": first}
return CLIENT.request(query, params)
@app.command()
def get_subaccounts(first: int, offset: int = 0) -> dict[str, Any]:
"""
Returns all subaccounts that belong to the Profile owner of the API Key.
first (int): limits the number of data points returned.
offset (int): skips elements of data points returned.
"""
query = """query getSubaccounts($first: Int, $offset: Int) {users(first: $first, offset: $offset) {edges {node {username}}}}"""
params = {"first": first, "offset": offset}
return CLIENT.request(query, params)
@app.command()
def get_subaccount_mining_summary(
subaccount: str,
mpn: str,
input_interval: str,
) -> dict[str, Any]:
"""
Returns an object of a subaccount mining summary.
subaccount (str): subaccount username
mpn (str): mining profile name, refers to the coin ticker
inputInterval (str): intervals to generate the mining summary lookback, options are: `_15_MINUTE`, `_1_HOUR`, `_1_HOUR` and `_1_DAY`
"""
query = """query getMiningSummary($mpn: MiningProfileName!, $userName: String!, $inputDuration: HashrateIntervals!) {
getMiningSummary(mpn: $mpn, userName: $userName, inputDuration: $inputDuration) {
hashrate
validShares
invalidShares
staleShares
badShares
lowDiffShares
revenue
}
}
"""
params = {
"userName": f"{subaccount}",
"mpn": mpn,
"inputDuration": input_interval,
}
return CLIENT.request(query, params)
@app.command()
def get_subaccount_hashrate_history(
subaccount: str,
mpn: str,
input_interval: str,
first: int,
) -> dict[str, Any]:
"""
Returns an object of a subaccount hashrate timeseries.
subaccount (str): subaccount username
mpn (str): mining profile name, refers to the coin ticker
input_interval (str): intervals to generate the timeseries, options are: `_15_MINUTE`, `_1_HOUR`, `_6_HOUR` and `_1_DAY`
first (int): limits the number of data points returned
"""
query = """query getHashrateHistory($inputUsername: String, $mpn: MiningProfileName, $inputInterval: HashrateIntervals, $first: Int) {
getHashrateHistory(inputUsername: $inputUsername, mpn: $mpn, inputInterval: $inputInterval, first: $first) {
edges {
node {
time
hashrate
}
}
}
}"""
params = {
"inputUsername": f"{subaccount}",
"mpn": mpn,
"inputInterval": input_interval,
"first": first,
}
return CLIENT.request(query, params)
@app.command()
def get_worker_details(
subaccount: str,
mpn: str,
minutes: int,
first: int,
) -> dict[str, Any]:
"""
Returns object of all workers pointed to a subaccount hashrate and efficiency details with a user-defined interval.
subaccount (str): subaccount username
mpn (str): mining profile name, refers to the coin ticker
minutes (int): minutes lookback to generate metrics
first (int): limits the number of data points returned
"""
query = """query getWorkerDetails($duration: IntervalInput!, $mpn: MiningProfileName!, $uname: String!, $first: Int) {
getWorkerDetails(
duration: $duration
mpn: $mpn
uname: $uname
first: $first
) {
edges {
node {
workerName
hashrate
validShares
staleShares
badShares
duplicateShares
invalidShares
lowDiffShares
efficiency
revenue
status
updatedAt
}
}
}
}"""
duration = {"minutes": minutes}
params = {
"duration": duration,
"mpn": mpn,
"uname": f"{subaccount}",
"first": first,
}
return CLIENT.request(query, params)
@app.command()
def get_worker_details_1H(
subaccount: str,
mpn: str,
first: int,
) -> dict[str, Any]:
"""
Returns object of all workers pointed to a subaccount hashrate and efficiency details in the last hour.
subaccount (str): subaccount username
mpn (str): mining profile name, refers to the coin ticker
first (int): limits the number of data points returned
"""
query = """query getWorkersOverview($mpn: MiningProfileName, $username: String, $first: Int) {
miners(filter: {
miningProfileName: { equalTo: $mpn }
user: { username: { equalTo: $username } }
}, first: $first) {
edges {
node {
workerName
details1H {
hashrate
status
efficiency
validShares
staleShares
badShares
duplicateShares
invalidShares
lowDiffShares
}
}
}
}
}"""
params = {"username": f"{subaccount}", "mpn": mpn, "first": first}
return CLIENT.request(query, params)
@app.command()
def get_worker_details_24H(
subaccount: str,
mpn: str,
first: int,
) -> dict[str, Any]:
"""
Returns object of all workers pointed to a subaccount hashrate and efficiency details in the last 24 hours.
subaccount (str): subaccount username
mpn (str): mining profile name, refers to the coin ticker
first (int): limits the number of data points returned
"""
query = """query getWorkersOverview($mpn: MiningProfileName, $username: String, $first: Int) {
miners(filter: {
miningProfileName: { equalTo: $mpn }
user: { username: { equalTo: $username } }
}, first: $first) {
edges {
node {
workerName
details24H {
hashrate
status
efficiency
validShares
staleShares
badShares
duplicateShares
invalidShares
lowDiffShares
}
}
}
}
}"""
params = {"username": f"{subaccount}", "mpn": mpn, "first": first}
return CLIENT.request(query, params)
@app.command()
def get_worker_hashrate_history(
subaccount: str,
workername: str,
mpn: str,
input_bucket: str,
input_duration: str,
first: int,
) -> dict[str, Any]:
"""
Returns an object of a miner hashrate timeseries.
subaccount (str): subaccount username
workername (str): rig identifier
mpn (str): mining profile name, refers to the coin ticker
input_bucket (str): intervals to generate the timeseries, options are: `_15_MINUTE`, `_1_HOUR`, `_6_HOUR` and `_1_DAY`
input_duration (str): intervals to generate the timeseries, options are: `_15_MINUTE`, `_1_HOUR`, `_6_HOUR` and `_1_DAY`
first (int): limits the number of data points returned
"""
query = """query getWorkerHashrateHistory($inputUsername: String!, $workerName: String!, $mpn: MiningProfileName!, $inputBucket: HashrateIntervals!, $inputDuration: HashrateIntervals!, $first: Int) {
getWorkerHashrateHistory(username: $inputUsername, workerName: $workerName, mpn: $mpn, inputBucket: $inputBucket, inputDuration: $inputDuration, first: $first) {
edges {
node {
time
hashrate
}
}
}
}"""
params = {
"inputUsername": f"{subaccount}",
"workerName": workername,
"mpn": mpn,
"inputBucket": input_bucket,
"inputDuration": input_duration,
"first": first,
}
return CLIENT.request(query, params)
@app.command()
def get_subaccount_workers_status(
mpn: str,
subaccount: str,
) -> dict[str, Any]:
"""
Returns an integer count of distinct Profile active workers.
mpn (str): mining profile name, refers to the coin ticker
subaccount (str): subaccount name
"""
query = """query getUserMinersStatusCount($usrname: String!, $mpn: MiningProfileName!) {
getUserMinersStatusCount(usrname: $usrname, mpn: $mpn) {
dead
warning
active
}
}
"""
params = {"mpn": mpn, "usrname": f"{subaccount}"}
return CLIENT.request(query, params)
@app.command()
def get_pool_hashrate(mpn: str, org_slug: str) -> dict[str, Any]:
"""
Returns an integer count of distinct Profile active workers.
mpn (str): mining profile name, refers to the coin ticker
org_slug (str): organization name
"""
query = """query getPoolHashrate {
getPoolHashrate(mpn: BTC, orgSlug: "luxor")
}
"""
params = {"mpn": mpn, "orgSlug": org_slug}
return CLIENT.request(query, params)
@app.command()
def get_revenue(
subaccount: str,
mpn: str,
start_interval: str,
end_interval: str,
) -> dict[str, Any]:
"""
Returns on-chain transactions for a subaccount and currency combo.
subaccount (str): subaccount username
mpn (str): mining profile name, refers to the coin ticker
cid (str): currency identifier, refers to the coin ticker
start_interval (str): string JSON representation of an interval of time that has passed
end_interval (str): string JSON representation of an interval of time that has passed
"""
query = """query getRevenue($uname: String!, $cid: CurrencyProfileName!, $startInterval: IntervalInput!, $endInterval: IntervalInput!) {
getRevenue(uname: $uname, cid: $cid, startInterval: $startInterval, endInterval: $endInterval)
}"""
params = {
"uname": f"{subaccount}",
"cid": mpn,
"startInterval": json.loads(start_interval),
"endInterval": json.loads(end_interval),
}
return CLIENT.request(query, params)
@app.command()
def get_profile_active_worker_count(mpn: str) -> dict[str, Any]:
"""
Returns an integer count of distinct Profile active workers.
Workers are classified as active if we recorded a share in the last 15 minutes.
mpn (str): mining profile name, refers to the coin ticker
"""
query = """query getUserMinersStatusCount {
getUserMinersStatusCount(mpn: BTC)
}
"""
params = {"mpn": mpn}
return CLIENT.request(query, params)
@app.command()
def get_profile_inactive_worker_count(mpn: str) -> dict[str, Any]:
"""
Returns an integer count of distinct Profile inactive workers.
Workers are classified as inactive if we have not recorded a share in the last 15 minutes.
mpn (str): Mining profile name, refers to the coin ticker
"""
query = """query getInactiveWorkers {
getProfileInactiveWorkers(mpn: BTC)
}
"""
params = {"mpn": mpn}
return CLIENT.request(query, params)
@app.command()
def get_transaction_history(
subaccount: str,
cid: str,
first: int,
) -> dict[str, Any]:
"""
Returns on-chain transactions for a subaccount and currency combo.
subaccount (str): Subaccount username
cid (str): Currency identifier, refers to the coin ticker
first (int): Limits the number of data points returned
"""
query = """query getTransactionHistory($uname: String!, $cid: CurrencyProfileName!, $first: Int) {
getTransactionHistory(uname: $uname, cid: $cid, first: $first, orderBy: CREATED_AT_DESC) {
edges {
node {
createdAt
amount
status
transactionId
}
}
}
}"""
params = {"uname": f"{subaccount}", "cid": cid, "first": first}
return CLIENT.request(query, params)
@app.command()
def get_hashrate_score_history(
subaccount: str,
mpn: str,
first: int,
) -> dict[str, Any]:
"""
Returns a subaccount earnings, scoring hashrate and efficiency per day.
Args:
subaccount (str): Subaccount username
mpn (str): Mining profile name, refers to the coin ticker
first (int): Limits the number of data points returned
Returns:
dict[str, Any]: JSON response of the graphql query
"""
query = """ query getHashrateScoreHistory($mpn: MiningProfileName!, $uname: String!, $first : Int) {
getHashrateScoreHistory(mpn: $mpn, uname: $uname, first: $first, orderBy: DATE_DESC) {
nodes {
date
hashrate
efficiency
revenue
}
}
}"""
params = {"uname": f"{subaccount}", "mpn": mpn, "first": first}
return CLIENT.request(query, params)
@app.command()
def get_revenue_ph(mpn: str) -> dict[str, Any]:
"""
Returns average Hashprice per PH over the last 24H.
Args:
mpn (str): Mining profile name, refers to the coin ticker
Returns:
dict[str, Any]: JSON response of the graphql query
"""
query = """query getRevenuePh($mpn: MiningProfileName!) {
getRevenuePh(mpn: $mpn)
}
"""
params = {"mpn": mpn}
return CLIENT.request(query, params)
@app.command()
def create_custom_request(query: str, params: str) -> dict[str, Any]:
"""
Returns the result of the custom graphql query sent to the endpoint
Args:
query (str): The GraphQl query to execute
params (str): The string representation of the params e.g. '{"argument": "value"}'
Returns:
dict[str, Any]: JSON response of the graphql query.
"""
return CLIENT.request(query, json.loads(params))
if __name__ == "__main__":
app()