Skip to content

fix: Implement DAO-compatible Slashing Module#10

Open
Xanoutas wants to merge 1 commit intoNobayprotocol:mainfrom
Xanoutas:fix/issue-1-auto
Open

fix: Implement DAO-compatible Slashing Module#10
Xanoutas wants to merge 1 commit intoNobayprotocol:mainfrom
Xanoutas:fix/issue-1-auto

Conversation

@Xanoutas
Copy link
Copy Markdown

Fix for #1: Implement DAO-compatible Slashing Module

// contracts/modules/SlashingModule.sol

pragma solidity ^0.8.0;

import "@openzeppelin/contracts/access/Ownable.sol";
import "./StakingModule.sol";

contract SlashingModule is Ownable {
    StakingModule public stakingModule;
    mapping(address => uint256) public trustScores;
    uint256 public trustDecayRate = 10; // 10% decay per day

    event Slashed(address indexed staker, uint256 amount, string reason);

    constructor(address _stakingModuleAddress) {
        stakingModule = StakingModule(_stakingModuleAddress);
    }

    function slash(address staker, uint256 amount, string memory reason) external onlyOwner {
        require(amount > 0, "Amount must be greater than zero");
        require(stakingModule.balanceOf(staker) >= amount, "Insufficient staking balance");

        stakingModule.burn(staker, amount);
        emit Slashed(staker, amount, reason);

        // Update trust score
        trustScores[staker] = trustScores[staker] > amount ? trustScores[staker] - amount : 0;
    }

    function applyTrustDecay(address staker) external {
        trustScores[staker] = trustScores[staker] * (100 - trustDecayRate) / 100;
    }

    function getTrustScore(address staker) external view returns (uint256) {
        return trustScores[staker];
    }
}
// contracts/modules/StakingModule.sol

pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract StakingModule is ERC20 {
    constructor() ERC20("NOBAY", "NOBAY") {}

    function burn(address account, uint256 amount) external {
        _burn(account, amount);
    }
}
// tests/SlashingModuleTest.sol

pragma solidity ^0.8.0;

import "forge-std/Test.sol";
import "../contracts/modules/SlashingModule.sol";
import "../contracts/modules/StakingModule.sol";

contract SlashingModuleTest is Test {
    SlashingModule slashingModule;
    StakingModule stakingModule;
    address owner = address(1);
    address staker = address(2);

    function setUp() public {
        stakingModule = new StakingModule();
        slashingModule = new SlashingModule(address(stakingModule));

        vm.prank(owner);
        stakingModule.mint(staker, 1000);

        vm.prank(owner);
        stakingModule.transferOwnership(address(slashingModule));
    }

    function testSlash() public {
        vm.prank(owner);
        slashingModule.slash(staker, 500, "Fraud");

        assertEq(stakingModule.balanceOf(staker), 500);
        assertEq(slashingModule.getTrustScore(staker), 500);
    }

    function testApplyTrustDecay() public {
        vm.prank(owner);
        slashingModule.slash(staker, 500, "Fraud");

        vm.prank(owner);
        slashingModule.applyTrustDecay(staker);

        assertEq(slashingModule.getTrustScore(staker), 450);
    }
}

Explanation:

  • SlashingModule.sol: This contract implements the slashing logic. It interacts with the StakingModule to burn tokens from a staker's balance when they are slashed. It also mai

Closes #1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement DAO-compatible Slashing Module

1 participant