forked from rabbitholegg/quest-protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoostPass.t.sol
237 lines (177 loc) · 8.14 KB
/
BoostPass.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
import "forge-std/Test.sol";
import "../contracts/BoostPass.sol";
import {ERC1967Factory} from "solady/utils/ERC1967Factory.sol";
import {LibString} from "solady/utils/LibString.sol";
import {TestUtils} from "./helpers/TestUtils.sol";
contract BoostPassTest is Test, TestUtils {
using LibString for *;
using LibString for address;
using LibString for uint256;
error AddressNotSigned();
error TokenNotTransferable();
error AddressAlreadyMinted();
error InvalidMintFee();
error ToAddressIsNotSender();
// This error exists in Solady ERC721.sol
error TokenDoesNotExist();
event BoostPassMinted(address indexed minter, address indexed referrer, uint256 referrerFee, uint256 treasuryFee, uint256 tokenId);
BoostPass internal boostPass;
ERC1967Factory internal factory;
uint256 claimSignerPrivateKey;
address internal claimSignerAddr;
address internal owner;
address internal user;
uint256 public mintFee;
address public treasuryAddress;
address public referrerAddress;
string private baseURI;
function setUp() public virtual {
owner = makeAddr("owner");
user = makeAddr("user");
mintFee = 2000000000000000;
treasuryAddress = makeAddr("treasuryAddress");
referrerAddress = makeAddr("referrerAddress");
baseURI = 'https://baseuri.com/';
Vm.Wallet memory claimSigner = vm.createWallet("claimSigner");
claimSignerPrivateKey = claimSigner.privateKey;
claimSignerAddr = claimSigner.addr;
address boostPassImp = address(new BoostPass());
factory = new ERC1967Factory();
// initializeCallData is setting up: BoostPass.initialize(owner, claimSignerAddr, mintFee, treasuryAddress, baseURI);
bytes memory initializeCallData = abi.encodeWithSignature("initialize(address,address,uint256,address,string)", owner, claimSignerAddr, mintFee, treasuryAddress, baseURI);
address boostPassAddr = factory.deployAndCall(boostPassImp, owner, initializeCallData);
boostPass = BoostPass(boostPassAddr);
vm.deal(owner, 1 ether);
vm.deal(user, 1 ether);
vm.label(address(boostPass), "BoostPass");
}
function test_initialize() public {
assertEq(boostPass.owner(), owner);
assertEq(boostPass.claimSignerAddress(), claimSignerAddr);
assertEq(boostPass.symbol(), "BP");
assertEq(boostPass.name(), "Boost Pass");
assertEq(boostPass.mintFee(), mintFee);
assertEq(boostPass.treasuryAddress(), treasuryAddress);
}
function test_mint() public {
bytes memory data = abi.encode(user, address(0));
bytes32 msgHash = keccak256(data);
bytes memory signature = signHash(msgHash, claimSignerPrivateKey);
uint256 referralFee = 0;
uint256 treasuryFee = mintFee;
uint256 tokenId = 1; // assume this is the first mint
vm.expectEmit();
emit BoostPassMinted(user, address(0), referralFee, treasuryFee, tokenId);
vm.prank(user);
boostPass.mint{value: mintFee}(signature, data);
assertEq(boostPass.balanceOf(user), 1);
assertEq(address(treasuryAddress).balance, mintFee);
}
function test_mint_with_referrer() public {
bytes memory data = abi.encode(user, referrerAddress);
bytes32 msgHash = keccak256(data);
bytes memory signature = signHash(msgHash, claimSignerPrivateKey);
uint256 referralFee = mintFee / 2;
uint256 treasuryFee = mintFee - referralFee;
uint256 tokenId = 1; // assume this is the first mint
vm.expectEmit();
emit BoostPassMinted(user, referrerAddress, referralFee, treasuryFee, tokenId);
vm.prank(user);
boostPass.mint{value: mintFee}(signature, data);
assertEq(boostPass.balanceOf(user), 1);
assertEq(address(treasuryAddress).balance, treasuryFee);
assertEq(address(referrerAddress).balance, referralFee);
}
function test_mint_for_different_address() public {
bytes memory data = abi.encode(user, address(0));
bytes32 msgHash = keccak256(data);
bytes memory signature = signHash(msgHash, claimSignerPrivateKey);
uint256 referralFee = 0;
uint256 treasuryFee = mintFee;
uint256 tokenId = 1; // assume this is the first mint
vm.expectEmit();
emit BoostPassMinted(user, address(0), referralFee, treasuryFee, tokenId);
vm.prank(owner);
boostPass.mint{value: mintFee}(signature, data);
assertEq(boostPass.balanceOf(user), 1);
}
function test_mint_with_referrer_as_minter() public {
bytes memory data = abi.encode(user, user);
bytes32 msgHash = keccak256(data);
bytes memory signature = signHash(msgHash, claimSignerPrivateKey);
uint256 userBalanceBefore = address(user).balance;
uint256 referralFee = 0;
uint256 treasuryFee = mintFee;
uint256 tokenId = 1; // assume this is the first mint
vm.expectEmit();
emit BoostPassMinted(user, address(0), referralFee, treasuryFee, tokenId);
vm.prank(user);
boostPass.mint{value: mintFee}(signature, data);
assertEq(boostPass.balanceOf(user), 1);
assertEq(address(treasuryAddress).balance, mintFee);
assertEq(address(user).balance, userBalanceBefore - mintFee);
}
function test_mint__reverts_if_already_minted() public {
bytes memory data = abi.encode(user, address(0));
bytes memory signature = signHash(keccak256(data), claimSignerPrivateKey);
vm.startPrank(user);
boostPass.mint{value: mintFee}(signature, data);
vm.expectRevert(abi.encodeWithSelector(AddressAlreadyMinted.selector));
boostPass.mint{value: mintFee}(signature, data);
vm.stopPrank();
}
function test_mint__reverts_if_not_signed() public {
bytes memory data = abi.encode(user, address(0));
bytes memory badSignature = signHash(keccak256(data), 1);
vm.expectRevert(abi.encodeWithSelector(AddressNotSigned.selector));
vm.prank(user);
boostPass.mint{value: mintFee}(badSignature, data);
}
function test_mint__reverts_if_not_enough_fee() public {
bytes memory data = abi.encode(user, address(0));
bytes memory signature = signHash(keccak256(data), claimSignerPrivateKey);
vm.expectRevert(abi.encodeWithSelector(InvalidMintFee.selector));
vm.prank(user);
boostPass.mint{value: mintFee - 1}(signature, data);
}
function test_setClaimSignerAddress() public {
assertEq(boostPass.claimSignerAddress(), claimSignerAddr);
vm.prank(owner);
boostPass.setClaimSignerAddress(owner);
assertEq(boostPass.claimSignerAddress(), owner);
}
function test_setMintFee() public {
assertEq(boostPass.mintFee(), mintFee);
vm.prank(owner);
boostPass.setMintFee(100);
assertEq(boostPass.mintFee(), 100);
}
function test_setTreasuryAddress() public {
assertEq(boostPass.treasuryAddress(), treasuryAddress);
vm.prank(owner);
boostPass.setTreasuryAddress(owner);
assertEq(boostPass.treasuryAddress(), owner);
}
function test_tokenURI() public {
bytes memory data = abi.encode(user, address(0));
bytes32 msgHash = keccak256(data);
bytes memory signature = signHash(msgHash, claimSignerPrivateKey);
vm.prank(user);
boostPass.mint{value: mintFee}(signature, data);
assertEq(boostPass.tokenURI(1), LibString.concat(baseURI, user.toHexString()));
}
function test_revert_if_token_does_not_exist() public {
vm.expectRevert(abi.encodeWithSelector(TokenDoesNotExist.selector));
boostPass.tokenURI(69);
}
function test_revert_if_transfer() public {
bytes memory data = abi.encode(user, address(0));
bytes memory signature = signHash(keccak256(data), claimSignerPrivateKey);
vm.prank(user);
boostPass.mint{value: mintFee}(signature, data);
vm.expectRevert(abi.encodeWithSelector(TokenNotTransferable.selector));
boostPass.transferFrom(user, owner, 1);
}
}