99 lines
2.9 KiB
Solidity
99 lines
2.9 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/security/ReentrancyGuard.sol";
|
|
|
|
library LabHelper {
|
|
using SafeMath for uint256;
|
|
|
|
struct LabMetadata {
|
|
address owner;
|
|
uint256 createdAt;
|
|
uint256 lastUpdated;
|
|
uint256 totalContributions;
|
|
bool isActive;
|
|
string labName;
|
|
uint8 reputationScore;
|
|
}
|
|
|
|
struct ContributionRecord {
|
|
address contributor;
|
|
uint256 amount;
|
|
uint256 timestamp;
|
|
string contributionType;
|
|
bool isValidated;
|
|
}
|
|
|
|
event ContributionRecorded(
|
|
address indexed contributor,
|
|
uint256 amount,
|
|
string contributionType,
|
|
uint256 timestamp
|
|
);
|
|
|
|
event LabStatusChanged(
|
|
address indexed lab,
|
|
bool newStatus,
|
|
uint256 timestamp
|
|
);
|
|
|
|
function validateContribution(
|
|
ContributionRecord memory record,
|
|
uint256 minContributionAmount
|
|
) internal pure returns (bool) {
|
|
require(record.amount >= minContributionAmount, "Contribution below minimum threshold");
|
|
require(bytes(record.contributionType).length > 0, "Invalid contribution type");
|
|
require(record.contributor != address(0), "Invalid contributor address");
|
|
|
|
return true;
|
|
}
|
|
|
|
function calculateReputationScore(
|
|
LabMetadata memory lab,
|
|
uint256 newContributionAmount
|
|
) internal pure returns (uint8) {
|
|
uint256 totalScore = lab.totalContributions.add(newContributionAmount);
|
|
|
|
// Non-linear reputation scoring with diminishing returns
|
|
if (totalScore < 1 ether) return 1;
|
|
if (totalScore < 10 ether) return 3;
|
|
if (totalScore < 50 ether) return 5;
|
|
if (totalScore < 100 ether) return 7;
|
|
|
|
return 10;
|
|
}
|
|
|
|
function updateLabStatus(
|
|
LabMetadata storage lab,
|
|
bool newStatus
|
|
) internal {
|
|
require(lab.owner != address(0), "Lab must exist");
|
|
|
|
if (lab.isActive != newStatus) {
|
|
lab.isActive = newStatus;
|
|
lab.lastUpdated = block.timestamp;
|
|
|
|
emit LabStatusChanged(lab.owner, newStatus, block.timestamp);
|
|
}
|
|
}
|
|
|
|
function recordContribution(
|
|
LabMetadata storage lab,
|
|
ContributionRecord memory record
|
|
) internal {
|
|
require(validateContribution(record, 0.01 ether), "Invalid contribution");
|
|
|
|
lab.totalContributions = lab.totalContributions.add(record.amount);
|
|
lab.reputationScore = calculateReputationScore(lab, record.amount);
|
|
lab.lastUpdated = block.timestamp;
|
|
|
|
emit ContributionRecorded(
|
|
record.contributor,
|
|
record.amount,
|
|
record.contributionType,
|
|
block.timestamp
|
|
);
|
|
}
|
|
} |