ITPub博客

首页 > 区块链 > 区块链 > 查询 Eth ERC20 Token的余额

查询 Eth ERC20 Token的余额

区块链 作者:奥斯科技 时间:2020-03-17 21:23:45 0 删除 编辑

pyhton script

import jsonimport web3from web3 import Web3, HTTPProvider
my = "0xD551234Ae421e3BCBA99A0Da6d736074f22192FF"eos_contract_address = "0x86Fa049857E0209aa7D9e616F7eb3b3B78ECfdb0"contract_source_code='''
[{
  "type":"function",
  "name":"balanceOf",
  "constant":true,
  "payable":false,
  "inputs":[{"name":"","type":"address"}],
  "outputs":[{"name":"","type":"uint256","value":"0"}]
}]
'''abi = json.loads(contract_source_code)web3 = Web3(HTTPProvider('http://localhost:8545') )#web3 = Web3(HTTPProvider('https://mainnet.infura.io/<key>') )source_code = web3.eth.getCode(eos_contract_address)contract = web3.eth.contract(abi=abi, address=eos_contract_address)contract.call().balanceOf(my)

erc20 contract abi

[
  {
    "constant": true,
    "inputs": [],
    "name": "name",
    "outputs": [
      {
        "name": "",
        "type": "string"
      }
    ],
    "type": "function"
  },
  {
    "constant": false,
    "inputs": [
      {
        "name": "_from",
        "type": "address"
      },
      {
        "name": "_to",
        "type": "address"
      },
      {
        "name": "_value",
        "type": "uint256"
      }
    ],
    "name": "transferFrom",
    "outputs": [
      {
        "name": "success",
        "type": "bool"
      }
    ],
    "type": "function"
  },
  {
    "constant": true,
    "inputs": [],
    "name": "decimals",
    "outputs": [
      {
        "name": "",
        "type": "uint8"
      }
    ],
    "type": "function"
  },
  {
    "constant": true,
    "inputs": [
      {
        "name": "",
        "type": "address"
      }
    ],
    "name": "balanceOf",
    "outputs": [
      {
        "name": "",
        "type": "uint256"
      }
    ],
    "type": "function"
  },
  {
    "constant": true,
    "inputs": [],
    "name": "symbol",
    "outputs": [
      {
        "name": "",
        "type": "string"
      }
    ],
    "type": "function"
  },
  {
    "constant": false,
    "inputs": [
      {
        "name": "_to",
        "type": "address"
      },
      {
        "name": "_value",
        "type": "uint256"
      }
    ],
    "name": "transfer",
    "outputs": [],
    "type": "function"
  },
  {
    "constant": false,
    "inputs": [
      {
        "name": "_spender",
        "type": "address"
      },
      {
        "name": "_value",
        "type": "uint256"
      },
      {
        "name": "_extraData",
        "type": "bytes"
      }
    ],
    "name": "approveAndCall",
    "outputs": [
      {
        "name": "success",
        "type": "bool"
      }
    ],
    "type": "function"
  },
  {
    "constant": true,
    "inputs": [
      {
        "name": "",
        "type": "address"
      },
      {
        "name": "",
        "type": "address"
      }
    ],
    "name": "spentAllowance",
    "outputs": [
      {
        "name": "",
        "type": "uint256"
      }
    ],
    "type": "function"
  },
  {
    "constant": true,
    "inputs": [
      {
        "name": "",
        "type": "address"
      },
      {
        "name": "",
        "type": "address"
      }
    ],
    "name": "allowance",
    "outputs": [
      {
        "name": "",
        "type": "uint256"
      }
    ],
    "type": "function"
  },
  {
    "inputs": [
      {
        "name": "initialSupply",
        "type": "uint256"
      },
      {
        "name": "tokenName",
        "type": "string"
      },
      {
        "name": "decimalUnits",
        "type": "uint8"
      },
      {
        "name": "tokenSymbol",
        "type": "string"
      }
    ],
    "type": "constructor"
  },
  {
    "anonymous": false,
    "inputs": [
      {
        "indexed": true,
        "name": "from",
        "type": "address"
      },
      {
        "indexed": true,
        "name": "to",
        "type": "address"
      },
      {
        "indexed": false,
        "name": "value",
        "type": "uint256"
      }
    ],
    "name": "Transfer",
    "type": "event"
  }]

参考:

https://github.com/ethereum/web3.py
https://github.com/ethereum/wiki/wiki/JavaScript-API
http://nuclearcryptobuddha.blog/2017/06/how-to-send-receive-and-check-balance-of-erc20-tokens-using-geth/
https://tokenbalance.com/
https://infura.io/



来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69966803/viewspace-2681022/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: 以太坊文档整理
请登录后发表评论 登录
全部评论
专业区块链/网站开发

注册时间:2020-03-17

  • 博文量
    16
  • 访问量
    11115