-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_parse_backup.py
169 lines (166 loc) · 8.7 KB
/
block_parse_backup.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
# !/usr/bin/env python
# -*-coding:utf-8-*-
from web3 import Web3
from web3.middleware import geth_poa_middleware
from web3 import exceptions
from helper import _parseValue
import json
import requests
from decimal import Decimal
from config import config
import sys
import time
import os
network = sys.argv[1]
UUID = sys.argv[2]
numberType = sys.argv[3] # ODD / EVEN
filePath = os.path.dirname(os.path.realpath(__file__)) + '/'
blockFilePath = filePath + 'last_blocks/' + network + config('NODE', 'CHAIN') + '_' + numberType + '.json'
job = open(blockFilePath)
job = json.load(job)
blockNum = job['BLOCK']
while True:
customTxh = []
printLog = 0
# Tek mi, çift mi?
if int(blockNum) % 2 == 0 and numberType == 'ODD':
blockNum -= 1
elif int(blockNum) % 2 != 0 and numberType == 'EVEN':
blockNum -= 1
# \ Tek mi, çift mi?
try:
exportList = []
printLog += 1
print(printLog, "a")
w3 = Web3(Web3.WebsocketProvider(config('NODE', network + config('NODE', 'CHAIN')).format(config('MORALIS', UUID)), websocket_timeout=900, websocket_kwargs={'max_size': 999999999}))
printLog += 1
print(printLog, "b")
w3.middleware_onion.inject(geth_poa_middleware, layer=0)
print(str(blockNum) + " started")
latestBlock = w3.eth.getBlock('latest').number
printLog += 1
print(printLog, "c")
if latestBlock > int(blockNum):
printLog += 1
print(printLog, "ç")
latestBlock = int(w3.eth.getBlock('latest').number)
wallets = requests.post(config('REMOTE', 'SITE') + '/api/network/wallets/27ba4f9a-8bee-49ed-a945-b90d4b89a874/' + network).json()
if network == 'BSC':
with open(filePath + "abis/bep20.json") as f:
abi = json.load(f)
else:
with open(filePath + "abis/erc20.json") as f:
abi = json.load(f)
printLog += 1
print(printLog, "çç")
block = w3.eth.getBlock(blockNum, full_transactions=True).transactions
printLog += 1
print(printLog, "d")
parseBlock = _parseValue(block)
printLog += 1
print(printLog, "e")
for block in parseBlock:
printLog += 1
print(printLog, "f")
print(w3.toHex(block['hash']))
try:
receipt = w3.eth.get_transaction_receipt(block['hash'])
transaction = w3.eth.get_transaction(block['hash'])
logs = receipt["logs"]
if len(logs) > 0:
printLog += 1
print(printLog, "k")
for log in logs:
if w3.toHex(block['hash']) not in customTxh:
smart_contract = log["address"]
contract = w3.eth.contract(smart_contract, abi=abi)
printLog += 1
print(printLog, "iiss")
receipt_event_signature_hex = w3.toHex(log["topics"][0])
printLog += 1
print(printLog, "fsdfds")
abi_events = [abi for abi in contract.abi if abi["type"] == "event"]
for event in abi_events:
printLog += 1
print(printLog, "aaxxx")
# Get event signature components
name = event["name"]
inputs = [param["type"] for param in event["inputs"]]
inputs = ",".join(inputs)
# Hash event signature
event_signature_text = f"{name}({inputs})"
event_signature_hex = w3.toHex(w3.keccak(text=event_signature_text))
if event_signature_hex == receipt_event_signature_hex:
printLog += 1
print(printLog, "dsdssd")
# Decode matching log
decoded_logs = contract.events[event["name"]]().processReceipt(receipt)
if len(decoded_logs) < 20:
for decoded_log in decoded_logs:
printLog += 1
print(printLog, "dsdssd")
if decoded_log['event'] == 'Transfer':
printLog += 1
print(printLog, "Transfer")
trans = _parseValue(decoded_log)
# print("token", trans)
if trans['args']['from'] in wallets or trans['args']['to'] in wallets:
printLog += 1
print(printLog, "cccdds")
token = w3.eth.contract(address=trans['address'], abi=abi)
exportList.append({
'block_number': trans['blockNumber'],
'from': trans['args']['from'],
'to': trans['args']['to'],
'contract': trans['address'],
'fee': str(w3.fromWei(w3.fromWei(receipt['gasUsed'] * transaction['gasPrice'], 'wei'), 'ether')),
'txh': trans['transactionHash'],
'value': str(Decimal(trans['args']['value'] / (10 ** token.functions.decimals().call()))),
'network': network,
'status': receipt['status']
})
else:
customTxh.append(w3.toHex(block['hash']))
print("to loong logs.")
else:
printLog += 1
print(printLog, "k")
trans = _parseValue(transaction)
printLog += 1
print(printLog, "h")
if trans['from'] in wallets or trans['to'] in wallets:
exportList.append({
'block_number': trans['blockNumber'],
'from': trans['from'],
'to': trans['to'],
'contract': None,
'fee': str(w3.fromWei(w3.fromWei(receipt['gasUsed'] * transaction['gasPrice'], 'wei'), 'ether')),
'txh': trans['hash'],
'value': str(Web3.fromWei(trans['value'], 'ether')),
'network': network,
'status': receipt['status']
})
except Exception as e:
print("error:", str(e))
if len(exportList) > 0:
request = requests.post(config('REMOTE', 'SITE') + '/api/network/set-transactions/' + network, json=exportList, headers={"Content-Type": "text/json"}).status_code
if request != 200:
raise Exception("transaction request fail")
if len(customTxh) > 0:
print(customTxh)
printLog += 1
print(printLog, "ş")
blockNum += 2
job['BLOCK'] = blockNum
update = open(blockFilePath, "w")
update.write(json.dumps(job))
update.close()
printLog += 1
print(printLog)
print(str(blockNum) + " parsed")
except exceptions.SolidityError as error:
print(error)
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print("error----------", exc_type, fname, exc_tb.tb_lineno)
time.sleep(5)