Scuti Chapter 3 - Compiling and Deploying Ethereum Smart Contracts

unsplash-image-uPXs5Vx5bIg.jpg

Paul Gege Christian Lee David Dang

Overview 

To have a contract which we can interact with, we need to compile the contract then deploy it to the blockchain. Once a contract is on the network, then we can send transactions to the blockchain specifying what actions we would like to perform on the blockchain. 

Compiling a smart contract translates the Solidity code into the contract byte code and the application binary interface ABI. The contract ABI helps us communicate with the contract from outside the blockchain using JavaScript. The contract bytecode on the other hand is what gets deployed to the blockchain. To compile and deploy solidity contracts we have tools such as Remix, Truffle, Solc, and Web3 ready to use. The simplest way to compile and deploy it using the Remix IDE where compiling and deploying your contract is as simple as a button click. Within the Remix IDE developers are also able to view the contract byte code and the contract ABI. Truffle is another popular tool that is capable of handling our contract compilation and deployment. Under the hood, Truffle uses Solc (a node package) to help with compiling our contract. Once our code has been compiled, we can deploy our contract bytecode to the blockchain using Truffle which uses Web3 to deploy as well. 

Screen Shot 2021-10-05 at 8.56.31 PM.png

Deploying contracts to a blockchain is not free, all transactions on the Ethereum blockchain cost a certain amount of ‘gas’ to complete. The purpose of gas is to limit the amount of work needed to execute a transaction. The amount of gas required for a transaction varies based on the number of modifications made to the blockchain within that transaction. The gas price of a transaction is a value set by the creator of the transaction. The total cost of a transaction is `gas_price * gas`. Because gas prices are usually estimated, there is a possibility of overestimation or underestimation. In the case of overestimation, if some gas is left over after execution, it gets refunded. On the other hand, if the gas gets used up at any point, an out-of-gas exception gets triggered and all modifications made to state in the current call frame gets reverted. 

In this tutorial, we’ll be going over how to use truffle to compile and deploy our smart contracts. We’re going to continue using our FundRaiser smart contract from Chapter 2.

Screen Shot 2021-10-05 at 8.59.02 PM.png

Setup: 

First, we need to install the truffle using ` npm install -g truffle`. Truffle requires that we have a running Ethereum client, however, it comes with a development blockchain built-in. After installing the truffle on our machine, we can now begin creating a project. Truffle has boilerplates that come prepackaged with helpful modules, contracts, and libraries for smart contract development called Truffle Boxes. We will be using the metacoin box for this tutorial. 

Now that we understand what we need to do to get set up let’s get started. In the terminal run the following to create and navigate into a folder where we can unbox our prepackaged truffle box.  

mkdir sc-tutorial cd sc-tutorial

Next, we need to download and unbox the metacoin truffle box

truffle unbox metacoin

Once this is completed, we will have a folder structure with the following items:

  • the contract is a directory for solidity contracts 

  • migration is a directory for deployment files 

  • the test is a directory for testing our application and contracts 

  • truffle-config.js is the truffle configuration file

Compiling: 

Open up the project in your local IDE, under the contracts directory delete all the example contracts then add a file called ‘FundRaiser.sol’ and paste the example code provided earlier. Your contracts directory should look like this: 

Screen Shot 2021-10-05 at 9.07.15 PM.png

Next we need to ensure truffle uses the correct solidity compiler version, in the truffle-config.js file replace the existing code with the following:

Screen Shot 2021-10-05 at 9.08.45 PM.png

This sets the solc compiler version in truffle to the version we have in our smart contract. Skipping this step would result in the compilation failing. 

To compile our contract all we need to do is run truffle compile.  

Deploying: 

To deploy our contract to a network, we need to have our truffle development blockchain running. To start up the network, run truffle development within our current project directory. Truffle develop starts a development blockchain that’s built into Truffle, it helps set up an integrated blockchain environment without having to do any extra installations or setup. It runs a client on http://127.0.0.1:9545. After running this command, truffle develop provides us with a console to run all the available truffle commands, this means instead of running truffle compile, we can omit the truffle and only type compile. 

Screen Shot 2021-10-05 at 9.09.52 PM.png

We now need to set up our migrations to help deploy our contract to the Ethereum network. Our migration files are stored under the migrations directory in our truffle box. Delete all preexisting migrations and create one with the following filename 1_deploy_contracts.js (Note that the filename is prefixed with a number and suffixed by a description) and replace the contents with the following code:

Screen Shot 2021-10-05 at 9.10.47 PM.png

At the beginning of the migration, we tell truffle the contracts that we want to interact with using the artifacts.require() method, if you’re familiar with Node import statements this is similar to Node’s require. The name specified should match the name of the contract definition within the source file, hence FundRaiser

Every file in the migrations folder needs to export a function via the module.exports syntax. The function exported should accept a deployer object as it’s the first parameter. The deployer object helps with deploying by providing a clear syntax for the contract. 

Next, return back to your terminal and run truffle migrate or simply migrate if using the truffle develop console. If you see the following output, your contract was deployed successfully: 

Screen Shot 2021-10-05 at 9.11.51 PM.png

Next Steps: 

The next step would be to interact with our smart contract. The easiest way to do so would be to use Web3 to send transactions to the blockchain in order to interact with the smart contract. This will be covered in Chapter 4 where we walk you through using JavaScript to interact with your smart contracts. 

Previous
Previous

GoodLabs A.I. Syndrome Anomaly Detection System Selected by Canada’s Department of National Defence for Phase 2 $1MM Funding

Next
Next

NFTs for Comic Book Collectibles