Published in


How To: Write a Simple Smart Contract

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 . Navigate into and type . Answer the prompts (or simply press return a few times) until your Node project is created. Create a folder called .

Inside the folder, create a file called ( 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 in the code block of our contract, we also specify that it is a publicly visible variable and its type is .

is a variable (also a state variable), which means that it defines the contract’s state and is only changed by function invocations (we’ll get into the difference between and 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 ).

constructor function

If a contract has a constructor function (as ours does — ), 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 function will allow a user to change the value of from its initial value. Both the function and the constructor function have 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:

pragma solidity ^0.4.21;contract Hello {
string public message;
function Hello(string initialMessage) public {
message = initialMessage;
function setMessage(string newMessage) public {
message = newMessage;

Join Coinmonks Telegram Channel and Youtube Channel get daily Crypto News

Also, Read



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Morgan Fogarty

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