-
Notifications
You must be signed in to change notification settings - Fork 162
/
OffChainAuction.t.sol
211 lines (191 loc) · 7.91 KB
/
OffChainAuction.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
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.23;
import "forge-std/Test.sol";
import "solmate/tokens/ERC721.sol";
import "../patterns/off-chain-storage/OffChainAuction.sol";
import "./TestUtils.sol";
contract OffChainAuctionTest is Test, TestUtils {
OffChainAuction protocol = new OffChainAuction();
MintableERC721 token = new MintableERC721();
function test_canCreateAuction() external {
(uint256 auctionId, OffChainAuction.AuctionState memory state) =
_createAuction(0.1 ether, 10 seconds);
assertTrue(protocol.auctionStateHashes(auctionId) != 0);
assertEq(address(state.token), address(token));
assertEq(state.topBid, 0.1 ether);
assertEq(state.topBidder, address(0));
assertEq(token.ownerOf(state.tokenId), address(protocol));
}
function test_canBidOnAuction() external {
(uint256 auctionId, OffChainAuction.AuctionState memory state) =
_createAuction(0.1 ether, 10 seconds);
address firstBidder = _randomAddress();
uint256 firstBid = state.topBid;
vm.deal(firstBidder, firstBid);
vm.prank(firstBidder);
state = protocol.bid{ value: firstBid }(auctionId, state);
assertEq(state.topBidder, firstBidder);
assertEq(state.topBid, firstBid);
assertEq(firstBidder.balance, 0);
address nextBidder = _randomAddress();
uint256 nextBid = state.topBid + 1;
vm.deal(nextBidder, nextBid);
vm.prank(nextBidder);
state = protocol.bid{ value: nextBid }(auctionId, state);
assertEq(state.topBidder, nextBidder);
assertEq(state.topBid, nextBid);
assertEq(firstBidder.balance, firstBid);
assertEq(nextBidder.balance, 0);
}
function test_canExpireAuction() external {
(uint256 auctionId, OffChainAuction.AuctionState memory state) =
_createAuction(0.1 ether, 10 seconds);
skip(10 seconds);
protocol.settle(auctionId, state);
assertEq(token.ownerOf(state.tokenId), state.owner);
assertEq(protocol.auctionStateHashes(auctionId), 0);
}
function test_canSettleAuction() external {
(uint256 auctionId, OffChainAuction.AuctionState memory state) =
_createAuction(0.1 ether, 10 seconds);
address bidder = _randomAddress();
uint256 bid = state.topBid;
vm.deal(bidder, bid);
vm.prank(bidder);
state = protocol.bid{ value: bid }(auctionId, state);
skip(state.duration);
protocol.settle(auctionId, state);
assertEq(token.ownerOf(state.tokenId), bidder);
assertEq(state.owner.balance, state.topBid);
assertEq(state.topBidder.balance, 0);
assertEq(protocol.auctionStateHashes(auctionId), 0);
}
function test_cannotStartBiddingTooLow() external {
(uint256 auctionId, OffChainAuction.AuctionState memory state) =
_createAuction(0.1 ether, 10 seconds);
address bidder = _randomAddress();
uint256 bid = state.topBid - 1;
vm.deal(bidder, bid);
vm.prank(bidder);
vm.expectRevert('bid too low');
state = protocol.bid{ value: bid }(auctionId, state);
}
function test_cannotContinueBiddingTooLow() external {
(uint256 auctionId, OffChainAuction.AuctionState memory state) =
_createAuction(0.1 ether, 10 seconds);
address firstBidder = _randomAddress();
uint256 firstBid = state.topBid;
vm.deal(firstBidder, firstBid);
vm.prank(firstBidder);
state = protocol.bid{ value: firstBid }(auctionId, state);
address nextBidder = _randomAddress();
uint256 nextBid = state.topBid;
vm.deal(nextBidder, nextBid);
vm.prank(nextBidder);
vm.expectRevert('bid too low');
state = protocol.bid{ value: nextBid }(auctionId, state);
}
function test_cannotBidIfExpired() external {
(uint256 auctionId, OffChainAuction.AuctionState memory state) =
_createAuction(0.1 ether, 10 seconds);
address bidder = _randomAddress();
uint256 bid = state.topBid;
skip(state.duration);
vm.deal(bidder, bid);
vm.prank(bidder);
vm.expectRevert('expired');
state = protocol.bid{ value: bid }(auctionId, state);
}
function test_cannotBidIfConcluded() external {
(uint256 auctionId, OffChainAuction.AuctionState memory state) =
_createAuction(0.1 ether, 10 seconds);
address bidder = _randomAddress();
uint256 bid = state.topBid;
vm.deal(bidder, bid);
vm.prank(bidder);
state = protocol.bid{ value: bid }(auctionId, state);
skip(state.duration);
bid = state.topBid + 1;
vm.deal(bidder, bid);
vm.prank(bidder);
vm.expectRevert('concluded');
state = protocol.bid{ value: bid }(auctionId, state);
}
function test_cannotBidWithBadState() external {
(uint256 auctionId, OffChainAuction.AuctionState memory state) =
_createAuction(0.1 ether, 10 seconds);
address bidder = _randomAddress();
uint256 bid = state.topBid;
vm.deal(bidder, bid);
vm.prank(bidder);
vm.expectRevert('invalid auction');
state.owner = _randomAddress();
state = protocol.bid{ value: bid }(auctionId, state);
}
function test_cannotSettleBeforeExpiry() external {
(uint256 auctionId, OffChainAuction.AuctionState memory state) =
_createAuction(0.1 ether, 10 seconds);
skip(state.duration - 1);
address bidder = _randomAddress();
uint256 bid = state.topBid;
vm.deal(bidder, bid);
vm.prank(bidder);
state = protocol.bid{ value: bid }(auctionId, state);
skip(state.duration - 1);
vm.expectRevert('not concluded');
protocol.settle(auctionId, state);
}
function test_cannotSettleBeforeConclusion() external {
(uint256 auctionId, OffChainAuction.AuctionState memory state) =
_createAuction(0.1 ether, 10 seconds);
skip(state.duration - 1);
vm.expectRevert('not expired');
protocol.settle(auctionId, state);
}
function test_cannotSettleTwice() external {
(uint256 auctionId, OffChainAuction.AuctionState memory state) =
_createAuction(0.1 ether, 10 seconds);
address bidder = _randomAddress();
uint256 bid = state.topBid;
vm.deal(bidder, bid);
vm.prank(bidder);
state = protocol.bid{ value: bid }(auctionId, state);
skip(state.duration);
protocol.settle(auctionId, state);
vm.expectRevert('invalid auction');
protocol.settle(auctionId, state);
}
function test_cannotSettleWithBadState() external {
(uint256 auctionId, OffChainAuction.AuctionState memory state) =
_createAuction(0.1 ether, 10 seconds);
address bidder = _randomAddress();
uint256 bid = state.topBid;
vm.deal(bidder, bid);
vm.prank(bidder);
state = protocol.bid{ value: bid }(auctionId, state);
skip(state.duration);
state.owner = _randomAddress();
vm.expectRevert('invalid auction');
protocol.settle(auctionId, state);
}
function _createAuction(uint256 minBid, uint256 duration)
private
returns (uint256 auctionId, OffChainAuction.AuctionState memory state)
{
address owner = _randomAddress();
uint256 tokenId = _randomUint256();
token.mint(owner, tokenId);
vm.prank(owner);
token.approve(address(protocol), tokenId);
vm.prank(owner);
(auctionId, state) =
protocol.createAuction(IERC721(address(token)), tokenId, minBid, duration);
}
}
contract MintableERC721 is ERC721 {
constructor() ERC721('TEST', 'TST') {}
function mint(address owner, uint256 id) external {
_mint(owner, id);
}
function tokenURI(uint256) public pure override returns (string memory) {}
}