How To: Write a Simple Smart Contract

Morgan Fogarty
Apr 26, 2018 · 4 min read

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.

Discover and review best Blockchain api and node products

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:

contract Hello {
string public message;
function Hello(string initialMessage) public {
message = initialMessage;
}
function setMessage(string newMessage) public {
message = newMessage;
}
}

Coinmonks

Coinmonks is a non-profit Crypto educational publication.

Sign up for Cryptoanarchy from Coinmonks

A newsletter that brings you week's best crypto and blockchain stories and trending news directly in your inbox, by CoinCodeCap.com

Create a free Medium account to get Cryptoanarchy in your inbox.

Morgan Fogarty

Written by

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

Coinmonks

Coinmonks

Coinmonks is a non-profit Crypto educational publication. Follow us on Twitter @coinmonks Our other project — https://coincodecap.com

Morgan Fogarty

Written by

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

Coinmonks

Coinmonks

Coinmonks is a non-profit Crypto educational publication. Follow us on Twitter @coinmonks Our other project — https://coincodecap.com

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

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