YangAndEcho

Description:

Smart contract deployed on Ethereum.

Blockchain: Ethereum

Source Code: View Code On The Blockchain

Solidity Source Code:

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

/// @title YangAndEcho - Memory Contract (Records meeting date, marriage date, and daily messages)
/// @author ChatGPT
/// @notice Stores memorable dates and messages, provides functions to check how many days have passed since meeting and marriage, and allows writing/retrieving messages.
contract YangAndEcho {
    // Fixed memorable dates (UTC timestamps)
    // 2021-01-01 00:00:00 UTC = 1609459200
    uint256 public constant meetingTimestamp = 1609459200;
    // 2017-01-01 00:00:00 UTC = 1483228800
    uint256 public constant marriageTimestamp = 1483228800;

    struct Message {
        address author;
        uint256 timestamp;
        string text;
    }

    Message[] private messages;

    event MessageWritten(address indexed author, uint256 indexed index, uint256 timestamp, string text);

    /// @notice Returns the number of days since the meeting date (2021-01-01)
    /// @return daysCount Number of days (rounded down)
    function daysSinceMeeting() public view returns (uint256 daysCount) {
        require(block.timestamp >= meetingTimestamp, "current time is before meeting date");
        return (block.timestamp - meetingTimestamp) / 1 days;
    }

    /// @notice Returns the number of days since the marriage date (2017-01-01)
    /// @return daysCount Number of days (rounded down)
    function daysSinceMarriage() public view returns (uint256 daysCount) {
        require(block.timestamp >= marriageTimestamp, "current time is before marriage date");
        return (block.timestamp - marriageTimestamp) / 1 days;
    }

    /// @notice Write a message (anyone can write)
    /// @param text The message content
    function writeMessage(string calldata text) external {
        messages.push(Message({author: msg.sender, timestamp: block.timestamp, text: text}));
        emit MessageWritten(msg.sender, messages.length - 1, block.timestamp, text);
    }

    /// @notice Get the total number of messages
    /// @return count Number of messages
    function getMessagesCount() public view returns (uint256 count) {
        return messages.length;
    }

    /// @notice Get a single message by index
    /// @param index The index of the message (starting from 0)
    /// @return author The message author's address
    /// @return timestamp The timestamp when the message was written
    /// @return text The content of the message
    function getMessage(uint256 index) public view returns (address author, uint256 timestamp, string memory text) {
        require(index < messages.length, "index out of range");
        Message storage m = messages[index];
        return (m.author, m.timestamp, m.text);
    }

    /// @notice Get all message texts (Warning: returning a large array may consume a lot of gas)
    /// @return texts Array of message texts
    function getAllTexts() public view returns (string[] memory texts) {
        uint256 n = messages.length;
        texts = new string[](n);
        for (uint256 i = 0; i < n; i++) {
            texts[i] = messages[i].text;
        }
        return texts;
    }

    /// @notice Get a range of message texts ([start, end), not including end)
    /// @dev end must be greater than start and less than or equal to getMessagesCount()
    function getTextsRange(uint256 start, uint256 end) public view returns (string[] memory texts) {
        require(start <= end, "start must be <= end");
        require(end <= messages.length, "end out of range");
        uint256 len = end - start;
        texts = new string[](len);
        for (uint256 i = 0; i < len; i++) {
            texts[i] = messages[start + i].text;
        }
        return texts;
    }
}

Tags:
addr:0xe29094102ae0960cfd8f0330f0d094eb392a80f8|verified:true|block:23537669|tx:0x449909bbc8833220ca0f4c641cc5c0eccd25a35edb6d34fc356046aa5f7be7a6|first_check:1759998576

Submitted on: 2025-10-09 10:29:36

Comments

Log in to comment.

No comments yet.