I try to write a script to get transactions of my ethereum address as they happen. This script here works when monitoring a contractAddress, but if i want to monitor my own address or non contract address, it dont get any transactions
def handle_event(event):
print(event)
print(Web3.toJSON(event))
def log_loop(event_filter, poll_interval):
while True:
for event in event_filter.get_new_entries():
handle_event(event)
time.sleep(poll_interval)
def main():
event_filter = web3.eth.filter({"address": trackAddress})
#get_block = web3.eth.get_block('latest')
#block_filter = web3.eth.filter('latest')
log_loop(event_filter, 2)
if __name__ == '__main__':
main()
As far as I know, using the filter command, you can only monitor events that occur on the contract. This option will not work in your case.
I'll write the JavaScript code as I'm more familiar with it, but you can adapt it to python. I would probably do something like this:
async function checkBlock(address) {
const block = await web3.eth.getBlock('latest');
console.log(`Checking new block ${block.number}`);
for (let txHash of block.transactions) {
const tx = await web3.eth.getTransaction(txHash);
if (address === tx.to.toLowerCase()) {
console.log(`New transaction found. Block - ${block.number}`);
console.log(`Transaction: ${tx}`);
}
}
}
You can insert this function into setInterval
and call it approximately every ~15 seconds. Why 15 seconds? On average, a new block appears just every 15 seconds.
Another option is using the ethers.js library. You can subscribe directly to blocks and request transaction information for a new block. But I don't know if such a library exists for python.
You can also do something like this with:
const topicSets = [
utils.id("Transfer(address,address,uint256)"),
null,
[
null,
hexZeroPad(address, 32)
]
]
provider.on(topicSets, (log, event) => {
// Emitted any token is sent TO your address
})
In this case, the handler will be triggered every time you receive ERC-20 tokens to your address.