Skip to content

Commit

Permalink
Add/refine deactivate and reactivate functions/test code to HatsTimeF…
Browse files Browse the repository at this point in the history
…rameModule
  • Loading branch information
tanapl committed Sep 30, 2024
1 parent 2f74faa commit eba3a49
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
11 changes: 5 additions & 6 deletions pkgs/contract/contracts/timeframe/HatsTimeFrameModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ contract HatsTimeFrameModule is
function reactivate(uint256 hatId, address wearer) external {
require(!isActive[hatId][wearer], "Hat is already active");
isActive[hatId][wearer] = true;
uint256 pausedTime = block.timestamp - deactivatedTime[hatId][wearer]; // Time the hat was paused
woreTime[hatId][wearer] += pausedTime; // Add paused time to keep the total time balanced
}
woreTime[hatId][wearer] += block.timestamp - deactivatedTime[hatId][wearer];
}

/**
* @dev Sets the timestamp when a specific hat was minted for a specific address.
Expand All @@ -83,8 +82,8 @@ contract HatsTimeFrameModule is

/**
* @dev Gets the elapsed time in seconds since the specific hat was minted for a specific address.
* If the hat is currently active, include the current active period.
* If the hat is inactive, count the active time up to the last deactivation.
* 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.
Expand All @@ -93,7 +92,7 @@ contract HatsTimeFrameModule is
address wearer,
uint256 hatId
) external view returns (uint256) {
uint256 activeTime = totalActiveTime[hatId][wearer];
uint256 activeTime = totalActiveTime[hatId][wearer];
if (isActive[hatId][wearer]) {
activeTime += block.timestamp - woreTime[hatId][wearer];
} else {
Expand Down
46 changes: 45 additions & 1 deletion pkgs/contract/test/HatsTimeFrameModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ describe("HatsTimeFrameModule", () => {
}
}

// アドレス1にRole hatをミント
await HatsTimeFrameModule.write.mintHat([
roleHatId,
address1.account?.address!,
Expand All @@ -158,5 +159,48 @@ describe("HatsTimeFrameModule", () => {
roleHatId,
])
).equal(BigInt(100));

// Fast forward time and check elapsed time while active
await time.increase(100);

let elapsedTime = await HatsTimeFrameModule.read.getWearingElapsedTime([
address1.account?.address!,
roleHatId
]);
expect(
elapsedTime
).to.equal(BigInt(100));

// Deactivate and verify that time stops accumulating
await HatsTimeFrameModule.write.deactivate([
roleHatId,
address1.account?.address!
]);

await time.increase(50);

elapsedTime = await HatsTimeFrameModule.read.getWearingElapsedTime([
address1.account?.address!,
roleHatId
]);
expect(
elapsedTime
).to.equal(BigInt(100)); // Time should not increase

// Reactivate and ensure time starts accumulating again
await HatsTimeFrameModule.write.reactivate([
roleHatId,
address1.account?.address!
]);

await time.increase(100);

elapsedTime = await HatsTimeFrameModule.read.getWearingElapsedTime([
address1.account?.address!,
roleHatId
]);
expect(
elapsedTime
).to.equal(BigInt(200)); // Total time should now be 200
});
});
});

0 comments on commit eba3a49

Please sign in to comment.