generated from 0xEssential/template-ethereum-contracts
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCryptOrchidERC721.test.ts
343 lines (279 loc) · 11.5 KB
/
CryptOrchidERC721.test.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import chai, {expect} from './chai-setup';
import {chunk} from 'lodash';
import {ethers, deployments, getUnnamedAccounts} from 'hardhat';
import {setupUsers} from './utils';
import {add, compareAsc} from 'date-fns';
import {BigNumber} from 'ethers';
const maxUnits = 20;
const minUnits = 1;
const keyhash =
'0x6c3699283bda56ad74f6b855546325b68d482e983852a7a82979cc4807b641f4';
const numberBetween = (min: number, max: number) =>
Math.floor(Math.random() * (max - min + 1)) + min;
describe('CryptOrchidERC721', function () {
let mockedCoordinator;
let cryptorchidsContract;
let account;
const setup = deployments.createFixture(async () => {
const [owner] = await ethers.getSigners();
const MockLink = await ethers.getContractFactory('MockLink');
const VRFCoordinatorMock = await ethers.getContractFactory(
'VRFCoordinatorMock'
);
const link = await MockLink.deploy();
const vrfCoordinatorMock = await VRFCoordinatorMock.deploy(link.address);
const CryptOrchidERC721 = await ethers.getContractFactory(
'CryptOrchidsMock'
);
const CryptOrchids = await CryptOrchidERC721.deploy(
vrfCoordinatorMock.address,
link.address,
keyhash
);
await link.transfer(CryptOrchids.address, '2000000000000000000');
const contracts = {
CryptOrchidERC721: CryptOrchids,
VRFCoordinatorMock: vrfCoordinatorMock,
};
const users = await setupUsers(await getUnnamedAccounts(), contracts);
await CryptOrchids.startSale();
await CryptOrchids.startGrowing();
return {
...contracts,
users,
owner,
};
});
const webMint = async (units: number) => {
const {CryptOrchidERC721, VRFCoordinatorMock, users} = await setup();
cryptorchidsContract = CryptOrchidERC721;
account = users[0];
mockedCoordinator = VRFCoordinatorMock;
await account.CryptOrchidERC721.webMint(units, {
value: ethers.utils.parseUnits('0.04', 'ether').mul(units),
});
};
const germinate = async (
tokenId: number,
pseudoRandom = numberBetween(0, 10_000)
) => {
const transaction = await account.CryptOrchidERC721.germinate(
tokenId,
pseudoRandom
);
const tx_receipt = await transaction.wait();
const requestIds = chunk(tx_receipt.events, 4).reduce(
(acc, chunk) => [...acc, chunk[3].data],
[]
);
return Promise.all(
requestIds.map(
async (requestId) =>
await mockedCoordinator.callBackWithRandomness(
requestId,
pseudoRandom,
cryptorchidsContract.address
)
)
);
};
it('Does not create any tokens on deployment', async function () {
const {CryptOrchidERC721} = await setup();
expect(await CryptOrchidERC721.totalSupply()).to.equal(0);
});
it('Uses 7 days as the GROWTH_CYCLE', async function () {
const {CryptOrchidERC721} = await setup();
const GROWTH_CYCLE = await CryptOrchidERC721.GROWTH_CYCLE();
const now = new Date();
const wateringStart = add(now, {seconds: GROWTH_CYCLE.toNumber()});
const expectedWateringStart = add(now, {days: 7});
expect(compareAsc(wateringStart, expectedWateringStart)).to.equal(0);
});
it('Uses 3 hours as the WATERING_WINDOW', async function () {
const {CryptOrchidERC721} = await setup();
const WATERING_WINDOW = await CryptOrchidERC721.WATERING_WINDOW();
const now = new Date();
const wateringEnd = add(now, {seconds: WATERING_WINDOW.toNumber()});
const expectedWateringEnd = add(now, {hours: 3});
expect(compareAsc(wateringEnd, expectedWateringEnd)).to.equal(0);
});
describe('minting', function () {
it('Mints units number of tokens for sender', async () => {
const units = numberBetween(maxUnits, minUnits);
await webMint(units);
const supply = await cryptorchidsContract.totalSupply();
const ownedBySender = await cryptorchidsContract.balanceOf(
account.address
);
expect(supply.toNumber()).to.equal(units);
expect(ownedBySender).to.equal(units);
});
});
describe('germinating', function () {
let tokenId;
beforeEach(async () => {
await webMint(1);
tokenId = await cryptorchidsContract.tokenOfOwnerByIndex(
account.address,
0
);
});
it('Throws when non-owner calls water function', async function () {
expect(async () => await cryptorchidsContract.germinate(tokenId)).to
.throw;
});
it('sets plantedAt', async () => {
await germinate(tokenId);
const {1: plantedAt} = await cryptorchidsContract.getTokenMetadata(
tokenId
);
expect(plantedAt).to.not.eq(0);
});
// For picking species, we receive a huge number from VRF which we grab the 10k modulus.
// Rather than using those massive numbers in test, we're using 0 - 10k, which results
// in the same modulus frequency.
it('Germinates a moth orchid for randomNumber <= 3074', async function () {
await germinate(tokenId, numberBetween(0, 3074));
const {0: species} = await cryptorchidsContract.getTokenMetadata(tokenId);
expect(species).to.equal('phalaenopsis micholitzii');
});
it('Germinates an orange cattelya for 3074 < randomNumber <= 6074', async function () {
await germinate(1, numberBetween(3075, 6074));
const {0: species} = await cryptorchidsContract.getTokenMetadata(tokenId);
expect(species).to.equal('guarianthe aurantiaca');
});
it('Germinates a blue vanda for 6074 < randomNumber <= 8074', async function () {
await germinate(tokenId, numberBetween(6075, 8074));
const {0: species} = await cryptorchidsContract.getTokenMetadata(tokenId);
expect(species).to.equal('vanda coerulea');
});
it("Germinates a lady's slipper for 8074 < randomNumber <= 9074", async function () {
await germinate(tokenId, numberBetween(8075, 9074));
const {0: species} = await cryptorchidsContract.getTokenMetadata(tokenId);
expect(species).to.equal('cypripedium calceolus');
});
it('Germinates a Vietnamese Paphiopedilum for 9_074 < randomNumber <= 9574', async function () {
await germinate(tokenId, numberBetween(9_075, 9_574));
const {0: species} = await cryptorchidsContract.getTokenMetadata(tokenId);
expect(species).to.equal('paphiopedilum vietnamense');
});
it('Germinates a Kayasima Miltonia for 9574 < randomNumber <= 9824', async function () {
await germinate(tokenId, numberBetween(9575, 9824));
const {0: species} = await cryptorchidsContract.getTokenMetadata(tokenId);
expect(species).to.equal('miltonia kayasimae');
});
it("Germinates a Hochstetter's butterfly orchid for 9824 < randomNumber <= 9924", async function () {
await germinate(tokenId, numberBetween(9_825, 9_924));
const {0: species} = await cryptorchidsContract.getTokenMetadata(tokenId);
expect(species).to.equal('platanthera azorica');
});
it('Germinates a Ghost orchid for 9_924 < randomNumber <= 9_974', async function () {
await germinate(tokenId, numberBetween(9_925, 9_974));
const {0: species} = await cryptorchidsContract.getTokenMetadata(tokenId);
expect(species).to.equal('dendrophylax lindenii');
});
it('Germinates a Gold of Kinabalu for 9_974 < randomNumber <= 9_999', async function () {
await germinate(tokenId, numberBetween(9_975, 9_999));
const {0: species} = await cryptorchidsContract.getTokenMetadata(tokenId);
expect(species).to.equal('paphiopedilum rothschildianum');
});
it('Germiantes THE Shenzen Nongke for randomNumber = 10_000', async function () {
await germinate(1, 10_000);
const {0: species} = await cryptorchidsContract.getTokenMetadata(tokenId);
expect(species).to.equal('shenzhenica orchidaceae');
});
});
describe('Token functionality', () => {
let tokenId;
let GROWTH_CYCLE_MS;
let WATERING_WINDOW_MS;
beforeEach(async () => {
await webMint(1);
const cycle = await cryptorchidsContract.GROWTH_CYCLE();
const wateringWindow = await cryptorchidsContract.WATERING_WINDOW();
GROWTH_CYCLE_MS = cycle.mul(BigNumber.from(1000));
WATERING_WINDOW_MS = wateringWindow.mul(BigNumber.from(1000));
tokenId = await cryptorchidsContract.tokenOfOwnerByIndex(
account.address,
0
);
await germinate(tokenId);
});
it('returns true when a plant is alive less time than GROWTH_CYCLE', async () => {
const alive = await cryptorchidsContract.alive(tokenId);
expect(alive).to.eq(true);
});
it('returns true when a plant is in first WATERING_WINDOW', async () => {
await cryptorchidsContract.timeTravel(
GROWTH_CYCLE_MS.add(WATERING_WINDOW_MS.sub(60_000)).div(1000)
);
const alive = await cryptorchidsContract.alive(tokenId);
expect(alive).to.eq(true);
});
it('returns false when a plant is past first WATERING_WINDOW', async () => {
await cryptorchidsContract.timeTravel(
GROWTH_CYCLE_MS.add(WATERING_WINDOW_MS.add(60_000)).div(1000)
);
const alive = await cryptorchidsContract.alive(tokenId);
expect(alive).to.eq(false);
});
it('returns true when a plant is in second WATERING_WINDOW', async () => {
await cryptorchidsContract.timeTravel(
GROWTH_CYCLE_MS.add(WATERING_WINDOW_MS.sub(60_000)).div(1000)
);
// Water during first watering window
const transaction = await account.CryptOrchidERC721.water(tokenId);
transaction.wait();
const {2: waterLevel} = await cryptorchidsContract.getTokenMetadata(
tokenId
);
expect(waterLevel).to.eq(1);
// move ahead to second window
await cryptorchidsContract.timeTravel(
GROWTH_CYCLE_MS.add(WATERING_WINDOW_MS.mul(2)).div(1000)
);
const alive = await cryptorchidsContract.alive(tokenId);
expect(alive).to.eq(true);
});
it('returns true when a plant is in third WATERING_WINDOW', async () => {
await cryptorchidsContract.timeTravel(
GROWTH_CYCLE_MS.add(WATERING_WINDOW_MS.sub(60_000)).div(1000)
);
// Water during first watering window
const transaction = await account.CryptOrchidERC721.water(tokenId);
transaction.wait();
const {2: waterLevel} = await cryptorchidsContract.getTokenMetadata(
tokenId
);
expect(waterLevel).to.eq(1);
// move ahead to second window
await cryptorchidsContract.timeTravel(
GROWTH_CYCLE_MS.mul(2).add(WATERING_WINDOW_MS.sub(60_000)).div(1000)
);
// Water during second watering window
const secondTransaction = await account.CryptOrchidERC721.water(tokenId);
secondTransaction.wait();
const {2: waterLevelTwo} = await cryptorchidsContract.getTokenMetadata(
tokenId
);
expect(waterLevelTwo).to.eq(2);
// Move ahead to third window
await cryptorchidsContract.timeTravel(
GROWTH_CYCLE_MS.mul(3).add(WATERING_WINDOW_MS.sub(60_000)).div(1000)
);
const alive = await cryptorchidsContract.alive(tokenId);
expect(alive).to.eq(true);
});
it('Increases water level when owner calls water function', async function () {
const transaction = await account.CryptOrchidERC721.water(tokenId);
transaction.wait();
const {2: waterLevel} = await cryptorchidsContract.getTokenMetadata(
tokenId
);
expect(waterLevel).to.eq(1);
});
it('Throws when non-owner calls water function', async function () {
expect(async () => await cryptorchidsContract.water(tokenId)).to.throw;
});
});
});