How to do it...

  1. Consider the following smart contract, HelloWorld.sol, as an example:
pragma solidity ^0.4.22;

contract HelloWorld {

string public greeting = "Hello World";

event greetingChanged(address by, string greeting);

function changeGreeting(string _newGreeting) public {
greeting = _newGreeting;
emit greetingChanged(msg.sender, _newGreeting);
}

}
  1. Verify your solc installation with the following command. It will show you all the parameters supported by the compiler:
$ solc --help
  1. For deploying and interaction, we need the ABI and binary of a smart contract. Use the --abi and --bin flags to generate them using the solc compiler:
$ solc --bin --abi HelloWorld.sol
  1. Compiling the contract with --bin and --abi will produce an output like this:

  1. ABI (Application Binary Interface) defines how your call functions are called and in which format the parameters should be passed from one component to the next. Binary or bytecode is the compiled output, which is deployed to Ethereum.

  1. Use -0 along with the path to save the output to separate files:

$ solc -o <path_to_output_directory> --bin --abi HelloWorld.sol
  1. The compiler will automatically read imported files in the contract. It is also possible to provide custom path redirects using the prefix=path command:
$ solc github.com/OpenZeppelin/zeppelin-solidity=/usr/local/zeppelin =/usr/local/others contract.sol

This asks the compiler to search for files in /usr/local/zeppelin when it finds imports with the prefix github.com/OpenZeppelin/zeppelin-solidity. If there are no files there, then it looks in /usr/local/others. The non-specified prefix will only work if you specify =/ as a remapping.

  1. If libraries are used in the contracts, bytecode will have substrings of the form 

    __LibraryName____

  2. solc can also be called with the --link flag, which interprets all input files to be unlinked binaries in the __LibraryName____ format and is linked in place (if the input is read from stdin, it is written to stdout). All options except --libraries are ignored in this case. 

  3. Use --optimize during compilation if you are planning to use the binary for deployment:
$ solc --optimize --bin HelloWorld.sol
  1. solc also supports more advanced outputs, and a few of them are listed here:
  --ast       AST of all source files.
--asm EVM assembly of the contracts.
--opcodes Opcodes of the contracts.
--hashes Function signature hashes of the contracts.
--userdoc Natspec user documentation of all contracts.
--formal Translated source suitable for formal analysis.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.7.240