forked from sourcekeeper_42/sourcekeeper_42-contract-lab
76 lines
2.1 KiB
Solidity
76 lines
2.1 KiB
Solidity
// SPDX-License-Identifier: MIT
|
|
pragma solidity ^0.8.19;
|
|
|
|
import "./interfaces/ILabRegistry.sol";
|
|
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
|
|
import "@openzeppelin/contracts/access/Ownable.sol";
|
|
|
|
library LabHelper {
|
|
using SafeMath for uint256;
|
|
|
|
struct LabMetadata {
|
|
address owner;
|
|
uint256 createdAt;
|
|
uint256 lastUpdated;
|
|
uint256 totalContributions;
|
|
bool isActive;
|
|
uint8 reputationScore;
|
|
}
|
|
|
|
struct ContributionRecord {
|
|
address contributor;
|
|
uint256 amount;
|
|
uint256 timestamp;
|
|
bytes32 recordHash;
|
|
bool validated;
|
|
}
|
|
|
|
event ContributionRecorded(
|
|
address indexed contributor,
|
|
uint256 amount,
|
|
uint256 timestamp
|
|
);
|
|
|
|
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 timestamp");
|
|
|
|
return true;
|
|
}
|
|
|
|
function calculateReputationScore(
|
|
LabMetadata memory metadata,
|
|
uint256 totalContributions
|
|
) internal pure returns (uint8) {
|
|
if (totalContributions == 0) return 0;
|
|
|
|
uint256 contributionRatio = metadata.totalContributions.mul(100).div(totalContributions);
|
|
|
|
if (contributionRatio > 50) return 5;
|
|
if (contributionRatio > 25) return 3;
|
|
if (contributionRatio > 10) return 2;
|
|
|
|
return 1;
|
|
}
|
|
|
|
function hashContributionRecord(
|
|
ContributionRecord memory record
|
|
) internal pure returns (bytes32) {
|
|
return keccak256(abi.encodePacked(
|
|
record.contributor,
|
|
record.amount,
|
|
record.timestamp
|
|
));
|
|
}
|
|
|
|
function verifyContributionIntegrity(
|
|
ContributionRecord memory record
|
|
) internal pure returns (bool) {
|
|
bytes32 calculatedHash = hashContributionRecord(record);
|
|
return calculatedHash == record.recordHash;
|
|
}
|
|
} |