Smart Contract Development - Hello World
Overview
In this post, I’ll be covering how to get a local dev environment setup and configured for doing smart contract development with Ethereum, and also a quick hello world app to get our very first smart contract under the belt
Prerequisites
You’re going to need to download and install the following.
Visual Studio Code
https://code.visualstudio.com/download
Visual Studio Code Solidity extension
https://marketplace.visualstudio.com/items?itemName=JuanBlanco.solidity
Ganache
Ganache is the local development blockchain emulator.
I’ve gone with the UI version for this post, but there is also a CLI version available.
https://www.trufflesuite.com/ganache
Node.js
For Node.js you can install it directly, but I would suggest that you use something like node version manager to manage multiple node version installations. I’m using version 16.11.1 currently.
Truffle
Truffle describes itself as “A world class development environment, testing framework and asset pipeline for blockchains using the Ethereum Virtual Machine (EVM), aiming to make life as a developer easier.”
To install truffle, open a terminal window and run
npm install truffle -g
Alright, now that all that’s done, let’s move on to creating our first smart contract.
Hello World
Start Ganache
First up, we need to start our local blockchain.
Once Ganache has started, on the Create a workspace
screen, select the Quickstart
button.
This will launch you onto the main app and display a list of wallet addresses.
Initialise the project
Open a terminal window, navigate to a folder for your source code and run
|
|
Now, from within the visual studio terminal, run the following.
|
|
You will end up with a folder structure like this.
Compile the project as a quick smoke test.
|
|
Configure Ganache
We need to configure truffle to use our local development blockchain.
Edit the truffle-config.js
file
Uncomment the networks/development
json node and its contents.
Update the port number to the port your Ganache app is running on, you can see this on the Ganasche UI under the RPC SERVER
title.
The result should look something like this (Note, this is just a partial excerpt)
|
|
While we’re in the truffle config, let’s also set up the solidity language version we will be using.
Uncomment the compilers/solc/version node, and set its value to >=0.7.0 <0.9.0
The result should look like this.
|
|
Define a test
Before we create our smart contract, lets define a test.
If you’ve defined any sort of JavaScript tests in the past, this should all be very familiar.
I think it might be mocha, but I’m not sure.
The test below checks that our (yet to be defined) greeting variable is populated when our smart contract is deployed. The test then verifies that the greeting can be updated.
Create a new file inside the tests folder named helloworld.js
Place the following code into the new file
|
|
Running the test
run truffle test
Hey! errors, just what we wanted.
The Smart Contract
Finally, let’s write that smart contract!
Create a new file within the contracts folder named HelloWorld.sol
Add the following code.
|
|
Assuming you’ve done some development in the past, this will be easy to grok; however, there are some things worth mentioning.
- The licence line is optional but encouraged.
- All contracts are defined within a contract - similar to a class.
- The
pragma
line is required, and defines the version of solidity being used.- This needs to align with what you configured in the
truffle-config.js
file earlier.
- This needs to align with what you configured in the
- Public variables, like
greeting
, are automatically given agetter
method. - The
memory
data annotation is needed for reference types- In this case, it means that the parameter itself does not need to be persisted to the blockchain state.
- If you want something to be persisted to the blockchain, you use the
storage
annotation.
Okay, let’s give that test another try and see what happens now.
Run truffle test
again
Okaaaay, the error has changed, but why are we still getting an error??
This brings us to the migrations.
Contract migrations
We need to create a migration for our new smart contract to publish it to the blockchain.
These migrations are similar in concept to database migrations like Entity Framework.
Create a new file named 2_deploy_contract.js
in the migrations folder and add the following code.
|
|
Now, once again, let’s run truffle test
ayyyy!! success! Congratulations, you just created, deployed and tested your very first smart contract.