当前位置:   article > 正文

通过Etherscan API获取某一账户的余额及真实货币_使用etherscan api

使用etherscan api
  1. import json
  2. import time
  3. import requests
  4. def account_balance(account_address, api_key):
  5. # 设置API URL
  6. url = "https://api.etherscan.io/api"
  7. # 设置查询参数
  8. params = {
  9. "module": "account",
  10. "action": "balance",
  11. "address": f"{account_address}",
  12. "tag": "latest",
  13. "apikey": api_key # 将YourApiKeyToken替换为你的实际API密钥
  14. }
  15. # 发送GET请求
  16. response = requests.get(url, params=params)
  17. # 检查响应状态码
  18. if response.status_code == 200:
  19. # 解析JSON响应
  20. data = response.json()
  21. # 提取余额数据
  22. balance = data.get("result")
  23. # 将余额从Wei转换为Ether
  24. balance_wei = int(balance)
  25. balance_eth = balance_wei / 1e18 # 1 Ether = 1e18 Wei
  26. exchange_rate = 1582.53 # 美元汇率
  27. currency_balance = balance_eth * exchange_rate
  28. print(f"账户余额 虚拟货币: {balance_eth} ETH \n账户余额 真实货币: {currency_balance} \n汇率: {exchange_rate}")
  29. else:
  30. print(f"请求失败,状态码: {response.status_code}")
  31. if __name__ == "__main__":
  32. account_address = "xxx"
  33. etherscan_api_key = "xxx"
  34. account_balance(account_address, etherscan_api_key)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/419654
推荐阅读
  

闽ICP备14008679号