Solidity is a language for creating Blockchain smart contracts. You can use it to create smart contracts, on more than 200 Blockchain networks, including Ethereum. The syntax looks like Javascript, but it works in a very different way.
To get started with Solidity, you go to Remix, an online IDE for smart contracts. You go to the file editor, and you create a new file for your smart contract. The file extension for solidity is sol. You specify the version of Solidity in the pragma statement:
pragma solidity 0.8.9;
You define a new smart contract with the contract keyword, and the name of your smart contract:
pragma solidity 0.8.9;
contract MyContract {}
You can define variables, and their value will be stored on the Blockchain. To define variables, you have to specify their type.
pragma solidity 0.8.9;
contract MyContract {
uint a;
}
Solidity is what we call a statically typed language, which is different from Javascript. To interact with these variables, you can create functions. To read a variable, you can define a read-only function. The function signature is slightly more complex compared to Javascript. In the function body you return the variable, and that’s it:
pragma solidity 0.8.9;
contract MyContract {
uint a;
function read() external view returns(uint) {
return a;
}
}
You can also define functions to modify the data of your smart contract. The function signature is a little bit different. And inside you can modify the variables of your contract, and the new value will be saved in the Blockchain:
pragma solidity 0.8.9;
contract MyContract {
uint a;
function read() external view returns(uint) {
return a;
}
function write(uint _a) external {
a = _a;
}
}
Solidity is what we call a compiled language, which means you cannot run its code directly, contrary to Javascript. For the compilation step you go to the compilation menu of Remix. You select the correct version of Solidity. And you click on compile. Now your Solidity code has been compiled into what we call the EVM Bytecode, a series of instructions that can be understood by the Blockchain.
The next step is to deploy your smart contract, and interact with it. For this, you go to the deployment tab pf Remix. You select the contract. You click on deploy. And after you can see the contract instance. In the box of the contract instance on Remix, you can interact with your contract by calling the different functions. It’s deployed on a local Blockchain, a safe sandbox where you can make mistake and experiment.
That was a very quick introduction to Solidity. If you want to dive deeper in Solidity, I also have a full Playlist on Solidity. The current version of Solidity is 0.8, and this series uses 0.5, but it’s fine because the language didn’t change that much. I will see there.
Leave a Reply