Skip to Content

👨🏻‍🎓 Register for live classes

Smart Contract Name

Your first smart contract will have your name registered on it, associating it to your wallet created in the last module.

It will be used to you subscribe to RSK Academy and let we know your name.

In the future, it will also be used in other projects of the course.

At different points in the course, we used the "do it first and then understand what was done" approach, so that the student has the pleasure of programming and understanding in practice, with less exclusive theorical time. This lesson will happen this way.

Let's use Remix to create your first smart contract!

Go to remix.ethereum.org

In the last lesson, the basics of Remix have already been explained, so let's directly create a new file.

Create a smart contract

  1. Click on the second button on the left side - file explorer
  2. Click on the button create a new file

create a new file

File name: Name.sol

filename Name.sol

Copy the smart contract inline below:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.1;
contract Name {
string private name;
address public owner;
constructor() {
owner = msg.sender;
name = "Your fullname";
}
function getName() public view returns (string memory) {
return name;
}
}

Paste it into Remix here:

Name.sol

Name.sol

This smart contract has:

  • A private variable name to store your name
  • A public variable owner to store the smart contract owner address
  • constructor, which is executed only in the moment of publish the smart contract
  • A function getName() to return the name stored at variable name

Update your name

Inside the constructor, on line 10, change the text Your fullname for your fullname.

For example, I will change it to Solange Gueiros:

name = "Solange Gueiros";

This is the result:

name Solange Gueiros

Compile a smart contract

If you enabled auto-compile, the smart contract is already compiled and a green light will appear next to the third button from the left - Solidity compiler.

compilation successful

If you haven't enabled it, perform the following steps:

  1. Click on the 3rd button at the left side, selecting Solidity compiler
  2. Click on the button Compile Register.sol.
  3. Check the green signal on the 3rd button with the message compilation successful.

Compile Name.sol

Publish on JavascriptVM

First of all we will deploy the smart contract on JavaScriptVM environment, a Blockchain simulator that takes place in the browser memory.

In the left side panel, go to the button Deploy and run transactions.

Deploy and run transactions

For now we have only one smart contract, so it is automatically selected in the dropdown.

Click the button Deploy.

Deploy

At bottom right, we can check the message: creation of Name pending...

And soon after, the transaction confirmed.

transaction confirmed

Click on the transaction line or the debug button (at the right side) to see more details of the transaction:

transaction details

Interact with the smart contract

When a smart contract is deployed with Remix, we can see it in the left panel under Deploy and run transactions:

deployed contracts

Click on the symbol > to expand the details of smart contract Name:

Deployed Name

A getName button will appear, corresponding to the function we created in our smart contract!

Also an owner button.

But ... we didn't create an owner function. Where did this button come from?

Whenever a variable with public visibility is created, Solidity creates a "getter" for her, that is, a function that returns its value.

This "getter" has its own variable name. So, in this case, it is the owner button.

The blue buttons are functions which are read-only and it does not change anything stored on the Blockchain. We do not need to expend gas when using them.

getName

First of all, we will check the name stored at deploy.

Click the getName button

The result is the name defined inside the constructor. In my example:

getName

At bottom right, we can check that it was a call to Name.getName() function:

call getName

Clicking on the transaction line or the debug button (at the right side), you can see more details of the transaction:

call getName details

owner

The owner button returns the address of the person who published the smart contract. It was defined inside the constructor, such as msg.sender, which is the address used to send the transaction to create the smart contract.

Click the owner button

owner

See the list of accounts. You can check that the returned account is first on the list.

first account

In the message area on the right, we can see that the Name.owner() function has been called:

call owner

Like we said before, to see more details of the transaction you can click on the transaction line or the debug button (at the right side):

call owner details

Final considerations

Did you imagine that it was so easy to use Remix to create a smart contract?

We will soon publish it on a "real" Blockchain network :)