Projects
Write and Compile

Write and Compile Smart Contract Code

Writing Smart Contract code is just like writing any other software program. You can use any IDE but the Remix IDE is highly recommended since it allows you to write, compile, and deploy all from the same IDE.

The Smart Contract code with detailed explanatory comments and instructions to compile the code will be made available to subscribers.

Compiling a Smart Contract

Smart Contract code is compiled using the Solidity compiler solc. Although, if you use the Remix IDE you do not need to explicitly run this.

The Remix IDE allows several options when compiling a Smart Contract:

  • You can select the version of the Ethereum Virtual Machine (EVM) you would like to compile the contract for.
  • You need to be aware of the size of the compiled code. Many EVMs may have limitations on the size of the contract that can be deployed. Remix offers an option to optimize the size of the code.
  • You can select several optimization parameters, which are not required at this stage of learning, but are important in real-world deployments. For example, by default, the optimizer will optimize the contract assuming it is called many times across its lifetime. In which case the deployment cost is higher but subsequent transactions are cheaper. In a reverse scenario, you may want the initial contract deployment to be cheaper and the later function executions to be more expensive.

When the contract is compiled the compiler generates a JSON object known as the Application Binary Interface (ABI). The ABI is required by other applications that want to interact with contracts, both from outside the blockchain (web apps for example) and for internal contract-to-contract interactions.

See the screenshots below:

  • Write the Smart Contract code

This is the code for a very simple example that comes with the IDE.

Remix IDE

  • Compile the code

You have several options when compiling. For a simple contract you do not need to do much, for more complex contracts the compiler version, optimization options, and target EVM are important decision.

  • Compilation Ouput

The compilation process generates a JSON object known as the Application Binary Interface (ABI). This is required in external application code (such as the web app) to inetract with the contract. You can copy the ABI directly from the IDE and paste it into your code as required.

  • Contract Size Warning

You may see this if the compiled ouput of your contract is too large. There are options to get around this problem, including breaking up your contract into multiple smaller contracts.

Remix IDE Contract Size Warning