Today begins your adventures with Solidity, the smart contract programming language invented for the Ethereum blockchain. Solidity allows your to harness the power of a global shared computer. Anyone can add programs and call functions on this computer. You can upload anything you want to this computer and nobody is going to stop you as long as you follow the rules of the network. Ethereum’s website says the network “allows you to build applications that run exactly as programmed without any possibility of downtime, censorship, fraud, or third-party interference.” Lets get started!
To get started you will write a “hello world” program in Solidity. This simple contract will store a string and have a couple functions for updating and reading the string. You will get a feel for how to use Remix, the Ethereum smart contract web IDE. Remix is a great tool for beginners because you do not have to install anything to get started. You can dive in right away.
- Go to https://remix.ethereum.org/
- Click the + sign in the top left corner
- Type “hello.sol” (without quotes) and then click Ok.
- Then paste the code below for the hello contract we will write
This is the pragma statement for Solidity. This lets compiler know what version of Solidity to expect. As Solidity evolves, it will usually make backwards compatible changes for the last number. The second number will change when they make backwards incompatible changes. It lets future readers know how old the code is they are reading.
This declares a contract named hello. Contracts store data and functions that modify the data they hold. They are similar to classes in other programming languages.
This holds a string, which is a list of alphanumeric characters that changes its size dynamically to fit whatever string it is holding.
The constructor function is called when the contract is first created. This constructor takes one argument, a string named _message.
This function returns the string that we set earlier. The view keyword means the function only reads data. It does not modify data.
This function sets our message to whatever was passed to it.
Hello world in action
First, we need to compile the contract. Under the compile tab, click compile. It should look like this:
Next, we will deploy the contract. Select hello from the list of contracts and type “hello world” for the argument:
To verify it is working, we can click getMessage to see the message we set:
Setting hello friends
The next thing we can do is set a new message: “hello friends”:
Finally, we can click getMessage again to see the new message:
You now have the basic information you need to get started with Solidity. There is much more to learn, but hopefully this is enough to get you started. There are more links at the bottom to continue your Solidity adventure. Good luck out there!
- Build an Unstoppable Counter with Solidity - Build an unstoppable counter! You can keep counting and counting. What will you do with this power?
- Creating a Virtual Time Capsule in Solidity, shows you how the payable modifier works in Solidity, the Ethereum smart contract programming language.