citizen: implement Convert latest review findings into one concrete code change with a short validation note.
This commit is contained in:
parent
fab0aaaa8c
commit
10a977300f
75
contracts/LabHelper.sol
Normal file
75
contracts/LabHelper.sol
Normal file
@ -0,0 +1,75 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
pragma solidity ^0.8.19;
|
||||
|
||||
import "./interfaces/ILabRegistry.sol";
|
||||
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
|
||||
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
|
||||
|
||||
library LabHelper {
|
||||
using SafeMath for uint256;
|
||||
|
||||
struct LabMetadata {
|
||||
address owner;
|
||||
uint256 createdAt;
|
||||
uint256 lastUpdated;
|
||||
uint256 totalContributions;
|
||||
bool isActive;
|
||||
uint8 securityLevel;
|
||||
}
|
||||
|
||||
struct ContributionRecord {
|
||||
address contributor;
|
||||
uint256 amount;
|
||||
uint256 timestamp;
|
||||
bytes32 commitHash;
|
||||
bool validated;
|
||||
}
|
||||
|
||||
enum ContributionStatus {
|
||||
PENDING,
|
||||
APPROVED,
|
||||
REJECTED
|
||||
}
|
||||
|
||||
function validateContribution(
|
||||
ContributionRecord memory record,
|
||||
uint256 minimumContribution
|
||||
) internal pure returns (bool) {
|
||||
require(record.amount >= minimumContribution, "Contribution below minimum threshold");
|
||||
require(record.contributor != address(0), "Invalid contributor address");
|
||||
require(record.timestamp > 0, "Invalid contribution timestamp");
|
||||
|
||||
// Additional validation logic
|
||||
bool hasValidCommitHash = record.commitHash != bytes32(0);
|
||||
return hasValidCommitHash && record.amount > 0;
|
||||
}
|
||||
|
||||
function calculateContributionScore(
|
||||
ContributionRecord memory record,
|
||||
uint256 baseScore
|
||||
) internal pure returns (uint256) {
|
||||
uint256 timeMultiplier = block.timestamp.sub(record.timestamp) / 1 days;
|
||||
uint256 amountMultiplier = record.amount.div(10**18); // Normalize to 1 ETH
|
||||
|
||||
return baseScore
|
||||
.mul(timeMultiplier)
|
||||
.mul(amountMultiplier)
|
||||
.div(100);
|
||||
}
|
||||
|
||||
function mergeLabMetadata(
|
||||
LabMetadata memory existing,
|
||||
LabMetadata memory update
|
||||
) internal pure returns (LabMetadata memory) {
|
||||
return LabMetadata({
|
||||
owner: update.owner != address(0) ? update.owner : existing.owner,
|
||||
createdAt: existing.createdAt,
|
||||
lastUpdated: block.timestamp,
|
||||
totalContributions: existing.totalContributions.add(update.totalContributions),
|
||||
isActive: update.isActive,
|
||||
securityLevel: update.securityLevel > existing.securityLevel
|
||||
? update.securityLevel
|
||||
: existing.securityLevel
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user