Skip to content

Commit a3592dd

Browse files
ae2079cursoragent
andcommitted
test: centralize erc20 mocks under test/mocks
Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 53e465a commit a3592dd

8 files changed

Lines changed: 46 additions & 58 deletions

test/DonationHandler.t.sol

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,14 @@ pragma solidity ^0.8.0;
33

44
import '../src/contracts/DonationHandler.sol';
55

6-
import './helpers/NoReturnMockERC20.sol';
6+
import './mocks/FailingMockERC20.sol';
7+
import './mocks/MockERC20.sol';
8+
import './mocks/NoReturnMockERC20.sol';
79

810
import '@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol';
9-
import '@openzeppelin/contracts/token/ERC20/ERC20.sol';
1011
import '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol';
1112
import 'forge-std/Test.sol';
1213

13-
// Mock ERC20 token for testing
14-
contract MockERC20 is ERC20 {
15-
constructor() ERC20('MockToken', 'MTK') {
16-
_mint(msg.sender, 1_000_000 * 10 ** 18);
17-
}
18-
}
19-
20-
contract FailingMockERC20 is ERC20 {
21-
constructor() ERC20('FailingToken', 'FAIL') {
22-
_mint(msg.sender, 1_000_000 * 10 ** 18);
23-
}
24-
25-
function transferFrom(address, address, uint256) public pure override returns (bool) {
26-
return false;
27-
}
28-
}
29-
3014
contract DonationHandlerTest is Test {
3115
DonationHandler public donationHandler;
3216
MockERC20 public mockToken;

test/DonationHandlerBugFix.t.sol

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,12 @@
22
pragma solidity ^0.8.0;
33

44
import '../src/contracts/DonationHandler.sol';
5+
6+
import './mocks/MockERC20.sol';
7+
58
import '@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol';
6-
import '@openzeppelin/contracts/token/ERC20/ERC20.sol';
79
import 'forge-std/Test.sol';
810

9-
// Mock ERC20 token for testing
10-
contract MockERC20 is ERC20 {
11-
constructor() ERC20('MockToken', 'MTK') {
12-
_mint(msg.sender, 1_000_000 * 10 ** 18);
13-
}
14-
}
15-
1611
/// @title DonationHandlerBugFixTest
1712
/// @notice Tests for the bug fix that ensures amounts array sums to totalAmount
1813
/// @dev These tests verify the fix for the vulnerability where mismatched amounts could lock or steal ETH/tokens
@@ -45,7 +40,7 @@ contract DonationHandlerBugFixTest is Test {
4540
vm.deal(eve, 100 ether);
4641

4742
// Give Alice some tokens
48-
mockToken.transfer(alice, 10000e18);
43+
mockToken.transfer(alice, 10_000e18);
4944
}
5045

5146
/// @notice Test that donateManyETH reverts when amounts sum is less than totalAmount
@@ -203,14 +198,14 @@ contract DonationHandlerBugFixTest is Test {
203198
to[2] = alice;
204199
to[3] = makeAddr('charlie');
205200
to[4] = makeAddr('dave');
206-
201+
207202
uint256[] memory amts = new uint256[](5);
208203
amts[0] = 1 ether;
209204
amts[1] = 2 ether;
210205
amts[2] = 3 ether;
211206
amts[3] = 2.5 ether;
212207
amts[4] = 1.5 ether; // Total = 10 ETH
213-
208+
214209
bytes[] memory data = new bytes[](5);
215210

216211
vm.prank(alice);

test/DonationHandlerMultisigCalls.t.sol

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ pragma solidity ^0.8.0;
33

44
import '../src/contracts/DonationHandler.sol';
55

6-
import {DonationHandlerSetup, FailingMockERC20, MockERC20} from './DonationHandlerSetup.t.sol';
6+
import './DonationHandlerSetup.t.sol';
7+
import './mocks/FailingMockERC20.sol';
8+
import './mocks/MockERC20.sol';
9+
710
import '@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol';
8-
import '@openzeppelin/contracts/token/ERC20/ERC20.sol';
911
import 'forge-std/Test.sol';
1012

11-
// Mock ERC20 token for testing
12-
1313
contract DummyMultisig {
1414
address public owner;
1515
bool public shouldRevert;
@@ -28,7 +28,11 @@ contract DummyMultisig {
2828
}
2929

3030
// Execute a call to the donation handler
31-
function executeTransaction(address target, uint256 value, bytes memory data) external payable returns (bool success) {
31+
function executeTransaction(
32+
address target,
33+
uint256 value,
34+
bytes memory data
35+
) external payable returns (bool success) {
3236
require(msg.sender == owner, 'Not authorized');
3337

3438
if (shouldRevert) {

test/DonationHandlerSetup.t.sol

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,12 @@ pragma solidity ^0.8.0;
33

44
import '../src/contracts/DonationHandler.sol';
55

6+
import './mocks/FailingMockERC20.sol';
7+
import './mocks/MockERC20.sol';
8+
69
import '@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol';
7-
import '@openzeppelin/contracts/token/ERC20/ERC20.sol';
810
import 'forge-std/Test.sol';
911

10-
// Mock ERC20 token for testing
11-
contract MockERC20 is ERC20 {
12-
constructor() ERC20('MockToken', 'MTK') {
13-
_mint(msg.sender, 1_000_000 * 10 ** 18);
14-
}
15-
}
16-
17-
contract FailingMockERC20 is ERC20 {
18-
constructor() ERC20('FailingToken', 'FAIL') {
19-
_mint(msg.sender, 1_000_000 * 10 ** 18);
20-
}
21-
22-
function transferFrom(address, address, uint256) public pure override returns (bool) {
23-
return false;
24-
}
25-
}
26-
2712
contract DonationHandlerSetup is Test {
2813
DonationHandler public donationHandler;
2914
MockERC20 public mockToken;

test/DonationHandlerStandardTests.t.sol

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,11 @@ pragma solidity ^0.8.0;
44
import '../src/contracts/DonationHandler.sol';
55

66
import './DonationHandlerSetup.t.sol';
7-
import './helpers/NoReturnMockERC20.sol';
7+
import './mocks/NoReturnMockERC20.sol';
88

9-
import '@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol';
10-
import '@openzeppelin/contracts/token/ERC20/ERC20.sol';
119
import '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol';
1210
import 'forge-std/Test.sol';
1311

14-
// Mock ERC20 token for testing
15-
1612
contract DonationHandlerStandardTests is DonationHandlerSetup {
1713
function setUp() public {
1814
// Deploy contracts

test/mocks/FailingMockERC20.sol

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity ^0.8.0;
3+
4+
import '@openzeppelin/contracts/token/ERC20/ERC20.sol';
5+
6+
contract FailingMockERC20 is ERC20 {
7+
constructor() ERC20('FailingToken', 'FAIL') {
8+
_mint(msg.sender, 1_000_000 * 10 ** 18);
9+
}
10+
11+
function transferFrom(address, address, uint256) public pure override returns (bool) {
12+
return false;
13+
}
14+
}

test/mocks/MockERC20.sol

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity ^0.8.0;
3+
4+
import '@openzeppelin/contracts/token/ERC20/ERC20.sol';
5+
6+
contract MockERC20 is ERC20 {
7+
constructor() ERC20('MockToken', 'MTK') {
8+
_mint(msg.sender, 1_000_000 * 10 ** 18);
9+
}
10+
}

0 commit comments

Comments
 (0)