-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathserver.py
More file actions
65 lines (54 loc) · 2.35 KB
/
server.py
File metadata and controls
65 lines (54 loc) · 2.35 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
import os
from flask import Flask, jsonify, send_from_directory
from flask_cors import CORS
from web3 import Web3
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
app = Flask(__name__, static_folder='static')
CORS(app)
# Connect to Ethereum network (using Infura as an example)
INFURA_URL = os.getenv('INFURA_URL', 'https://mainnet.infura.io/v3/0826cbe5c945461a8cde38f960d2f735')
w3 = Web3(Web3.HTTPProvider(INFURA_URL))
def validate_address(address):
return Web3.is_address(address)
@app.route('/api/balance/<address>')
def get_balance(address):
if not validate_address(address):
return jsonify({"error": "Invalid Ethereum address"}), 400
try:
balance = w3.eth.get_balance(address)
return jsonify({"balance": w3.from_wei(balance, 'ether')})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/api/transactions/<address>')
def get_transactions(address):
if not validate_address(address):
return jsonify({"error": "Invalid Ethereum address"}), 400
try:
# This is a simplified version. In a real-world scenario, you'd need to use an Ethereum explorer API
# or index transactions yourself, as getting all transactions for an address is not trivial.
latest_block = w3.eth.get_block('latest')
transactions = []
for i in range(10): # Get last 10 blocks as an example
block = w3.eth.get_block(latest_block.number - i, full_transactions=True)
for tx in block.transactions:
if tx['from'] == address or tx['to'] == address:
transactions.append({
"hash": tx['hash'].hex(),
"from": tx['from'],
"to": tx['to'],
"value": w3.from_wei(tx['value'], 'ether')
})
return jsonify({"transactions": transactions})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/', defaults={'path': 'index.html'})
@app.route('/<path:path>')
def serve_static(path):
if path != "" and os.path.exists(app.static_folder + '/' + path):
return send_from_directory(app.static_folder, path)
else:
return send_from_directory(app.static_folder, 'index.html')
if __name__ == '__main__':
app.run(host='localhost', port=7112, debug=True)