This repository has been archived by the owner on Jun 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathalephium.py
313 lines (242 loc) · 11.7 KB
/
alephium.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
import json
import logging
import os
import backoff
import requests
from utils import Utils
GENESIS_TS = 1231006504
FIRST_BLOCK_TS = 1636383298
ALPH_BASE = 10 ** 18
MINERS_ADDRESSES_FILE = 'address2name/miners.txt'
GENESIS_ADDRESSES_FILE = 'address2name/genesis.txt'
CLAIMED_ADDRESSES_FILE = 'address2name/known-wallets.txt'
API_BASE = Utils.apiBase
API_EXPLORER_BASE = Utils.apiExplorerBase
TIMEOUT_REQ = 120
class WhalesWatcher:
def __init__(self, session, telegramBot, twitterBot, minAmountAlert, minAmountAlertTweet, tickerHandler,
debug=False, fullnode_api_key=""):
self.session = session
self.minAmountAlert = minAmountAlert
self.minAmountAlertTweet = minAmountAlertTweet
self.telegramBot = telegramBot
self.twitterBot = twitterBot
self.debug = debug
self.headers = {}
if fullnode_api_key != "":
self.headers = {'X-API-KEY': fullnode_api_key}
self.ticker = tickerHandler
logging.getLogger('backoff').addHandler(logging.StreamHandler())
logging.getLogger('backoff').setLevel(logging.INFO)
def isGenesisAddress(self, addr):
# GENESIS_URL "https://raw.githubusercontent.com/alephium/alephium/master/flow/src/main/resources
# /mainnet_genesis.conf"
exist, name, exchange = WhalesWatcher.isIn(addr, GENESIS_ADDRESSES_FILE)
return exist
def isMinerAddress(self, addr):
exist, name, exchange = WhalesWatcher.isIn(addr, MINERS_ADDRESSES_FILE)
if exist: # schedule.every().second.do(stats, twitterBot, telegramBot, logPooler)
return True
try:
response = self.session.get(f"{API_EXPLORER_BASE}/addresses/{addr}/transactions", timeout=Utils.TIMEOUT_REQ)
try:
tx = json.loads(response.text)
except Exception as e:
print(response.content)
print(e)
for transaction in tx:
if transaction.get('type') == 'confirmed' and len(transaction.get('inputs')) <= 0:
# add miner address in file with timestamp
with open(f"{MINERS_ADDRESSES_FILE}", "a") as f:
f.write(f'{addr}\n')
return True
return False
except requests.exceptions.ConnectionError as e:
print(e)
return False
def isClaimed(self, addr):
return WhalesWatcher.isIn(addr, CLAIMED_ADDRESSES_FILE)
def formatAddress(self, addr):
exist, name, exchange = self.isClaimed(addr)
maxChar = 72
formattedAddr = ""
if exist and name != "":
if len(name) >= maxChar:
name = f"{(name[:maxChar]).strip()}..."
formattedAddr += name
else:
formattedAddr += f"{addr[:5]}...{addr[-5:]}"
return formattedAddr, exchange
@backoff.on_exception(Utils.BACKOFF_ALGO, (requests.exceptions.ConnectionError, requests.exceptions.Timeout), max_tries=Utils.BACKOFF_MAX_TRIES)
def getGenesisAddresses(self):
# GENESIS_URL = "https://raw.githubusercontent.com/alephium/alephium/master/flow/src/main/resources/mainnet_genesis.conf"
addresses = set()
try:
response = self.session.get(
f"{API_BASE}/blockflow/blocks?fromTs={GENESIS_TS * 1000}&toTs={(GENESIS_TS + 5) * 1000}", headers=self.headers,
timeout=Utils.TIMEOUT_REQ)
for inner_blocks in response.json()['blocks']:
for block in inner_blocks:
for tx in block['transactions']:
for txUnsigned in tx['unsigned']['fixedOutputs']:
addresses.add(txUnsigned['address'])
except Exception as e:
print(e)
return False
sourceFile = open(f'{GENESIS_ADDRESSES_FILE}', 'w')
print(*addresses, sep="", file=sourceFile)
return True
@backoff.on_exception(Utils.BACKOFF_ALGO, (requests.exceptions.ConnectionError, requests.exceptions.Timeout),max_tries=Utils.BACKOFF_MAX_TRIES)
def getBlockTsTransactions(self, start, end):
txs = []
try:
response = self.session.get(f"{API_BASE}/blockflow/blocks?fromTs={start * 1000}&toTs={end * 1000}",
headers=self.headers, timeout=Utils.TIMEOUT_REQ)
allBlocks = response.json()
except Exception as e:
print(e)
return txs
try:
for inBlock in allBlocks['blocks']:
for block in inBlock:
for transaction in block['transactions']:
if len(transaction['unsigned']['inputs']) > 0:
txs.append(transaction['unsigned']['txId'])
return txs
except KeyError as e:
print(e)
return txs
@backoff.on_exception(Utils.BACKOFF_ALGO, (requests.exceptions.ConnectionError, requests.exceptions.Timeout),max_tries=Utils.BACKOFF_MAX_TRIES)
def getUnconfirmedTransactions(self):
unconfirmedTxsId = set()
try:
response = self.session.get(f"{API_BASE}/transactions/unconfirmed", headers=self.headers,
timeout=Utils.TIMEOUT_REQ)
unconfirmedTxs = response.json()
except Exception as e:
print(e)
return unconfirmedTxsId
for unconfirmed in unconfirmedTxs:
for tx in unconfirmed['unconfirmedTransactions']:
unconfirmedTxsId.add(tx['unsigned']['txId'])
return unconfirmedTxsId
@backoff.on_exception(Utils.BACKOFF_ALGO, (requests.exceptions.ConnectionError, requests.exceptions.Timeout),max_tries=Utils.BACKOFF_MAX_TRIES)
def getBlockTransaction(self, transaction):
try:
response = self.session.get(f"{API_EXPLORER_BASE}/transactions/{transaction}", timeout=Utils.TIMEOUT_REQ)
respTxStatus = self.session.get(f"{API_BASE}/transactions/status?txId={transaction}", headers=self.headers,
timeout=Utils.TIMEOUT_REQ)
if not response.ok or not respTxStatus.ok:
return False
except Exception as e:
print(e)
return False
respTxStatus = respTxStatus.json()
try:
tx = json.loads(response.text)
except Exception as e:
print(response.content)
print(e)
return False
if respTxStatus.get('type') is not None:
txStatus = str.lower(respTxStatus.get('type'))
else:
txStatus = None
if tx.get('type') is not None:
txExplorerStatus = str.lower(tx.get('type'))
else:
txExplorerStatus = None
if self.debug:
print(
f"\nTransaction id: https://explorer.alephium.org/#/transactions/{transaction}, Tx status: {txStatus}")
try:
alphPrice = self.ticker.gatePrice()['ALPH_USDT']
except Exception as e:
print(e)
alphPrice = 0
addressIn = None
if txStatus == 'confirmed' and txExplorerStatus == 'confirmed':
for inputTx in tx['inputs']:
try:
addressIn = inputTx['address']
except KeyError as e:
print(e)
return False
try:
if addressIn is not None:
for output in tx['outputs']:
amount = float(output['attoAlphAmount']) / ALPH_BASE
addressOut = output['address']
if addressIn != addressOut and amount >= self.minAmountAlert:
addressInTxt, exchangeIn = self.formatAddress(addressIn)
addressOutTxt, exchangeOut = self.formatAddress(addressOut)
if amount >= self.minAmountAlertTweet:
text = WhalesWatcher.formatMessage(amount, addressInTxt, addressOutTxt, transaction,
self.isMinerAddress(addressIn),
self.isGenesisAddress(addressIn), exchangeIn, exchangeOut,
isTwitter=True, alphPrice=alphPrice)
self.twitterBot.sendMessage(text)
text = WhalesWatcher.formatMessage(amount, addressInTxt, addressOutTxt, transaction,
self.isMinerAddress(addressIn), self.isGenesisAddress(addressIn),
exchangeIn, exchangeOut, isTwitter=False, alphPrice=alphPrice)
self.telegramBot.sendMessage(text)
return True
else:
return False
except KeyError as e:
print(f"error key: {e}")
return False
elif txStatus == 'unconfirmed' or txStatus is None or txStatus == 'confirmed' or txExplorerStatus == 'confirmed':
return False
@staticmethod
def isIn(addr, file):
if os.path.isfile(f"{file}"):
with open(f'{file}', 'r') as f:
for line in f:
if line.split(';')[0].replace('\n', '') == addr:
exchange = None
if len(line.split(';')) >= 2:
name = line.split(';')[1].replace('\n', '')
if len(line.split(';')) > 3:
exchange = line.split(';')[3].replace('\n', '')
return True, name, exchange
else:
return True, None, None
return False, None, None
@staticmethod
def humanFormat(num, round_to=2):
# From https://stackoverflow.com/questions/579310/formatting-long-numbers-as-strings-in-python
magnitude = 0
while abs(num) >= 1000:
magnitude += 1
num = round(num / 1000.0, round_to)
return '{:.{}f} {}'.format(num, round_to, ['', 'K', 'M', 'G', 'T', 'P'][magnitude])
@staticmethod
def formatMessage(amount, addressIn, addressOut, transactionId, isMiner=False, isGenesis=False,
exchangeIn=None, exchangeOut=None, isTwitter=False, alphPrice=0):
# add exclaim proportionally of number of digit
numExclaim = '❗' * (len(str(abs(int(amount)))) - 1)
text = f"{numExclaim} {WhalesWatcher.humanFormat(amount)} ALPH transferred"
if alphPrice > 0:
text += f" ({WhalesWatcher.humanFormat(amount * alphPrice)} USDT)"
text += f"\n{addressIn} to {addressOut}"
if exchangeIn is None and exchangeOut is None:
text += "\n"
if exchangeIn is not None:
text += f"\nFrom exchange: {exchangeIn}\n"
if exchangeOut is not None:
text += f"\nTo exchange: {exchangeOut}\n"
if isTwitter:
text += f"\n\nhttps://explorer.alephium.org/#/transactions/{transactionId}"
text += "\n\n#alephium #blockchain "
if isGenesis:
text += "#genesis "
elif isMiner:
text += "#miner "
if isTwitter:
text += f"\nhttps://medium.com/@alephium/ttxoo-2-the-road-to-self-custody-cfea4ae89444"
if not isTwitter:
text += "#blockchain"
text += f"\n\n[TX link](https://explorer.alephium.org/#/transactions/{transactionId})"
text += f"\n[Road to self custody](https://medium.com/@alephium/ttxoo-2-the-road-to-self-custody-cfea4ae89444)"
return text