feat(developer-ecosystem): implement bounty and staking system with ZK-proof integration

Phase 1 Implementation Complete:
- AgentBounty.sol: Automated bounty board with ZK-proof verification
- AgentStaking.sol: Reputation-based yield farming with dynamic APY
- BountyIntegration.sol: Cross-contract event handling and auto-verification
- Database models: Complete bounty, staking, and ecosystem metrics schemas
- REST APIs: Full bounty and staking management endpoints
- Services: Business logic for bounty creation, verification, and staking operations
- Ecosystem dashboard: Analytics and metrics tracking system

Key Features:
- Multi-tier bounty system (Bronze, Silver, Gold, Platinum)
- Performance-based APY calculation with reputation multipliers
- ZK-proof integration with PerformanceVerifier.sol
- Automatic bounty completion detection
- Comprehensive analytics dashboard
- Risk assessment and leaderboards
- Real-time metrics and predictions

Security Features:
- Reentrancy protection on all contracts
- Role-based access control
- Dispute resolution mechanism
- Early unbonding penalties
- Platform fee collection

Economic Model:
- Creation fees: 0.5%
- Success fees: 2%
- Platform fees: 1%
- Staking APY: 5-20% based on performance
- Dispute fees: 0.1%
This commit is contained in:
oib
2026-02-27 17:51:23 +01:00
parent 27e836bf3f
commit a477681c4b
11 changed files with 6695 additions and 0 deletions

718
contracts/AgentBounty.sol Normal file
View File

@@ -0,0 +1,718 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/security/Pausable.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "./PerformanceVerifier.sol";
import "./AIToken.sol";
/**
* @title Agent Bounty System
* @dev Automated bounty board for AI agent capabilities with ZK-proof verification
* @notice Allows DAO and users to create bounties that are automatically completed when agents submit valid ZK-proofs
*/
contract AgentBounty is Ownable, ReentrancyGuard, Pausable {
// State variables
IERC20 public aitbcToken;
PerformanceVerifier public performanceVerifier;
uint256 public bountyCounter;
uint256 public creationFeePercentage = 50; // 0.5% in basis points
uint256 public successFeePercentage = 200; // 2% in basis points
uint256 public disputeFeePercentage = 10; // 0.1% in basis points
uint256 public platformFeePercentage = 100; // 1% in basis points
// Bounty tiers
enum BountyTier { BRONZE, SILVER, GOLD, PLATINUM }
// Bounty status
enum BountyStatus { CREATED, ACTIVE, SUBMITTED, VERIFIED, COMPLETED, EXPIRED, DISPUTED }
// Submission status
enum SubmissionStatus { PENDING, VERIFIED, REJECTED, DISPUTED }
// Structs
struct Bounty {
uint256 bountyId;
string title;
string description;
uint256 rewardAmount;
address creator;
BountyTier tier;
BountyStatus status;
bytes32 performanceCriteria; // Hash of performance requirements
uint256 minAccuracy;
uint256 deadline;
uint256 creationTime;
uint256 maxSubmissions;
uint256 submissionCount;
address winningSubmission;
bool requiresZKProof;
mapping(address => bool) authorizedSubmitters;
}
struct Submission {
uint256 submissionId;
uint256 bountyId;
address submitter;
bytes zkProof;
bytes32 performanceHash;
uint256 accuracy;
uint256 responseTime;
uint256 submissionTime;
SubmissionStatus status;
string disputeReason;
address verifier;
}
struct BountyStats {
uint256 totalBounties;
uint256 activeBounties;
uint256 completedBounties;
uint256 totalValueLocked;
uint256 averageReward;
uint256 successRate;
}
// Mappings
mapping(uint256 => Bounty) public bounties;
mapping(uint256 => Submission) public submissions;
mapping(uint256 => uint256[]) public bountySubmissions;
mapping(address => uint256[]) public userSubmissions;
mapping(address => uint256[]) public creatorBounties;
mapping(BountyTier => uint256) public tierRequirements;
mapping(uint256 => mapping(address => bool)) public hasSubmitted;
// Arrays
uint256[] public activeBountyIds;
address[] public authorizedCreators;
// Events
event BountyCreated(
uint256 indexed bountyId,
string title,
uint256 rewardAmount,
address indexed creator,
BountyTier tier,
uint256 deadline
);
event BountySubmitted(
uint256 indexed bountyId,
uint256 indexed submissionId,
address indexed submitter,
bytes32 performanceHash,
uint256 accuracy
);
event BountyVerified(
uint256 indexed bountyId,
uint256 indexed submissionId,
address indexed submitter,
bool success,
uint256 rewardAmount
);
event BountyCompleted(
uint256 indexed bountyId,
address indexed winner,
uint256 rewardAmount,
uint256 completionTime
);
event BountyExpired(
uint256 indexed bountyId,
uint256 refundAmount
);
event BountyDisputed(
uint256 indexed bountyId,
uint256 indexed submissionId,
address indexed disputer,
string reason
);
event PlatformFeeCollected(
uint256 indexed bountyId,
uint256 feeAmount,
address indexed collector
);
// Modifiers
modifier bountyExists(uint256 _bountyId) {
require(_bountyId < bountyCounter, "Bounty does not exist");
_;
}
modifier onlyAuthorizedCreator() {
require(isAuthorizedCreator(msg.sender), "Not authorized to create bounties");
_;
}
modifier validBountyStatus(uint256 _bountyId, BountyStatus _requiredStatus) {
require(bounties[_bountyId].status == _requiredStatus, "Invalid bounty status");
_;
}
modifier beforeDeadline(uint256 _deadline) {
require(block.timestamp <= _deadline, "Deadline passed");
_;
}
modifier sufficientBalance(uint256 _amount) {
require(aitbcToken.balanceOf(msg.sender) >= _amount, "Insufficient balance");
_;
}
constructor(address _aitbcToken, address _performanceVerifier) {
aitbcToken = IERC20(_aitbcToken);
performanceVerifier = PerformanceVerifier(_performanceVerifier);
// Set tier requirements (minimum reward amounts)
tierRequirements[BountyTier.BRONZE] = 100 * 10**18; // 100 AITBC
tierRequirements[BountyTier.SILVER] = 500 * 10**18; // 500 AITBC
tierRequirements[BountyTier.GOLD] = 1000 * 10**18; // 1000 AITBC
tierRequirements[BountyTier.PLATINUM] = 5000 * 10**18; // 5000 AITBC
}
/**
* @dev Creates a new bounty
* @param _title Bounty title
* @param _description Detailed description
* @param _rewardAmount Reward amount in AITBC tokens
* @param _tier Bounty tier
* @param _performanceCriteria Hash of performance requirements
* @param _minAccuracy Minimum accuracy required
* @param _deadline Bounty deadline
* @param _maxSubmissions Maximum number of submissions allowed
* @param _requiresZKProof Whether ZK-proof is required
*/
function createBounty(
string memory _title,
string memory _description,
uint256 _rewardAmount,
BountyTier _tier,
bytes32 _performanceCriteria,
uint256 _minAccuracy,
uint256 _deadline,
uint256 _maxSubmissions,
bool _requiresZKProof
) external
onlyAuthorizedCreator
sufficientBalance(_rewardAmount)
beforeDeadline(_deadline)
nonReentrant
returns (uint256)
{
require(_rewardAmount >= tierRequirements[_tier], "Reward below tier minimum");
require(_minAccuracy <= 100, "Invalid accuracy");
require(_maxSubmissions > 0, "Invalid max submissions");
require(_deadline > block.timestamp, "Invalid deadline");
uint256 bountyId = bountyCounter++;
Bounty storage bounty = bounties[bountyId];
bounty.bountyId = bountyId;
bounty.title = _title;
bounty.description = _description;
bounty.rewardAmount = _rewardAmount;
bounty.creator = msg.sender;
bounty.tier = _tier;
bounty.status = BountyStatus.CREATED;
bounty.performanceCriteria = _performanceCriteria;
bounty.minAccuracy = _minAccuracy;
bounty.deadline = _deadline;
bounty.creationTime = block.timestamp;
bounty.maxSubmissions = _maxSubmissions;
bounty.submissionCount = 0;
bounty.requiresZKProof = _requiresZKProof;
// Calculate and collect creation fee
uint256 creationFee = (_rewardAmount * creationFeePercentage) / 10000;
uint256 totalRequired = _rewardAmount + creationFee;
require(aitbcToken.balanceOf(msg.sender) >= totalRequired, "Insufficient total amount");
// Transfer tokens to contract
require(aitbcToken.transferFrom(msg.sender, address(this), totalRequired), "Transfer failed");
// Transfer creation fee to DAO treasury (owner for now)
if (creationFee > 0) {
require(aitbcToken.transfer(owner(), creationFee), "Fee transfer failed");
emit PlatformFeeCollected(bountyId, creationFee, owner());
}
// Update tracking arrays
activeBountyIds.push(bountyId);
creatorBounties[msg.sender].push(bountyId);
// Activate bounty
bounty.status = BountyStatus.ACTIVE;
emit BountyCreated(bountyId, _title, _rewardAmount, msg.sender, _tier, _deadline);
return bountyId;
}
/**
* @dev Submits a solution to a bounty
* @param _bountyId Bounty ID
* @param _zkProof Zero-knowledge proof (if required)
* @param _performanceHash Hash of performance metrics
* @param _accuracy Achieved accuracy
* @param _responseTime Response time in milliseconds
*/
function submitBountySolution(
uint256 _bountyId,
bytes memory _zkProof,
bytes32 _performanceHash,
uint256 _accuracy,
uint256 _responseTime
) external
bountyExists(_bountyId)
validBountyStatus(_bountyId, BountyStatus.ACTIVE)
beforeDeadline(bounties[_bountyId].deadline)
nonReentrant
returns (uint256)
{
Bounty storage bounty = bounties[_bountyId];
require(!hasSubmitted[_bountyId][msg.sender], "Already submitted");
require(bounty.submissionCount < bounty.maxSubmissions, "Max submissions reached");
if (bounty.requiresZKProof) {
require(_zkProof.length > 0, "ZK-proof required");
}
uint256 submissionId = bounty.submissionCount; // Use count as ID
Submission storage submission = submissions[submissionId];
submission.submissionId = submissionId;
submission.bountyId = _bountyId;
submission.submitter = msg.sender;
submission.zkProof = _zkProof;
submission.performanceHash = _performanceHash;
submission.accuracy = _accuracy;
submission.responseTime = _responseTime;
submission.submissionTime = block.timestamp;
submission.status = SubmissionStatus.PENDING;
// Update tracking
bounty.submissionCount++;
hasSubmitted[_bountyId][msg.sender] = true;
bountySubmissions[_bountyId].push(submissionId);
userSubmissions[msg.sender].push(submissionId);
// Auto-verify if ZK-proof is provided
if (_zkProof.length > 0) {
_verifySubmission(_bountyId, submissionId);
}
emit BountySubmitted(_bountyId, submissionId, msg.sender, _performanceHash, _accuracy);
return submissionId;
}
/**
* @dev Manually verifies a submission (oracle or automated)
* @param _bountyId Bounty ID
* @param _submissionId Submission ID
* @param _verified Whether the submission is verified
* @param _verifier Address of the verifier
*/
function verifySubmission(
uint256 _bountyId,
uint256 _submissionId,
bool _verified,
address _verifier
) external
bountyExists(_bountyId)
nonReentrant
{
Bounty storage bounty = bounties[_bountyId];
Submission storage submission = submissions[_submissionId];
require(submission.status == SubmissionStatus.PENDING, "Submission not pending");
require(submission.bountyId == _bountyId, "Submission bounty mismatch");
submission.status = _verified ? SubmissionStatus.VERIFIED : SubmissionStatus.REJECTED;
submission.verifier = _verifier;
if (_verified) {
// Check if this meets the bounty requirements
if (submission.accuracy >= bounty.minAccuracy) {
_completeBounty(_bountyId, _submissionId);
}
}
emit BountyVerified(_bountyId, _submissionId, submission.submitter, _verified, bounty.rewardAmount);
}
/**
* @dev Disputes a submission
* @param _bountyId Bounty ID
* @param _submissionId Submission ID
* @param _reason Reason for dispute
*/
function disputeSubmission(
uint256 _bountyId,
uint256 _submissionId,
string memory _reason
) external
bountyExists(_bountyId)
nonReentrant
{
Bounty storage bounty = bounties[_bountyId];
Submission storage submission = submissions[_submissionId];
require(submission.status == SubmissionStatus.VERIFIED, "Can only dispute verified submissions");
require(block.timestamp - submission.submissionTime <= 86400, "Dispute window expired"); // 24 hours
submission.status = SubmissionStatus.DISPUTED;
submission.disputeReason = _reason;
bounty.status = BountyStatus.DISPUTED;
// Collect dispute fee
uint256 disputeFee = (bounty.rewardAmount * disputeFeePercentage) / 10000;
if (disputeFee > 0) {
require(aitbcToken.transferFrom(msg.sender, address(this), disputeFee), "Dispute fee transfer failed");
}
emit BountyDisputed(_bountyId, _submissionId, msg.sender, _reason);
}
/**
* @dev Resolves a dispute
* @param _bountyId Bounty ID
* @param _submissionId Submission ID
* @param _upholdDispute Whether to uphold the dispute
*/
function resolveDispute(
uint256 _bountyId,
uint256 _submissionId,
bool _upholdDispute
) external onlyOwner bountyExists(_bountyId) nonReentrant {
Bounty storage bounty = bounties[_bountyId];
Submission storage submission = submissions[_submissionId];
require(bounty.status == BountyStatus.DISPUTED, "No dispute to resolve");
require(submission.status == SubmissionStatus.DISPUTED, "Submission not disputed");
if (_upholdDispute) {
// Reject the submission
submission.status = SubmissionStatus.REJECTED;
bounty.status = BountyStatus.ACTIVE;
// Return dispute fee
uint256 disputeFee = (bounty.rewardAmount * disputeFeePercentage) / 10000;
if (disputeFee > 0) {
require(aitbcToken.transfer(msg.sender, disputeFee), "Dispute fee return failed");
}
} else {
// Uphold the submission
submission.status = SubmissionStatus.VERIFIED;
_completeBounty(_bountyId, _submissionId);
}
}
/**
* @dev Expires a bounty and returns funds to creator
* @param _bountyId Bounty ID
*/
function expireBounty(uint256 _bountyId) external bountyExists(_bountyId) nonReentrant {
Bounty storage bounty = bounties[_bountyId];
require(bounty.status == BountyStatus.ACTIVE, "Bounty not active");
require(block.timestamp > bounty.deadline, "Deadline not passed");
bounty.status = BountyStatus.EXPIRED;
// Return funds to creator
uint256 refundAmount = bounty.rewardAmount;
require(aitbcToken.transfer(bounty.creator, refundAmount), "Refund transfer failed");
// Remove from active bounties
_removeFromActiveBounties(_bountyId);
emit BountyExpired(_bountyId, refundAmount);
}
/**
* @dev Authorizes a creator to create bounties
* @param _creator Address to authorize
*/
function authorizeCreator(address _creator) external onlyOwner {
require(_creator != address(0), "Invalid address");
require(!isAuthorizedCreator(_creator), "Already authorized");
authorizedCreators.push(_creator);
bounties[0].authorizedSubmitters[_creator] = true; // Use bounty 0 as storage
}
/**
* @dev Revokes creator authorization
* @param _creator Address to revoke
*/
function revokeCreator(address _creator) external onlyOwner {
require(isAuthorizedCreator(_creator), "Not authorized");
bounties[0].authorizedSubmitters[_creator] = false; // Use bounty 0 as storage
// Remove from array
for (uint256 i = 0; i < authorizedCreators.length; i++) {
if (authorizedCreators[i] == _creator) {
authorizedCreators[i] = authorizedCreators[authorizedCreators.length - 1];
authorizedCreators.pop();
break;
}
}
}
/**
* @dev Updates fee percentages
* @param _creationFee New creation fee percentage
* @param _successFee New success fee percentage
* @param _platformFee New platform fee percentage
*/
function updateFees(
uint256 _creationFee,
uint256 _successFee,
uint256 _platformFee
) external onlyOwner {
require(_creationFee <= 500, "Creation fee too high"); // Max 5%
require(_successFee <= 500, "Success fee too high"); // Max 5%
require(_platformFee <= 500, "Platform fee too high"); // Max 5%
creationFeePercentage = _creationFee;
successFeePercentage = _successFee;
platformFeePercentage = _platformFee;
}
/**
* @dev Updates tier requirements
* @param _tier Bounty tier
* @param _minimumReward New minimum reward
*/
function updateTierRequirement(BountyTier _tier, uint256 _minimumReward) external onlyOwner {
tierRequirements[_tier] = _minimumReward;
}
// View functions
/**
* @dev Gets bounty details
* @param _bountyId Bounty ID
*/
function getBounty(uint256 _bountyId) external view bountyExists(_bountyId) returns (
string memory title,
string memory description,
uint256 rewardAmount,
address creator,
BountyTier tier,
BountyStatus status,
bytes32 performanceCriteria,
uint256 minAccuracy,
uint256 deadline,
uint256 creationTime,
uint256 maxSubmissions,
uint256 submissionCount,
bool requiresZKProof
) {
Bounty storage bounty = bounties[_bountyId];
return (
bounty.title,
bounty.description,
bounty.rewardAmount,
bounty.creator,
bounty.tier,
bounty.status,
bounty.performanceCriteria,
bounty.minAccuracy,
bounty.deadline,
bounty.creationTime,
bounty.maxSubmissions,
bounty.submissionCount,
bounty.requiresZKProof
);
}
/**
* @dev Gets submission details
* @param _submissionId Submission ID
*/
function getSubmission(uint256 _submissionId) external view returns (
uint256 bountyId,
address submitter,
bytes32 performanceHash,
uint256 accuracy,
uint256 responseTime,
uint256 submissionTime,
SubmissionStatus status,
address verifier
) {
Submission storage submission = submissions[_submissionId];
return (
submission.bountyId,
submission.submitter,
submission.performanceHash,
submission.accuracy,
submission.responseTime,
submission.submissionTime,
submission.status,
submission.verifier
);
}
/**
* @dev Gets all submissions for a bounty
* @param _bountyId Bounty ID
*/
function getBountySubmissions(uint256 _bountyId) external view bountyExists(_bountyId) returns (uint256[] memory) {
return bountySubmissions[_bountyId];
}
/**
* @dev Gets all bounties created by a user
* @param _creator Creator address
*/
function getCreatorBounties(address _creator) external view returns (uint256[] memory) {
return creatorBounties[_creator];
}
/**
* @dev Gets all submissions by a user
* @param _submitter Submitter address
*/
function getUserSubmissions(address _submitter) external view returns (uint256[] memory) {
return userSubmissions[_submitter];
}
/**
* @dev Gets all active bounty IDs
*/
function getActiveBounties() external view returns (uint256[] memory) {
return activeBountyIds;
}
/**
* @dev Gets bounty statistics
*/
function getBountyStats() external view returns (BountyStats memory) {
uint256 totalValue = 0;
uint256 activeCount = 0;
uint256 completedCount = 0;
for (uint256 i = 0; i < bountyCounter; i++) {
if (bounties[i].status == BountyStatus.ACTIVE) {
activeCount++;
totalValue += bounties[i].rewardAmount;
} else if (bounties[i].status == BountyStatus.COMPLETED) {
completedCount++;
totalValue += bounties[i].rewardAmount;
}
}
uint256 avgReward = bountyCounter > 0 ? totalValue / bountyCounter : 0;
uint256 successRate = completedCount > 0 ? (completedCount * 100) / bountyCounter : 0;
return BountyStats({
totalBounties: bountyCounter,
activeBounties: activeCount,
completedBounties: completedCount,
totalValueLocked: totalValue,
averageReward: avgReward,
successRate: successRate
});
}
/**
* @dev Checks if an address is authorized to create bounties
* @param _creator Address to check
*/
function isAuthorizedCreator(address _creator) public view returns (bool) {
return bounties[0].authorizedSubmitters[_creator]; // Use bounty 0 as storage
}
// Internal functions
function _verifySubmission(uint256 _bountyId, uint256 _submissionId) internal {
Bounty storage bounty = bounties[_bountyId];
Submission storage submission = submissions[_submissionId];
// Verify ZK-proof using PerformanceVerifier
bool proofValid = performanceVerifier.verifyPerformanceProof(
0, // Use dummy agreement ID for bounty verification
submission.responseTime,
submission.accuracy,
95, // Default availability
100, // Default compute power
submission.zkProof
);
if (proofValid && submission.accuracy >= bounty.minAccuracy) {
submission.status = SubmissionStatus.VERIFIED;
_completeBounty(_bountyId, _submissionId);
} else {
submission.status = SubmissionStatus.REJECTED;
}
}
function _completeBounty(uint256 _bountyId, uint256 _submissionId) internal {
Bounty storage bounty = bounties[_bountyId];
Submission storage submission = submissions[_submissionId];
require(bounty.status == BountyStatus.ACTIVE || bounty.status == BountyStatus.SUBMITTED, "Bounty not active");
bounty.status = BountyStatus.COMPLETED;
bounty.winningSubmission = submission.submitter;
// Calculate fees
uint256 successFee = (bounty.rewardAmount * successFeePercentage) / 10000;
uint256 platformFee = (bounty.rewardAmount * platformFeePercentage) / 10000;
uint256 totalFees = successFee + platformFee;
uint256 winnerReward = bounty.rewardAmount - totalFees;
// Transfer reward to winner
if (winnerReward > 0) {
require(aitbcToken.transfer(submission.submitter, winnerReward), "Reward transfer failed");
}
// Transfer fees to treasury
if (totalFees > 0) {
require(aitbcToken.transfer(owner(), totalFees), "Fee transfer failed");
emit PlatformFeeCollected(_bountyId, totalFees, owner());
}
// Remove from active bounties
_removeFromActiveBounties(_bountyId);
emit BountyCompleted(_bountyId, submission.submitter, winnerReward, block.timestamp);
}
function _removeFromActiveBounties(uint256 _bountyId) internal {
for (uint256 i = 0; i < activeBountyIds.length; i++) {
if (activeBountyIds[i] == _bountyId) {
activeBountyIds[i] = activeBountyIds[activeBountyIds.length - 1];
activeBountyIds.pop();
break;
}
}
}
/**
* @dev Emergency pause function
*/
function pause() external onlyOwner {
_pause();
}
/**
* @dev Unpause function
*/
function unpause() external onlyOwner {
_unpause();
}
}

827
contracts/AgentStaking.sol Normal file
View File

@@ -0,0 +1,827 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/security/Pausable.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "./PerformanceVerifier.sol";
import "./AIToken.sol";
/**
* @title Agent Staking System
* @dev Reputation-based yield farming for AI agents with dynamic APY calculation
* @notice Allows users to stake AITBC tokens on agent wallets and earn rewards based on agent performance
*/
contract AgentStaking is Ownable, ReentrancyGuard, Pausable {
// State variables
IERC20 public aitbcToken;
PerformanceVerifier public performanceVerifier;
uint256 public stakeCounter;
uint256 public baseAPY = 500; // 5% base APY in basis points
uint256 public maxAPY = 2000; // 20% max APY in basis points
uint256 public minStakeAmount = 100 * 10**18; // 100 AITBC minimum
uint256 public maxStakeAmount = 100000 * 10**18; // 100k AITBC maximum
uint256 public unbondingPeriod = 7 days;
uint256 public rewardDistributionInterval = 1 days;
uint256 public platformFeePercentage = 100; // 1% platform fee
uint256 public earlyUnbondPenalty = 1000; // 10% penalty for early unbonding
// Staking status
enum StakeStatus { ACTIVE, UNBONDING, COMPLETED, SLASHED }
// Agent performance tier
enum PerformanceTier { BRONZE, SILVER, GOLD, PLATINUM, DIAMOND }
// Structs
struct Stake {
uint256 stakeId;
address staker;
address agentWallet;
uint256 amount;
uint256 lockPeriod;
uint256 startTime;
uint256 endTime;
StakeStatus status;
uint256 accumulatedRewards;
uint256 lastRewardTime;
uint256 currentAPY;
PerformanceTier agentTier;
bool autoCompound;
}
struct AgentMetrics {
address agentWallet;
uint256 totalStaked;
uint256 stakerCount;
uint256 totalRewardsDistributed;
uint256 averageAccuracy;
uint256 totalSubmissions;
uint256 successfulSubmissions;
uint256 lastUpdateTime;
PerformanceTier currentTier;
uint256 tierScore;
}
struct StakingPool {
address agentWallet;
uint256 totalStaked;
uint256 totalRewards;
uint256 poolAPY;
uint256 lastDistributionTime;
mapping(address => uint256) stakerShares;
address[] stakers;
}
struct RewardCalculation {
uint256 baseRewards;
uint256 performanceBonus;
uint256 lockBonus;
uint256 tierBonus;
uint256 totalRewards;
uint256 platformFee;
}
// Mappings
mapping(uint256 => Stake) public stakes;
mapping(address => uint256[]) public stakerStakes;
mapping(address => uint256[]) public agentStakes;
mapping(address => AgentMetrics) public agentMetrics;
mapping(address => StakingPool) public stakingPools;
mapping(PerformanceTier => uint256) public tierMultipliers;
mapping(uint256 => uint256) public lockPeriodMultipliers;
// Arrays
address[] public supportedAgents;
uint256[] public activeStakeIds;
// Events
event StakeCreated(
uint256 indexed stakeId,
address indexed staker,
address indexed agentWallet,
uint256 amount,
uint256 lockPeriod,
uint256 apy
);
event StakeUpdated(
uint256 indexed stakeId,
uint256 newAmount,
uint256 newAPY
);
event RewardsDistributed(
uint256 indexed stakeId,
address indexed staker,
uint256 rewardAmount,
uint256 platformFee
);
event StakeUnbonded(
uint256 indexed stakeId,
address indexed staker,
uint256 amount,
uint256 penalty
);
event StakeCompleted(
uint256 indexed stakeId,
address indexed staker,
uint256 totalAmount,
uint256 totalRewards
);
event AgentTierUpdated(
address indexed agentWallet,
PerformanceTier oldTier,
PerformanceTier newTier,
uint256 tierScore
);
event PoolRewardsDistributed(
address indexed agentWallet,
uint256 totalRewards,
uint256 stakerCount
);
event PlatformFeeCollected(
uint256 indexed stakeId,
uint256 feeAmount,
address indexed collector
);
// Modifiers
modifier stakeExists(uint256 _stakeId) {
require(_stakeId < stakeCounter, "Stake does not exist");
_;
}
modifier onlyStakeOwner(uint256 _stakeId) {
require(stakes[_stakeId].staker == msg.sender, "Not stake owner");
_;
}
modifier supportedAgent(address _agentWallet) {
require(agentMetrics[_agentWallet].agentWallet != address(0) || _agentWallet == address(0), "Agent not supported");
_;
}
modifier validStakeAmount(uint256 _amount) {
require(_amount >= minStakeAmount && _amount <= maxStakeAmount, "Invalid stake amount");
_;
}
modifier sufficientBalance(uint256 _amount) {
require(aitbcToken.balanceOf(msg.sender) >= _amount, "Insufficient balance");
_;
}
constructor(address _aitbcToken, address _performanceVerifier) {
aitbcToken = IERC20(_aitbcToken);
performanceVerifier = PerformanceVerifier(_performanceVerifier);
// Set tier multipliers (in basis points)
tierMultipliers[PerformanceTier.BRONZE] = 1000; // 1x
tierMultipliers[PerformanceTier.SILVER] = 1200; // 1.2x
tierMultipliers[PerformanceTier.GOLD] = 1500; // 1.5x
tierMultipliers[PerformanceTier.PLATINUM] = 2000; // 2x
tierMultipliers[PerformanceTier.DIAMOND] = 3000; // 3x
// Set lock period multipliers
lockPeriodMultipliers[30 days] = 1100; // 1.1x for 30 days
lockPeriodMultipliers[90 days] = 1250; // 1.25x for 90 days
lockPeriodMultipliers[180 days] = 1500; // 1.5x for 180 days
lockPeriodMultipliers[365 days] = 2000; // 2x for 365 days
}
/**
* @dev Creates a new stake on an agent wallet
* @param _agentWallet Address of the agent wallet
* @param _amount Amount to stake
* @param _lockPeriod Lock period in seconds
* @param _autoCompound Whether to auto-compound rewards
*/
function stakeOnAgent(
address _agentWallet,
uint256 _amount,
uint256 _lockPeriod,
bool _autoCompound
) external
supportedAgent(_agentWallet)
validStakeAmount(_amount)
sufficientBalance(_amount)
nonReentrant
returns (uint256)
{
require(_lockPeriod >= 1 days, "Lock period too short");
require(_lockPeriod <= 365 days, "Lock period too long");
uint256 stakeId = stakeCounter++;
// Calculate initial APY
PerformanceTier agentTier = _getAgentTier(_agentWallet);
uint256 apy = _calculateAPY(_agentWallet, _lockPeriod, agentTier);
Stake storage stake = stakes[stakeId];
stake.stakeId = stakeId;
stake.staker = msg.sender;
stake.agentWallet = _agentWallet;
stake.amount = _amount;
stake.lockPeriod = _lockPeriod;
stake.startTime = block.timestamp;
stake.endTime = block.timestamp + _lockPeriod;
stake.status = StakeStatus.ACTIVE;
stake.accumulatedRewards = 0;
stake.lastRewardTime = block.timestamp;
stake.currentAPY = apy;
stake.agentTier = agentTier;
stake.autoCompound = _autoCompound;
// Update agent metrics
_updateAgentMetrics(_agentWallet, _amount, true);
// Update staking pool
_updateStakingPool(_agentWallet, msg.sender, _amount, true);
// Update tracking arrays
stakerStakes[msg.sender].push(stakeId);
agentStakes[_agentWallet].push(stakeId);
activeStakeIds.push(stakeId);
// Transfer tokens to contract
require(aitbcToken.transferFrom(msg.sender, address(this), _amount), "Transfer failed");
emit StakeCreated(stakeId, msg.sender, _agentWallet, _amount, _lockPeriod, apy);
return stakeId;
}
/**
* @dev Adds more tokens to an existing stake
* @param _stakeId Stake ID
* @param _additionalAmount Additional amount to stake
*/
function addToStake(
uint256 _stakeId,
uint256 _additionalAmount
) external
stakeExists(_stakeId)
onlyStakeOwner(_stakeId)
validStakeAmount(_additionalAmount)
sufficientBalance(_additionalAmount)
nonReentrant
{
Stake storage stake = stakes[_stakeId];
require(stake.status == StakeStatus.ACTIVE, "Stake not active");
// Calculate new APY
uint256 newTotalAmount = stake.amount + _additionalAmount;
uint256 newAPY = _calculateAPY(stake.agentWallet, stake.lockPeriod, stake.agentTier);
// Update stake
stake.amount = newTotalAmount;
stake.currentAPY = newAPY;
// Update agent metrics
_updateAgentMetrics(stake.agentWallet, _additionalAmount, true);
// Update staking pool
_updateStakingPool(stake.agentWallet, msg.sender, _additionalAmount, true);
// Transfer additional tokens
require(aitbcToken.transferFrom(msg.sender, address(this), _additionalAmount), "Transfer failed");
emit StakeUpdated(_stakeId, newTotalAmount, newAPY);
}
/**
* @dev Initiates unbonding for a stake
* @param _stakeId Stake ID
*/
function unbondStake(uint256 _stakeId) external
stakeExists(_stakeId)
onlyStakeOwner(_stakeId)
nonReentrant
{
Stake storage stake = stakes[_stakeId];
require(stake.status == StakeStatus.ACTIVE, "Stake not active");
require(block.timestamp >= stake.endTime, "Lock period not ended");
// Calculate final rewards
_calculateRewards(_stakeId);
stake.status = StakeStatus.UNBONDING;
// Remove from active stakes
_removeFromActiveStakes(_stakeId);
}
/**
* @dev Completes unbonding and returns stake + rewards
* @param _stakeId Stake ID
*/
function completeUnbonding(uint256 _stakeId) external
stakeExists(_stakeId)
onlyStakeOwner(_stakeId)
nonReentrant
{
Stake storage stake = stakes[_stakeId];
require(stake.status == StakeStatus.UNBONDING, "Stake not unbonding");
require(block.timestamp >= stake.endTime + unbondingPeriod, "Unbonding period not ended");
uint256 totalAmount = stake.amount;
uint256 totalRewards = stake.accumulatedRewards;
// Apply early unbonding penalty if applicable
uint256 penalty = 0;
if (block.timestamp < stake.endTime + 30 days) {
penalty = (totalAmount * earlyUnbondPenalty) / 10000;
totalAmount -= penalty;
}
stake.status = StakeStatus.COMPLETED;
// Update agent metrics
_updateAgentMetrics(stake.agentWallet, stake.amount, false);
// Update staking pool
_updateStakingPool(stake.agentWallet, msg.sender, stake.amount, false);
// Transfer tokens back to staker
if (totalAmount > 0) {
require(aitbcToken.transfer(msg.sender, totalAmount), "Stake transfer failed");
}
if (totalRewards > 0) {
require(aitbcToken.transfer(msg.sender, totalRewards), "Rewards transfer failed");
}
emit StakeCompleted(_stakeId, msg.sender, totalAmount, totalRewards);
emit StakeUnbonded(_stakeId, msg.sender, totalAmount, penalty);
}
/**
* @dev Distributes agent earnings to stakers
* @param _agentWallet Agent wallet address
* @param _totalEarnings Total earnings to distribute
*/
function distributeAgentEarnings(
address _agentWallet,
uint256 _totalEarnings
) external
supportedAgent(_agentWallet)
nonReentrant
{
require(_totalEarnings > 0, "No earnings to distribute");
StakingPool storage pool = stakingPools[_agentWallet];
require(pool.totalStaked > 0, "No stakers in pool");
// Calculate platform fee
uint256 platformFee = (_totalEarnings * platformFeePercentage) / 10000;
uint256 distributableAmount = _totalEarnings - platformFee;
// Transfer platform fee
if (platformFee > 0) {
require(aitbcToken.transferFrom(msg.sender, owner(), platformFee), "Platform fee transfer failed");
}
// Transfer distributable amount to contract
require(aitbcToken.transferFrom(msg.sender, address(this), distributableAmount), "Earnings transfer failed");
// Distribute to stakers proportionally
uint256 totalDistributed = 0;
for (uint256 i = 0; i < pool.stakers.length; i++) {
address staker = pool.stakers[i];
uint256 stakerShare = pool.stakerShares[staker];
uint256 stakerReward = (distributableAmount * stakerShare) / pool.totalStaked;
if (stakerReward > 0) {
// Find and update all stakes for this staker on this agent
uint256[] storage stakesForAgent = agentStakes[_agentWallet];
for (uint256 j = 0; j < stakesForAgent.length; j++) {
uint256 stakeId = stakesForAgent[j];
Stake storage stake = stakes[stakeId];
if (stake.staker == staker && stake.status == StakeStatus.ACTIVE) {
stake.accumulatedRewards += stakerReward;
break;
}
}
totalDistributed += stakerReward;
}
}
// Update agent metrics
agentMetrics[_agentWallet].totalRewardsDistributed += totalDistributed;
emit PoolRewardsDistributed(_agentWallet, totalDistributed, pool.stakers.length);
}
/**
* @dev Updates agent performance metrics and tier
* @param _agentWallet Agent wallet address
* @param _accuracy Latest accuracy score
* @param _successful Whether the submission was successful
*/
function updateAgentPerformance(
address _agentWallet,
uint256 _accuracy,
bool _successful
) external
supportedAgent(_agentWallet)
nonReentrant
{
AgentMetrics storage metrics = agentMetrics[_agentWallet];
metrics.totalSubmissions++;
if (_successful) {
metrics.successfulSubmissions++;
}
// Update average accuracy (weighted average)
uint256 totalAccuracy = metrics.averageAccuracy * (metrics.totalSubmissions - 1) + _accuracy;
metrics.averageAccuracy = totalAccuracy / metrics.totalSubmissions;
metrics.lastUpdateTime = block.timestamp;
// Calculate new tier
PerformanceTier newTier = _calculateAgentTier(_agentWallet);
PerformanceTier oldTier = metrics.currentTier;
if (newTier != oldTier) {
metrics.currentTier = newTier;
// Update APY for all active stakes on this agent
uint256[] storage stakesForAgent = agentStakes[_agentWallet];
for (uint256 i = 0; i < stakesForAgent.length; i++) {
uint256 stakeId = stakesForAgent[i];
Stake storage stake = stakes[stakeId];
if (stake.status == StakeStatus.ACTIVE) {
stake.currentAPY = _calculateAPY(_agentWallet, stake.lockPeriod, newTier);
stake.agentTier = newTier;
}
}
emit AgentTierUpdated(_agentWallet, oldTier, newTier, metrics.tierScore);
}
}
/**
* @dev Adds a supported agent
* @param _agentWallet Agent wallet address
* @param _initialTier Initial performance tier
*/
function addSupportedAgent(
address _agentWallet,
PerformanceTier _initialTier
) external onlyOwner {
require(_agentWallet != address(0), "Invalid agent address");
require(agentMetrics[_agentWallet].agentWallet == address(0), "Agent already supported");
agentMetrics[_agentWallet] = AgentMetrics({
agentWallet: _agentWallet,
totalStaked: 0,
stakerCount: 0,
totalRewardsDistributed: 0,
averageAccuracy: 0,
totalSubmissions: 0,
successfulSubmissions: 0,
lastUpdateTime: block.timestamp,
currentTier: _initialTier,
tierScore: _getTierScore(_initialTier)
});
// Initialize staking pool
stakingPools[_agentWallet].agentWallet = _agentWallet;
stakingPools[_agentWallet].totalStaked = 0;
stakingPools[_agentWallet].totalRewards = 0;
stakingPools[_agentWallet].poolAPY = baseAPY;
stakingPools[_agentWallet].lastDistributionTime = block.timestamp;
supportedAgents.push(_agentWallet);
}
/**
* @dev Removes a supported agent
* @param _agentWallet Agent wallet address
*/
function removeSupportedAgent(address _agentWallet) external onlyOwner {
require(agentMetrics[_agentWallet].agentWallet != address(0), "Agent not supported");
require(agentMetrics[_agentWallet].totalStaked == 0, "Agent has active stakes");
// Remove from supported agents
for (uint256 i = 0; i < supportedAgents.length; i++) {
if (supportedAgents[i] == _agentWallet) {
supportedAgents[i] = supportedAgents[supportedAgents.length - 1];
supportedAgents.pop();
break;
}
}
delete agentMetrics[_agentWallet];
delete stakingPools[_agentWallet];
}
/**
* @dev Updates configuration parameters
* @param _baseAPY New base APY
* @param _maxAPY New maximum APY
* @param _platformFee New platform fee percentage
*/
function updateConfiguration(
uint256 _baseAPY,
uint256 _maxAPY,
uint256 _platformFee
) external onlyOwner {
require(_baseAPY <= _maxAPY, "Base APY cannot exceed max APY");
require(_maxAPY <= 5000, "Max APY too high"); // Max 50%
require(_platformFee <= 500, "Platform fee too high"); // Max 5%
baseAPY = _baseAPY;
maxAPY = _maxAPY;
platformFeePercentage = _platformFee;
}
// View functions
/**
* @dev Gets stake details
* @param _stakeId Stake ID
*/
function getStake(uint256 _stakeId) external view stakeExists(_stakeId) returns (
address staker,
address agentWallet,
uint256 amount,
uint256 lockPeriod,
uint256 startTime,
uint256 endTime,
StakeStatus status,
uint256 accumulatedRewards,
uint256 currentAPY,
PerformanceTier agentTier,
bool autoCompound
) {
Stake storage stake = stakes[_stakeId];
return (
stake.staker,
stake.agentWallet,
stake.amount,
stake.lockPeriod,
stake.startTime,
stake.endTime,
stake.status,
stake.accumulatedRewards,
stake.currentAPY,
stake.agentTier,
stake.autoCompound
);
}
/**
* @dev Gets agent metrics
* @param _agentWallet Agent wallet address
*/
function getAgentMetrics(address _agentWallet) external view returns (
uint256 totalStaked,
uint256 stakerCount,
uint256 totalRewardsDistributed,
uint256 averageAccuracy,
uint256 totalSubmissions,
uint256 successfulSubmissions,
PerformanceTier currentTier,
uint256 tierScore
) {
AgentMetrics storage metrics = agentMetrics[_agentWallet];
return (
metrics.totalStaked,
metrics.stakerCount,
metrics.totalRewardsDistributed,
metrics.averageAccuracy,
metrics.totalSubmissions,
metrics.successfulSubmissions,
metrics.currentTier,
metrics.tierScore
);
}
/**
* @dev Gets staking pool information
* @param _agentWallet Agent wallet address
*/
function getStakingPool(address _agentWallet) external view returns (
uint256 totalStaked,
uint256 totalRewards,
uint256 poolAPY,
uint256 stakerCount
) {
StakingPool storage pool = stakingPools[_agentWallet];
return (
pool.totalStaked,
pool.totalRewards,
pool.poolAPY,
pool.stakers.length
);
}
/**
* @dev Calculates current rewards for a stake
* @param _stakeId Stake ID
*/
function calculateRewards(uint256 _stakeId) external view stakeExists(_stakeId) returns (uint256) {
Stake storage stake = stakes[_stakeId];
if (stake.status != StakeStatus.ACTIVE) {
return stake.accumulatedRewards;
}
uint256 timeElapsed = block.timestamp - stake.lastRewardTime;
uint256 yearlyRewards = (stake.amount * stake.currentAPY) / 10000;
uint256 currentRewards = (yearlyRewards * timeElapsed) / 365 days;
return stake.accumulatedRewards + currentRewards;
}
/**
* @dev Gets all stakes for a staker
* @param _staker Staker address
*/
function getStakerStakes(address _staker) external view returns (uint256[] memory) {
return stakerStakes[_staker];
}
/**
* @dev Gets all stakes for an agent
* @param _agentWallet Agent wallet address
*/
function getAgentStakes(address _agentWallet) external view returns (uint256[] memory) {
return agentStakes[_agentWallet];
}
/**
* @dev Gets all supported agents
*/
function getSupportedAgents() external view returns (address[] memory) {
return supportedAgents;
}
/**
* @dev Gets all active stake IDs
*/
function getActiveStakes() external view returns (uint256[] memory) {
return activeStakeIds;
}
/**
* @dev Calculates APY for a stake
* @param _agentWallet Agent wallet address
* @param _lockPeriod Lock period
* @param _agentTier Agent performance tier
*/
function calculateAPY(
address _agentWallet,
uint256 _lockPeriod,
PerformanceTier _agentTier
) external view returns (uint256) {
return _calculateAPY(_agentWallet, _lockPeriod, _agentTier);
}
// Internal functions
function _calculateAPY(
address _agentWallet,
uint256 _lockPeriod,
PerformanceTier _agentTier
) internal view returns (uint256) {
uint256 tierMultiplier = tierMultipliers[_agentTier];
uint256 lockMultiplier = lockPeriodMultipliers[_lockPeriod];
uint256 apy = (baseAPY * tierMultiplier * lockMultiplier) / (10000 * 10000);
// Cap at maximum APY
return apy > maxAPY ? maxAPY : apy;
}
function _calculateRewards(uint256 _stakeId) internal {
Stake storage stake = stakes[_stakeId];
if (stake.status != StakeStatus.ACTIVE) {
return;
}
uint256 timeElapsed = block.timestamp - stake.lastRewardTime;
uint256 yearlyRewards = (stake.amount * stake.currentAPY) / 10000;
uint256 currentRewards = (yearlyRewards * timeElapsed) / 365 days;
stake.accumulatedRewards += currentRewards;
stake.lastRewardTime = block.timestamp;
// Auto-compound if enabled
if (stake.autoCompound && currentRewards >= minStakeAmount) {
stake.amount += currentRewards;
stake.accumulatedRewards = 0;
}
}
function _getAgentTier(address _agentWallet) internal view returns (PerformanceTier) {
AgentMetrics storage metrics = agentMetrics[_agentWallet];
return metrics.currentTier;
}
function _calculateAgentTier(address _agentWallet) internal view returns (PerformanceTier) {
AgentMetrics storage metrics = agentMetrics[_agentWallet];
uint256 successRate = metrics.totalSubmissions > 0 ?
(metrics.successfulSubmissions * 100) / metrics.totalSubmissions : 0;
uint256 score = (metrics.averageAccuracy * 50) / 100 + (successRate * 50) / 100;
if (score >= 95) return PerformanceTier.DIAMOND;
if (score >= 90) return PerformanceTier.PLATINUM;
if (score >= 80) return PerformanceTier.GOLD;
if (score >= 70) return PerformanceTier.SILVER;
return PerformanceTier.BRONZE;
}
function _getTierScore(PerformanceTier _tier) internal pure returns (uint256) {
if (_tier == PerformanceTier.DIAMOND) return 95;
if (_tier == PerformanceTier.PLATINUM) return 90;
if (_tier == PerformanceTier.GOLD) return 80;
if (_tier == PerformanceTier.SILVER) return 70;
return 60;
}
function _updateAgentMetrics(address _agentWallet, uint256 _amount, bool _isStake) internal {
AgentMetrics storage metrics = agentMetrics[_agentWallet];
if (_isStake) {
metrics.totalStaked += _amount;
if (metrics.totalStaked == _amount) {
metrics.stakerCount = 1;
}
} else {
metrics.totalStaked -= _amount;
if (metrics.totalStaked == 0) {
metrics.stakerCount = 0;
}
}
metrics.currentTier = _calculateAgentTier(_agentWallet);
metrics.tierScore = _getTierScore(metrics.currentTier);
}
function _updateStakingPool(address _agentWallet, address _staker, uint256 _amount, bool _isStake) internal {
StakingPool storage pool = stakingPools[_agentWallet];
if (_isStake) {
if (pool.stakerShares[_staker] == 0) {
pool.stakers.push(_staker);
}
pool.stakerShares[_staker] += _amount;
pool.totalStaked += _amount;
} else {
pool.stakerShares[_staker] -= _amount;
pool.totalStaked -= _amount;
// Remove staker from array if no shares left
if (pool.stakerShares[_staker] == 0) {
for (uint256 i = 0; i < pool.stakers.length; i++) {
if (pool.stakers[i] == _staker) {
pool.stakers[i] = pool.stakers[pool.stakers.length - 1];
pool.stakers.pop();
break;
}
}
}
}
// Update pool APY
if (pool.totalStaked > 0) {
pool.poolAPY = _calculateAPY(_agentWallet, 30 days, agentMetrics[_agentWallet].currentTier);
}
}
function _removeFromActiveStakes(uint256 _stakeId) internal {
for (uint256 i = 0; i < activeStakeIds.length; i++) {
if (activeStakeIds[i] == _stakeId) {
activeStakeIds[i] = activeStakeIds[activeStakeIds.length - 1];
activeStakeIds.pop();
break;
}
}
}
/**
* @dev Emergency pause function
*/
function pause() external onlyOwner {
_pause();
}
/**
* @dev Unpause function
*/
function unpause() external onlyOwner {
_unpause();
}
}

View File

@@ -0,0 +1,616 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "./AgentBounty.sol";
import "./AgentStaking.sol";
import "./PerformanceVerifier.sol";
import "./AIToken.sol";
/**
* @title Bounty Integration Layer
* @dev Bridges PerformanceVerifier with bounty and staking contracts
* @notice Handles automatic bounty completion detection and cross-contract event handling
*/
contract BountyIntegration is Ownable, ReentrancyGuard {
// State variables
AgentBounty public agentBounty;
AgentStaking public agentStaking;
PerformanceVerifier public performanceVerifier;
AIToken public aitbcToken;
uint256 public integrationCounter;
uint256 public autoVerificationThreshold = 90; // 90% accuracy for auto-verification
uint256 public batchProcessingLimit = 50;
uint256 public gasOptimizationThreshold = 100000;
// Integration status
enum IntegrationStatus { PENDING, PROCESSING, COMPLETED, FAILED }
// Performance to bounty mapping
struct PerformanceMapping {
uint256 mappingId;
bytes32 performanceHash;
uint256 bountyId;
uint256 submissionId;
IntegrationStatus status;
uint256 createdAt;
uint256 processedAt;
string errorMessage;
}
// Batch processing
struct BatchRequest {
uint256 batchId;
uint256[] bountyIds;
uint256[] submissionIds;
bytes32[] performanceHashes;
uint256[] accuracies;
uint256[] responseTimes;
IntegrationStatus status;
uint256 createdAt;
uint256 processedAt;
uint256 successCount;
uint256 failureCount;
}
// Event handlers
struct EventHandler {
bytes32 eventType;
address targetContract;
bytes4 functionSelector;
bool isActive;
uint256 priority;
}
// Mappings
mapping(uint256 => PerformanceMapping) public performanceMappings;
mapping(bytes32 => uint256) public performanceHashToMapping;
mapping(uint256 => BatchRequest) public batchRequests;
mapping(bytes32 => EventHandler) public eventHandlers;
mapping(address => bool) public authorizedIntegrators;
// Arrays
uint256[] public pendingMappings;
bytes32[] public performanceHashes;
address[] public authorizedIntegratorList;
// Events
event PerformanceMapped(
uint256 indexed mappingId,
bytes32 indexed performanceHash,
uint256 indexed bountyId,
uint256 submissionId
);
event BountyAutoCompleted(
uint256 indexed bountyId,
uint256 indexed submissionId,
address indexed submitter,
uint256 rewardAmount
);
event StakingRewardsTriggered(
address indexed agentWallet,
uint256 totalEarnings,
uint256 stakerCount
);
event BatchProcessed(
uint256 indexed batchId,
uint256 successCount,
uint256 failureCount,
uint256 gasUsed
);
event IntegrationFailed(
uint256 indexed mappingId,
string errorMessage,
bytes32 indexed performanceHash
);
event EventHandlerRegistered(
bytes32 indexed eventType,
address indexed targetContract,
bytes4 functionSelector
);
// Modifiers
modifier mappingExists(uint256 _mappingId) {
require(_mappingId < integrationCounter, "Mapping does not exist");
_;
}
modifier onlyAuthorizedIntegrator() {
require(authorizedIntegrators[msg.sender], "Not authorized integrator");
_;
}
modifier validPerformanceHash(bytes32 _performanceHash) {
require(_performanceHash != bytes32(0), "Invalid performance hash");
_;
}
constructor(
address _agentBounty,
address _agentStaking,
address _performanceVerifier,
address _aitbcToken
) {
agentBounty = AgentBounty(_agentBounty);
agentStaking = AgentStaking(_agentStaking);
performanceVerifier = PerformanceVerifier(_performanceVerifier);
aitbcToken = AIToken(_aitbcToken);
// Register default event handlers
_registerEventHandler(
keccak256("BOUNTY_COMPLETED"),
_agentStaking,
AgentStaking.distributeAgentEarnings.selector
);
_registerEventHandler(
keccak256("PERFORMANCE_VERIFIED"),
_agentBounty,
AgentBounty.verifySubmission.selector
);
}
/**
* @dev Maps performance verification to bounty completion
* @param _performanceHash Hash of performance metrics
* @param _bountyId Bounty ID
* @param _submissionId Submission ID
*/
function mapPerformanceToBounty(
bytes32 _performanceHash,
uint256 _bountyId,
uint256 _submissionId
) external
onlyAuthorizedIntegrator
validPerformanceHash(_performanceHash)
nonReentrant
returns (uint256)
{
require(performanceHashToMapping[_performanceHash] == 0, "Performance already mapped");
uint256 mappingId = integrationCounter++;
PerformanceMapping storage mapping = performanceMappings[mappingId];
mapping.mappingId = mappingId;
mapping.performanceHash = _performanceHash;
mapping.bountyId = _bountyId;
mapping.submissionId = _submissionId;
mapping.status = IntegrationStatus.PENDING;
mapping.createdAt = block.timestamp;
performanceHashToMapping[_performanceHash] = mappingId;
pendingMappings.push(mappingId);
performanceHashes.push(_performanceHash);
emit PerformanceMapped(mappingId, _performanceHash, _bountyId, _submissionId);
// Attempt auto-processing
_processMapping(mappingId);
return mappingId;
}
/**
* @dev Processes a single performance mapping
* @param _mappingId Mapping ID
*/
function processMapping(uint256 _mappingId) external
onlyAuthorizedIntegrator
mappingExists(_mappingId)
nonReentrant
{
_processMapping(_mappingId);
}
/**
* @dev Processes multiple mappings in a batch
* @param _mappingIds Array of mapping IDs
*/
function processBatchMappings(uint256[] calldata _mappingIds) external
onlyAuthorizedIntegrator
nonReentrant
{
require(_mappingIds.length <= batchProcessingLimit, "Batch too large");
uint256 batchId = integrationCounter++;
BatchRequest storage batch = batchRequests[batchId];
batch.batchId = batchId;
batch.bountyIds = new uint256[](_mappingIds.length);
batch.submissionIds = new uint256[](_mappingIds.length);
batch.performanceHashes = new bytes32[](_mappingIds.length);
batch.accuracies = new uint256[](_mappingIds.length);
batch.responseTimes = new uint256[](_mappingIds.length);
batch.status = IntegrationStatus.PROCESSING;
batch.createdAt = block.timestamp;
uint256 gasStart = gasleft();
uint256 successCount = 0;
uint256 failureCount = 0;
for (uint256 i = 0; i < _mappingIds.length; i++) {
try this._processMappingInternal(_mappingIds[i]) {
successCount++;
} catch {
failureCount++;
}
}
batch.successCount = successCount;
batch.failureCount = failureCount;
batch.processedAt = block.timestamp;
batch.status = IntegrationStatus.COMPLETED;
uint256 gasUsed = gasStart - gasleft();
emit BatchProcessed(batchId, successCount, failureCount, gasUsed);
}
/**
* @dev Auto-verifies bounty submissions based on performance metrics
* @param _bountyId Bounty ID
* @param _submissionId Submission ID
* @param _accuracy Achieved accuracy
* @param _responseTime Response time
*/
function autoVerifyBountySubmission(
uint256 _bountyId,
uint256 _submissionId,
uint256 _accuracy,
uint256 _responseTime
) external
onlyAuthorizedIntegrator
nonReentrant
{
// Get bounty details
(,,,,,, bytes32 performanceCriteria, uint256 minAccuracy,,,, bool requiresZKProof) = agentBounty.getBounty(_bountyId);
// Check if auto-verification conditions are met
if (_accuracy >= autoVerificationThreshold && _accuracy >= minAccuracy) {
// Verify the submission
agentBounty.verifySubmission(_bountyId, _submissionId, true, address(this));
// Get submission details to calculate rewards
(address submitter,,,,,,,) = agentBounty.getSubmission(_submissionId);
// Trigger staking rewards if applicable
_triggerStakingRewards(submitter, _accuracy);
emit BountyAutoCompleted(_bountyId, _submissionId, submitter, 0); // Reward amount will be set by bounty contract
}
}
/**
* @dev Handles performance verification events
* @param _verificationId Performance verification ID
* @param _accuracy Accuracy achieved
* @param _responseTime Response time
* @param _performanceHash Hash of performance metrics
*/
function handlePerformanceVerified(
uint256 _verificationId,
uint256 _accuracy,
uint256 _responseTime,
bytes32 _performanceHash
) external
onlyAuthorizedIntegrator
nonReentrant
{
// Check if this performance is mapped to any bounties
uint256 mappingId = performanceHashToMapping[_performanceHash];
if (mappingId > 0) {
PerformanceMapping storage mapping = performanceMappings[mappingId];
// Update agent staking metrics
(address submitter,,,,,,,) = agentBounty.getSubmission(mapping.submissionId);
agentStaking.updateAgentPerformance(submitter, _accuracy, _accuracy >= autoVerificationThreshold);
// Auto-verify bounty if conditions are met
_autoVerifyBounty(mapping.bountyId, mapping.submissionId, _accuracy, _responseTime);
}
}
/**
* @dev Registers an event handler for cross-contract communication
* @param _eventType Event type identifier
* @param _targetContract Target contract address
* @param _functionSelector Function selector to call
*/
function registerEventHandler(
bytes32 _eventType,
address _targetContract,
bytes4 _functionSelector
) external onlyOwner {
require(_targetContract != address(0), "Invalid target contract");
require(_functionSelector != bytes4(0), "Invalid function selector");
eventHandlers[_eventType] = EventHandler({
eventType: _eventType,
targetContract: _targetContract,
functionSelector: _functionSelector,
isActive: true,
priority: 0
});
emit EventHandlerRegistered(_eventType, _targetContract, _functionSelector);
}
/**
* @dev Authorizes an integrator address
* @param _integrator Address to authorize
*/
function authorizeIntegrator(address _integrator) external onlyOwner {
require(_integrator != address(0), "Invalid integrator address");
require(!authorizedIntegrators[_integrator], "Already authorized");
authorizedIntegrators[_integrator] = true;
authorizedIntegratorList.push(_integrator);
}
/**
* @dev Revokes integrator authorization
* @param _integrator Address to revoke
*/
function revokeIntegrator(address _integrator) external onlyOwner {
require(authorizedIntegrators[_integrator], "Not authorized");
authorizedIntegrators[_integrator] = false;
// Remove from list
for (uint256 i = 0; i < authorizedIntegratorList.length; i++) {
if (authorizedIntegratorList[i] == _integrator) {
authorizedIntegratorList[i] = authorizedIntegratorList[authorizedIntegratorList.length - 1];
authorizedIntegratorList.pop();
break;
}
}
}
/**
* @dev Updates configuration parameters
* @param _autoVerificationThreshold New auto-verification threshold
* @param _batchProcessingLimit New batch processing limit
* @param _gasOptimizationThreshold New gas optimization threshold
*/
function updateConfiguration(
uint256 _autoVerificationThreshold,
uint256 _batchProcessingLimit,
uint256 _gasOptimizationThreshold
) external onlyOwner {
require(_autoVerificationThreshold <= 100, "Invalid threshold");
require(_batchProcessingLimit <= 100, "Batch limit too high");
autoVerificationThreshold = _autoVerificationThreshold;
batchProcessingLimit = _batchProcessingLimit;
gasOptimizationThreshold = _gasOptimizationThreshold;
}
// View functions
/**
* @dev Gets performance mapping details
* @param _mappingId Mapping ID
*/
function getPerformanceMapping(uint256 _mappingId) external view mappingExists(_mappingId) returns (
bytes32 performanceHash,
uint256 bountyId,
uint256 submissionId,
IntegrationStatus status,
uint256 createdAt,
uint256 processedAt,
string memory errorMessage
) {
PerformanceMapping storage mapping = performanceMappings[_mappingId];
return (
mapping.performanceHash,
mapping.bountyId,
mapping.submissionId,
mapping.status,
mapping.createdAt,
mapping.processedAt,
mapping.errorMessage
);
}
/**
* @dev Gets batch request details
* @param _batchId Batch ID
*/
function getBatchRequest(uint256 _batchId) external view returns (
uint256[] memory bountyIds,
uint256[] memory submissionIds,
IntegrationStatus status,
uint256 createdAt,
uint256 processedAt,
uint256 successCount,
uint256 failureCount
) {
BatchRequest storage batch = batchRequests[_batchId];
return (
batch.bountyIds,
batch.submissionIds,
batch.status,
batch.createdAt,
batch.processedAt,
batch.successCount,
batch.failureCount
);
}
/**
* @dev Gets pending mappings
*/
function getPendingMappings() external view returns (uint256[] memory) {
return pendingMappings;
}
/**
* @dev Gets all performance hashes
*/
function getPerformanceHashes() external view returns (bytes32[] memory) {
return performanceHashes;
}
/**
* @dev Gets authorized integrators
*/
function getAuthorizedIntegrators() external view returns (address[] memory) {
return authorizedIntegratorList;
}
/**
* @dev Checks if an address is authorized
* @param _integrator Address to check
*/
function isAuthorizedIntegrator(address _integrator) external view returns (bool) {
return authorizedIntegrators[_integrator];
}
/**
* @dev Gets integration statistics
*/
function getIntegrationStats() external view returns (
uint256 totalMappings,
uint256 pendingCount,
uint256 completedCount,
uint256 failedCount,
uint256 averageProcessingTime
) {
uint256 completed = 0;
uint256 failed = 0;
uint256 totalTime = 0;
uint256 processedCount = 0;
for (uint256 i = 0; i < integrationCounter; i++) {
PerformanceMapping storage mapping = performanceMappings[i];
if (mapping.status == IntegrationStatus.COMPLETED) {
completed++;
totalTime += mapping.processedAt - mapping.createdAt;
processedCount++;
} else if (mapping.status == IntegrationStatus.FAILED) {
failed++;
}
}
uint256 avgTime = processedCount > 0 ? totalTime / processedCount : 0;
return (
integrationCounter,
pendingMappings.length,
completed,
failed,
avgTime
);
}
// Internal functions
function _processMapping(uint256 _mappingId) internal {
PerformanceMapping storage mapping = performanceMappings[_mappingId];
if (mapping.status != IntegrationStatus.PENDING) {
return;
}
try this._processMappingInternal(_mappingId) {
mapping.status = IntegrationStatus.COMPLETED;
mapping.processedAt = block.timestamp;
} catch Error(string memory reason) {
mapping.status = IntegrationStatus.FAILED;
mapping.errorMessage = reason;
mapping.processedAt = block.timestamp;
emit IntegrationFailed(_mappingId, reason, mapping.performanceHash);
} catch {
mapping.status = IntegrationStatus.FAILED;
mapping.errorMessage = "Unknown error";
mapping.processedAt = block.timestamp;
emit IntegrationFailed(_mappingId, "Unknown error", mapping.performanceHash);
}
// Remove from pending
_removeFromPending(_mappingId);
}
function _processMappingInternal(uint256 _mappingId) external {
PerformanceMapping storage mapping = performanceMappings[_mappingId];
// Get bounty details
(,,,,,, bytes32 performanceCriteria, uint256 minAccuracy,,,, bool requiresZKProof) = agentBounty.getBounty(mapping.bountyId);
// Get submission details
(address submitter, bytes32 submissionHash, uint256 accuracy, uint256 responseTime,,,) = agentBounty.getSubmission(mapping.submissionId);
// Verify performance criteria match
require(mapping.performanceHash == submissionHash, "Performance hash mismatch");
// Check if accuracy meets requirements
require(accuracy >= minAccuracy, "Accuracy below minimum");
// Auto-verify if conditions are met
if (accuracy >= autoVerificationThreshold) {
agentBounty.verifySubmission(mapping.bountyId, mapping.submissionId, true, address(this));
// Update agent staking metrics
agentStaking.updateAgentPerformance(submitter, accuracy, true);
// Trigger staking rewards
_triggerStakingRewards(submitter, accuracy);
}
}
function _autoVerifyBounty(
uint256 _bountyId,
uint256 _submissionId,
uint256 _accuracy,
uint256 _responseTime
) internal {
if (_accuracy >= autoVerificationThreshold) {
agentBounty.verifySubmission(_bountyId, _submissionId, true, address(this));
}
}
function _triggerStakingRewards(address _agentWallet, uint256 _accuracy) internal {
// Calculate earnings based on accuracy
uint256 baseEarnings = (_accuracy * 100) * 10**18; // Simplified calculation
// Distribute to stakers
try agentStaking.distributeAgentEarnings(_agentWallet, baseEarnings) {
emit StakingRewardsTriggered(_agentWallet, baseEarnings, 0);
} catch {
// Handle staking distribution failure
}
}
function _registerEventHandler(
bytes32 _eventType,
address _targetContract,
bytes4 _functionSelector
) internal {
eventHandlers[_eventType] = EventHandler({
eventType: _eventType,
targetContract: _targetContract,
functionSelector: _functionSelector,
isActive: true,
priority: 0
});
}
function _removeFromPending(uint256 _mappingId) internal {
for (uint256 i = 0; i < pendingMappings.length; i++) {
if (pendingMappings[i] == _mappingId) {
pendingMappings[i] = pendingMappings[pendingMappings.length - 1];
pendingMappings.pop();
break;
}
}
}
}