84 lines
2.6 KiB
Solidity
84 lines
2.6 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;
|
|
}
|
|
|
|
struct ContributionRecord {
|
|
address contributor;
|
|
uint256 amount;
|
|
uint256 timestamp;
|
|
bytes32 recordHash;
|
|
}
|
|
|
|
enum ContributionStatus {
|
|
PENDING,
|
|
APPROVED,
|
|
REJECTED
|
|
}
|
|
|
|
function validateContribution(ContributionRecord memory record) internal pure returns (bool) {
|
|
require(record.contributor != address(0), "Invalid contributor address");
|
|
require(record.amount > 0, "Contribution amount must be positive");
|
|
require(record.timestamp > 0, "Invalid timestamp");
|
|
|
|
// Generate a unique record hash for verification
|
|
bytes32 calculatedHash = keccak256(abi.encodePacked(
|
|
record.contributor,
|
|
record.amount,
|
|
record.timestamp
|
|
));
|
|
|
|
return calculatedHash == record.recordHash;
|
|
}
|
|
|
|
function calculateContributionScore(ContributionRecord memory record) internal pure returns (uint256) {
|
|
// Complex scoring mechanism based on contribution attributes
|
|
uint256 baseScore = record.amount;
|
|
uint256 timeMultiplier = block.timestamp.sub(record.timestamp).div(1 days);
|
|
|
|
return baseScore.mul(timeMultiplier).div(10);
|
|
}
|
|
|
|
function mergeContributions(
|
|
ContributionRecord memory existingRecord,
|
|
ContributionRecord memory newRecord
|
|
) internal pure returns (ContributionRecord memory) {
|
|
require(
|
|
existingRecord.contributor == newRecord.contributor,
|
|
"Contribution mismatch"
|
|
);
|
|
|
|
return ContributionRecord({
|
|
contributor: existingRecord.contributor,
|
|
amount: existingRecord.amount.add(newRecord.amount),
|
|
timestamp: block.timestamp,
|
|
recordHash: keccak256(abi.encodePacked(
|
|
existingRecord.contributor,
|
|
existingRecord.amount.add(newRecord.amount),
|
|
block.timestamp
|
|
))
|
|
});
|
|
}
|
|
|
|
function isLabMetadataValid(LabMetadata memory metadata) internal pure returns (bool) {
|
|
return (
|
|
metadata.owner != address(0) &&
|
|
metadata.createdAt > 0 &&
|
|
metadata.lastUpdated >= metadata.createdAt &&
|
|
metadata.totalContributions >= 0
|
|
);
|
|
}
|
|
} |