Skip to content

Commit

Permalink
fuzzing: view functions
Browse files Browse the repository at this point in the history
  • Loading branch information
anajuliabit committed Aug 11, 2024
1 parent 2f67e53 commit 1aa8bf4
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 5 deletions.
2 changes: 0 additions & 2 deletions src/DelegateStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ interface IStaking {
*
* When staking, you must specify a keyper address. This symbolically demonstrates your support
* for that keyper. The keyper address must be a valid keyper in the staking contract.
* Staking, unstaking, and claiming rewards are based on shares rather than the balance directly.
* This method ensures the balance can change over time without needing too many storage updates.
*
* @dev SHU tokens transferred into the contract without using the `stake` function will be included
* in the rewards distribution and shared among all stakers. This contract only supports SHU
Expand Down
3 changes: 1 addition & 2 deletions src/Staking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ contract Staking is BaseStaking {
/// @param _minStake The minimum stake amount
function setMinStake(uint256 _minStake) external onlyOwner {
minStake = _minStake;

emit NewMinStake(_minStake);
}

Expand Down Expand Up @@ -321,7 +320,7 @@ contract Staking is BaseStaking {
/// - if the keyper sSHU balance is less or equal than the minimum
/// stake or the total locked amount, the function will return 0
/// @param keyper The keyper address
/// @param unlockedAmount The amount of assets to unlock
/// @param unlockedAmount The amount of unlocked assets
/// @return amount The maximum amount of assets that a keyper can withdraw after unlocking a certain amount
function _maxWithdraw(
address keyper,
Expand Down
116 changes: 116 additions & 0 deletions test/DelegateStaking.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1561,4 +1561,120 @@ contract ViewFunctions is DelegateStakingTest {

assertApproxEqAbs(maxWithdraw, rewards, 0.1e18, "Wrong max withdraw");
}

function testFuzz_MaxWithdrawDepositorHasMultipleLockedStakes(
address _keyper,
address _depositor,
uint256 _amount1,
uint256 _amount2,
uint256 _jump
) public {
_amount1 = _boundToRealisticStake(_amount1);
_amount2 = _boundToRealisticStake(_amount2);
_jump = _boundUnlockedTime(_jump);

_mintGovToken(_depositor, _amount1 + _amount2);
_setKeyper(_keyper, true);

_stake(_depositor, _keyper, _amount1);
_stake(_depositor, _keyper, _amount2);

uint256 maxWithdraw = delegate.maxWithdraw(_depositor);
assertEq(maxWithdraw, 0, "Wrong max withdraw");
}

function testFuzz_convertToSharesNoSupply(uint256 assets) public view {
assertEq(delegate.convertToShares(assets), assets);
}

function testFuzz_ConvertToSharesHasSupplySameBlock(
address _keyper,
address _depositor,
uint256 _assets
) public {
_assets = _boundToRealisticStake(_assets);

_mintGovToken(_depositor, _assets);
_setKeyper(_keyper, true);

_stake(_depositor, _keyper, _assets);

uint256 shares = delegate.convertToShares(_assets);

assertEq(shares, _assets, "Wrong shares");
}

function testFuzz_ConvertToAssetsHasSupplySameBlock(
address _keyper,
address _depositor,
uint256 _assets
) public {
_assets = _boundToRealisticStake(_assets);

_mintGovToken(_depositor, _assets);
_setKeyper(_keyper, true);

_stake(_depositor, _keyper, _assets);

uint256 shares = delegate.convertToShares(_assets);
uint256 assets = delegate.convertToAssets(shares);

assertEq(assets, _assets, "Wrong assets");
}

function testFuzz_GetUserStakeIds(
address _keyper,
address _depositor,
uint256 _amount1,
uint256 _amount2
) public {
_amount1 = _boundToRealisticStake(_amount1);
_amount2 = _boundToRealisticStake(_amount2);

_mintGovToken(_depositor, _amount1 + _amount2);
_setKeyper(_keyper, true);

uint256 stakeId1 = _stake(_depositor, _keyper, _amount1);
uint256 stakeId2 = _stake(_depositor, _keyper, _amount2);

uint256[] memory stakeIds = delegate.getUserStakeIds(_depositor);

assertEq(stakeIds.length, 2, "Wrong stake ids");
assertEq(stakeIds[0], stakeId1, "Wrong stake id");
assertEq(stakeIds[1], stakeId2, "Wrong stake id");
}
}

contract Transfer is DelegateStakingTest {
function testFuzz_RevertWith_transferDisabled(
address _from,
address _to,
uint256 _amount
) public {
_amount = _boundToRealisticStake(_amount);

_mintGovToken(_from, _amount);
_setKeyper(_from, true);

_stake(_from, _from, _amount);

vm.expectRevert(BaseStaking.TransferDisabled.selector);
delegate.transfer(_to, _amount);
}

function testFuzz_RevertWith_transferFromDisabled(
address _from,
address _to,
uint256 _amount
) public {
_amount = _boundToRealisticStake(_amount);

_mintGovToken(_from, _amount);
_setKeyper(_from, true);

_stake(_from, _from, _amount);

vm.expectRevert(BaseStaking.TransferDisabled.selector);
delegate.transferFrom(_from, _to, _amount);
}
}
1 change: 1 addition & 0 deletions test/RewardsDistributor.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ contract OwnableFunctions is RewardsDistributorTest {
function testFuzz_RevertIf_SetRewardConfigurationEmissionRateZero(
address _receiver
) public {
vm.assume(_receiver != address(0));
vm.expectRevert(RewardsDistributor.EmissionRateZero.selector);
rewardsDistributor.setRewardConfiguration(_receiver, 0);
}
Expand Down
2 changes: 1 addition & 1 deletion test/Staking.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1771,7 +1771,7 @@ contract ViewFunctions is StakingTest {
assertEq(assets, _assets, "Wrong assets");
}

function testFuzz_GetKeyperStakeIds(
function testFuzz_GetUserStakeIds(
address _depositor,
uint256 _amount1,
uint256 _amount2
Expand Down

0 comments on commit 1aa8bf4

Please sign in to comment.