Go-Ethereum

通過 JSON RPC 獲取智能合約代幣餘額

  • June 13, 2018

通過 Geth 命令行,我可以獲得我的智能合約代幣餘額:

var mytest = eth.contract([{interface}]).at(contract address);
mytest.balanceOf(eth.accounts[0])
>998

我想在我的網站上顯示代幣餘額。我怎樣才能做到這一點?

我嘗試了 JSON-RPC 和幾種方法,例如eth_getTransactionByHash,但不知道如何獲得平衡。

使用web3.js在您的網站上顯示余額。

Geth 的控制台(您可以在其中獲取代幣餘額)是 Javascript 並在後台使用 web3.js,因此您的網站程式碼幾乎相同。

否則,您必須使用 JSON-RPC,eth_call並且必須對您正在呼叫的函式及其參數進行ABI編碼。它要復雜得多,例如: How to call a contract method using the eth_call JSON-RPC API


如果需要伺服器端渲染,則可以檢查該設計決策,因為使用者應該能夠在沒有伺服器的情況下執行去中心化應用程序:Swarm 和 IPFS 是 DApp 可能採用的技術,需要牢記在心。

添加到@eth♦ 的答案和@Jeroen 的評論

使用web3 1.0.0-beta.31獲取 Smartcontract 代幣餘額

在 1.0.0-beta 中……現在大部分是通過Promises處理的,因此你可以這樣做:

var mytest = new web3.eth.Contract({interface}, '0x123....', {
   from: '0x456...', // default from address
   gasPrice: '20000000000' // default gas price in wei, 20 gwei in this case
});

mytest.methods.balanceOf('0x456...').call()
   .then(function(result){
   //the result holds your Token Balance that you can assign to a var
   var myTokenBalance = result;
   return result;
});
  • {interface}您的 Contract 的 ABI在哪裡0x123....
  • 0x123.... 您的契約地址在哪裡。
  • 您持有代幣的乙太坊賬戶的地址在哪裡0x456....(即您希望從中檢索其代幣餘額的 ETH 賬戶)

不要忘記添加new前面web3.eth.Contract(....)

@Jeroen這樣你就不會得到錯誤

MyContract.at 不是函式

了。

注意:我正在使用IPC Provider geth.ipc 與我的節點進行互動(因為我在同一台 PC 本地主機上,因此更多保存而不是使用 HTTP 請求)。

 if (typeof web3 !== 'undefined') {
   web3 = new Web3(web3.currentProvider);
 } else {
   var net = require('net');
   var web3 = new Web3('/home/yourHomeFolder/.ethereum/geth.ipc', net);
 };

web3.eth.Contract({interface}編輯:作為我用於我的智能合約的 ABI JSON () 的參考,作為myContractABI我的{interface}

   myContractABI = [
   {
       "constant": false,
       "inputs": [
           {
               "name": "newSellPrice",
               "type": "uint256"
           },
           {
               "name": "newBuyPrice",
               "type": "uint256"
           }
       ],
       "name": "setPrices",
       "outputs": [],
       "payable": false,
       "stateMutability": "nonpayable",
       "type": "function"
   },
   {
       "constant": true,
       "inputs": [],
       "name": "name",
       "outputs": [
           {
               "name": "",
               "type": "string"
           }
       ],
       "payable": false,
       "stateMutability": "view",
       "type": "function"
   },
   {
       "constant": false,
       "inputs": [
           {
               "name": "_spender",
               "type": "address"
           },
           {
               "name": "_value",
               "type": "uint256"
           }
       ],
       "name": "approve",
       "outputs": [
           {
               "name": "success",
               "type": "bool"
           }
       ],
       "payable": false,
       "stateMutability": "nonpayable",
       "type": "function"
   },
   {
       "constant": true,
       "inputs": [],
       "name": "totalSupply",
       "outputs": [
           {
               "name": "",
               "type": "uint256"
           }
       ],
       "payable": false,
       "stateMutability": "view",
       "type": "function"
   },
   {
       "constant": false,
       "inputs": [
           {
               "name": "_from",
               "type": "address"
           },
           {
               "name": "_to",
               "type": "address"
           },
           {
               "name": "_value",
               "type": "uint256"
           }
       ],
       "name": "transferFrom",
       "outputs": [
           {
               "name": "success",
               "type": "bool"
           }
       ],
       "payable": false,
       "stateMutability": "nonpayable",
       "type": "function"
   },
   {
       "constant": true,
       "inputs": [],
       "name": "decimals",
       "outputs": [
           {
               "name": "",
               "type": "uint8"
           }
       ],
       "payable": false,
       "stateMutability": "view",
       "type": "function"
   },
   {
       "constant": false,
       "inputs": [
           {
               "name": "_value",
               "type": "uint256"
           }
       ],
       "name": "burn",
       "outputs": [
           {
               "name": "success",
               "type": "bool"
           }
       ],
       "payable": false,
       "stateMutability": "nonpayable",
       "type": "function"
   },
   {
       "constant": true,
       "inputs": [],
       "name": "sellPrice",
       "outputs": [
           {
               "name": "",
               "type": "uint256"
           }
       ],
       "payable": false,
       "stateMutability": "view",
       "type": "function"
   },
   {
       "constant": true,
       "inputs": [
           {
               "name": "",
               "type": "address"
           }
       ],
       "name": "balanceOf",
       "outputs": [
           {
               "name": "",
               "type": "uint256"
           }
       ],
       "payable": false,
       "stateMutability": "view",
       "type": "function"
   },
   {
       "constant": false,
       "inputs": [
           {
               "name": "target",
               "type": "address"
           },
           {
               "name": "mintedAmount",
               "type": "uint256"
           }
       ],
       "name": "mintToken",
       "outputs": [],
       "payable": false,
       "stateMutability": "nonpayable",
       "type": "function"
   },
   {
       "constant": false,
       "inputs": [
           {
               "name": "_from",
               "type": "address"
           },
           {
               "name": "_value",
               "type": "uint256"
           }
       ],
       "name": "burnFrom",
       "outputs": [
           {
               "name": "success",
               "type": "bool"
           }
       ],
       "payable": false,
       "stateMutability": "nonpayable",
       "type": "function"
   },
   {
       "constant": true,
       "inputs": [],
       "name": "buyPrice",
       "outputs": [
           {
               "name": "",
               "type": "uint256"
           }
       ],
       "payable": false,
       "stateMutability": "view",
       "type": "function"
   },
   {
       "constant": true,
       "inputs": [],
       "name": "owner",
       "outputs": [
           {
               "name": "",
               "type": "address"
           }
       ],
       "payable": false,
       "stateMutability": "view",
       "type": "function"
   },
   {
       "constant": true,
       "inputs": [],
       "name": "symbol",
       "outputs": [
           {
               "name": "",
               "type": "string"
           }
       ],
       "payable": false,
       "stateMutability": "view",
       "type": "function"
   },
   {
       "constant": false,
       "inputs": [],
       "name": "buy",
       "outputs": [],
       "payable": true,
       "stateMutability": "payable",
       "type": "function"
   },
   {
       "constant": false,
       "inputs": [
           {
               "name": "_to",
               "type": "address"
           },
           {
               "name": "_value",
               "type": "uint256"
           }
       ],
       "name": "transfer",
       "outputs": [],
       "payable": false,
       "stateMutability": "nonpayable",
       "type": "function"
   },
   {
       "constant": true,
       "inputs": [
           {
               "name": "",
               "type": "address"
           }
       ],
       "name": "frozenAccount",
       "outputs": [
           {
               "name": "",
               "type": "bool"
           }
       ],
       "payable": false,
       "stateMutability": "view",
       "type": "function"
   },
   {
       "constant": false,
       "inputs": [
           {
               "name": "_spender",
               "type": "address"
           },
           {
               "name": "_value",
               "type": "uint256"
           },
           {
               "name": "_extraData",
               "type": "bytes"
           }
       ],
       "name": "approveAndCall",
       "outputs": [
           {
               "name": "success",
               "type": "bool"
           }
       ],
       "payable": false,
       "stateMutability": "nonpayable",
       "type": "function"
   },
   {
       "constant": true,
       "inputs": [
           {
               "name": "",
               "type": "address"
           },
           {
               "name": "",
               "type": "address"
           }
       ],
       "name": "allowance",
       "outputs": [
           {
               "name": "",
               "type": "uint256"
           }
       ],
       "payable": false,
       "stateMutability": "view",
       "type": "function"
   },
   {
       "constant": false,
       "inputs": [
           {
               "name": "amount",
               "type": "uint256"
           }
       ],
       "name": "sell",
       "outputs": [],
       "payable": false,
       "stateMutability": "nonpayable",
       "type": "function"
   },
   {
       "constant": false,
       "inputs": [
           {
               "name": "target",
               "type": "address"
           },
           {
               "name": "freeze",
               "type": "bool"
           }
       ],
       "name": "freezeAccount",
       "outputs": [],
       "payable": false,
       "stateMutability": "nonpayable",
       "type": "function"
   },
   {
       "constant": false,
       "inputs": [
           {
               "name": "newOwner",
               "type": "address"
           }
       ],
       "name": "transferOwnership",
       "outputs": [],
       "payable": false,
       "stateMutability": "nonpayable",
       "type": "function"
   },
   {
       "inputs": [
           {
               "name": "initialSupply",
               "type": "uint256"
           },
           {
               "name": "tokenName",
               "type": "string"
           },
           {
               "name": "tokenSymbol",
               "type": "string"
           }
       ],
       "payable": false,
       "stateMutability": "nonpayable",
       "type": "constructor"
   },
   {
       "anonymous": false,
       "inputs": [
           {
               "indexed": false,
               "name": "target",
               "type": "address"
           },
           {
               "indexed": false,
               "name": "frozen",
               "type": "bool"
           }
       ],
       "name": "FrozenFunds",
       "type": "event"
   },
   {
       "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"
   },
   {
       "anonymous": false,
       "inputs": [
           {
               "indexed": true,
               "name": "from",
               "type": "address"
           },
           {
               "indexed": false,
               "name": "value",
               "type": "uint256"
           }
       ],
       "name": "Burn",
       "type": "event"
   }
];

引用自:https://ethereum.stackexchange.com/questions/3952