forked from sourcekeeper_42/sourcekeeper_42-contract-lab
83 lines
2.5 KiB
Solidity
83 lines
2.5 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;
|
|
string contributionType;
|
|
bool isVerified;
|
|
}
|
|
|
|
event ContributionRecorded(
|
|
address indexed lab,
|
|
address indexed contributor,
|
|
uint256 amount,
|
|
string contributionType
|
|
);
|
|
|
|
function validateContribution(
|
|
ContributionRecord memory record,
|
|
uint256 minContributionAmount
|
|
) internal pure returns (bool) {
|
|
require(record.contributor != address(0), "Invalid contributor address");
|
|
require(record.amount >= minContributionAmount, "Contribution below minimum threshold");
|
|
require(record.timestamp > 0, "Invalid contribution timestamp");
|
|
|
|
return true;
|
|
}
|
|
|
|
function calculateReputationScore(
|
|
LabMetadata memory metadata,
|
|
uint256 totalContributions
|
|
) internal pure returns (uint8) {
|
|
if (totalContributions == 0) return 0;
|
|
|
|
uint256 scoreCalculation = (metadata.totalContributions * 100) / totalContributions;
|
|
|
|
if (scoreCalculation > 100) return 100;
|
|
return uint8(scoreCalculation);
|
|
}
|
|
|
|
function updateLabMetadata(
|
|
LabMetadata storage metadata,
|
|
ContributionRecord memory contribution
|
|
) internal {
|
|
require(metadata.isActive, "Lab is not active");
|
|
|
|
metadata.totalContributions = metadata.totalContributions.add(contribution.amount);
|
|
metadata.lastUpdated = block.timestamp;
|
|
|
|
emit ContributionRecorded(
|
|
metadata.owner,
|
|
contribution.contributor,
|
|
contribution.amount,
|
|
contribution.contributionType
|
|
);
|
|
}
|
|
|
|
function isLabEligibleForReward(
|
|
LabMetadata memory metadata,
|
|
uint256 minContributionThreshold
|
|
) internal pure returns (bool) {
|
|
return metadata.isActive &&
|
|
metadata.totalContributions >= minContributionThreshold &&
|
|
block.timestamp - metadata.createdAt > 30 days;
|
|
}
|
|
} |