By the end of this lesson, you will be able to:
In this lesson, we provide our readers a detailed guide on how to write a simple Hello World
smart contract based on the Solidity programming language. The smart contract has a simple functionality of returning a single string value as output.
This is a hands-on guide, we encourage our readers to perform these tasks along for a better understanding. Before starting off with the practical demo, it is necessary to make sure you have the following software requisites installed.
Make sure you have set up the dev environment, by following the previous lesson and have Nodejs, Truffle, Metamask installed.
Within the contracts/bsc
folder rename the SimpleStorage.sol
to HelloWorld.sol
. Remove the contents of the file, we will use this file for storing our smart contract. Here, .sol
denotes that this is a Solidity file. We will be writign our smart contract in teh Solidity programming language which is one of the most widely used language for smart contract development.
Copy and paste the following code into the HelloWorld.sol
file.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract HelloWorld {
string public greet = "Set a name please"; //variable for storing name
/** @dev Retrieve Message to Print
* @return The Message to Print, Hello, Concatenated with the User Name
*/
function getMessage() public view returns(string memory){
return concat("Hello, " , greet);
}
/** @dev Set the Name to Greet
* @param _name user name
* @return success Returns bool value (True or False) to indicate if save was successful or not
*/
function setName(string memory _name) public returns(bool success){
require(bytes(_name).length > 0);
greet= _name;
// accounts[msg.sender] = _name;
return true;
}
/** @dev Set the Name to Greet
* @param _base contains the base value " Hello, "
* @param _value contains the name to append to message to display
* @return the concatenated string of _base+_value i.e. Hello, Name
*/
function concat(string memory _base, string memory _value) internal pure returns (string memory) {
bytes memory _baseBytes = bytes(_base);
bytes memory _valueBytes = bytes(_value);
string memory _tmpValue = new string(_baseBytes.length + _valueBytes.length);
bytes memory _newValue = bytes(_tmpValue);
uint i;
uint j;
for(i=0; i<_baseBytes.length; i++) {
_newValue[j++] = _baseBytes[i];
}
for(i=0; i<_valueBytes.length; i++) {
_newValue[j++] = _valueBytes[i];
}
return string(_newValue);
}
}
pragma
is used for specifying the Solidity compiler version to be used with our smart contract.greet
that will used for storing names and the getMessage
function is used for returning a greeting message with the name stored in the greet
variable, e.g., "Hello, Maryam". The setName
function is used for setting user desired name to greet
. Whereas, the concat
function is utitlity function used for concatenating the message Hello,
with the name stored in the variable greet
.In this lesson, we provided a detailed guide on how to write a simple Hello World
smart contract based in the Solidity programming language. In the upcoming lessons, we guide our readers on how to compile and deploy this smart contract onto the BNB Smart Chain test network.