menu
arrow_back
Ethereum Simple Smart Contract with Solidity.
In today’s article we will look at an example of a simple Ethereum smart contract developed with Solidity.
  • 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.

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.

pragma

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 .

state variables

The second thing we do is create state variables. These variables are stored in the blockchain.

Memory

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 .

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.

abi.encodePacked()

In Solidity we cannot concatenate variables in an easy way, like in JavaScript or PHP with “+” or “.”, respectively. So, to get the greeting “Hello Chiyana”, to concatenate, we use abi.encodePacked (greetingPrefix, name)) as a helper function.

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 click on Deploy and you will see the result in the Remix console.

Deployed contracts

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.

  • 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.

conclusion

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.

 
 
 
keyboard_arrow_up