Web3 by Example: Smart Contract Compilation & ABI

A "smart contract" is simply a program that runs on the Ethereum blockchain. It's a collection of code (its functions) and data (its state) that resides at a specific address on the Ethereum blockchain. We will show how to compile a simple smart contract in this example.

Use npm for a convenient and portable way to install solcjs, a Solidity compiler. The solcjs program has fewer features than the solc. Install full-featured compiler solc.

Compile the smart contract with solcjs. Specify the --abi and --bin options to generate the ABI and binary code.

The Contract Application Binary Interface (ABI) is the standard way to interact with contracts in the Ethereum ecosystem, both from outside the blockchain and for contract-to-contract interaction. Data is encoded according to its type, as described in this specification. The encoding is not self describing and thus requires a schema in order to decode.

The EVM bytecode is the compiled code of the smart contract. It is a sequence of bytes that is executed by the EVM.

storage.sol

// SPDX-License-Identifier: GPL-3.0
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.16 <0.9.0;
contract SimpleStorage {
event ValueChanged(
address indexed author,
string oldValue,
string newValue
);
string _value;
constructor(string memory value) {
_value = value;
}
function getValue() public view returns (string memory) {
return _value;
}
function setValue(string memory value) public {
_value = value;
emit ValueChanged(msg.sender, _value, value);
}
}

console

➜ npm install -g solc
➜ solcjs --abi --bin storage.sol

Next example: Deploying a Smart Contract