Quote dApp
We will create a smart contract to save a message or quote on the Blockchain.
The smart contract is called Quote.
We will use Remix to create the smart contract.
Go to remix.ethereum.org
Create a smart contract
- Click on the second button on the left side - file explorer
- Click on the button create a new file
File name: Name.sol
Copy and paste the smart contract inline below:
// SPDX-License-Identifier: MITpragma solidity 0.8.1;contract Quote {address public owner;string private message;mapping (address => bool) public whiteList;constructor() {owner = msg.sender;whiteList[msg.sender] = true;}modifier onlyOwner {require(msg.sender == owner,"Only owner");_;}modifier onlyWhitelist {require(whiteList[msg.sender] == true, "Only whitelist");_;}function setQuote(string memory _message) public onlyWhitelist {message = _message;}function getQuote() public view returns (string memory) {return message;}function addMember (address _member) public onlyOwner {whiteList[_member] = true;}function delMember (address _member) public onlyOwner {whiteList[_member] = false;}}
Quote.sol
Let's understand the smart contract.
Variables
owner
address public owner;
Address of the creator of the smart contract;
message
string private message;
Message stored in the Blockchain.
whiteList
mapping (address => bool) public whiteList;
Address list defining who can update the variable message.
Constructor
Function that is runned ONLY during the smart contract deploy.
owner = msg.sender;whiteList[msg.sender] = true;
We are initializing variables.
msg.sender is the address that sent the transaction to create the smart contract.
- We store the msg.sender in the owner variable.
- We add in the whiteList, that is, we say that it is authorized, with the Boolean value true.
Modifier
The modifier defines requirements that will be automatically checked before executing a function.
It is very common to use the require command within a modifier.
onlyOwner
modifier onlyOwner {require(msg.sender == owner,"Only owner");_;}
This modifier checks if the person who is sending the transaction is the owner of the smart contract. If not, the error message Only owner is sent.
onlyWhitelist
modifier onlyWhitelist {require(whiteList[msg.sender] == true, "Only whitelist");_;}
This modifier verifies that whoever is sending the transaction has the value true in whiteList. If not, the Only whitelist error message is sent.
If the address is not previously initialized in the whitelist, the value default is false.
Message functions
These are the functions related to the message stored in the smart contract.
setQuote
function setQuote(string memory _message) public onlyWhitelist {message = _message;}
Function to record a message, that is, update the message variable.
Uses the onlyWhitelist modifier, that is, only those on the whitelist can change the stored message.
getQuote
function getQuote() public view returns (string memory) {return message;}
Function to return the message saved in the variable message.
Member administration
We have functions to manage the whitelist, that is, to define who is and who is not in the whitelist.
These functions are controlled by the onlyOwner modifier, that is, only the creator of the smart contract can administer the whitelist.
addMember
function addMember (address _member) public onlyOwner {whiteList[_member] = true;}
Defines that for a given address, which is the key, the value true will be stored.
delMember
function delMember (address _member) public onlyOwner {whiteList[_member] = false;}
Defines that for a given address, which is the key, the value false, which is the default value, will be stored. If at any time the address had the value true associated with it, it is necessary to set the value false so that it no longer has access.
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.
If you haven't enabled it, perform the following steps:
- Click on the 3rd button at the left side, selecting Solidity compiler
- Click on the button
Compile Register.sol
. - Check the green signal on the 3rd button with the message
compilation successful
.
Requirements
- Selected the RSK network selected the web wallet, Nifty or Metamask.
- Have tR-BTC in your wallet.
- On Remix, at Deploy and run transactions, under Environment, make sure you have selected the Injected Web3 option.
Deploy
In Remix, on the left side, locate the fourth button: Deploy and run transactions.
For now we have only one smart contract, so it is automatically selected in the dropdown list.
Click the button Deploy.
It will open a popup window on the web wallet, to confirm the transaction set up by Remix to publish the smart contract.
Click confirm / submit.
At bottom right, we can check the message: creation of Name pending...
A block in the RSK network takes, on average, 30 seconds to be mined, so we must wait for this time.
Smart contract address
Copy the address of your smart contract and save it. It will be used at other times during the course.
There are a few ways to copy it:
- No Remix
After the smart contract is published using Remix, we can see your instance in the left panel, at the bottom of deploy and run transactions.
Go to the section deployed contracts, locate the smart contract and click the copy icon.
This is the address of my quote smart contract:
0xF2FD988A92484ff16Edd5BD9DAbE5eb809c5B3Dd
Interact with the smart contract
We have already interacted with the smart contract published in JavascriptVM, and we can also use Remix to interact with a smart contract published on any network.
The only change is the Environment, that is, the connected network, which is now Injected Web3.
In deployed contracts section, in the smart contract, click the symbol > to see the datails.
Final considerations
Congratulations!
You published the smart contract Quote on the RSK Testnet Blockchain and interacted with it.