contract MiniByte is Ownable,ReentrancyGuard,Pausable {
uint256 public REWARD_PER_GH_PER_DAY;
uint256 public constant SECONDS_IN_A_DAY = 86400;
uint256 public checkinPower;
IERC20 public rewardToken;
bool public claimStatus;
uint256 public endTime;
uint256 public inviteepower;
uint256 public inviterpower;
uint256 public checkinfees;
struct PowerGrant {
uint256 power;
uint256 startTime;
uint256 granttype; // 1=register , 2= referral 3 = daily check-in
address referredUser;
}
struct User {
bool registered;
string referralCode;
address inviter;
uint256 claimedReward;
uint256 totalPower;
uint256 refcount;
PowerGrant[] powerGrants;
}
mapping(address => User) public users;
mapping(string => address) public referralToAddress;
mapping(address => uint256[]) public referralTimestamps;
mapping(address => uint256) public lastCheckin;
event Registered(address user, address inviter, uint256 ghPower, string referralCode);
event Claimed(address user, uint256 amount);
event EndTimeUpdated(uint256 newEndTime);
event ClaimStatusUpdated(bool status);
modifier onlyRegistered() {
require(users[msg.sender].registered, "Not registered");
_;
}
constructor() Ownable(msg.sender) {
inviteepower = 10;
inviterpower = 5;
REWARD_PER_GH_PER_DAY = 100e18;
checkinPower = 2;
rewardToken= IERC20(0xfde4C96c8593536E31F229EA8f37b2ADa2699bb2);
checkinfees = 0.0000042e18;
string memory userCode = generateReferralCode(msg.sender);
users[msg.sender].referralCode = userCode;
referralToAddress[userCode] = msg.sender;
users[msg.sender].registered = true;
}
function generateReferralCode(address user) internal view returns (string memory) {
bytes memory charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
bytes memory code = new bytes(6);
uint256 random = uint256(keccak256(abi.encodePacked(block.timestamp, user, blockhash(block.number - 1))));
for (uint256 i = 0; i < 6; i++) {
code[i] = charset[random % charset.length];
random /= charset.length;
}
return string(code);
}
function register(string memory refCode) external whenNotPaused {
require(!users[msg.sender].registered, "Already registered");
require(bytes(refCode).length > 0 , "Please enter valid referral code to register!");
address inviter = referralToAddress[refCode];
require(inviter != address(0) , "Referral code enter is not valid!");
uint256[] storage timestamps = referralTimestamps[inviter];
uint256 cutoff = block.timestamp - 86400;
uint256 recentCount = 0;
for (uint256 i = timestamps.length; i > 0; i--) {
if (timestamps[i - 1] < cutoff) break;
recentCount++;
if (recentCount >= 3) {
revert("Referral limit exceeded (3 per 24h)");
}
}
users[inviter].powerGrants.push(PowerGrant({
power: inviterpower,
startTime: block.timestamp,
referredUser: msg.sender,
granttype : 2
}));
users[inviter].totalPower += inviterpower;
users[inviter].refcount += 1;
referralTimestamps[inviter].push(block.timestamp);
users[msg.sender].powerGrants.push(PowerGrant({
power: inviteepower,
startTime: block.timestamp,
referredUser: address(0),
granttype : 1
}));
users[msg.sender].totalPower += inviteepower;
string memory userCode = generateReferralCode(msg.sender);
while (referralToAddress[userCode] != address(0)) {
userCode = generateReferralCode(msg.sender); // Regenerate if already taken
}
users[msg.sender].registered = true;
users[msg.sender].referralCode = userCode;
users[msg.sender].inviter = inviter;
users[msg.sender].claimedReward = 0;
referralToAddress[userCode] = msg.sender;
emit Registered(msg.sender, inviter, inviteepower, userCode);
}
function calculateReward(address userAddr) public view returns (uint256) {
User storage user = users[userAddr];
if (!user.registered) return 0;
uint256 totalReward = 0;
uint256 toTime = endTime > 0 ? endTime : block.timestamp;
for (uint256 i = 0; i < user.powerGrants.length; i++) {
PowerGrant memory grant = user.powerGrants[i];
if (toTime > grant.startTime) {
uint256 duration = toTime - grant.startTime;
uint256 reward = (grant.power * REWARD_PER_GH_PER_DAY * duration) / SECONDS_IN_A_DAY;
totalReward += reward;
}
}
return totalReward;
}
function claim() external onlyRegistered nonReentrant {
require(claimStatus, "Claiming disabled");
require(endTime > 0, "End time not set");
uint256 totalReward = calculateReward(msg.sender);
uint256 rewardToClaim = totalReward - users[msg.sender].claimedReward;
require(rewardToClaim > 0, "Nothing to claim");
users[msg.sender].claimedReward = totalReward;
rewardToken.transfer(msg.sender, rewardToClaim);
emit Claimed(msg.sender, rewardToClaim);
}
function dailyCheckin() external onlyRegistered nonReentrant payable {
require(block.timestamp >= lastCheckin[msg.sender] + 1 days, "Already checked in today!");
require(msg.value >= checkinfees, "fees error!");
users[msg.sender].powerGrants.push(PowerGrant({
power: checkinPower,
startTime: block.timestamp,
referredUser: address(0),
granttype: 3
}));
users[msg.sender].totalPower += checkinPower;
lastCheckin[msg.sender] = block.timestamp;
}
function setCheckInfees(uint256 _checkinfees) external onlyOwner{
checkinfees = _checkinfees;
}
function setEndTime(uint256 _endTime) external onlyOwner {
require(_endTime >= block.timestamp, "End time must be in future");
endTime = _endTime;
emit EndTimeUpdated(_endTime);
}
function setClaimStatus(bool status) external onlyOwner {
claimStatus = status;
emit ClaimStatusUpdated(status);
}
function setInviteepower(uint256 _inviteepower) external onlyOwner {
inviteepower = _inviteepower;
}
function setInviterpower(uint256 _inviterpower) external onlyOwner {
inviterpower = _inviterpower;
}
function setRewardGhRate(uint256 _rewardhhrate ) external onlyOwner{
REWARD_PER_GH_PER_DAY = _rewardhhrate;
}
function setRewardToken(address _rewardtoken) external onlyOwner{
rewardToken = IERC20(_rewardtoken);
}
function setCheckinPower(uint256 _power) external onlyOwner {
checkinPower = _power;
}
function userPowerGrants(address _address, uint256 start, uint256 end) public view returns (PowerGrant[] memory) {
uint256 total = users[_address].powerGrants.length;
// Return empty if no records or start is out of bounds
if (total == 0 || start >= total || start > end) {
return new PowerGrant[](0);
}
// Clamp end to the max available index
if (end >= total) {
end = total - 1;
}
uint256 length = end - start + 1;
PowerGrant[] memory rh = new PowerGrant[](length);
uint256 currentIndex = 0;
for (uint256 i = start; i <= end; i++) {
rh[currentIndex] = users[_address].powerGrants[i];
currentIndex++;
}
return rh;
}
function rescueFunds() external onlyOwner {
(bool os,) = payable(owner()).call{value: address(this).balance}("");
require(os);
}
function rescueToken(address _token , uint _balance) external onlyOwner{
IERC20(_token).transfer(owner(),_balance);
}
}
abstract contract Ownable is Context {
address private _owner;
/**
* @dev The caller account is not authorized to perform an operation.
*/
error OwnableUnauthorizedAccount(address account);
/**
* @dev The owner is not a valid owner account. (eg. `address(0)`)
*/
error OwnableInvalidOwner(address owner);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev Initializes the contract setting the address provided by the deployer as the initial owner.
*/
constructor(address initialOwner) {
if (initialOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(initialOwner);
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
_checkOwner();
_;
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view virtual returns (address) {
return _owner;
}
/**
* @dev Throws if the sender is not the owner.
*/
function _checkOwner() internal view virtual {
if (owner() != _msgSender()) {
revert OwnableUnauthorizedAccount(_msgSender());
}
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby disabling any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
if (newOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Internal function without access restriction.
*/
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
abstract contract Pausable is Context {
/**
* @dev Emitted when the pause is triggered by `account`.
*/
event Paused(address account);
/**
* @dev Emitted when the pause is lifted by `account`.
*/
event Unpaused(address account);
bool private _paused;
/**
* @dev Initializes the contract in unpaused state.
*/
constructor() {
_paused = false;
}
/**
* @dev Modifier to make a function callable only when the contract is not paused.
*
* Requirements:
*
* - The contract must not be paused.
*/
modifier whenNotPaused() {
_requireNotPaused();
_;
}
/**
* @dev Modifier to make a function callable only when the contract is paused.
*
* Requirements:
*
* - The contract must be paused.
*/
modifier whenPaused() {
_requirePaused();
_;
}
/**
* @dev Returns true if the contract is paused, and false otherwise.
*/
function paused() public view virtual returns (bool) {
return _paused;
}
/**
* @dev Throws if the contract is paused.
*/
function _requireNotPaused() internal view virtual {
require(!paused(), "Pausable: paused");
}
/**
* @dev Throws if the contract is not paused.
*/
function _requirePaused() internal view virtual {
require(paused(), "Pausable: not paused");
}
/**
* @dev Triggers stopped state.
*
* Requirements:
*
* - The contract must not be paused.
*/
function _pause() internal virtual whenNotPaused {
_paused = true;
emit Paused(_msgSender());
}
/**
* @dev Returns to normal state.
*
* Requirements:
*
* - The contract must be paused.
*/
function _unpause() internal virtual whenPaused {
_paused = false;
emit Unpaused(_msgSender());
}
}
abstract contract ReentrancyGuard {
// Booleans are more expensive than uint256 or any type that takes up a full
// word because each write operation emits an extra SLOAD to first read the
// slot's contents, replace the bits taken up by the boolean, and then write
// back. This is the compiler's defense against contract upgrades and
// pointer aliasing, and it cannot be disabled.
// The values being non-zero value makes deployment a bit more expensive,
// but in exchange the refund on every call to nonReentrant will be lower in
// amount. Since refunds are capped to a percentage of the total
// transaction's gas, it is best to keep them low in cases like this one, to
// increase the likelihood of the full refund coming into effect.
uint256 private constant NOT_ENTERED = 1;
uint256 private constant ENTERED = 2;
uint256 private _status;
/**
* @dev Unauthorized reentrant call.
*/
error ReentrancyGuardReentrantCall();
constructor() {
_status = NOT_ENTERED;
}
/**
* @dev Prevents a contract from calling itself, directly or indirectly.
* Calling a `nonReentrant` function from another `nonReentrant`
* function is not supported. It is possible to prevent this from happening
* by making the `nonReentrant` function external, and making it call a
* `private` function that does the actual work.
*/
modifier nonReentrant() {
_nonReentrantBefore();
_;
_nonReentrantAfter();
}
function _nonReentrantBefore() private {
// On the first call to nonReentrant, _status will be NOT_ENTERED
if (_status == ENTERED) {
revert ReentrancyGuardReentrantCall();
}
// Any calls to nonReentrant after this point will fail
_status = ENTERED;
}
function _nonReentrantAfter() private {
// By storing the original value once again, a refund is triggered (see
// https://eips.ethereum.org/EIPS/eip-2200)
_status = NOT_ENTERED;
}
/**
* @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a
* `nonReentrant` function in the call stack.
*/
function _reentrancyGuardEntered() internal view returns (bool) {
return _status == ENTERED;
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
function _contextSuffixLength() internal view virtual returns (uint256) {
return 0;
}
}
interface IERC20 {
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/
event Transfer(address indexed from, address indexed to, uint256 value);
/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/
event Approval(address indexed owner, address indexed spender, uint256 value);
/**
* @dev Returns the value of tokens in existence.
*/
function totalSupply() external view returns (uint256);
/**
* @dev Returns the value of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);
/**
* @dev Moves a `value` amount of tokens from the caller's account to `to`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transfer(address to, uint256 value) external returns (bool);
/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/
function allowance(address owner, address spender) external view returns (uint256);
/**
* @dev Sets a `value` amount of tokens as the allowance of `spender` over the
* caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/
function approve(address spender, uint256 value) external returns (bool);
/**
* @dev Moves a `value` amount of tokens from `from` to `to` using the
* allowance mechanism. `value` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transferFrom(address from, address to, uint256 value) external returns (bool);
}