Create Your First Smart Contract
Now we are creating a new file and inserting some Solidity code. Donāt worry if you donāt fully understand everything - we have to start somewhere and weāre here to play around. Just follow along, I promise everything will be clear later on!
Create A New File
Click on the plus icon in the code-editor and create a new file, name it āMyContract.solā. The sol-extension stands for Solidity.
Add Solidity Hello World Code
Now add the following code to the file:
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.1;
contract MyContract {
string public myString = 'hello world';
}
It should look like this and the āCompilerā Plugin should have a green checkmark badge. Thatās the icon in the left side panel:
This is a very basic version of a Smart Contract. Letās go through it line by line:
// SPDX-License-Identifier: GPL-3.0
: The The Software Package Data ExchangeĀ® (SPDXĀ®)ā identifier is there to clearly communicate the license under which the Solidity file will be made available. Well, if you make it available. But you should. Smart Contracts transparency and trust greatly benefit from the source being published and sometimes itās not 100% clear under which license the source is out in the wild. The SPDX identifier is optionalā, but recommended.
pragma solidity ^0.8.1
: The pragma
keyword is for the compiler to enable certain features or check certain things. The version pragma is a safety measure, to let the compiler know for which compiler version the Solidity file was written for. It follows the SemVer versioning standardā. ^0.8.1 means >=0.8.1 and <0.9.0.
contract MyContract
: Thatās the actual beginning of the Smart Contract. Like a Class in almost any other programming language.
string public myString = 'hello world'
: That is a storage variable. Itās public and Solidity will automatically generate a getter function for it - youāll see that in a minute!
Perfect! Letās proceed to deploy this Smart Contract to a Blockchain!