Solidity is the programming language used for developing a smart contract over Ethereum blockchain. Here, in this article, we will learn the basics of Solidity and we’ll also write our first smart contract of "Hello World".
Solidity is a contract-oriented programing language; bear in mind, it’s not object-oriented. Each smart contract has its own state. By using a state, you can store the data to the blockchain. However, storing data is not free; it costs some Gas. Thus, whenever you write the code on the public blockchain, keep in mind, each block of your code should consume a minimum amount of Gas. If it’s not clear at this point, don’t worry, we will take a look at it later with examples.
Initially, there is no setup required to start with Solidity. Ethereum provides an online IDE called Remix, on which you can develop and deploy your smart contract. For learning purpose, we will use the same throughout this series.
Remix Introduction
When you open the Remix in your browser, you will see the screen as below.
Click here to see the image in full width
There are four different sections.
1. File explore
Using file explorer, we can manage our contract files. Note that by default, all the files are stored in your browser, hence, clearing the browser storage will permanently delete all the Solidity files that you added. Explorer has other options, such as exporting all the contract files to GitHub and copying all files to another instance.
2. Code Editor
Code Editor is your main section where we’ll write the Solidity code. The Editor re-compiles the code each time the current file is changed. It also provides syntax highlighting and font-size option.
3. Terminal
It displays the log/debugging information while you interact with Remix. You can trace the information of transaction you made or see the event info there.
4. Deploy And Test
The fourth section contains various options for analysis, compilation, deployment, and testing of your smart contract. Here, we have an option of auto-compile. If you enable that option, it will compile the smart contract every 5 seconds. We’ll see the usage of options while developing a smart contract.
Write Your Contract
First, we will remove the existing default contract files (i.e. ballot.sol and ballot_test.sol) and add a new file with the name myContract.sol with the following code.
pragma solidity ^0.5.0;
contract helloWorld {
function printHelloWorld() public pure returns (string memory) {
return 'helloWorld';
}
}
Here, we have created one smart contract named helloWorld. Let’s understand the contract structure in brief.
Version Pragma
Before going to write any code, we should annotate the source file with the compiler version. It will let the system know that the code you are going to write should compile with the specific compiler, so, your code won’t break even if future versions might have incompatible changes.
The pragma version is used as below.
pragma solidity ^0.4.0;
Such source files won’t compile earlier than the version 0.4.0. Here, (^) is indicating the upper version, it means the following code can be compiled on 0.4.0 or 0.4.x but it should not compile on 0.5.0 or later. There are other different methods to write the pragma version. For better clarity, you can also write as below, which works the same as described.
pragma solidity >=0.4.0 <0.5.0;
Contract Structure
contract [contract name] {
function [method name]()
[visibility specifiers] [modifiers] [returns] ([return type(s)]) {
//code here
}
...
..
}
Deploy Contract on Remix
To deploy the smart contract, go to the “Run tab” right upper corner, and click on the deploy button. Make sure right contract is selected if there are multiple contracts present.
Click here to see the image in full width
As there is no deployed contract at this point of time, “Deployed Contract” section has no information.
Also note we’re going to execute this contract on JavaScript VM, which is the sandbox blockchain environment and it won’t be persisted in any kind of state information. Page refresh will start a new blockchain again from the first.
Click here to see the image in full width
Once we deployed our smart contract, that section will be updated with contract information, and you should see the method there. The terminal is now updated with the information of the transaction that we have made. You can also track gas amount, contract address etc. from the terminal.
Deployment cost
Anything that changes the state of blockchain will cost some amount, here in our example before deployment we had 100 ether in my account, and some ether has been used during deployment.
Run Contract on Remix
Next to run our method, click on printHelloWorld text which appears as a button.
Click here to see the image in full width
Congratulations! 🎉 you have developed and deployed your first smart contract successfully.
In this article we have discussed how to use Remix IDE and created our first smart contract, in the next article we will explore more about Variables And Types In Solidity.