-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathtest-readme.js
217 lines (177 loc) · 6.22 KB
/
test-readme.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
/* eslint-disable import/order -- https://github.com/endojs/endo/issues/1235 */
import { test } from '../../prepare-test-env-ava.js';
// @ts-check
import { E } from '@endo/eventual-send';
// We need to disable this lint until @agoric/vats is released
// and adopted in package.json.
// eslint-disable-next-line import/no-extraneous-dependencies
import { makeFakeBoard } from '@agoric/vats/tools/board-utils.js';
// #region importErtp
import { makeIssuerKit, AmountMath, AssetKind } from '@agoric/ertp';
// #endregion importErtp
test('ertp guide readme', async t => {
// #region makeIssuerKit
const {
issuer: quatloosIssuer,
mint: quatloosMint,
brand: quatloosBrand,
} = makeIssuerKit('quatloos');
// #endregion makeIssuerKit
t.is(quatloosBrand, quatloosIssuer.getBrand());
// #region seven
const quatloosSeven = AmountMath.make(quatloosBrand, 7n);
// #endregion seven
// #region mintPayment
const quatloosPayment = quatloosMint.mintPayment(quatloosSeven);
// #endregion mintPayment
// #region deposit
const quatloosPurse = quatloosIssuer.makeEmptyPurse();
quatloosPurse.deposit(quatloosPayment);
// #endregion deposit
// #region five
const quatloosFive = AmountMath.make(quatloosBrand, 5n);
// #endregion five
// #region withdraw
const myQuatloosPayment = quatloosPurse.withdraw(quatloosFive);
// #endregion withdraw
const aliceQuatloosPurse = quatloosIssuer.makeEmptyPurse();
// #region depositFacet
const aliceQuatloosDepositFacet = aliceQuatloosPurse.getDepositFacet();
// #endregion depositFacet
const board = makeFakeBoard();
// #region getId
const aliceQuatloosDepositFacetId = await E(board).getId(
aliceQuatloosDepositFacet,
);
// #endregion getId
// #region getValue
const depositFacet = E(board).getValue(aliceQuatloosDepositFacetId);
await E(depositFacet).receive(myQuatloosPayment);
// #endregion getValue
t.is(await depositFacet, aliceQuatloosDepositFacet);
t.deepEqual(await aliceQuatloosPurse.getCurrentAmount(), quatloosFive);
// #region ticketValues
const startDateString = new Date(2019, 11, 9, 20, 30).toISOString();
const ticketValues = Array(1114)
.fill()
.map((_, i) => ({
seat: i + 1,
show: 'Hamilton',
start: startDateString,
}));
// #endregion ticketValues
// #region makeTicketIssuer
const { mint: agoricTheatreTicketMint, brand: agoricTheatreTicketBrand } =
makeIssuerKit('Agoric Theater tickets', AssetKind.SET);
// #endregion makeTicketIssuer
// #region ticketPayments
const ticketAmounts = ticketValues.map(ticketValue =>
AmountMath.make(agoricTheatreTicketBrand, harden([ticketValue])),
);
const agoricTheatreTicketPayments = ticketAmounts.map(ticketAmount =>
agoricTheatreTicketMint.mintPayment(ticketAmount),
);
// #endregion ticketPayments
const agoricTheatreTicketIssuer = agoricTheatreTicketMint.getIssuer();
const allLive = await Promise.all(
agoricTheatreTicketPayments.map(payment =>
agoricTheatreTicketIssuer.isLive(payment),
),
);
t.truthy(allLive.every(a => a));
});
test('MarkM 2024-10 talk', t => {
/** @type {Record<string, {issuer: Issuer, brand: Brand}>} */
// #region amountMathProps
const { make, isGTE } = AmountMath;
// #endregion amountMathProps
// #region declareShared
const shared = {};
// #endregion declareShared
// #region aliceBuyer1
/** @param {{ bucks: Payment, vendor: any }} some */
const makeBuyer = some => {
const { bucks, tickets } = shared;
const my = {
bucks: bucks.issuer.makeEmptyPurse(),
tickets: tickets.issuer.makeEmptyPurse(),
};
my.bucks.deposit(some.bucks);
// #endregion aliceBuyer1
// #region aliceBuyer2
return harden({
buyTicket() {
const pmt = my.bucks.withdraw(make(bucks.brand, 10n));
const allegedTicket = some.vendor.buy(pmt);
// #endregion aliceBuyer2
// #region aliceBuyer3
const got = my.tickets.deposit(allegedTicket);
t.log('Alice got', got);
isGTE(got, make(tickets.brand, 1n)) || assert.fail();
return got;
},
getBalances: () => ({
bucks: my.bucks.getCurrentAmount(),
tickets: my.tickets.getCurrentAmount(),
}),
});
};
// #endregion aliceBuyer3
// #region bobSeller
/** @param {{ bucks: Payment, tickets: Payment}} some */
const makeSeller = some => {
const { bucks, tickets } = shared;
const my = {
bucks: bucks.issuer.makeEmptyPurse(),
tickets: tickets.issuer.makeEmptyPurse(),
};
my.bucks.deposit(some.bucks);
my.tickets.deposit(some.tickets);
return harden({
/** @param {Payment} allegedPayment */
buy(allegedPayment) {
const amt = my.bucks.deposit(allegedPayment);
isGTE(amt, make(bucks.brand, 10n)) || assert.fail();
t.log('Bob got', amt);
return my.tickets.withdraw(make(tickets.brand, 1n));
},
getBalances: () => ({
bucks: my.bucks.getCurrentAmount(),
tickets: my.tickets.getCurrentAmount(),
}),
});
};
// #endregion bobSeller
// #region makeBucks
const bucksKit = makeIssuerKit('Bucks');
const { mint: bucksMint, ...bucks } = bucksKit;
Object.assign(shared, { bucks });
// #endregion makeBucks
// #region bucksAmount
const bucks100 = AmountMath.make(bucks.brand, 100n);
// #endregion bucksAmount
// #region bucksPayment100
const paymentA = bucksMint.mintPayment(bucks100);
// #endregion bucksPayment100
// #region bobPayments
const { mint: ticketsMint, ...tickets } = makeIssuerKit('Tickets');
Object.assign(shared, { tickets });
const paymentsB = {
bucks: bucksMint.mintPayment(make(bucks.brand, 200n)),
tickets: ticketsMint.mintPayment(make(tickets.brand, 50n)),
};
// #endregion bobPayments
// #region aliceBuysFromBob
const bob = makeSeller(paymentsB);
const alice = makeBuyer({ bucks: paymentA, vendor: bob });
const howMuch = (bv, tv) => ({
bucks: make(bucks.brand, bv),
tickets: make(tickets.brand, tv),
});
t.deepEqual(alice.getBalances(), howMuch(100n, 0n));
t.deepEqual(bob.getBalances(), howMuch(200n, 50n));
alice.buyTicket();
t.deepEqual(alice.getBalances(), howMuch(90n, 1n));
t.deepEqual(bob.getBalances(), howMuch(210n, 49n));
// #endregion aliceBuysFromBob
});