forked from sourcekeeper_42/sourcekeeper_42-contract-lab
77 lines
2.3 KiB
Solidity
77 lines
2.3 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 isValidated;
|
|
}
|
|
|
|
enum ContributionStatus {
|
|
Pending,
|
|
Accepted,
|
|
Rejected
|
|
}
|
|
|
|
function validateContribution(
|
|
ContributionRecord memory record,
|
|
uint256 minimumContributionAmount
|
|
) internal pure returns (bool) {
|
|
require(record.contributor != address(0), "Invalid contributor address");
|
|
require(record.amount >= minimumContributionAmount, "Contribution below minimum threshold");
|
|
require(record.timestamp > 0, "Invalid contribution timestamp");
|
|
|
|
return true;
|
|
}
|
|
|
|
function calculateContributionScore(
|
|
ContributionRecord memory record
|
|
) internal pure returns (uint8) {
|
|
if (record.amount == 0 || !record.isValidated) {
|
|
return 0;
|
|
}
|
|
|
|
uint256 scoreBase = record.amount / 1 ether;
|
|
return uint8(scoreBase > 10 ? 10 : scoreBase);
|
|
}
|
|
|
|
function mergeContributionRecords(
|
|
ContributionRecord memory existingRecord,
|
|
ContributionRecord memory newRecord
|
|
) internal pure returns (ContributionRecord memory) {
|
|
require(
|
|
existingRecord.contributor == newRecord.contributor,
|
|
"Contributor mismatch"
|
|
);
|
|
|
|
return ContributionRecord({
|
|
contributor: existingRecord.contributor,
|
|
amount: existingRecord.amount.add(newRecord.amount),
|
|
timestamp: block.timestamp,
|
|
contributionType: string(abi.encodePacked(
|
|
existingRecord.contributionType,
|
|
",",
|
|
newRecord.contributionType
|
|
)),
|
|
isValidated: existingRecord.isValidated && newRecord.isValidated
|
|
});
|
|
}
|
|
} |