Skip to content

Commit 3aa703f

Browse files
committed
add tests for claimYield
1 parent c488d74 commit 3aa703f

File tree

4 files changed

+126
-8
lines changed

4 files changed

+126
-8
lines changed

src/treasury/TreasuryV1.sol

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,8 @@ contract TreasuryV1 is StorageV1 {
8484
function claimYield(uint256 amount, address receiver) external onlyOwner {
8585
if (amount > availableYield) revert YieldTooLow();
8686

87-
if (amount == type(uint256).max) {
88-
uint256 availableYield_ = availableYield;
89-
availableYield = 0;
90-
IERC20(EURE).safeTransfer(receiver, availableYield_);
91-
} else {
92-
availableYield -= amount;
93-
IERC20(EURE).safeTransfer(receiver, amount);
94-
}
87+
availableYield -= amount;
88+
IERC20(EURE).safeTransfer(receiver, amount);
9589
}
9690

9791
/**
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.22;
3+
4+
import {Treasury_Fuzz_Test} from "./_Treasury.fuzz.t.sol";
5+
6+
import {FixedPointMathLib} from "../../../lib/solmate/src/utils/FixedPointMathLib.sol";
7+
import {IERC20} from "../../../lib/openzeppelin-contracts/contracts/interfaces/IERC20.sol";
8+
import {ILocker} from "../../../src/lockers/interfaces/ILocker.sol";
9+
import {TreasuryV1} from "../../../src/treasury/TreasuryV1.sol";
10+
11+
/**
12+
* @notice Fuzz tests for the function "claimYield" of contract "Treasury".
13+
*/
14+
contract ClaimYield_Treasury_Fuzz_Test is Treasury_Fuzz_Test {
15+
using FixedPointMathLib for uint256;
16+
/* ///////////////////////////////////////////////////////////////
17+
SETUP
18+
/////////////////////////////////////////////////////////////// */
19+
20+
function setUp() public override {
21+
Treasury_Fuzz_Test.setUp();
22+
}
23+
24+
/*//////////////////////////////////////////////////////////////
25+
TESTS
26+
//////////////////////////////////////////////////////////////*/
27+
function testFuzz_Revert_claimYield_NotOwner(address random, uint256 amount, address receiver) public {
28+
vm.assume(random != users.dao);
29+
30+
vm.startPrank(random);
31+
bytes memory expectedError = abi.encodeWithSelector(TreasuryV1.OnlyOwner.selector);
32+
vm.expectRevert(expectedError);
33+
treasury.claimYield(amount, receiver);
34+
vm.stopPrank();
35+
}
36+
37+
function testFuzz_Revert_claimYield_YieldTooLow(uint256 totalYield, uint256 amountToClaim, address receiver)
38+
public
39+
{
40+
// Given: availableYield is lower than yield to claim.
41+
vm.assume(amountToClaim > 1);
42+
totalYield = bound(totalYield, 0, amountToClaim - 1);
43+
44+
// And: availableYield is set.
45+
treasury.setAvailableYield(totalYield);
46+
47+
// When: Claiming yield it should revert.
48+
vm.startPrank(users.dao);
49+
vm.expectRevert(TreasuryV1.YieldTooLow.selector);
50+
treasury.claimYield(amountToClaim, receiver);
51+
vm.stopPrank();
52+
}
53+
54+
function testFuzz_Success_claimYield(uint256 totalYield, uint256 amountToClaim, address receiver) public {
55+
// Given: availableYield is lower than yield to claim.
56+
// And: Amount to claim should not be equal to max type(uint256).max.
57+
vm.assume(amountToClaim > 0);
58+
vm.assume(amountToClaim < type(uint256).max - 2);
59+
totalYield = bound(totalYield, amountToClaim + 1, type(uint256).max - 1);
60+
61+
// And: availableYield is set.
62+
treasury.setAvailableYield(totalYield);
63+
64+
// And: Yield is available in the Treasury
65+
EURE.mint(address(treasury), totalYield);
66+
67+
// When: Claiming yield
68+
vm.prank(users.dao);
69+
treasury.claimYield(amountToClaim, receiver);
70+
71+
// Then: Yield should have been sent to receiver
72+
// And: Available yield should have been lowered.
73+
assertEq(EURE.balanceOf(receiver), amountToClaim);
74+
assertEq(EURE.balanceOf(address(treasury)), totalYield - amountToClaim);
75+
assertEq(treasury.availableYield(), totalYield - amountToClaim);
76+
assert(treasury.availableYield() > 0);
77+
}
78+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.22;
3+
4+
import {Treasury_Fuzz_Test} from "./_Treasury.fuzz.t.sol";
5+
6+
import {FixedPointMathLib} from "../../../lib/solmate/src/utils/FixedPointMathLib.sol";
7+
import {IERC20} from "../../../lib/openzeppelin-contracts/contracts/interfaces/IERC20.sol";
8+
import {ILocker} from "../../../src/lockers/interfaces/ILocker.sol";
9+
import {TreasuryV1} from "../../../src/treasury/TreasuryV1.sol";
10+
import {TreasuryV1Extension} from "../../utils/extensions/TreasuryV1Extension.sol";
11+
12+
/**
13+
* @notice Fuzz tests for the function "initialize" of contract "Treasury".
14+
*/
15+
contract Initialize_Treasury_Fuzz_Test is Treasury_Fuzz_Test {
16+
using FixedPointMathLib for uint256;
17+
/* ///////////////////////////////////////////////////////////////
18+
SETUP
19+
/////////////////////////////////////////////////////////////// */
20+
21+
function setUp() public override {
22+
Treasury_Fuzz_Test.setUp();
23+
}
24+
25+
/*//////////////////////////////////////////////////////////////
26+
TESTS
27+
//////////////////////////////////////////////////////////////*/
28+
function testFuzz_Revert_initialize_AlreadyInitialized() public {
29+
vm.expectRevert(TreasuryV1.AlreadyInitialized.selector);
30+
treasury.initialize(address(EURE));
31+
}
32+
33+
function testFuzz_Success_initialize(address random) public {
34+
TreasuryV1Extension treasury_;
35+
vm.startPrank(random);
36+
treasury_ = new TreasuryV1Extension();
37+
treasury_.initialize(address(EURE));
38+
39+
assertEq(treasury_.owner(), random);
40+
assertEq(treasury_.EURE(), address(EURE));
41+
}
42+
}

test/utils/extensions/TreasuryV1Extension.sol

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@ contract TreasuryV1Extension is TreasuryV1 {
2020
function getWeightsLength() public view returns (uint256 length) {
2121
length = lockersWeights.length;
2222
}
23+
24+
function setAvailableYield(uint256 yield) public {
25+
availableYield = yield;
26+
}
2327
}

0 commit comments

Comments
 (0)