Let’s Build Your First Smart Contract in Solidity and Truffle

Amazingandyyy
Etherereum Salon
Published in
6 min readJul 30, 2017

Everyone is talking about Blockchain/Ethereum/Smart Contracts, and the price of Ether just went up and down. In my opinion, to know the value of ether and to take the advantage of this Blockchain Technology, getting hands on and diving into the smart contract development is the best and most effective way. I hope this series of tutorials will inspire you and lead you to start learning and applying smart contracts.

After this extremely short tutorials, you can tell people,

I just build my first smart contract this morning! [plus learned to debug]

In this first tutorial, you will learn writing solidity language and truffle CLI tool to write and deploy a hello-world contract in a reasonably short amount of time.

All resources are available in my GitHub repo as following.

Before we build the exciting contract, let me share you something useful knowledge, in case your friends ask you.

What is Solidity? — An OOP programming language

Ethereum Network is a distributed, purely decentralized developer network. Developers can deploy and run an application on the network that others can execute it and make things work. The application is called smart contract, and the official programming language to write a contract is generally Solidity, an OOP-fashion programming language. You can think it’s like Swift for iOS and Kotlin for Android app.

What is truffle? — A CLI tool.

Truffle is a CLI helper tool, it helps developers’ life easier and make the development process faster.

Let’s get started.

The fast way to install solidity is using npm. It’s official package called solc. The installing process usually takes around 30~60 minutes… I know… it’s painful to wait, but you only need to do it once. (if you already have it installed, skip and go to next step)

$ npm i -g solc

Open a new tab/window in your terminal. Type npm install command to install truffle:

$ npm i -g truffle

create a project called hello-ethereum-salon-contract and go to the project

$ mkdir hello-ethereum-salon-contract
$ cd hello-ethereum-salon-contract

Make sure you are in the right directory/folder, then let’s use truffle init to init a project. Truffle is just a CLI tool which helps us generate some robotic code snippets following the official Solidity Style Guide .

$ truffle init 

Next, open the project/this folder in your favorite editor such as VSCode, Atom, Vim…etc. The structure of the project should now look like the following:

.
├── contracts
│ └── Migrations.sol
├── migrations
│ └── 1_initial_migration.js
├── test
├── truffle-config.js
└── truffle.js
3 directories, 4 files

Let’s just ignore all these files for now. Next, use truffle create contract -ContractName- to create a new contract. (Note, by convention, the contract name is Camel Case, for example, ItsNiceToMeetYouMyFriend)

$ truffle create contract HelloEthSalon

it should look like this now:

.
├── contracts
│ ├── HelloEthSalon.sol
│ └── Migrations.sol
├── migrations
│ └── 1_initial_migration.js
├── test
├── truffle-config.js
└── truffle.js
3 directories, 5 files

As you can see, there is a new file called HelloEthSalon.sol under the contracts folder and notice the extension of the file is called .sol and this is the file extension of solidity language, think like .js for JavaScript files or .html for HTML templates. This is what it looks like for the brand new HelloEthSalon.sol file.

pragma solidity ^0.4.4;contract HelloEthSalon {
function HelloEthSalon() {
// constructor
}
}

As we can see, only the contract name is special. We are good to go, let’s make some more exciting changes.

Let’s add two things:

1. add one string global variable called message and initialize it with a content as 'Hello Ethereum Salon!' .

2. add a new function called GetMessage() which will return the message

The file should now look like following:

pragma solidity ^0.4.4;contract HelloEthSalon {
string message = "Hello Ethereum Salon!";

function HelloEthSalon() {
// constructor
}
function GetMessage() return string {
return message;
}
}

Congratulations! You have your very first ever smart contract.

But in order to make it smart, we need to make sure this contract/solidity code is correct and will not break down. It’s easy, use truffle compile helper commands to check if everything is all right.

$ truffle compile

Boom! We got an error!

➜ truffle compile
Error parsing .../01-HelloWorld/contracts/HelloEthSalon.sol: ParsedContract.sol:6:3: ParserError: Expected token Semicolon got 'Function'
function HelloEthSalon() {
^
Compilation failed. See above.

truffle complain there is a ParserError on line 6 which is token Semicolon got ‘Function’ .

Nice catch! Let’s fix it by add the missing ; at the end of line 4. Yes, change ‘Hello Ethereum Salon!’ to ‘Hello Ethereum Salon!’;

do truffle compile again in your terminal;

Oh! Again! one more error! (no worries, we will fix it together.)

➜ truffle compile
Error parsing .../01-HelloWorld/contracts/HelloEthSalon.sol: ParsedContract.sol:10:25: ParserError: Expected token LBrace got 'Return'
function GetMessage() return string {
^
Compilation failed. See above.

compiler throws aParserError on line 10 and it's Expected token LBrace got ‘Return’. This is not a healthy and understandable report as we want, but I get it: from my experience and official document, the return should be changed to returns ~~ Nice catch again.

Let’s run truffle compile again in the terminal; Boom!

➜ truffle compile
Error parsing .../01-HelloWorld/contracts/HelloEthSalon.sol: ParsedContract.sol:10:33: ParserError: Expected token LParen got 'string'
function GetMessage() returns string {
^
Compilation failed. See above.

One more errors, but TRUST me, this is the last one in this tutorial. Well this will not be the last errors you will get (if you keep learning and doing development like a rock star )… so please don’t be afraid of these errors, Errors are the most healthy and good sign when you doing coding especially when you do smart contract development. When you do truffle compile , the compile helpfully, generously help us find out the critical error, isn’t it so handy? 😃

Let’s fix the ParserError by add parenthesis to the string (how I know to solve it like this? from previous experiences), so change function GetMessage() returns string { to be function GetMessage() returns (string) {

Let’s run truffle compile again in the terminal;

If you get no error messages(you may get many warnings messages, but they can be ignored here) and have it show Writing artifacts to ./build/contracts~

Congrats! We finish our first, no-bug, tiny, sweet, contract! Cheers! Go buy a beer and like this post :)

Nice! We get no errors now! The whole updated contract should be looked like following :)

You know we can deploy this contract to Ethereum network right now! Anyone on the planet can call this contract such as HelloEthSalon.getMessage() and it will get a result as Hello Ethereum Salon !

All resources are available in this github repo ->

Next, I will teach you how to

  1. run your contract and test it locally.
  2. deploy to testnetwork, and send it to your friends.
  3. deploy to live ethereum network, and send it to the world!

If you enjoyed this article, please share with your developer/blockchain friends and claps if this write-up is anyhow inspiring and helpful. Please feel free to comment if you encounter any errors and any feedback will be appreciated.

--

--