Solidity Programming: The Blockchain Messenger Project

In this article, we'll create a smart contract that is can store a string on the blockchain, is readable by everyone, and is writeable by the person that deployed the smart contract.

2 years ago   •   2 min read

By Peter Foy

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:

Stay up to date with AI

We're an independent group of machine learning engineers, quantitative analysts, and quantum computing enthusiasts. Subscribe to our newsletter and never miss our articles, latest news, etc.

Great! Check your inbox and click the link.
Sorry, something went wrong. Please try again.

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 the changeCounter
//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.

Resources

Spread the word

Keep reading