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)

Installing a solidity compiler

You have multiple options when it comes to compiling smart contracts written in solidity. In this recipe, you will learn about installing a solidity compiler and using it to compile your smart contract.

Getting ready

If you are using macOS, you will need homebrew to install the binary compiler. There are other options, such as using npm or docker, which require Node.js and Docker, respectively, installed on your machine.

How to do it...

  1. If you are in Ubuntu, use ppa to install the compiler by running the following command:
sudo add-apt-repository ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install solc
  1. If you are using macOS, install the compiler using brew:
brew update
brew tap ethereum/ethereum
brew install solidity
  1. Verify the installation with the following command:
solc --version
  1. Use this command to compile a contract and print the binary:
solc --bin SampleContract.sol
  1. You want to get some of the more advanced output from solc:
solc -o outDirectory --bin --ast --asm --abi --opcodes SampleContract.sol

We can configure it using these provided flags:

  • --ast: Abstracts the syntax trees of source files
  • --asm: EVM assembly of the contracts
  • --abi: ABI specification of the contracts
  • --opcodes: Opcodes of the contracts

You can get the complete list of operations supported by the solc compiler by running solc --help.

There's more...

There is also a JavaScript-based solidity compiler available for you to use. You can install SolcJS using npm. The solc-js project is derived from the C++ solc project and can be used in JavaScript projects directly:

npm install -g solc

To see all the supported features, run the following command:

solcjs --help