- Ethereum Wallet and Basic Concepts
- Etherscan
- The TestRPC
- Interacting with Ethereum via GETH
- Interacting with Ethereum via web3
- Running an Ethereum Node
- Running a Private Ethereum Network
- Conclusion
Interacting with Ethereum via web3
The GETH interactive console is convenient to test and experiment with the Ethereum blockchain using the JavaScript API methods. But to access the Ethereum blockchain from an application, you can use the JavaScript API directly from a web page.
The web page in Figure 5.3 shows an application that queries an Ethereum account’s balance. The user enters an account address, and the JavaScript API retrieves and displays the account balance.
FIGURE 5.3 A demo page for web3.js
Via the web3.js library provided by the Ethereum project, the JavaScript on the page first connects to an Ethereum node. Here, you can put in your own Ethereum node (e.g., http://node.ip.addr:8545) or a public node INFURA provides (see the following example). For a local TestRPC node, you can simply use the http://localhost:8545 URL.
web3 = new Web3(new Web3.providers.HttpProvider( "https://mainnet.infura.io/"));
Then the JavaScript uses web3.js functions to query the address. These are the same JavaScript method calls we can make in the GETH console.
var balanceWei = web3.eth.getBalance(acct).toNumber(); var balance = web3.fromWei(balanceWei, 'ether');
Our demo application queries the account balance. The balance is public information and does not require any account private key. If your web3.js application needs to send ETH from one account to another, you will need access to the sending account’s private key. There are several ways to do this, and I will cover them in Chapter 8.