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 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.
Now we write the 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:
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
).
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.
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!
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
- Copy Trading | Crypto Tax Software
- Grid Trading | Crypto Hardware Wallet
- Crypto Telegram Signals | Crypto Trading Bot
- Pionex Dual Investment | AdvCash Review | Uphold Review
- 8 Best Cryptocurrency APIs for Developers
- Uphold Card Review | Trust Wallet vs MetaMask
- Earn Sign-up Bonus — 10 Best Crypto Platforms
- Best Crypto Exchange | Best Crypto Exchange in India
- Best Crypto APIs for Developers
- Best Crypto Lending Platform
- An ultimate guide to Leveraged Token