-
Notifications
You must be signed in to change notification settings - Fork 208
/
test-sellTickets.js
527 lines (458 loc) · 16.2 KB
/
test-sellTickets.js
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
// eslint-disable-next-line import/no-extraneous-dependencies
import '@agoric/install-ses';
// eslint-disable-next-line import/no-extraneous-dependencies
import test from 'ava';
import bundleSource from '@agoric/bundle-source';
import { makeIssuerKit, makeLocalAmountMath } from '@agoric/ertp';
import { E } from '@agoric/eventual-send';
import fakeVatAdmin from '../../../src/contractFacet/fakeVatAdmin';
// noinspection ES6PreferShortImport
import { makeZoe } from '../../../src/zoeService/zoe';
import { defaultAcceptanceMsg } from '../../../src/contractSupport';
const mintAndSellNFTRoot = `${__dirname}/../../../src/contracts/mintAndSellNFT`;
const sellItemsRoot = `${__dirname}/../../../src/contracts/sellItems`;
test(`mint and sell tickets for multiple shows`, async t => {
// Setup initial conditions
const zoe = makeZoe(fakeVatAdmin);
const mintAndSellNFTBundle = await bundleSource(mintAndSellNFTRoot);
const mintAndSellNFTInstallation = await E(zoe).install(mintAndSellNFTBundle);
const sellItemsBundle = await bundleSource(sellItemsRoot);
const sellItemsInstallation = await E(zoe).install(sellItemsBundle);
const { issuer: moolaIssuer, amountMath: moolaAmountMath } = makeIssuerKit(
'moola',
);
const { creatorFacet: ticketMaker } = await E(zoe).startInstance(
mintAndSellNFTInstallation,
);
const { sellItemsCreatorSeat, sellItemsInstance } = await E(
ticketMaker,
).sellTokens({
customValueProperties: {
show: 'Steven Universe, the Opera',
start: 'Wed, March 25th 2020 at 8pm',
},
count: 3,
moneyIssuer: moolaIssuer,
sellItemsInstallation,
pricePerItem: moolaAmountMath.make(20),
});
t.is(
await sellItemsCreatorSeat.getOfferResult(),
defaultAcceptanceMsg,
`escrowTicketsOutcome is default acceptance message`,
);
const ticketIssuerP = E(ticketMaker).getIssuer();
const ticketBrand = await E(ticketIssuerP).getBrand();
const ticketSalesPublicFacet = await E(zoe).getPublicFacet(sellItemsInstance);
const ticketsForSale = await E(ticketSalesPublicFacet).getAvailableItems();
t.deepEqual(
ticketsForSale,
{
brand: ticketBrand,
value: [
{
show: 'Steven Universe, the Opera',
start: 'Wed, March 25th 2020 at 8pm',
number: 1,
},
{
show: 'Steven Universe, the Opera',
start: 'Wed, March 25th 2020 at 8pm',
number: 2,
},
{
show: 'Steven Universe, the Opera',
start: 'Wed, March 25th 2020 at 8pm',
number: 3,
},
],
},
`the tickets are up for sale`,
);
const { sellItemsInstance: sellItemsInstance2 } = await E(
ticketMaker,
).sellTokens({
customValueProperties: {
show: 'Reserved for private party',
start: 'Tues May 12, 2020 at 8pm',
},
count: 2,
moneyIssuer: moolaIssuer,
sellItemsInstallation,
pricePerItem: moolaAmountMath.make(20),
});
const sellItemsPublicFacet2 = await E(zoe).getPublicFacet(sellItemsInstance2);
const ticketsForSale2 = await E(sellItemsPublicFacet2).getAvailableItems();
t.deepEqual(
ticketsForSale2,
{
brand: ticketBrand,
value: [
{
show: 'Reserved for private party',
start: 'Tues May 12, 2020 at 8pm',
number: 1,
},
{
show: 'Reserved for private party',
start: 'Tues May 12, 2020 at 8pm',
number: 2,
},
],
},
`we can reuse the mint to make more tickets and sell them in a different instance`,
);
});
// __Test Scenario__
// The Opera de Bordeaux plays the contract creator and the auditorium
// It creates the contract for a show ("Steven Universe, the Opera", Wed, March
// 25th 2020 at 8pm, 3 tickets)
// The Opera wants 22 moolas per ticket
// Alice buys ticket #1
// Then, the Joker tries malicious things:
// - they try to buy ticket #1 (and will fail because Alice already
// bought it)
// - they try to buy to buy ticket #2 for 1 moola (and will fail)
// Then, Bob buys ticket #2 and #3
// The Opera is told about the show being sold out. It gets all the moolas from
// the sale
test(`mint and sell opera tickets`, async t => {
// Setup initial conditions
const {
mint: moolaMint,
issuer: moolaIssuer,
amountMath: { make: moola },
} = makeIssuerKit('moola');
const zoe = makeZoe(fakeVatAdmin);
const mintAndSellNFTBundle = await bundleSource(mintAndSellNFTRoot);
const mintAndSellNFTInstallation = await E(zoe).install(mintAndSellNFTBundle);
const sellItemsBundle = await bundleSource(sellItemsRoot);
const sellItemsInstallation = await E(zoe).install(sellItemsBundle);
// === Initial Opera de Bordeaux part ===
// create an instance of the venue contract
const mintTickets = async () => {
const { creatorFacet: ticketSeller } = await E(zoe).startInstance(
mintAndSellNFTInstallation,
);
const {
sellItemsCreatorSeat,
sellItemsCreatorFacet,
sellItemsPublicFacet,
} = await E(ticketSeller).sellTokens({
customValueProperties: {
show: 'Steven Universe, the Opera',
start: 'Wed, March 25th 2020 at 8pm',
},
count: 3,
moneyIssuer: moolaIssuer,
sellItemsInstallation,
pricePerItem: moola(22),
});
const ticketsForSale = await E(sellItemsPublicFacet).getAvailableItems();
t.is(ticketsForSale.value.length, 3, `3 tickets for sale`);
return harden({
sellItemsCreatorSeat,
sellItemsCreatorFacet,
});
};
// === Alice part ===
// Alice is given an invitation for the ticket sales instance
// and she has 100 moola
const aliceBuysTicket1 = async (invitation, moola100Payment) => {
const invitationIssuer = E(zoe).getInvitationIssuer();
const {
value: [{ instance }],
} = await E(invitationIssuer).getAmountOf(invitation);
const ticketSalesPublicFacet = await E(zoe).getPublicFacet(instance);
const terms = await E(zoe).getTerms(instance);
const ticketIssuer = await E(ticketSalesPublicFacet).getItemsIssuer();
const ticketAmountMath = await makeLocalAmountMath(ticketIssuer);
const alicePurse = await E(moolaIssuer).makeEmptyPurse();
await E(alicePurse).deposit(moola100Payment);
t.deepEqual(terms.pricePerItem, moola(22), `pricePerItem is 22 moola`);
const availableTickets = await E(
ticketSalesPublicFacet,
).getAvailableItems();
t.is(
availableTickets.value.length,
3,
'Alice should see 3 available tickets',
);
t.truthy(
availableTickets.value.find(ticket => ticket.number === 1),
`availableTickets contains ticket number 1`,
);
t.truthy(
availableTickets.value.find(ticket => ticket.number === 2),
`availableTickets contains ticket number 2`,
);
t.truthy(
availableTickets.value.find(ticket => ticket.number === 3),
`availableTickets contains ticket number 3`,
);
// find the value corresponding to ticket #1
const ticket1Value = availableTickets.value.find(
ticket => ticket.number === 1,
);
// make the corresponding amount
const ticket1Amount = ticketAmountMath.make(harden([ticket1Value]));
const aliceProposal = harden({
give: { Money: terms.pricePerItem },
want: { Items: ticket1Amount },
});
const alicePaymentForTicket = alicePurse.withdraw(terms.pricePerItem);
const alicePaymentKeywordRecord = harden({ Money: alicePaymentForTicket });
const seat = await E(zoe).offer(
invitation,
aliceProposal,
alicePaymentKeywordRecord,
);
const aliceTickets = seat.getPayout('Items');
const aliceBoughtTicketAmount = await E(ticketIssuer).getAmountOf(
aliceTickets,
);
t.is(
aliceBoughtTicketAmount.value[0].show,
'Steven Universe, the Opera',
'Alice should have received the ticket for the correct show',
);
t.is(
aliceBoughtTicketAmount.value[0].number,
1,
'Alice should have received the ticket for the correct number',
);
};
// === Joker part ===
// Joker starts with 100 moolas
// Joker attempts to buy ticket 1 (and should fail)
const jokerBuysTicket1 = async (untrustedInvitation, moola100Payment) => {
const invitationIssuer = E(zoe).getInvitationIssuer();
const invitation = await E(invitationIssuer).claim(untrustedInvitation);
const {
value: [{ instance: ticketSalesInstance }],
} = await E(invitationIssuer).getAmountOf(invitation);
const ticketSalesPublicFacet = await E(zoe).getPublicFacet(
ticketSalesInstance,
);
const ticketIssuer = await E(ticketSalesPublicFacet).getItemsIssuer();
const ticketAmountMath = await makeLocalAmountMath(ticketIssuer);
const jokerPurse = await E(moolaIssuer).makeEmptyPurse();
await E(jokerPurse).deposit(moola100Payment);
const { pricePerItem } = await E(zoe).getTerms(ticketSalesInstance);
// Joker does NOT check available tickets and tries to buy the ticket
// number 1(already bought by Alice, but he doesn't know)
const ticket1Amount = ticketAmountMath.make(
harden([
{
show: 'Steven Universe, the Opera',
start: 'Wed, March 25th 2020 at 8pm',
number: 1,
},
]),
);
const jokerProposal = harden({
give: { Money: pricePerItem },
want: { Items: ticket1Amount },
});
const jokerPaymentForTicket = jokerPurse.withdraw(pricePerItem);
const seat = await zoe.offer(
invitation,
jokerProposal,
harden({
Money: jokerPaymentForTicket,
}),
);
console.log(
'EXPECTED ERROR: Some of the wanted items were not available for sale >>>',
);
await t.throwsAsync(
seat.getOfferResult(),
{ message: /Some of the wanted items were not available for sale/ },
'ticket 1 is no longer available',
);
const jokerTicketPayoutAmount = await ticketIssuer.getAmountOf(
seat.getPayout('Items'),
);
const jokerMoneyPayoutAmount = await moolaIssuer.getAmountOf(
seat.getPayout('Money'),
);
t.truthy(
ticketAmountMath.isEmpty(jokerTicketPayoutAmount),
'Joker should not receive ticket #1',
);
t.deepEqual(
jokerMoneyPayoutAmount,
moola(22),
'Joker should get a refund after trying to get ticket #1',
);
};
// Joker attempts to buy ticket 2 for 1 moola (and should fail)
const jokerTriesToBuyTicket2 = async (
untrustedInvitation,
moola100Payment,
) => {
const invitationIssuer = E(zoe).getInvitationIssuer();
const invitation = await E(invitationIssuer).claim(untrustedInvitation);
const {
value: [{ instance: ticketSalesInstance }],
} = await E(invitationIssuer).getAmountOf(invitation);
const ticketSalesPublicFacet = await E(zoe).getPublicFacet(
ticketSalesInstance,
);
const ticketIssuer = await E(ticketSalesPublicFacet).getItemsIssuer();
const ticketAmountMath = await makeLocalAmountMath(ticketIssuer);
const jokerPurse = await E(moolaIssuer).makeEmptyPurse();
await E(jokerPurse).deposit(moola100Payment);
const ticket2Amount = ticketAmountMath.make(
harden([
{
show: 'Steven Universe, the Opera',
start: 'Wed, March 25th 2020 at 8pm',
number: 2,
},
]),
);
const insufficientAmount = moola(1);
const jokerProposal = harden({
give: { Money: insufficientAmount },
want: { Items: ticket2Amount },
});
const jokerInsufficientPaymentForTicket = jokerPurse.withdraw(
insufficientAmount,
);
const seat = await zoe.offer(
invitation,
jokerProposal,
harden({
Money: jokerInsufficientPaymentForTicket,
}),
);
console.log(
'EXPECTED ERROR: More money is required to buy these items >>> ',
);
await t.throwsAsync(
seat.getOfferResult(),
{ message: /More money.*is required to buy these items/ },
'outcome from Joker should throw when trying to buy a ticket for 1 moola',
);
const jokerTicketPayoutAmount = await ticketIssuer.getAmountOf(
seat.getPayout('Items'),
);
const jokerMoneyPayoutAmount = await moolaIssuer.getAmountOf(
seat.getPayout('Money'),
);
t.truthy(
ticketAmountMath.isEmpty(jokerTicketPayoutAmount),
'Joker should not receive ticket #2',
);
t.deepEqual(
jokerMoneyPayoutAmount,
insufficientAmount,
'Joker should get a refund after trying to get ticket #2 for 1 moola',
);
};
const bobBuysTicket2And3 = async (untrustedInvitation, moola100Payment) => {
const invitationIssuer = E(zoe).getInvitationIssuer();
const invitation = await E(invitationIssuer).claim(untrustedInvitation);
const {
value: [{ instance: ticketSalesInstance }],
} = await E(invitationIssuer).getAmountOf(invitation);
const ticketSalesPublicFacet = await E(zoe).getPublicFacet(
ticketSalesInstance,
);
const terms = await E(zoe).getTerms(ticketSalesInstance);
const ticketIssuer = await E(ticketSalesPublicFacet).getItemsIssuer();
const ticketAmountMath = await makeLocalAmountMath(ticketIssuer);
const bobPurse = await E(moolaIssuer).makeEmptyPurse();
await E(bobPurse).deposit(moola100Payment);
/** @type {Amount} */
const availableTickets = await E(
ticketSalesPublicFacet,
).getAvailableItems();
// Bob sees the currently available tickets
t.is(
availableTickets.value.length,
2,
'Bob should see 2 available tickets',
);
t.truthy(
!availableTickets.value.find(ticket => ticket.number === 1),
`availableTickets should NOT contain ticket number 1`,
);
t.truthy(
availableTickets.value.find(ticket => ticket.number === 2),
`availableTickets should still contain ticket number 2`,
);
t.truthy(
availableTickets.value.find(ticket => ticket.number === 3),
`availableTickets should still contain ticket number 3`,
);
// Bob buys tickets 2 and 3
const ticket2and3Amount = ticketAmountMath.make(
harden([
availableTickets.value.find(ticket => ticket.number === 2),
availableTickets.value.find(ticket => ticket.number === 3),
]),
);
const totalCost = moola(2 * terms.pricePerItem.value);
const bobProposal = harden({
give: { Money: totalCost },
want: { Items: ticket2and3Amount },
});
const bobPaymentForTicket = await E(bobPurse).withdraw(totalCost);
const paymentKeywordRecord = harden({
Money: bobPaymentForTicket,
});
const seat = await E(zoe).offer(
invitation,
bobProposal,
paymentKeywordRecord,
);
const bobTicketAmount = await E(ticketIssuer).getAmountOf(
seat.getPayout('Items'),
);
t.is(bobTicketAmount.value.length, 2, 'Bob should have received 2 tickets');
t.truthy(
bobTicketAmount.value.find(ticket => ticket.number === 2),
'Bob should have received tickets #2',
);
t.truthy(
bobTicketAmount.value.find(ticket => ticket.number === 3),
'Bob should have received tickets #3',
);
};
// === Final Opera part ===
const ticketSellerClosesContract = async sellItemsCreatorSeat => {
const operaPurse = moolaIssuer.makeEmptyPurse();
const moneyPayment = await E(sellItemsCreatorSeat).getPayout('Money');
await E(operaPurse).deposit(moneyPayment);
const currentPurseBalance = await E(operaPurse).getCurrentAmount();
t.is(
currentPurseBalance.value,
3 * 22,
`The Opera should get ${3 * 22} moolas from ticket sales`,
);
};
const { sellItemsCreatorSeat, sellItemsCreatorFacet } = await mintTickets();
const ticketSalesInvitation1 = E(sellItemsCreatorFacet).makeBuyerInvitation();
await aliceBuysTicket1(
ticketSalesInvitation1,
moolaMint.mintPayment(moola(100)),
);
const ticketSalesInvitation2 = E(sellItemsCreatorFacet).makeBuyerInvitation();
await jokerBuysTicket1(
ticketSalesInvitation2,
moolaMint.mintPayment(moola(100)),
);
const ticketSalesInvitation3 = E(sellItemsCreatorFacet).makeBuyerInvitation();
await jokerTriesToBuyTicket2(
ticketSalesInvitation3,
moolaMint.mintPayment(moola(100)),
);
const ticketSalesInvitation4 = E(sellItemsCreatorFacet).makeBuyerInvitation();
await bobBuysTicket2And3(
ticketSalesInvitation4,
moolaMint.mintPayment(moola(100)),
);
await ticketSellerClosesContract(sellItemsCreatorSeat);
});