Book Image

Ethereum Cookbook

By : Manoj P R
Book Image

Ethereum Cookbook

By: Manoj P R

Overview of this book

Ethereum and Blockchain will change the way software is built for business transactions. Most industries have been looking to leverage these new technologies to gain efficiencies and create new business models and opportunities. The Ethereum Cookbook covers various solutions such as setting up Ethereum, writing smart contracts, and creating tokens, among others. You’ll learn about the security vulnerabilities, along with other protocols of Ethereum. Once you have understood the basics, you’ll move on to exploring various design decisions and tips to make your application scalable and secure. In addition to this, you’ll work with various Ethereum packages such as Truffle, Web3, and Ganache. By the end of this book, you’ll have comprehensively grasped the Ethereum principles and ecosystem.
Table of Contents (13 chapters)

Saving time and money with INFURA

If you don't want to set up a full node for you decentralized application, then this is the recipe for you. INFURA provides an infrastructure for Ethereum and you can make use of it for free.

How to do it...

  1. Go to the INFURA signup page (https://infura.io/signup) and enter your name and email.
  2. Once you acknowledge the terms and conditions and have signed up, you will receive an email from INFURA with the providers for each network.
  3. For each network, you will receive an endpoint and an API key:
    • Main Ethereum network: https://mainnet.infura.io/<api-key>
    • Test Ethereum network (Ropsten): https://ropsten.infura.io/<api-key>
    • Test Ethereum network (Rinkeby): https://rinkeby.infura.io/<api-key>
    • Test Ethereum network (INFURAnet): https://infuranet.infura.io/<api-key>

You can interact with the providers just like you do with your local node. You can use JSON RPC, Web3JS, or even the REST API provided by INFURA to interact with the nodes.

  1. Get the current block number with JSON RPC using cURL:

curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc": "2.0", "id": 1, "method": "eth_blockNumber", "params": []}' "https://mainnet.infura.io/<your-api-key>"

This will return the current block number, as follows:

{"jsonrpc":"2.0","id":1,"result":"0x512bab"}
  1. INFURA also provides an easy-to-use REST API interface for JSON RPC commands. You can use the JSON RPC methods as the path to get the result:

https://api.infura.io/v1/jsonrpc/mainnet/eth_blockNumber?token=<your-api-key>

  1. To use INFURA with your web3js application, you can set the endpoints as HTTP providers:
web3 = new Web3(new Web3.providers.HttpProvider ("https://mainnet.infura.io/<your_api_key>"));

There's more...

INFURA also provides support for IPFS and you can use their server as a gateway: