In this article, we're going to put together the fundamental concepts discussed in previous Solidity programming tutorials and create a "blockchain messenger" smart contract.
The blockchain messenger contract can store a string on the blockchain. It is readable by everyone, but is writeable only by the person that deployed the smart contract.
The contract will also tell us how many times it has been updated.
This article is based on notes from this Blockchain Developer course - you can find previous tutorials that this project builds on below:
- Creating Your First Smart Contract with Solidity
- Reading and Writing Smart Contract Data
- Understanding Booleans and Integers
- Strings, Bytes, and Address Types
- Understanding Function Types and the Constructor
Stay up to date with AI
Blockchain Messenger Project
To start, we'll create a new file in Remix called BlockchainMessenger.sol
and copy the license and pragma line from previous contracts.
We'll start the BlockchainMessenger
contract with a public uint
called changeCounter
. Next, we'll create:
- A public address owner
- A public string with
theMesssage
- A
constructor
that will set the owner to the message sender - A function to update the string memory publicly if the
msg.sender == owner
and increment thechangeCounter
//SPDX-License-Identifier: MIT
pragma solidity 0.8.15;
contract BlockchainMessenger {
uint public changeCounter;
address public owner;
string public theMessage;
constructor() {
owner = msg.sender;
}
function updateTheMessage(string memory _newMessage) public {
if(msg.sender == owner) {
theMessage = _newMessage;
changeCounter++;
}
}
}
Now if we deploy this, we see that we are the owner, the message is nothing initially and the change count is 0.
We can now update the message to "Hello World!" and see the updated message and count:
If we switch to a different account and update the message, we can see that it goes through, but our message and change counter remain the same this account is not the owner:
Summary:
That's it for this very simple blockchain messenger project.
The goal of this project was to familiarize ourselves with fundamental Solidity programming concepts such as:
- Booleans & integers
- Addresses
- Functions
- Data types
- Compilation and deployment of smart contract
Now that we have the basics, in the following tutorials we'll move on to more advanced Solidity programming and smart contract.
This will include moving on from the sandbox environment, using test networks, sending ether between wallets, and more.