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];
}
}
Submitted on: 2025-09-19 12:04:45
Comments
Log in to comment.
No comments yet.