-
Notifications
You must be signed in to change notification settings - Fork 593
/
FulfillAdvancedOrderCriteria.t.sol
212 lines (189 loc) · 6.57 KB
/
FulfillAdvancedOrderCriteria.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import { BaseOrderTest } from "./utils/BaseOrderTest.sol";
import { Merkle } from "murky/Merkle.sol";
import {
ConsiderationInterface
} from "../../contracts/interfaces/ConsiderationInterface.sol";
import {
CriteriaResolver,
OfferItem,
OrderComponents,
AdvancedOrder
} from "../../contracts/lib/ConsiderationStructs.sol";
import { ItemType, Side } from "../../contracts/lib/ConsiderationEnums.sol";
contract FulfillAdvancedOrderCriteria is BaseOrderTest {
Merkle merkle = new Merkle();
FuzzArgs empty;
struct FuzzArgs {
uint256[8] identifiers;
uint8 index;
}
struct Context {
ConsiderationInterface consideration;
FuzzArgs args;
}
function test(
function(Context memory) external fn,
Context memory context
) internal {
try fn(context) {} catch (bytes memory reason) {
assertPass(reason);
}
}
function testFulfillAdvancedOrderWithCriteria(FuzzArgs memory args) public {
test(
this.fulfillAdvancedOrderWithCriteria,
Context(consideration, args)
);
test(
this.fulfillAdvancedOrderWithCriteria,
Context(referenceConsideration, args)
);
}
function testFulfillAdvancedOrderWithCriteriaPreimage(
FuzzArgs memory args
) public {
test(
this.fulfillAdvancedOrderWithCriteriaPreimage,
Context(consideration, args)
);
test(
this.fulfillAdvancedOrderWithCriteriaPreimage,
Context(referenceConsideration, args)
);
}
function prepareCriteriaOfferOrder(
Context memory context
)
internal
returns (
bytes32[] memory hashedIdentifiers,
AdvancedOrder memory advancedOrder
)
{
// create a new array to store bytes32 hashes of identifiers
hashedIdentifiers = new bytes32[](context.args.identifiers.length);
for (uint256 i = 0; i < context.args.identifiers.length; i++) {
// try to mint each identifier; fuzzer may include duplicates
try test721_1.mint(alice, context.args.identifiers[i]) {} catch (
bytes memory
) {}
// hash identifier and store to generate proof
hashedIdentifiers[i] = keccak256(
abi.encode(context.args.identifiers[i])
);
}
bytes32 root = merkle.getRoot(hashedIdentifiers);
addOfferItem721Criteria(address(test721_1), uint256(root));
addEthConsiderationItem(alice, 1);
_configureOrderParameters(alice, address(0), bytes32(0), 0, false);
OrderComponents memory orderComponents = getOrderComponents(
baseOrderParameters,
context.consideration.getCounter(alice)
);
bytes memory signature = signOrder(
context.consideration,
alicePk,
context.consideration.getOrderHash(orderComponents)
);
advancedOrder = AdvancedOrder(baseOrderParameters, 1, 1, signature, "");
}
function fulfillAdvancedOrderWithCriteria(
Context memory context
) external stateless {
// pick a "random" index in the array of identifiers; use that identifier
context.args.index = context.args.index % 8;
uint256 identifier = context.args.identifiers[context.args.index];
(
bytes32[] memory hashedIdentifiers,
AdvancedOrder memory advancedOrder
) = prepareCriteriaOfferOrder(context);
// create resolver for identifier including proof for token at index
CriteriaResolver memory resolver = CriteriaResolver(
0,
Side.OFFER,
0,
identifier,
merkle.getProof(hashedIdentifiers, context.args.index)
);
CriteriaResolver[] memory resolvers = new CriteriaResolver[](1);
resolvers[0] = resolver;
context.consideration.fulfillAdvancedOrder{ value: 1 }(
advancedOrder,
resolvers,
bytes32(0),
address(0)
);
assertEq(address(this), test721_1.ownerOf(identifier));
}
function fulfillAdvancedOrderWithCriteriaPreimage(
Context memory context
) external stateless {
context.args.index = context.args.index % 8;
(
bytes32[] memory hashedIdentifiers,
AdvancedOrder memory advancedOrder
) = prepareCriteriaOfferOrder(context);
// grab a random proof
bytes32[] memory proof = merkle.getProof(
hashedIdentifiers,
context.args.index
);
// copy all but the first element of the proof to a new array
bytes32[] memory truncatedProof = new bytes32[](proof.length - 1);
for (uint256 i = 0; i < truncatedProof.length - 1; i++) {
truncatedProof[i] = proof[i + 1];
}
// use the first element as a new token identifier
uint256 preimageIdentifier = uint256(proof[0]);
// try to mint preimageIdentifier; there's a chance it's already minted
try test721_1.mint(alice, preimageIdentifier) {} catch (bytes memory) {}
// create criteria resolver including first hash as identifier
CriteriaResolver memory resolver = CriteriaResolver(
0,
Side.OFFER,
0,
preimageIdentifier,
truncatedProof
);
CriteriaResolver[] memory resolvers = new CriteriaResolver[](1);
resolvers[0] = resolver;
vm.expectRevert(abi.encodeWithSignature("InvalidProof()"));
context.consideration.fulfillAdvancedOrder{ value: 1 }(
advancedOrder,
resolvers,
bytes32(0),
address(0)
);
}
function addOfferItem721Criteria(
address token,
uint256 identifierHash
) internal {
addOfferItem721Criteria(token, identifierHash, 1, 1);
}
function addOfferItem721Criteria(
address token,
uint256 identifierHash,
uint256 amount
) internal {
addOfferItem721Criteria(token, identifierHash, amount, amount);
}
function addOfferItem721Criteria(
address token,
uint256 identifierHash,
uint256 startAmount,
uint256 endAmount
) internal {
offerItems.push(
OfferItem(
ItemType.ERC721_WITH_CRITERIA,
token,
identifierHash,
startAmount,
endAmount
)
);
}
}