013_get_dalao_balance.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import requests
  2. import json
  3. url_0 = "https://eth-mainnet.g.alchemy.com/v2/WLI0ohbUlvbsJVmoIvB1wTSwUA8qP5qS"
  4. url_1 = ("https://eth-mainnet.g.alchemy.com/v2/ck1dtOx8tHkjAH2PX2FwTGN2T2gDaGtV")
  5. url_2 = "https://eth-mainnet.g.alchemy.com/v2/pxF-cEkEE1JxzxgotGyww5ra5w1IvAkJ"
  6. url = "https://eth-mainnet.g.alchemy.com/v2/WLI0ohbUlvbsJVmoIvB1wTSwUA8qP5qS"
  7. eth_getBalance_payload = {
  8. "id": 1,
  9. "jsonrpc": "2.0",
  10. "params": ["0x0f6e93a1d47c92ebf83bcb995e9fb4238187f030", "latest"],
  11. "method": "eth_getBalance"
  12. }
  13. headers = {
  14. "accept": "application/json",
  15. "content-type": "application/json"
  16. }
  17. req_response = requests.post(url, json=eth_getBalance_payload, headers=headers)
  18. print(req_response.text)
  19. response = json.loads(
  20. req_response.text)
  21. print(response)
  22. dalao_balance = int(response["result"], 16)/10**18
  23. print(dalao_balance)
  24. # https://docs.alchemy.com/reference/eth-getbalance
  25. # https://docs.alchemy.com/reference/alchemy-gettokenbalances
  26. 如果是在合约外部判断,则可以使用web3.eth.getCode(),或者是对应的JSON-RPC方法eth_getcode。
  27. getCode()用来获取参数地址所对应合约的代码,如果参数是一个外部账号地址,则返回"0x";如果参数是合约,则返回对应的字节码,如下所示:
  28. web3.eth.getCode("0xa5Acc472597C1e1651270da9081Cc5a0b38258E3")
  29. "0x"
  30. web3.eth.getCode("0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8")
  31. "0x600160008035811a818181146012578301005b601b6001356025565b8060005260206000f25b600060078202905091905056"
  32. 这样我们就可以通过getCode()的内容判断是哪一种地址了。