🚀 Guestbook Smart Contract on Conflux eSpace Testnet | Solidity Tutorial

Building a Decentralized Guestbook with Solidity on Conflux eSpace Testnet

Blockchain technology isn’t just for finance—it’s transforming how we interact online. One creative application is a decentralized guestbook, where messages are stored immutably on the blockchain for all to see. In this blog, we’ll walk through creating and deploying a DigitalGuestbook smart contract using Solidity, deploying it on the Conflux eSpace testnet with Remix, and interacting with it step-by-step.


What is a Decentralized Guestbook?

A decentralized guestbook allows users to leave public messages on the blockchain, timestamped and linked to their wallet address. Unlike traditional guestbooks, this smart contract ensures transparency and permanence, making it ideal for events, communities, or personal milestones. We’ll deploy it on the Conflux eSpace testnet, a high-performance blockchain network perfect for testing.


The Smart Contract Code

The DigitalGuestbook contract is written in Solidity (version 0.8.0) under the MIT license. It uses a Message struct to store each message’s sender address, content, and timestamp. All messages are kept in a public array for accessibility.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract DigitalGuestbook {
    // Define a struct for each message
    struct Message {
        address sender;
        string content;
        uint256 timestamp;
    }

    // Array to store all public messages
    Message[] public messages;

    // Event emitted when a new message is added
    event MessageAdded(address indexed sender, uint256 indexed id, string content, uint256 timestamp);

    // Function to add a new message
    function addMessage(string memory _content) public {
        uint256 id = messages.length;
        uint256 currentTime = block.timestamp;
        messages.push(Message({sender: msg.sender, content: _content, timestamp: currentTime}));
        emit MessageAdded(msg.sender, id, _content, currentTime);
    }

    // Function to get the total number of messages
    function getMessageCount() public view returns (uint256) {
        return messages.length;
    }

    // Function to get details of a specific message by index
    function getMessage(uint256 _index) public view returns (address sender, string memory content, uint256 timestamp) {
    require(_index < messages.length, "Message index out of bounds");
    Message memory message = messages[_index]; // Renamed the variable from `msg` to `message`
    return (message.sender, message.content, message.timestamp);
}
}

Key Features

  • Adding a Message:
    The addMessage function lets users submit a message. It assigns a unique ID based on the array length, records the current block timestamp, and emits a MessageAdded event with the sender’s address, ID, content, and timestamp.

  • Message Management:
    The getMessageCount function returns the total number of messages, while getMessage retrieves a message’s sender, content, and timestamp by index, with a safety check to avoid out-of-bounds errors.

  • Transparency:
    The public messages array and events ensure all actions are trackable.

This design creates a simple yet secure decentralized guestbook.


Deploying on Conflux eSpace Testnet with Remix

I used Remix, a popular online IDE for Ethereum smart contract development, to compile and deploy the contract. Here’s how:

  1. Connect MetaMask: Connect your MetaMask wallet to the Conflux eSpace testnet and obtain CFX test tokens from the faucet.
  2. Open Remix: Visit remix.ethereum.org, paste the contract code, and compile it.
  3. Deploy: Set the environment to Conflux eSpace testnet, deploy the contract, and confirm the transaction. You’ll receive the contract address upon success.

Interacting with the Contract

Let’s see it in action using Remix:

  • Add a Message: Submit a transaction to add a message, like “This contract is good.” The message is now on the blockchain.
  • Check Message Count: After adding one message, the count is 1, verifying the addition.
  • Retrieve Message Details: Use index 0 to get the sender’s address, content (“This contract is good”), and the timestamp of posting.
  • Verify Count: Call getMessageCount to confirm the total remains 1.

This demo highlights the contract’s real-time functionality.


Why Use Conflux eSpace Testnet?

The Conflux eSpace testnet provides a cost-effective, high-performance environment for testing smart contracts. Its scalability makes it a great choice for experimenting with decentralized applications.


Getting Started

To build your own:

  1. Set up MetaMask and connect to the Conflux eSpace testnet.
  2. Use Remix (remix.ethereum.org) to compile and deploy the contract.
  3. Follow the demo steps to interact with your guestbook.

Conclusion

Creating a decentralized guestbook is an excellent way to explore blockchain development. With Remix for deployment and the Conflux eSpace testnet’s capabilities, this project offers a practical introduction to decentralized apps. Whether you’re new to blockchain or a seasoned developer, this tutorial lays the groundwork for more advanced projects.

Video Tutorial: https://youtu.be/FsVKrkiwl9I?si=Lcg-TV9-V-hb6NlQ

GitHub repo: https://github.com/Vikash-8090-Yadav/Conflux-Tutorial/tree/main/Guestbook

Stay tuned for more blockchain tutorials, and share your guestbook messages in the comments!
Happy coding!

1 Like