[英]How to check Ethereum address balance without connecting to an Ethereum node?
[英]How to filter for ethereum address
我尝试编写一个脚本来获取我的以太坊地址发生的交易。 此脚本在监控contractAddress时有效,但如果我想监控我自己的地址或非合约地址,它不会得到任何交易
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()
据我所知,使用 filter 命令只能监控合约上发生的事件。 此选项不适用于您的情况。
我将编写 JavaScript 代码,因为我更熟悉它,但您可以将其改编为 python。 我可能会做这样的事情:
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}`);
}
}
}
您可以将此函数插入setInterval
并大约每15秒调用一次。 为什么是 15 秒? 平均而言,每 15 秒就会出现一个新块。
另一种选择是使用ethers.js库。 您可以直接订阅 区块并请求新区块的交易信息。 但我不知道python是否存在这样的库。
你也可以这样做:
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
})
在这种情况下,每次您收到 ERC-20 代币到您的地址时都会触发处理程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.