We come to the last post in our series on Blockchain and Blockchain development. In today’s article we will look at an example of a simple Ethereum smart contract developed with Solidity.
To do this we will use the erc20 token generator Remix IDE , and we will take advantage of the code to review some of the properties and features that we have been looking at these weeks.
Before proceeding, I recommend you read the previous articles related to Blockchain .
New Smart Contract with Remix
If you enter Remix for the first time, you will see that the platform loads some sample contracts and scripts by default . Inside the “contracts” folder you can edit one of the “.sol” files. “.sol” is the default extension for Solidity files.
You can rename it, for good practice, remove all the code from the file and start writing your own, or add the code from our example:
What does the above code do?
I won’t go into too much detail, because we already know what a Solidity source file looks like, but we will discuss what this code does and compile it to see the interaction.
Our example contract is a simple input/output contract. What it will do is create and return a greeting.
As you well recall, after declaring the license of the contract, Solidity requires the version (or versions) on which the contract will run to be declared, using pragma .
The second thing we do is create state variables. These variables are stored in the blockchain.
As you can see, from the code, Solidity is a strongly typed language, so we erc20 token development had to declare the type of the variable(s). In this case string . We also declare the access modifier as public. If we had declared private it would still be visible on the block chain.
In the constructor , in addition to the type string , we declare memory. It turns out that in Solidity, variables can be stored in different ways. The three options for storing variables are “ storage ”, “ memory ”, and “ calldata ”. You can learn more about them here .
You will see that “memory” is a type of variable that is in temporary memory and is removed when the function is exited. It is enough for the variables in our example.
View in getGreeting()
In the getGreeting() function we also declare view. If you remember last week’s article about function modifiers , view and pure are two of the most commonly used modifiers. In this case view means that this function will not change any data in the block chain.
Compile and deploy with Remix
Solidity is a compiled language like Java or C and Remix allows us to compile it. You can do it by clicking on the “Compile 3_Greeting.sol” button.
Then, in the sidebar, access “Deploy & run transactions”. In this case we do not deploy in a real blockchain, but in a test environment (sandbox).
Before clicking on “Deploy” you can add an “initialName”, since the constructor receives an initialName .
Then click on Deploy and you will see the result in the Remix console.
In Deployed contracts you will see your newly deployed contract. You will also see all the functions of the contract. erc20 development In addition Remix provides you with getters for declared states.
And if you click on “name”, for example, you will see the result that you entered before clicking on “Deploy”.
If you click on “getGreeting”, you will receive the expected greeting:
Set a new name
If you add a new name, for example “Sergio”
and click again on getGreeting you will see that the name of the greeted person has been changed.
As you can see, everything has worked correctly. Obviously this was a simple, simple, very basic example, but I think it will allow you to learn and get started, and familiarize yourself with the structure of a smart contract, as well as encourage you in the creation of smart contracts. In real life they will be much more complex and robust.