This repository was archived by the owner on Feb 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp.py
More file actions
342 lines (279 loc) · 11.1 KB
/
app.py
File metadata and controls
342 lines (279 loc) · 11.1 KB
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
from web3 import Web3
from flask import Flask, render_template, send_file, request, redirect, url_for
import json
from threading import Lock
from flask_socketio import SocketIO, emit
async_mode = None
config = open('config.json')
cfg = json.load(config)
abi = open('abi/erc20.abi.json').read()
coinName = cfg['coinName']
coinSymbol = cfg['coinSymbol']
coinSymbolLower = cfg['coinSymbolLower']
rpcUrl = cfg['rpcUrl']
app = Flask(__name__)
socketio = SocketIO(app, async_mode=async_mode)
thread = None
thread_lock = Lock()
web3 = Web3(Web3.HTTPProvider(rpcUrl))
def background_thread():
"""Example of how to send server generated events to clients."""
count = 0
while True:
socketio.sleep(1)
count += 1
blocks = web3.eth.blockNumber
socketio.emit('my_response',
{'data': str(blocks), 'count': count})
@socketio.event
def connect():
global thread
with thread_lock:
if thread is None:
thread = socketio.start_background_task(background_thread)
block = web3.eth.blockNumber
emit('my_response', {'data': str(block), 'count': 0})
def site():
app.run(host="0.0.0.0")
def isToken(address):
if len(web3.eth.getCode(address)) < 40:
return False
else:
return True
def getDecimals(address):
contract = web3.eth.contract(address=address, abi=abi)
return contract.functions.decimals().call()
def getSymbol(address):
contract = web3.eth.contract(address=address, abi=abi)
return contract.functions.symbol().call()
def getName(address):
contract = web3.eth.contract(address=address, abi=abi)
return contract.functions.name().call()
def getTotalSupply(address):
contract = web3.eth.contract(address=address, abi=abi)
return contract.functions.totalSupply().call()
@app.route("/static/<file>")
def style(file):
return send_file("static/{file}".format(file=file))
@app.route("/")
def index():
account = request.cookies.get('account')
if account is None:
account = "<a class=\"enableEthereumButton upperRight\" style=\"cursor: pointer;\">Connect MetaMask</a>"
hasAccount = False
else:
acc = account
try:
acc = web3.toChecksumAddress(acc)
except:
return render_template("error.html", error="Not even an address!")
hasAccount = True
account = "<p class=\"upperRight\" style=\"margin-right: 44ch;\">Your address: </p><a class=\"upperRight\" href=\"/account/{acc}\">{acc}</a>".format(
acc=acc)
latestBlock = web3.eth.block_number
gasPrice = web3.fromWei(web3.eth.gasPrice, "gwei")
return render_template("index.html", coinSymbolLower=coinSymbolLower, latestBlock=latestBlock, gasPrice=gasPrice, account=account, hasAccount=hasAccount)
# --- API block --- #
@app.route("/api/block/<int:number>")
def api_block(number):
block = web3.eth.get_block(number)
return str(block)[14:-1]
@app.route("/api/txhash/<txhash>")
def api_txhash(txhash):
tx = str(web3.eth.getTransaction(txhash))
return tx
@app.route("/api/balance/<address>")
def api_balance(address):
address = web3.toChecksumAddress(address)
balance = str(web3.eth.getBalance(address))
return balance
# --- End API block --- #
# --- Explorer block --- #
@app.route("/block/<number>")
def block(number):
try:
intNumber = int(number)
number = web3.eth.get_block(intNumber)['hash'].hex()
except ValueError:
pass
try:
block = web3.eth.get_block(number)
except:
return render_template("error.html", error="Block not found", coinSymbolLower=coinSymbolLower)
difficulty = block['difficulty']
extraData = block["extraData"].hex()
gasLimit = block["gasLimit"]
gasUsed = block["gasUsed"]
hash = block["hash"].hex()
if int(block["logsBloom"].hex(), 16) == 0:
logsBloom = "0x0"
else:
logsBloom = block["logsBloom"].hex()
miner = block["miner"]
mixHash = block["mixHash"].hex()
nonce = block["nonce"].hex()
number = block["number"]
parentHash = block["parentHash"].hex()
receiptsRoot = block["receiptsRoot"].hex()
sha3Uncles = block["sha3Uncles"].hex()
size = block["size"]
stateRoot = block["stateRoot"].hex()
timestamp = block["timestamp"]
totalDifficulty = block["totalDifficulty"]
if not block['transactions']:
transactions = "None"
else:
transactions = block['transactions']
transactionsRoot = block["transactionsRoot"].hex()
global uncles
uncles = block["uncles"]
return render_template("block.html", difficulty=str(difficulty), extraData=str(extraData),
gasLimit=str(gasLimit), gasUsed=str(gasUsed),
hash=str(hash), logsBloom=str(logsBloom),
miner=str(miner), mixHash=str(mixHash),
nonce=str(nonce), number=str(number),
parentHash=str(parentHash), receiptsRoot=str(receiptsRoot),
sha3Uncles=str(sha3Uncles), size=str(size),
stateRoot=str(stateRoot), timestamp=str(timestamp),
totalDifficulty=str(totalDifficulty), transactions=str(transactions),
transactionsRoot=str(transactionsRoot), uncles=str(uncles), coinSymbolLower=coinSymbolLower, coinSymbol=coinSymbol)
@app.route("/block/<number>/uncles")
def uncles(number):
try:
intNumber = int(number)
number = web3.eth.get_block(intNumber)['hash'].hex()
except ValueError:
pass
try:
block = web3.eth.get_block(number)
except:
return render_template("error.html", error="Block not found")
uncles = block["uncles"]
number = block["number"]
parsed = []
for uncle in uncles:
parsed.append(str(uncle.hex()))
return render_template("uncles.html", uncles=parsed, number=number, coinSymbolLower=coinSymbolLower, coinSymbol=coinSymbol)
@app.route("/block/<number>/transactions")
def transactions(number):
try:
intNumber = int(number)
number = web3.eth.get_block(intNumber)['hash'].hex()
except ValueError:
pass
try:
block = web3.eth.get_block(number)
except:
return render_template("error.html", error="Block not found", coinSymbolLower=coinSymbolLower)
transactions = block["transactions"]
number = block["number"]
parsed = []
for transaction in transactions:
parsed.append(str(transaction.hex()))
if not parsed:
parsed = ["None"]
return render_template("transactions.html", transactions=parsed, number=number, coinSymbolLower=coinSymbolLower, coinSymbol=coinSymbol)
@app.route("/bloominfo/<block>")
def bloominfo(block):
try:
block = web3.eth.get_block(block)
except:
return render_template("error.html", error="Block not found")
bloom = block["logsBloom"].hex()
blockNumber = block["number"]
return render_template("bloominfo.html", bloom=bloom, blockNumber=blockNumber, coinSymbolLower=coinSymbolLower, coinSymbol=coinSymbol)
@app.route("/account/<address>")
@app.route("/address/<address>")
def account(address):
try:
address = web3.toChecksumAddress(address)
except:
return render_template("error.html", error="Not even an address!", coinSymbolLower=coinSymbolLower)
if isToken(address):
# return render_template("error.html", error="заглушка для токена", coinSymbolLower=coinSymbolLower)
return redirect(f"/token/{address}")
balance = web3.eth.getBalance(address)
nonce = web3.eth.getTransactionCount(address)
balance_eth = web3.fromWei(balance, "ether")
return render_template("account.html", address=address, balance_eth=balance_eth, nonce=str(nonce), coinSymbolLower=coinSymbolLower, coinSymbol=coinSymbol)
@app.route("/tx/<txhash>")
def tx(txhash):
try:
tx = web3.eth.getTransaction(txhash)
except:
return render_template("error.html", error="Transaction not found")
txFrom = tx["from"]
txTo = [web3.eth.get_transaction_receipt(txhash)["contractAddress"], "c"]
txGas = tx["gas"]
gasPriceWei = tx["gasPrice"]
txGasPriceUnformatted = web3.fromWei(gasPriceWei, "ether")
txGasPrice = format(txGasPriceUnformatted, '.18f')
txHash = tx["hash"].hex()
txNonce = tx["nonce"]
txValueWei = tx["value"]
txValueUnformatted = web3.fromWei(txValueWei, "ether")
txValue = format(txValueUnformatted, '.18f')
txBlockHash = tx["blockHash"]
txBlockNumber = tx["blockNumber"]
if not txBlockNumber:
txStatus = "Unconfirmed"
else:
latestBlock = web3.eth.block_number
txStatus = f"Confirmed in block {txBlockNumber} ({latestBlock - txBlockNumber} blocks ago)"
return render_template("tx.html", coinName=coinName, txFrom=txFrom, txTo=txTo, txGas=txGas, txGasPrice=txGasPrice,
txHash=txHash, txNonce=txNonce, txValue=txValue, txBlockHash=txBlockHash,
txBlockNumber=txBlockNumber, coinSymbol=coinSymbol, coinSymbolLower=coinSymbolLower, txStatus=txStatus)
@app.route("/hash/<hash>")
def hash(hash):
try:
web3.eth.getTransaction(hash)
return redirect(f"/tx/{hash}")
except:
try:
web3.eth.get_block(hash)
return redirect(f"/block/{hash}")
except:
return render_template("error.html", error="Neither block nor transaction with this hash has not been found", coinSymbolLower=coinSymbolLower)
@app.route('/', methods=['POST'])
def define_redirect():
text = request.form['text']
type = ""
if len(text) == 42 and text.startswith("0x"):
type = "account"
elif len(text) == 66 and text.startswith("0x"):
type = "hash"
else:
try:
if int(text):
type = "block"
except:
return render_template("error.html", error="Malformed input", coinSymbolLower=coinSymbolLower, coinSymbol=coinSymbol)
if type == "account":
print(type)
del type
return redirect("/account/" + text)
if type == "hash":
print(type)
del type
return redirect("/hash/" + text)
if type == "block":
print(type)
del type
return redirect("/block/" + text)
return render_template("error.html", error="Malformed input", coinSymbolLower=coinSymbolLower, coinSymbol=coinSymbol)
@app.route("/token/<address>")
def token(address):
decimals = getDecimals(address)
name = getName(address)
symbol = getSymbol(address)
totalSupply = getTotalSupply(address)
totalSupply = web3.fromWei(totalSupply, "ether")
totalSupply = format(totalSupply, '.18f')
return render_template("token.html", tokenAddress=address, tokenDecimals=decimals, tokenName=name, tokenSymbol=symbol, tokenTotalSupply=totalSupply, coinSymbolLower=coinSymbolLower, coinSymbol=coinSymbol)
@app.route("/contractinfo")
@app.route("/contractInfo")
def contractinfo():
return render_template("contractinfo.html", coinSymbolLower=coinSymbolLower, coinSymbol=coinSymbol)
# --- End Explorer block --- #
# app.run(host="127.0.0.1")
socketio.run(app)