-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistener.py
96 lines (81 loc) · 3.95 KB
/
listener.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
from web3 import Web3
from web3.contract import Contract
from web3.providers.rpc import HTTPProvider
from web3.middleware import geth_poa_middleware #Necessary for POA chains
import json
from datetime import datetime
import pandas as pd
eventfile = 'deposit_logs.csv'
def scanBlocks(chain,start_block,end_block,contract_address):
"""
chain - string (Either 'bsc' or 'avax')
start_block - integer first block to scan
end_block - integer last block to scan
contract_address - the address of the deployed contract
This function reads "Deposit" events from the specified contract,
and writes information about the events to the file "deposit_logs.csv"
"""
if chain == 'avax':
api_url = f"https://api.avax-test.network/ext/bc/C/rpc" #AVAX C-chain testnet
if chain == 'bsc':
api_url = f"https://data-seed-prebsc-1-s1.binance.org:8545/" #BSC testnet
if chain in ['avax','bsc']:
w3 = Web3(Web3.HTTPProvider(api_url))
# inject the poa compatibility middleware to the innermost layer
w3.middleware_onion.inject(geth_poa_middleware, layer=0)
else:
w3 = Web3(Web3.HTTPProvider(api_url))
DEPOSIT_ABI = json.loads('[ { "anonymous": false, "inputs": [ { "indexed": true, "internalType": "address", "name": "token", "type": "address" }, { "indexed": true, "internalType": "address", "name": "recipient", "type": "address" }, { "indexed": false, "internalType": "uint256", "name": "amount", "type": "uint256" } ], "name": "Deposit", "type": "event" }]')
contract = w3.eth.contract(address=contract_address, abi=DEPOSIT_ABI)
arg_filter = {}
if start_block == "latest":
start_block = w3.eth.get_block_number()
if end_block == "latest":
end_block = w3.eth.get_block_number()
if end_block < start_block:
print( f"Error end_block < start_block!" )
print( f"end_block = {end_block}" )
print( f"start_block = {start_block}" )
if start_block == end_block:
print( f"Scanning block {start_block} on {chain}" )
else:
print( f"Scanning blocks {start_block} - {end_block} on {chain}" )
deposit_events = []
if end_block - start_block < 30:
event_filter = contract.events.Deposit.create_filter(fromBlock=start_block,toBlock=end_block,argument_filters=arg_filter)
events = event_filter.get_all_entries()
print( f"Got {len(events)} entries for block {block_num}" )
# YOUR CODE HERE
for event in events:
data = {
'chain': chain,
'token': event.args['token'],
'recipient': event.args['recipient'],
'amount': event.args['amount'],
'transactionHash': event.transactionHash.hex(),
'address': contract_address
}
deposit_events.append(data)
else:
for block_num in range(start_block,end_block+1):
event_filter = contract.events.Deposit.create_filter(fromBlock=block_num,toBlock=block_num,argument_filters=arg_filter)
events = event_filter.get_all_entries()
print( f"Got {len(events)} entries for block {block_num}" )
# YOUR CODE HERE
for event in events:
data = {
'chain': chain,
'token': event.args['token'],
'recipient': event.args['recipient'],
'amount': event.args['amount'],
'transactionHash': event.transactionHash.hex(),
'address': contract_address
}
deposit_events.append(data)
# Write the events to a CSV file
if deposit_events:
df = pd.DataFrame(deposit_events)
df.to_csv(eventfile, index=False, columns=['chain', 'token', 'recipient', 'amount', 'transactionHash', 'address'])
print(f"Successfully wrote {len(deposit_events)} events to {eventfile}")
else:
print(f"No Deposit events found for the given block range.")