Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

send error when the woretime is not passed #269

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 100 additions & 95 deletions pkgs/contract/contracts/timeframe/HatsTimeFrameModule.sol
Original file line number Diff line number Diff line change
@@ -1,113 +1,118 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import { IHatsTimeFrameModule } from "./IHatsTimeFrameModule.sol";
import { HatsModule } from "../hats/module/HatsModule.sol";
import {IHatsTimeFrameModule} from "./IHatsTimeFrameModule.sol";
import {HatsModule} from "../hats/module/HatsModule.sol";

contract HatsTimeFrameModule is HatsModule, IHatsTimeFrameModule {
// hatId => wearer => wore timestamp
mapping(uint256 => mapping(address => uint256)) public woreTime;
// hatId => wearer => wore timestamp
mapping(uint256 => mapping(address => uint256)) public woreTime;

// hatId => wearer => last deactivation timestamp
mapping(uint256 => mapping(address => uint256)) public deactivatedTime;
// hatId => wearer => last deactivation timestamp
mapping(uint256 => mapping(address => uint256)) public deactivatedTime;

// hatId => wearer => total active time
mapping(uint256 => mapping(address => uint256)) public totalActiveTime;
// hatId => wearer => total active time
mapping(uint256 => mapping(address => uint256)) public totalActiveTime;

// hatId => wearer => isActive
mapping(uint256 => mapping(address => bool)) public isActive;
// hatId => wearer => isActive
mapping(uint256 => mapping(address => bool)) public isActive;

/**
* @dev Constructor to initialize the trusted forwarder.
* @param _version The version of the contract.
*/
constructor(string memory _version) HatsModule(_version) {}
/**
* @dev Constructor to initialize the trusted forwarder.
* @param _version The version of the contract.
*/
constructor(string memory _version) HatsModule(_version) {}

/**
* @dev Mint a hat for a specific address.
* @param hatId The ID of the hat that was minted.
* @param wearer The address of the person who received the hat.
* @param time The specific timestamp when the hat was minted.
*/
function mintHat(uint256 hatId, address wearer, uint256 time) external {
_setWoreTime(wearer, hatId, time);
isActive[hatId][wearer] = true;
HATS().mintHat(hatId, wearer);
}
/**
* @dev Mint a hat for a specific address.
* @param hatId The ID of the hat that was minted.
* @param wearer The address of the person who received the hat.
* @param time The specific timestamp when the hat was minted.
*/
function mintHat(uint256 hatId, address wearer, uint256 time) external {
_setWoreTime(wearer, hatId, time);
isActive[hatId][wearer] = true;
HATS().mintHat(hatId, wearer);
}

/**
* @dev Deactivate the hat, pausing the contribution time.
* Calculate the contribution time up to deactivation.
* @param wearer The address of the person who received the hat.
* @param hatId The ID of the hat that was minted.
*/
function deactivate(uint256 hatId, address wearer) external {
// msg.sender should be the owner of the hat or parent hat owner
require(isActive[hatId][wearer], "Hat is already inactive");
isActive[hatId][wearer] = false;
deactivatedTime[hatId][wearer] = block.timestamp;
totalActiveTime[hatId][wearer] +=
block.timestamp -
woreTime[hatId][wearer];
}
/**
* @dev Deactivate the hat, pausing the contribution time.
* Calculate the contribution time up to deactivation.
* @param wearer The address of the person who received the hat.
* @param hatId The ID of the hat that was minted.
*/
function deactivate(uint256 hatId, address wearer) external {
// msg.sender should be the owner of the hat or parent hat owner
require(isActive[hatId][wearer], "Hat is already inactive");
isActive[hatId][wearer] = false;
deactivatedTime[hatId][wearer] = block.timestamp;
totalActiveTime[hatId][wearer] +=
block.timestamp -
woreTime[hatId][wearer];
}

/**
* @dev Reactivate the hat, resuming the contribution time.
* Reset woreTime for new active period.
* @param wearer The address of the person who received the hat.
* @param hatId The ID of the hat that was minted.
*/
function reactivate(uint256 hatId, address wearer) external {
require(!isActive[hatId][wearer], "Hat is already active");
isActive[hatId][wearer] = true;
woreTime[hatId][wearer] = block.timestamp;
}
/**
* @dev Reactivate the hat, resuming the contribution time.
* Reset woreTime for new active period.
* @param wearer The address of the person who received the hat.
* @param hatId The ID of the hat that was minted.
*/
function reactivate(uint256 hatId, address wearer) external {
require(!isActive[hatId][wearer], "Hat is already active");
isActive[hatId][wearer] = true;
woreTime[hatId][wearer] = block.timestamp;
}

/**
* @dev Sets the timestamp when a specific hat was minted for a specific address.
* Can only be called by the contract that handles the minting logic.
* @param hatId The ID of the hat that was minted.
*/
function _setWoreTime(
address wearer,
uint256 hatId,
uint256 time
) internal {
require(woreTime[hatId][wearer] == 0, "Hat already minted");
woreTime[hatId][wearer] = time == 0 ? block.timestamp : time;
}
/**
* @dev Sets the timestamp when a specific hat was minted for a specific address.
* Can only be called by the contract that handles the minting logic.
* @param hatId The ID of the hat that was minted.
*/
function _setWoreTime(
address wearer,
uint256 hatId,
uint256 time
) internal {
require(woreTime[hatId][wearer] == 0, "Hat already minted");
woreTime[hatId][wearer] = time == 0 ? block.timestamp : time;
}

/**
* @dev Gets the timestamp when a specific hat was minted for a specific address.
* @param wearer The address of the person who received the hat.
* @param hatId The ID of the hat that was minted.
*/
function getWoreTime(
address wearer,
uint256 hatId
) external view returns (uint256) {
return woreTime[hatId][wearer];
}
/**
* @dev Gets the timestamp when a specific hat was minted for a specific address.
* @param wearer The address of the person who received the hat.
* @param hatId The ID of the hat that was minted.
*/
function getWoreTime(
address wearer,
uint256 hatId
) external view returns (uint256) {
return woreTime[hatId][wearer];
}

/**
* @dev Gets the elapsed time in seconds since the specific hat was minted for a specific address.
* If the hat is active, calculate time from the last wear time to the current time.
* If the hat is inactive, calculate time up to the deactivation.
* @param wearer The address of the person who received the hat.
* @param hatId The ID of the hat that was minted.
* @return The elapsed time in seconds.
*/
function getWearingElapsedTime(
address wearer,
uint256 hatId
) external view returns (uint256) {
uint256 activeTime = totalActiveTime[hatId][wearer];
/**
* @dev Gets the elapsed time in seconds since the specific hat was minted for a specific address.
* If the hat is active, calculate time from the last wear time to the current time.
* If the hat is inactive, calculate time up to the deactivation.
* @param wearer The address of the person who received the hat.
* @param hatId The ID of the hat that was minted.
* @return The elapsed time in seconds.
*/
function getWearingElapsedTime(
address wearer,
uint256 hatId
) external view returns (uint256) {
require(
block.timestamp >= woreTime[hatId][wearer],
"Invalid wore time"
);

if (isActive[hatId][wearer]) {
// If active, calculate time from the last woreTime to the current time
activeTime += block.timestamp - woreTime[hatId][wearer];
}
uint256 activeTime = totalActiveTime[hatId][wearer];

return activeTime;
}
if (isActive[hatId][wearer]) {
// If active, calculate time from the last woreTime to the current time
activeTime += block.timestamp - woreTime[hatId][wearer];
}

return activeTime;
}
}
Loading