DailyStreak

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.0;

contract DailyStreak {
    mapping(address => uint256) public streaks; // Streak hiện tại của user
    mapping(address => uint256) public lastCheckIn; // Timestamp check-in cuối

    event CheckedIn(address user, uint256 newStreak);

    function checkIn() public {
        uint256 currentTime = block.timestamp;
        uint256 lastTime = lastCheckIn[msg.sender];
        
        // Tính khoảng cách ngày (giả sử 1 ngày = 86400 giây)
        if (lastTime == 0) {
            // Lần đầu check-in
            streaks[msg.sender] = 1;
        } else {
            uint256 daysPassed = (currentTime - lastTime) / 86400;
            
            if (daysPassed == 1) {
                // Tiếp tục streak
                streaks[msg.sender] += 1;
            } else if (daysPassed > 1) {
                // Reset streak
                streaks[msg.sender] = 1;
            } else {
                // Đã check-in hôm nay, không thay đổi
                return;
            }
        }
        
        lastCheckIn[msg.sender] = currentTime;
        emit CheckedIn(msg.sender, streaks[msg.sender]);
    }

    function getStreak(address user) public view returns (uint256) {
        return streaks[user];
    }
}

Tags:
addr:0xc83030e4c48eb33205051991943c7f1d3b62a6db|verified:true|block:23393777|tx:0x490a34271062884d746df8f7d8fcea446d8e7e97d5701d8e0992c4bceeaa34ee|first_check:1758276284

Submitted on: 2025-09-19 12:04:45

Comments

Log in to comment.

No comments yet.