Skip to content

Commit

Permalink
testAddKeyper
Browse files Browse the repository at this point in the history
  • Loading branch information
anajuliabit committed Jun 15, 2024
1 parent 41e5fd6 commit 0e63a96
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/RewardsDistributor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ contract RewardsDistributor is Ownable2StepUpgradeable {
__Ownable2Step_init();

// Transfer ownership to the DAO contract
transferOwnership(newOwner);
_transferOwnership(newOwner);
}

/// @notice Add a reward configuration
Expand Down
10 changes: 8 additions & 2 deletions src/Staking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ interface IRewardsDistributor {
}

// TODO should be pausable?
// TODO use SafeTransferLib to every calculation
// TODO is this vulnerable to first deposit attack?
// TODO check calculations
contract Staking is ERC20VotesUpgradeable, Ownable2StepUpgradeable {
/*//////////////////////////////////////////////////////////////
LIBRARIES
Expand Down Expand Up @@ -86,6 +87,7 @@ contract Staking is ERC20VotesUpgradeable, Ownable2StepUpgradeable {
);
event Unstaked(address user, uint256 amount, uint256 shares);
event ClaimRewards(address user, address rewardToken, uint256 rewards);
event KeyperSet(address keyper, bool isKeyper);

/*//////////////////////////////////////////////////////////////
MODIFIERS
Expand Down Expand Up @@ -121,7 +123,7 @@ contract Staking is ERC20VotesUpgradeable, Ownable2StepUpgradeable {
__Ownable2Step_init();

// Transfer ownership to the DAO contract
transferOwnership(newOwner);
_transferOwnership(newOwner);

stakingToken = IERC20(_stakingToken);
rewardsDistributor = IRewardsDistributor(_rewardsDistributor);
Expand Down Expand Up @@ -408,6 +410,8 @@ contract Staking is ERC20VotesUpgradeable, Ownable2StepUpgradeable {
/// @param isKeyper Whether the keyper is a keyper or not
function setKeyper(address keyper, bool isKeyper) external onlyOwner {
keypers[keyper] = isKeyper;

emit KeyperSet(keyper, isKeyper);
}

/// @notice Set multiple keypers
Expand All @@ -419,6 +423,8 @@ contract Staking is ERC20VotesUpgradeable, Ownable2StepUpgradeable {
) external onlyOwner {
for (uint256 i = 0; i < _keypers.length; i++) {
keypers[_keypers[i]] = isKeyper;

emit KeyperSet(_keypers[i], isKeyper);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/interfaces/IStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ interface IStaking {
);
event Unstaked(address user, uint256 amount, uint256 shares);
event ClaimRewards(address user, address rewardToken, uint256 rewards);
event KeyperSet(address keyper, bool isKeyper);
}
44 changes: 37 additions & 7 deletions test/unit/StakingUnitTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,33 @@ import {IRewardsDistributor} from "../../src/interfaces/IRewardsDistributor.sol"
import {MockShu} from "../mocks/MockShu.sol";

contract StakingUnitTest is Test {
IStaking staking;
IStaking public staking;

uint256 constant lockPeriod = 60 * 24 * 30 * 6; // 6 months
uint256 constant minStake = 50_000 * 1e18; // 50k

address keyper = address(0x1234);

function setUp() public {
// deploy mock shu
MockShu shu = new MockShu();

shu.mint(keyper, 1_000_000 * 1e18);

// deploy rewards distributor
address rewardsDistributor = address(new RewardsDistributor());

TransparentUpgradeableProxy rewardsDistributionProxy = new TransparentUpgradeableProxy(
address rewardsDistributionProxy = address(
new TransparentUpgradeableProxy(
rewardsDistributor,
address(this),
abi.encodeWithSignature("initialize(address)", address(this))
);
""
)
);

RewardsDistributor(rewardsDistributionProxy).initialize(
address(this) // owner
);

// deploy staking
address stakingContract = address(new Staking());
Expand All @@ -35,9 +48,6 @@ contract StakingUnitTest is Test {
new TransparentUpgradeableProxy(stakingContract, address(this), "")
);

uint256 lockPeriod = 60 * 24 * 30 * 6; // 6 months
uint256 minStake = 50_000 * 1e18; // 50k

Staking(address(stakingProxy)).initialize(
address(this), // owner
address(shu),
Expand All @@ -48,4 +58,24 @@ contract StakingUnitTest is Test {

staking = IStaking(stakingProxy);
}

function testAddKeyper() public {
vm.expectEmit(address(staking));
emit IStaking.KeyperSet(keyper, true);
staking.setKeyper(keyper, true);
}

function testStakeSucceed() public {
testAddKeyper();

vm.expectEmit(true, true, true, true, address(staking));
emit IStaking.Staked(
address(this),
minStake,
minStake, // first stake, shares == amount
lockPeriod
);
vm.prank(keyper);
staking.stake(50_000 * 1e18);
}
}

0 comments on commit 0e63a96

Please sign in to comment.