-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBase.t.sol
165 lines (135 loc) Β· 7.15 KB
/
Base.t.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.22;
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { Test } from "forge-std/src/Test.sol";
import { FlowNFTDescriptor } from "src/FlowNFTDescriptor.sol";
import { ISablierFlow } from "src/interfaces/ISablierFlow.sol";
import { SablierFlow } from "src/SablierFlow.sol";
import { ERC20MissingReturn } from "./mocks/ERC20MissingReturn.sol";
import { ERC20Mock } from "./mocks/ERC20Mock.sol";
import { ContractWithoutReceive, ContractWithReceive } from "./mocks/Receive.sol";
import { Assertions } from "./utils/Assertions.sol";
import { Modifiers } from "./utils/Modifiers.sol";
import { Users } from "./utils/Types.sol";
import { Vars } from "./utils/Vars.sol";
abstract contract Base_Test is Assertions, Modifiers, Test {
/*//////////////////////////////////////////////////////////////////////////
VARIABLES
//////////////////////////////////////////////////////////////////////////*/
Users internal users;
Vars internal vars;
/*//////////////////////////////////////////////////////////////////////////
TEST CONTRACTS
//////////////////////////////////////////////////////////////////////////*/
ContractWithoutReceive internal contractWithoutReceive;
ContractWithReceive internal contractWithReceive;
ERC20Mock internal dai;
ERC20Mock internal tokenWithoutDecimals;
ERC20Mock internal tokenWithProtocolFee;
ERC20Mock internal usdc;
ERC20MissingReturn internal usdt;
ISablierFlow internal flow;
FlowNFTDescriptor internal nftDescriptor;
/*//////////////////////////////////////////////////////////////////////////
SET-UP FUNCTION
//////////////////////////////////////////////////////////////////////////*/
function setUp() public virtual {
users.admin = payable(makeAddr("admin"));
if (!isBenchmarkProfile() && !isTestOptimizedProfile()) {
nftDescriptor = new FlowNFTDescriptor();
flow = new SablierFlow(users.admin, nftDescriptor);
} else {
flow = deployOptimizedSablierFlow();
}
contractWithoutReceive = new ContractWithoutReceive();
contractWithReceive = new ContractWithReceive();
// Label the contracts.
vm.label({ account: address(flow), newLabel: "Flow" });
vm.label({ account: address(contractWithoutReceive), newLabel: "Contract without Receive" });
vm.label({ account: address(contractWithReceive), newLabel: "Contract with Receive" });
// Create new tokens and label them.
createAndLabelTokens();
// Turn on the protocol fee for tokenWithProtocolFee.
resetPrank(users.admin);
flow.setProtocolFee(tokenWithProtocolFee, PROTOCOL_FEE);
// Create the users.
users.broker = createUser("broker");
users.eve = createUser("eve");
users.operator = createUser("operator");
users.recipient = createUser("recipient");
users.sender = createUser("sender");
// Set the variables in Modifiers contract.
setVariables(users);
resetPrank(users.sender);
// Warp to May 1, 2024 at 00:00 GMT to provide a more realistic testing environment.
vm.warp({ newTimestamp: OCT_1_2024 });
}
/*//////////////////////////////////////////////////////////////////////////
HELPERS
//////////////////////////////////////////////////////////////////////////*/
/// @dev Create new tokens and label them.
function createAndLabelTokens() internal {
// Deploy the tokens.
tokenWithoutDecimals = createToken("Token without Decimals", "TWD", 0);
tokenWithProtocolFee = createToken("Token with Protocol Fee", "TPF", 6);
dai = createToken("Dai stablecoin", "DAI", 18);
usdc = createToken("USD Coin", "USDC", 6);
usdt = new ERC20MissingReturn("Tether", "USDT", 6);
// Label the tokens.
vm.label(address(tokenWithoutDecimals), "TWD");
vm.label(address(tokenWithProtocolFee), "TPF");
vm.label(address(dai), "DAI");
vm.label(address(usdc), "USDC");
vm.label(address(usdt), "USDT");
}
/// @dev Creates a new ERC-20 token with `decimals`.
function createToken(uint8 decimals) internal returns (ERC20Mock) {
return createToken("", "", decimals);
}
/// @dev Creates a new ERC-20 token with `name`, `symbol` and `decimals`.
function createToken(string memory name, string memory symbol, uint8 decimals) internal returns (ERC20Mock) {
return new ERC20Mock(name, symbol, decimals);
}
function createUser(string memory name) internal returns (address payable) {
address payable user = payable(makeAddr(name));
vm.deal({ account: user, newBalance: 100 ether });
deal({ token: address(tokenWithoutDecimals), to: user, give: 1_000_000 });
deal({ token: address(tokenWithProtocolFee), to: user, give: 1_000_000e6 });
deal({ token: address(dai), to: user, give: 1_000_000e18 });
deal({ token: address(usdc), to: user, give: 1_000_000e6 });
deal({ token: address(usdt), to: user, give: 1_000_000e6 });
resetPrank(user);
dai.approve({ spender: address(flow), value: UINT256_MAX });
usdc.approve({ spender: address(flow), value: UINT256_MAX });
usdt.approve({ spender: address(flow), value: UINT256_MAX });
return user;
}
/// @dev Deploys {SablierFlow} from an optimized source compiled with `--via-ir`.
function deployOptimizedSablierFlow() internal returns (SablierFlow) {
nftDescriptor = FlowNFTDescriptor(deployCode("out-optimized/FlowNFTDescriptor.sol/FlowNFTDescriptor.json"));
return SablierFlow(
deployCode(
"out-optimized/SablierFlow.sol/SablierFlow.json", abi.encode(users.admin, address(nftDescriptor))
)
);
}
/*//////////////////////////////////////////////////////////////////////////
CALL EXPECTS
//////////////////////////////////////////////////////////////////////////*/
/// @dev Expects a call to {IERC20.transfer}.
function expectCallToTransfer(address to, uint256 amount) internal {
vm.expectCall({ callee: address(dai), data: abi.encodeCall(IERC20.transfer, (to, amount)) });
}
/// @dev Expects a call to {IERC20.transfer}.
function expectCallToTransfer(IERC20 token, address to, uint256 amount) internal {
vm.expectCall({ callee: address(token), data: abi.encodeCall(IERC20.transfer, (to, amount)) });
}
/// @dev Expects a call to {IERC20.transferFrom}.
function expectCallToTransferFrom(address from, address to, uint256 amount) internal {
vm.expectCall({ callee: address(dai), data: abi.encodeCall(IERC20.transferFrom, (from, to, amount)) });
}
/// @dev Expects a call to {IERC20.transferFrom}.
function expectCallToTransferFrom(IERC20 token, address from, address to, uint256 amount) internal {
vm.expectCall({ callee: address(token), data: abi.encodeCall(IERC20.transferFrom, (from, to, amount)) });
}
}