-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb3_test.py
More file actions
71 lines (62 loc) · 2.54 KB
/
web3_test.py
File metadata and controls
71 lines (62 loc) · 2.54 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
# web3_test.py — быстрый тест: соединение, price, последние события Paid
import os, json
from web3 import Web3
from dotenv import load_dotenv
load_dotenv()
WEB3_HTTP = os.getenv("WEB3_HTTP")
WEB3_WS = os.getenv("WEB3_WS")
CONTRACT_ADDRESS = os.getenv("CONTRACT_ADDRESS")
CONTRACT_ABI_PATH = os.getenv("CONTRACT_ABI_PATH", "contracts/MinimalPaymentReceiver.json")
provider_url = WEB3_HTTP or WEB3_WS
if not provider_url:
raise RuntimeError("Установи WEB3_HTTP или WEB3_WS в .env")
w3 = Web3(Web3.HTTPProvider(provider_url))
print("Connected:", w3.is_connected())
try:
print("Chain id:", w3.eth.chain_id)
except Exception as e:
print("Can't read chain id:", e)
# load ABI
if not os.path.exists(CONTRACT_ABI_PATH):
raise FileNotFoundError(f"ABI not found at {CONTRACT_ABI_PATH}")
with open(CONTRACT_ABI_PATH, "r", encoding="utf-8") as f:
abi = json.load(f)
contract = w3.eth.contract(address=Web3.to_checksum_address(CONTRACT_ADDRESS), abi=abi)
# read price
try:
price = contract.functions.price().call()
print("Contract price (token units):", price)
except Exception as e:
print("Failed to read price():", e)
# try a small recent-events scan (last 5000 blocks)
try:
latest = w3.eth.block_number
from_block = max(0, latest - 5000)
print(f"Scanning Paid events from block {from_block} to {latest} ...")
# prefer get_logs for reliability
event_abi = None
for item in abi:
if item.get("type") == "event" and item.get("name") == "Paid":
event_abi = item
break
if event_abi is None:
print("Paid event ABI not found in ABI")
else:
topic = w3.sha3(text="Paid(address,uint256,bytes32)").hex()
# better to use contract.events if available
try:
evs = contract.events.Paid().get_logs(fromBlock=from_block, toBlock=latest)
print("Events found via contract.events.Paid():", len(evs))
for ev in evs[:5]:
print(" payer:", ev['args'].get('payer'), " amount:", ev['args'].get('amount'), " ref:", ev['args'].get('paymentReference'))
except Exception as e:
# fallback to raw logs (less convenient)
print("contract.events.Paid get_logs failed:", e)
logs = w3.eth.get_logs({
"fromBlock": from_block,
"toBlock": latest,
"address": Web3.to_checksum_address(CONTRACT_ADDRESS)
})
print("Raw logs count:", len(logs))
except Exception as e:
print("Events scan failed:", e)