From 0247c3b915a41f1cfb0139c4463e4e1fdff8ff68 Mon Sep 17 00:00:00 2001 From: Andres Aiello Date: Wed, 25 Sep 2024 13:39:31 -0300 Subject: [PATCH] refactor to constants --- .../proof-of-liveness/ProofOfLiveness.sol | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/packages/zevm-app-contracts/contracts/proof-of-liveness/ProofOfLiveness.sol b/packages/zevm-app-contracts/contracts/proof-of-liveness/ProofOfLiveness.sol index a90fbd3..a530654 100644 --- a/packages/zevm-app-contracts/contracts/proof-of-liveness/ProofOfLiveness.sol +++ b/packages/zevm-app-contracts/contracts/proof-of-liveness/ProofOfLiveness.sol @@ -2,22 +2,25 @@ pragma solidity ^0.8.20; contract ProofOfLiveness { + uint256 constant PROOF_PERIOD = 24 hours; + uint256 constant LAST_PERIODS_LENGTH = 5; + // Mapping to track the last time the user proved liveness mapping(address => uint256) public lastProofTimestamp; - // Custom error for when a user has already proved liveness within the last 24 hours + // Custom error for when a user has already proved liveness within the last PROOF_PERIOD error ProofWithinLast24Hours(uint256 lastProofTime); // Event to log when liveness is proved event LivenessProved(address indexed user, uint256 proofTimestamp); - // The function to prove liveness, can only be called once every 24 hours + // The function to prove liveness, can only be called once every PROOF_PERIOD function proveLiveness() external { uint256 currentTime = block.timestamp; uint256 lastProofTime = lastProofTimestamp[msg.sender]; - // Check if the user has proved liveness within the last 24 hours - if (currentTime < lastProofTime + 24 hours) { + // Check if the user has proved liveness within the last PROOF_PERIOD + if (currentTime < lastProofTime + PROOF_PERIOD) { revert ProofWithinLast24Hours(lastProofTime); } @@ -28,22 +31,22 @@ contract ProofOfLiveness { emit LivenessProved(msg.sender, currentTime); } - // Helper function to check if a user can prove liveness (returns true if 24 hours have passed) + // Helper function to check if a user can prove liveness (returns true if PROOF_PERIOD have passed) function canProveLiveness(address user) external view returns (bool) { uint256 currentTime = block.timestamp; - return currentTime >= lastProofTimestamp[user] + 24 hours; + return currentTime >= lastProofTimestamp[user] + PROOF_PERIOD; } - // View function to return the liveness proof status for the last 5 periods (each 24 hours long) - function getLastFivePeriodsStatus(address user) external view returns (bool[5] memory) { + // View function to return the liveness proof status for the last LAST_PERIODS_LENGTH periods (each PROOF_PERIOD long) + function getLastPeriodsStatus(address user) external view returns (bool[LAST_PERIODS_LENGTH] memory) { uint256 currentTime = block.timestamp; uint256 lastProofTime = lastProofTimestamp[user]; - bool[5] memory proofStatus; - uint256 periodDuration = 24 hours; + bool[LAST_PERIODS_LENGTH] memory proofStatus; + uint256 periodDuration = PROOF_PERIOD; - for (uint256 i = 0; i < 5; i++) { - // Calculate the start of the period (going back i * 24 hours) + for (uint256 i = 0; i < LAST_PERIODS_LENGTH; i++) { + // Calculate the start of the period (going back i * PROOF_PERIOD) uint256 periodStart = currentTime - (i * periodDuration); uint256 periodEnd = periodStart - periodDuration;