How To: Write a Simple Smart Contract

Morgan Fogarty
Coinmonks
4 min readApr 26, 2018

--

We’re going to write a simple smart contract using the Solidity programming language, and in the following posts we’ll test and deploy it to a test network.

A Solidity syntax highlighter is not necessary but makes the code look prettier so grab one if you want one. (Atom, VsCode, Sublime, VIM, Webstorm)

First, we will initialize a Node project. If you do not have Node installed yet, please do so. If you need directions, go to the npm website.

After you’ve installed Node…

In your terminal, navigate to the directory where you’d like to put your project and create a new project called hello . Navigate into hello and type npm init. Answer the prompts (or simply press return a few times) until your Node project is created. Create a folder called contracts.

Inside the contracts folder, create a file called hello.sol (.sol is the Solidity file extension). At the top of the file, declare the version of Solidity you’d like to use. We declare the version so that the compiler will know how to perform.

P.S. There’s a copy/paste-able version of this smart contract at the bottom of this post.

version 0.4.21 now supported

Now we write the contract.

simple smart contract

We’ll get deeper into syntax in future posts. In the meantime, here are a couple things to remember about the Solidity language:

Contracts are declared with an uppercase first letter. They act like classes in Ruby, Java and Python. Solidity is a statically or strongly typed language. In these languages, variable types are checked at compile time, rather than at run-time (dynamically typed). For example, if a variable is declared a string, and later we try to pass it as an integer, an error will be raised.

Here are a few things specific to this smart contract:

storage variable

When we declare the variable message in the code block of our contract, we also specify that it is a publicly visible variable and its type is string.

message is a storage variable (also a state variable), which means that it defines the contract’s state and is only changed by .send function invocations (we’ll get into the difference between .send and .call methods in a future post). It also has an automatic getter method generated for it (if you’re familiar with Ruby, this is similar to attr_reader ).

constructor function

If a contract has a constructor function (as ours does — Hello ), its name will match that of the contract itself. Each time an instance of the contract is deployed, the constructor function will be invoked automatically. Only one constructor function per contract is allowed.

just a cool function that changes the content of the message

The setMessage function will allow a user to change the value of message from its initial value. Both the setMessage function and the constructor function have public visibility. This means anyone can call the function once the contract is deployed — not just the owner of the contract.

Congrats! You wrote a smart contract!

tiny beautiful smart contract

Next, learn How To: Compile a Solidity Smart Contract Using Node.js

Copy/paste-able code with latest Solidity version:

Join Coinmonks Telegram Channel and Youtube Channel get daily Crypto News

Also, Read

--

--

Morgan Fogarty
Coinmonks

I’m just a girl, standing in front of some code, asking it to love her.