There are many clients/nodes implementing Ethereum protocol, each written in a different programming language, two most popular of them are go-ethereum
written in Go and parity
in Rust. In this series we will be using the client go-ethereum
popularly known as Geth
.
-
Setting up a Ethereum Node
Download & Install Geth.
After installing, run Geth to start syncing blockchain. Full Instructions for running Geth can be found here . We will explore some of these options.
Running Geth
Sync Ropsten Test Network
geth --testnet
Sync Rinkeby Test Network
geth --rinkeby
Running with console
geth --testnet console
Attach console to already running Geth node
# Windows:
geth attach ipc:\\.\pipe\geth.ipc
# Linux:
geth attach <datadir>/testnet/geth.ipc
Geth offers management API's besides official developer(JSON-RPC) API's.
Developer API's: web3,eth,db,shh
Management API's: admin,debug,miner,personal,txpool
Geth by default exposes all the API's over IPC interface. One can expose Developer(JSON-RPC) API's over HTTP interface by enabling HTTP-RPC server
geth --testnet --rpc
it will open a HTTP-RPC server endpoint at http://127.0.0.1:8545/. To override ip address and port, use --rpcaddr & --rpcport flags.
geth --testnet --rpc --rpcaddr <ip> --rpcport <portnumber>
for accessing API's in browser based applications, use --rpccorsdomain flag to set CORS domain
geth --testnet --rpccorsdomain <url>
Exposing WebSocket interface by enabling WS-RPC server
geth --testnet --ws --wsaddr <ip> --wsport <portnumber> --wsorigins <url>
It will run WS-RPC server with endpoint at http://127.0.0.1:8546
-
Setting up a Ethereum Test RPC Client
Even without installing Geth, one can simulate Ethereum node with a Test RPC client which exposes popular RPC functions. Ganache CLI formerly known as
testrpc
is a fast Ethereum RPC client for testing and development of smart contracts.To install Ganache-CLI, first install Node.js, Installation will also include Node.js package manager
npm
, then run following command.
npm install -g ganache-cli
To run Ganache-cli use following command
ganache-cli <options>
It will start a HTTP-RPC server at http://localhost:8545. Apart from running server a new HD wallet will be created, and first 10 addresses will get loaded each with 100 ethers.
-
Interacting with a Ethereum Client/Node
web3.js is popular library written in javascript to interact with Ethereum Clients/Nodes. It can consume API's over IPC,RPC and WS Interfaces.
Current stable release 0.20.x does not support WS interface, but beta release 1.0.0 does also support WS interface
How to use web3.js library with Node.js
Install
web3.js
a javascript library for interacting with Ethereum RPC interface as a Node.js module.npm install -g web3
Accessing development API's in Node.js Dapp
a. Connect with IPC interface
const net = require('net') const Web3 = require('web3') const web3 = new Web3(new Web3.providers.IpcProvider("\\\\.\\pipe\\geth.ipc",net)) console.log(web3.isConnected()) // return true if connected
b. Connect with HTTP-RPC interface
const Web3 = require('web3') const web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:8545")) console.log(web3.isConnected()) // return true if connected
How to use MetaMask with local/remote Geth node
MetaMask is a browser extension which enables interaction between Ethereum Dapps in browsers and Ethereum blockchain without running a Ethereum node. It also includes a identity vault.
Install MetaMask Extension, after installing select the network and node to connect. MetaMask also gives option to connect with local running node as well as any other custom remote node besides pre-configured networks. On selection MetaMask extension will inject a web3 instance in browser window, which can be used in Dapp.if(type of web3 !== 'undefined'){ // Use injected web3 web3js = new Web3(web3.currentProvider); } else { // Fallback to local node // Should have Web3.js library loaded // by default local HTTP-RPC server exposes port 8545, but // you can configure it to other ports // you can use Infura Node Urls also // 'http://127.0.0.1:8545' will be replaced by // 'https://ropsten.infura.io/<API KEy>' web3js = new Web3(new Web3.providers.HttpProvider('http://127.0.0.1:8545')); }
You should initialize web3 instance after window load event has fired to avoid any race condition.
window.addEventListener('load',cb)
To access local node and consume RPC API's in browser over HTTP interface, run Geth node with flag --rpccorsdomain set to relevant domains
# Example for allowing all domain geth --testnet --rpc --rpccorsdomain="*" # Example for allowing multiple domains geth --testnet --rpc --rpccorsdomain="http://127.0.0.1:8080,http://localhost" # Example for allowing MetaMask chrome extension geth --testnet --rpc --rpccorsdomain="chrome-extension://<ID>"
Access RPC API's over WebSockets with web3.js
a. Run Geth exposing websocket interface with --wsorigins flag
# Allow all domain geth --testnet --ws --wsorigins="*" # Allow specific domain geth --testnet --ws --wsorigins="http://localhost:8080"
b. Download/install web3.js version 1.0.0-beta.XX , web3.js version 0.20.X does not provide support for websockets. Furthermore disable MetaMask extension, which uses web3.js version 0.20.X that can lead to conflict with web3.js version 1.0.0-beta.XX.
if( Web3.providers.WebsocketProvider !== 'undefined' ){ web3js = new Web3("ws://127.0.0.1:8546"); console.log(web3js.version); // 1.0.0-beta.XX }
This brings us to the end of Part 1 of Building a Dapp on Ethereum Series.In this article we have seen different ways of setting up & communicating with an Ethereum node/client. In Building a Dapp on Ethereum Series Part 2 of this series, we'll see how to create a smart contract using solidity, then compile and deploy it on ethereum blockchain.
If you have liked this article, please share it with others.