2015-08-31 10 views

risposta

40

Sul Web:

(non programmatico, ma per completezza ...) Se si desidera solo per ottenere l'equilibrio di un account o di un contratto, si può visitare http://etherchain.org o http://etherscan.io.

Dalle Geth, ETH, console pyeth:

Utilizzando l'API Javascript, (che è quello che i Geth, ETH e console pyeth usano), è possibile ottenere l'equilibrio di un account con il seguente:

web3.fromWei(eth.getBalance(eth.coinbase)); 

"web3" è il Ethereum-compatible Javascript library web3.js.

"eth" è in realtà una scorciatoia per "web3.eth" (disponibile automaticamente in geth). Quindi, in realtà, quanto sopra dovrebbe essere scritto:

web3.fromWei(web3.eth.getBalance(web3.eth.coinbase)); 

"web3.eth.coinbase" è l'account predefinito per la sessione della console. Puoi inserire altri valori per questo, se vuoi. Tutti i saldi dei conti sono aperti in Ethereum. Ex, se si dispone di più account:

web3.fromWei(web3.eth.getBalance(web3.eth.accounts[0])); 
web3.fromWei(web3.eth.getBalance(web3.eth.accounts[1])); 
web3.fromWei(web3.eth.getBalance(web3.eth.accounts[2])); 

o

web3.fromWei(web3.eth.getBalance('0x2910543af39aba0cd09dbb2d50200b3e800a63d2')); 

EDIT: Ecco uno script a portata di mano della messa in vendita i saldi di tutti i conti:

function checkAllBalances() { var i =0; eth.accounts.forEach(function(e){ console.log(" eth.accounts["+i+"]: " + e + " \tbalance: " + web3.fromWei(eth.getBalance(e), "ether") + " ether"); i++; })}; checkAllBalances(); 

Contratti Interno:

Contratti interni, Solidity fornisce una simulazione un modo per ottenere equilibri. Ogni indirizzo ha una proprietà .balance, che restituisce il valore in wei. contratto del campione:

contract ownerbalancereturner { 

    address owner; 

    function ownerbalancereturner() public { 
     owner = msg.sender; 
    } 

    function getOwnerBalance() constant returns (uint) { 
     return owner.balance; 
    } 
} 
+2

Posso consiglio che lo script della messa in saldi può essere molto più semplice: eth.accounts.forEach (function (e, i) {console.log (" eth.accounts [ "+ i +"]: "+ e +" \ tbalance: "+ web3.fromWei (eth.getBalance (e)," etere ") +" etere ")}) – user208769

+0

eth.getBalance() riceve 2 parametri, non 1, cosa succede quando ne fornite uno solo? qual è il valore predefinito per il secondo? – knocte

0

Per la nuova release dell'API web3:

L'ultima versione di web3 API (. vers beta 1.xx) utilizza promesse (asincrona, come callback). Dokumentation: web3 beta 1.xx

Quindi è una Promessa e restituisce String per l'indirizzo specificato in wei.

io sono su Linux (openSUSE), Geth 1.7.3, Rinkeby Ethereum testnet, utilizzando Meteor 1.6.1, e ce l'ha a lavorare nel modo seguente collegamento via IPC Provider al mio nodo Geth:

// serverside js file 
 

 
import Web3 from 'web3'; 
 

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

 
    // set the default account 
 
    web3.eth.defaultAccount = '0x123..............'; 
 

 
    web3.eth.coinbase = '0x123..............'; 
 

 
    web3.eth.getAccounts(function(err, acc) { 
 
    _.each(acc, function(e) { 
 
     web3.eth.getBalance(e, function (error, result) { 
 
     if (!error) { 
 
      console.log(e + ': ' + result); 
 
     }; 
 
     }); 
 
    }); 
 
    });

Problemi correlati