forked from ethereum/ERCs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatalog.ts
310 lines (276 loc) · 10.2 KB
/
catalog.ts
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import { loadFixture } from "@nomicfoundation/hardhat-network-helpers";
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import { expect } from "chai";
import { ethers } from "hardhat";
import { CatalogMock } from "../typechain-types";
async function catalogFixture(): Promise<CatalogMock> {
const Catalog = await ethers.getContractFactory("CatalogMock");
const testCatalog = await Catalog.deploy("ipfs//:meta", "misc");
await testCatalog.deployed();
return testCatalog;
}
describe("CatalogMock", async () => {
let testCatalog: CatalogMock;
let addrs: SignerWithAddress[];
const metadataUriDefault = "src";
const noType = 0;
const slotType = 1;
const fixedType = 2;
const sampleSlotPartData = {
itemType: slotType,
z: 0,
equippable: [],
metadataURI: metadataUriDefault,
};
beforeEach(async () => {
[, ...addrs] = await ethers.getSigners();
testCatalog = await loadFixture(catalogFixture);
});
describe("Init Catalog", async function () {
it("has right metadataURI", async function () {
expect(await testCatalog.getMetadataURI()).to.equal("ipfs//:meta");
});
it("has right type", async function () {
expect(await testCatalog.getType()).to.equal("misc");
});
it("supports interface", async function () {
expect(await testCatalog.supportsInterface("0xd912401f")).to.equal(true);
});
it("does not support other interfaces", async function () {
expect(await testCatalog.supportsInterface("0xffffffff")).to.equal(false);
});
});
describe("add catalog entries", async function () {
it("can add fixed part", async function () {
const partId = 1;
const partData = {
itemType: fixedType,
z: 0,
equippable: [],
metadataURI: metadataUriDefault,
};
await testCatalog.addPart({ partId: partId, part: partData });
expect(await testCatalog.getPart(partId)).to.eql([
2,
0,
[],
metadataUriDefault,
]);
});
it("can add slot part", async function () {
const partId = 2;
await testCatalog.addPart({ partId: partId, part: sampleSlotPartData });
expect(await testCatalog.getPart(partId)).to.eql([
1,
0,
[],
metadataUriDefault,
]);
});
it("can add parts list", async function () {
const partId = 1;
const partId2 = 2;
const partData1 = {
itemType: slotType,
z: 0,
equippable: [],
metadataURI: "src1",
};
const partData2 = {
itemType: fixedType,
z: 1,
equippable: [],
metadataURI: "src2",
};
await testCatalog.addPartList([
{ partId: partId, part: partData1 },
{ partId: partId2, part: partData2 },
]);
expect(await testCatalog.getParts([partId, partId2])).to.eql([
[slotType, 0, [], "src1"],
[fixedType, 1, [], "src2"],
]);
});
it("cannot add part with id 0", async function () {
const partId = 0;
await expect(
testCatalog.addPart({ partId: partId, part: sampleSlotPartData })
).to.be.revertedWithCustomError(testCatalog, "IdZeroForbidden");
});
it("cannot add part with existing partId", async function () {
const partId = 3;
await testCatalog.addPart({ partId: partId, part: sampleSlotPartData });
await expect(
testCatalog.addPart({ partId: partId, part: sampleSlotPartData })
).to.be.revertedWithCustomError(testCatalog, "PartAlreadyExists");
});
it("cannot add part with item type None", async function () {
const partId = 1;
const badPartData = {
itemType: noType,
z: 0,
equippable: [],
metadataURI: metadataUriDefault,
};
await expect(
testCatalog.addPart({ partId: partId, part: badPartData })
).to.be.revertedWithCustomError(testCatalog, "BadConfig");
});
it("cannot add fixed part with equippable addresses", async function () {
const partId = 1;
const badPartData = {
itemType: fixedType,
z: 0,
equippable: [addrs[3].address],
metadataURI: metadataUriDefault,
};
await expect(
testCatalog.addPart({ partId: partId, part: badPartData })
).to.be.revertedWithCustomError(testCatalog, "BadConfig");
});
it("is not equippable if address was not added", async function () {
const partId = 4;
await testCatalog.addPart({ partId: partId, part: sampleSlotPartData });
expect(
await testCatalog.checkIsEquippable(partId, addrs[1].address)
).to.eql(false);
});
it("is equippable if added in the part definition", async function () {
const partId = 1;
const partData = {
itemType: slotType,
z: 0,
equippable: [addrs[1].address, addrs[2].address],
metadataURI: metadataUriDefault,
};
await testCatalog.addPart({ partId: partId, part: partData });
expect(
await testCatalog.checkIsEquippable(partId, addrs[2].address)
).to.eql(true);
});
it("is equippable if added afterward", async function () {
const partId = 1;
await expect(
testCatalog.addPart({ partId: partId, part: sampleSlotPartData })
)
.to.emit(testCatalog, "AddedPart")
.withArgs(
partId,
sampleSlotPartData.itemType,
sampleSlotPartData.z,
sampleSlotPartData.equippable,
sampleSlotPartData.metadataURI
);
await expect(
testCatalog.addEquippableAddresses(partId, [addrs[1].address])
)
.to.emit(testCatalog, "AddedEquippables")
.withArgs(partId, [addrs[1].address]);
expect(
await testCatalog.checkIsEquippable(partId, addrs[1].address)
).to.eql(true);
});
it("is equippable if set afterward", async function () {
const partId = 1;
await testCatalog.addPart({ partId: partId, part: sampleSlotPartData });
await expect(
testCatalog.setEquippableAddresses(partId, [addrs[1].address])
)
.to.emit(testCatalog, "SetEquippables")
.withArgs(partId, [addrs[1].address]);
expect(
await testCatalog.checkIsEquippable(partId, addrs[1].address)
).to.eql(true);
});
it("is equippable if set to all", async function () {
const partId = 1;
await testCatalog.addPart({ partId: partId, part: sampleSlotPartData });
await expect(testCatalog.setEquippableToAll(partId))
.to.emit(testCatalog, "SetEquippableToAll")
.withArgs(partId);
expect(await testCatalog.checkIsEquippableToAll(partId)).to.eql(true);
expect(
await testCatalog.checkIsEquippable(partId, addrs[1].address)
).to.eql(true);
});
it("cannot add nor set equippable addresses for non existing part", async function () {
const partId = 1;
await testCatalog.addPart({ partId: partId, part: sampleSlotPartData });
await expect(
testCatalog.addEquippableAddresses(partId, [])
).to.be.revertedWithCustomError(testCatalog, "ZeroLengthIdsPassed");
await expect(
testCatalog.setEquippableAddresses(partId, [])
).to.be.revertedWithCustomError(testCatalog, "ZeroLengthIdsPassed");
});
it("cannot add nor set empty list of equippable addresses", async function () {
const NonExistingPartId = 1;
await expect(
testCatalog.addEquippableAddresses(NonExistingPartId, [
addrs[1].address,
])
).to.be.revertedWithCustomError(testCatalog, "PartDoesNotExist");
await expect(
testCatalog.setEquippableAddresses(NonExistingPartId, [
addrs[1].address,
])
).to.be.revertedWithCustomError(testCatalog, "PartDoesNotExist");
await expect(
testCatalog.setEquippableToAll(NonExistingPartId)
).to.be.revertedWithCustomError(testCatalog, "PartDoesNotExist");
});
it("cannot add nor set equippable addresses to non slot part", async function () {
const fixedPartId = 1;
const partData = {
itemType: fixedType, // This is what we're testing
z: 0,
equippable: [],
metadataURI: metadataUriDefault,
};
await testCatalog.addPart({ partId: fixedPartId, part: partData });
await expect(
testCatalog.addEquippableAddresses(fixedPartId, [addrs[1].address])
).to.be.revertedWithCustomError(testCatalog, "PartIsNotSlot");
await expect(
testCatalog.setEquippableAddresses(fixedPartId, [addrs[1].address])
).to.be.revertedWithCustomError(testCatalog, "PartIsNotSlot");
await expect(
testCatalog.setEquippableToAll(fixedPartId)
).to.be.revertedWithCustomError(testCatalog, "PartIsNotSlot");
});
it("cannot set equippable to all on non existing part", async function () {
const nonExistingPartId = 1;
await expect(
testCatalog.setEquippableToAll(nonExistingPartId)
).to.be.revertedWithCustomError(testCatalog, "PartDoesNotExist");
});
it("resets equippable to all if addresses are set", async function () {
const partId = 1;
await testCatalog.addPart({ partId: partId, part: sampleSlotPartData });
await testCatalog.setEquippableToAll(partId);
// This should reset it:
testCatalog.setEquippableAddresses(partId, [addrs[1].address]);
expect(await testCatalog.checkIsEquippableToAll(partId)).to.eql(false);
});
it("resets equippable to all if addresses are added", async function () {
const partId = 1;
await testCatalog.addPart({ partId: partId, part: sampleSlotPartData });
await testCatalog.setEquippableToAll(partId);
// This should reset it:
await testCatalog.addEquippableAddresses(partId, [addrs[1].address]);
expect(await testCatalog.checkIsEquippableToAll(partId)).to.eql(false);
});
it("can reset equippable addresses", async function () {
const partId = 1;
await testCatalog.addPart({ partId: partId, part: sampleSlotPartData });
await testCatalog.addEquippableAddresses(partId, [
addrs[1].address,
addrs[2].address,
]);
await testCatalog.resetEquippableAddresses(partId);
expect(
await testCatalog.checkIsEquippable(partId, addrs[1].address)
).to.eql(false);
});
});
});