@agoric/ertp 0.16.3-u17.1 → 0.16.3-u18.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/types.ts ADDED
@@ -0,0 +1,468 @@
1
+ import type { LatestTopic } from '@agoric/notifier';
2
+ import type { ERef } from '@endo/far';
3
+ import type { RemotableObject } from '@endo/pass-style';
4
+ import type { CopyBag, CopySet, Key, Pattern } from '@endo/patterns';
5
+ import type { AssetKind } from './amountMath.js';
6
+
7
+ export type { AssetKind } from './amountMath.js';
8
+
9
+ export type NatAmount = {
10
+ brand: Brand<'nat'>;
11
+ value: bigint;
12
+ };
13
+ export type SetAmount<K extends Key> = {
14
+ brand: Brand<'set'>;
15
+ value: K[];
16
+ };
17
+ export type CopySetAmount<K extends Key> = {
18
+ brand: Brand<'copySet'>;
19
+ value: CopySet<K>;
20
+ };
21
+ export type CopyBagAmount<K extends Key> = {
22
+ brand: Brand<'copyBag'>;
23
+ value: CopyBag<K>;
24
+ };
25
+ export type AnyAmount = {
26
+ brand: Brand<any>;
27
+ value: any;
28
+ };
29
+ /**
30
+ * Amounts are descriptions of digital assets, answering the questions "how
31
+ * much" and "of what kind". Amounts are values labeled with a brand.
32
+ * AmountMath executes the logic of how amounts are changed when digital
33
+ * assets are merged, separated, or otherwise manipulated. For example, a
34
+ * deposit of 2 bucks into a purse that already has 3 bucks gives a new purse
35
+ * balance of 5 bucks. An empty purse has 0 bucks. AmountMath relies heavily
36
+ * on polymorphic MathHelpers, which manipulate the unbranded portion.
37
+ */
38
+ export type Amount<
39
+ K extends AssetKind = AssetKind,
40
+ M extends Key = Key,
41
+ > = K extends 'nat'
42
+ ? NatAmount
43
+ : K extends 'set'
44
+ ? SetAmount<M>
45
+ : K extends 'copySet'
46
+ ? CopySetAmount<M>
47
+ : K extends 'copyBag'
48
+ ? CopyBagAmount<M>
49
+ : AnyAmount;
50
+ /**
51
+ * An `AmountValue` describes a set or quantity of assets that can be owned or
52
+ * shared.
53
+ *
54
+ * A fungible `AmountValue` uses a non-negative bigint to represent a quantity
55
+ * of that many assets.
56
+ *
57
+ * A non-fungible `AmountValue` uses an array or CopySet of `Key`s to represent
58
+ * a set of whatever asset each key represents. A `Key` is a passable value
59
+ * that can be used as an element in a set (SetStore or CopySet) or as the key
60
+ * in a map (MapStore or CopyMap).
61
+ *
62
+ * `SetValue` is for the deprecated set representation, using an array directly
63
+ * to represent the array of its elements. `CopySet` is the proper
64
+ * representation using a CopySet.
65
+ *
66
+ * A semi-fungible `CopyBag` is represented as a `CopyBag` of `Key` objects.
67
+ * "Bag" is synonymous with MultiSet, where an element of a bag can be present
68
+ * once or more times, i.e., some positive bigint number of times,
69
+ * representing that quantity of the asset represented by that key.
70
+ */
71
+ export type AmountValue =
72
+ | NatValue
73
+ | SetValue
74
+ | CopySet
75
+ | import('@endo/patterns').CopyBag;
76
+ /**
77
+ * See doc-comment
78
+ * for `AmountValue`.
79
+ */
80
+ export type AssetValueForKind<
81
+ K extends AssetKind,
82
+ M extends Key = Key,
83
+ > = K extends 'nat'
84
+ ? NatValue
85
+ : K extends 'set'
86
+ ? SetValue<M>
87
+ : K extends 'copySet'
88
+ ? CopySet<M>
89
+ : K extends 'copyBag'
90
+ ? CopyBag<M>
91
+ : never;
92
+ export type AssetKindForValue<V extends AmountValue> = V extends NatValue
93
+ ? 'nat'
94
+ : V extends SetValue
95
+ ? 'set'
96
+ : V extends CopySet
97
+ ? 'copySet'
98
+ : V extends import('@endo/patterns').CopyBag
99
+ ? 'copyBag'
100
+ : never;
101
+
102
+ export type Ratio = { numerator: Amount<'nat'>; denominator: Amount<'nat'> };
103
+
104
+ /** @deprecated */
105
+ export type DisplayInfo<K extends AssetKind = AssetKind> = {
106
+ /**
107
+ * Tells the display software how many
108
+ * decimal places to move the decimal over to the left, or in other words,
109
+ * which position corresponds to whole numbers. We require fungible digital
110
+ * assets to be represented in integers, in the smallest unit (i.e. USD might
111
+ * be represented in mill, a thousandth of a dollar. In that case,
112
+ * `decimalPlaces` would be 3.) This property is optional, and for
113
+ * non-fungible digital assets, should not be specified. The decimalPlaces
114
+ * property should be used for _display purposes only_. Any other use is an
115
+ * anti-pattern.
116
+ */
117
+ decimalPlaces?: number | undefined;
118
+ /**
119
+ * - the kind of asset, either AssetKind.NAT (fungible)
120
+ * or AssetKind.SET or AssetKind.COPY_SET (non-fungible)
121
+ */
122
+ assetKind: K;
123
+ };
124
+ export type BrandMethods<K extends AssetKind> = {
125
+ /**
126
+ * Should be used with `issuer.getBrand` to ensure an issuer and brand match.
127
+ */
128
+ isMyIssuer: (allegedIssuer: ERef<Issuer<K>>) => Promise<boolean>;
129
+ getAllegedName: () => string;
130
+ /** @deprecated look up in boardAux */
131
+ getDisplayInfo: () => DisplayInfo<K>;
132
+ getAmountShape: () => Pattern;
133
+ };
134
+ /**
135
+ * The brand identifies the
136
+ * kind of issuer, and has a function to get the alleged name for the kind of
137
+ * asset described. The alleged name (such as 'BTC' or 'moola') is provided by
138
+ * the maker of the issuer and should not be trusted as accurate.
139
+ *
140
+ * Every amount created by a particular issuer will share the same brand, but
141
+ * recipients cannot rely on the brand to verify that a purported amount
142
+ * represents the issuer they intended, since the same brand can be reused by
143
+ * a misbehaving issuer.
144
+ */
145
+ export type Brand<K extends AssetKind = AssetKind> = RemotableObject &
146
+ BrandMethods<K>;
147
+ /**
148
+ * Return true if the payment continues to exist.
149
+ *
150
+ * If the payment is a promise, the operation will proceed upon fulfillment.
151
+ */
152
+ export type IssuerIsLive = (payment: ERef<Payment>) => Promise<boolean>;
153
+ /**
154
+ * Get the amount of digital assets in the payment.
155
+ * Because the payment is not trusted, we cannot call a method on it directly,
156
+ * and must use the issuer instead.
157
+ *
158
+ * If the payment is a promise, the operation will proceed upon fulfillment.
159
+ */
160
+ export type IssuerGetAmountOf<K extends AssetKind, M extends Key = Key> = (
161
+ payment: ERef<Payment<K, M>>,
162
+ ) => Promise<Amount<K, M>>;
163
+ /**
164
+ * Burn all of the digital assets in the payment.
165
+ * `optAmountShape` is optional. If the `optAmountShape` pattern is present,
166
+ * the amount of the digital assets in the payment must match
167
+ * `optAmountShape`, to prevent sending the wrong payment and other
168
+ * confusion.
169
+ *
170
+ * If the payment is a promise, the operation will proceed upon fulfillment.
171
+ *
172
+ * As always with optional `Pattern` arguments, keep in mind that technically
173
+ * the value `undefined` itself is a valid `Key` and therefore a valid
174
+ * `Pattern`. But in optional pattern position, a top level `undefined` will
175
+ * be interpreted as absence. If you want to express a `Pattern` that will
176
+ * match only `undefined`, use `M.undefined()` instead.
177
+ */
178
+ export type IssuerBurn = (
179
+ payment: ERef<Payment>,
180
+ optAmountShape?: Pattern,
181
+ ) => Promise<Amount>;
182
+ /**
183
+ * Work around JSDoc union handling
184
+ */
185
+ export type IssuerMethods<K extends AssetKind, M extends Key> = {
186
+ /**
187
+ * Get the Brand for this Issuer. The Brand
188
+ * indicates the type of digital asset and is shared by the mint, the issuer,
189
+ * and any purses and payments of this particular kind. The brand is not
190
+ * closely held, so this function should not be trusted to identify an issuer
191
+ * alone. Fake digital assets and amount can use another issuer's brand.
192
+ */
193
+ getBrand: () => Brand<K>;
194
+ /**
195
+ * Get the allegedName for this
196
+ * mint/issuer
197
+ */
198
+ getAllegedName: () => string;
199
+ /**
200
+ * Get the kind of MathHelpers used by this
201
+ * Issuer.
202
+ */
203
+ getAssetKind: () => K;
204
+ /** @deprecated look up in boardAux */
205
+ getDisplayInfo: () => DisplayInfo<K>;
206
+ /**
207
+ * Make an empty purse of this
208
+ * brand.
209
+ */
210
+ makeEmptyPurse: () => Purse<K, M>;
211
+ isLive: IssuerIsLive;
212
+ getAmountOf: IssuerGetAmountOf<K, M>;
213
+ burn: IssuerBurn;
214
+ };
215
+ /**
216
+ * The issuer cannot
217
+ * mint a new amount, but it can create empty purses and payments. The issuer
218
+ * can also transform payments (splitting payments, combining payments,
219
+ * burning payments, and claiming payments exclusively). The issuer should be
220
+ * gotten from a trusted source and then relied upon as the decider of whether
221
+ * an untrusted payment is valid.
222
+ */
223
+ export type Issuer<
224
+ K extends AssetKind = AssetKind,
225
+ M extends Key = Key,
226
+ > = RemotableObject & IssuerMethods<K, M>;
227
+ export type PaymentLedger<K extends AssetKind = AssetKind> = {
228
+ mint: Mint<K>;
229
+ /**
230
+ * Externally useful only if this issuer
231
+ * uses recovery sets. Can be used to get the recovery set associated with
232
+ * minted payments that are still live.
233
+ */
234
+ mintRecoveryPurse: Purse<K>;
235
+ issuer: Issuer<K>;
236
+ brand: Brand<K>;
237
+ };
238
+ export type IssuerKit<K extends AssetKind = AssetKind, M extends Key = Key> = {
239
+ mint: Mint<K, M>;
240
+ /**
241
+ * Externally useful only if this
242
+ * issuer uses recovery sets. Can be used to get the recovery set associated
243
+ * with minted payments that are still live.
244
+ */
245
+ mintRecoveryPurse: Purse<K, M>;
246
+ issuer: Issuer<K, M>;
247
+ brand: Brand<K>;
248
+ displayInfo: DisplayInfo;
249
+ };
250
+ export type AdditionalDisplayInfo = {
251
+ /**
252
+ * Tells the display software how many
253
+ * decimal places to move the decimal over to the left, or in other words,
254
+ * which position corresponds to whole numbers. We require fungible digital
255
+ * assets to be represented in integers, in the smallest unit (i.e. USD might
256
+ * be represented in mill, a thousandth of a dollar. In that case,
257
+ * `decimalPlaces` would be 3.) This property is optional, and for
258
+ * non-fungible digital assets, should not be specified. The decimalPlaces
259
+ * property should be used for _display purposes only_. Any other use is an
260
+ * anti-pattern.
261
+ */
262
+ decimalPlaces?: number | undefined;
263
+ assetKind?: AssetKind | undefined;
264
+ };
265
+ /**
266
+ * Holding a Mint carries the right to issue new digital
267
+ * assets. These assets all have the same kind, which is called a Brand.
268
+ */
269
+ export type Mint<K extends AssetKind = AssetKind, M extends Key = Key> = {
270
+ /**
271
+ * Gets the Issuer for this mint.
272
+ */
273
+ getIssuer: () => Issuer<K, M>;
274
+ /**
275
+ * Creates a new
276
+ * Payment containing newly minted amount.
277
+ */
278
+ mintPayment: (newAmount: Amount<K>) => Payment<K, M>;
279
+ };
280
+ /**
281
+ * Issuers first became durable with mandatory recovery sets. Later they were
282
+ * made optional, but there is no support for converting from one state to the
283
+ * other. Thus, absence of a `RecoverySetsOption` state is equivalent to
284
+ * `'hasRecoverySets'`. In the absence of a `recoverySetsOption` parameter,
285
+ * upgradeIssuerKit defaults to the predecessor's `RecoverySetsOption` state, or
286
+ * `'hasRecoverySets'` if none.
287
+ *
288
+ * At this time, issuers started in one of the states (`'noRecoverySets'`, or
289
+ * `'hasRecoverySets'`) cannot be converted to the other on upgrade. If this
290
+ * transition is needed, it can likely be supported in a future upgrade. File an
291
+ * issue on github and explain what you need and why.
292
+ */
293
+ export type RecoverySetsOption = 'hasRecoverySets' | 'noRecoverySets';
294
+ export type DepositFacetReceive = (
295
+ payment: Payment,
296
+ optAmountShape?: Pattern,
297
+ ) => Amount;
298
+ export type DepositFacet = {
299
+ /**
300
+ * Deposit all the contents of payment
301
+ * into the purse that made this facet, returning the amount. If the optional
302
+ * argument `optAmount` does not equal the amount of digital assets in the
303
+ * payment, throw an error.
304
+ *
305
+ * If payment is a promise, throw an error.
306
+ */
307
+ receive: DepositFacetReceive;
308
+ };
309
+ /**
310
+ * Purses hold amount of
311
+ * digital assets of the same brand, but unlike Payments, they are not meant
312
+ * to be sent to others. To transfer digital assets, a Payment should be
313
+ * withdrawn from a Purse. The amount of digital assets in a purse can change
314
+ * through the action of deposit() and withdraw().
315
+ */
316
+ export type Purse<
317
+ K extends AssetKind = AssetKind,
318
+ M extends Key = Key,
319
+ > = RemotableObject & PurseMethods<K, M>;
320
+ /**
321
+ * The primary use for Purses and Payments is for
322
+ * currency-like and goods-like digital assets, but they can also be used to
323
+ * represent other kinds of rights, such as the right to participate in a
324
+ * particular contract.
325
+ */
326
+ export type PurseMethods<
327
+ K extends AssetKind = AssetKind,
328
+ M extends Key = Key,
329
+ > = {
330
+ /**
331
+ * Get the alleged Brand for this
332
+ * Purse
333
+ */
334
+ getAllegedBrand: () => Brand<K>;
335
+ /**
336
+ * Get the amount contained in
337
+ * this purse.
338
+ */
339
+ getCurrentAmount: () => Amount<K, M>;
340
+ /**
341
+ * Get a
342
+ * lossy notifier for changes to this purse's balance.
343
+ */
344
+ getCurrentAmountNotifier: () => LatestTopic<Amount<K, M>>;
345
+ /**
346
+ * Deposit all the contents of payment into this purse, returning the amount. If
347
+ * the optional argument `optAmount` does not equal the amount of digital
348
+ * assets in the payment, throw an error.
349
+ *
350
+ * If payment is a promise, throw an error.
351
+ */
352
+ deposit: <P extends Payment<K, M>>(
353
+ payment: P,
354
+ optAmountShape?: Pattern,
355
+ ) => P extends Payment<K, M> ? Amount<K, M> : never;
356
+ /**
357
+ * Return an object whose
358
+ * `receive` method deposits to the current Purse.
359
+ */
360
+ getDepositFacet: () => DepositFacet;
361
+ /**
362
+ * Withdraw amount
363
+ * from this purse into a new Payment.
364
+ */
365
+ withdraw: (amount: Amount<K, M>) => Payment<K, M>;
366
+ /**
367
+ * The set of payments
368
+ * withdrawn from this purse that are still live. These are the payments that
369
+ * can still be recovered in emergencies by, for example, depositing into this
370
+ * purse. Such a deposit action is like canceling an outstanding check because
371
+ * you're tired of waiting for it. Once your cancellation is acknowledged, you
372
+ * can spend the assets at stake on other things. Afterwards, if the recipient
373
+ * of the original check finally gets around to depositing it, their deposit
374
+ * fails.
375
+ *
376
+ * Returns an empty set if this issuer does not support recovery sets.
377
+ */
378
+ getRecoverySet: () => CopySet<Payment<K, M>>;
379
+ /**
380
+ * For use in emergencies, such as
381
+ * coming back from a traumatic crash and upgrade. This deposits all the
382
+ * payments in this purse's recovery set into the purse itself, returning the
383
+ * total amount of assets recovered.
384
+ *
385
+ * Returns an empty amount if this issuer does not support recovery sets.
386
+ */
387
+ recoverAll: () => Amount<K, M>;
388
+ };
389
+ /**
390
+ * Payments hold amount
391
+ * of digital assets of the same brand in transit. Payments can be deposited
392
+ * in purses, split into multiple payments, combined, and claimed (getting an
393
+ * exclusive payment). Payments are linear, meaning that either a payment has
394
+ * the same amount of digital assets it started with, or it is used up
395
+ * entirely. It is impossible to partially use a payment.
396
+ *
397
+ * Payments are often received from other actors and therefore should not be
398
+ * trusted themselves. To get the amount of digital assets in a payment, use
399
+ * the trusted issuer: issuer.getAmountOf(payment),
400
+ *
401
+ * Payments can be converted to Purses by getting a trusted issuer and calling
402
+ * `issuer.makeEmptyPurse()` to create a purse, then
403
+ * `purse.deposit(payment)`.
404
+ */
405
+ export type Payment<
406
+ K extends AssetKind = AssetKind,
407
+ M extends Key = Key,
408
+ > = RemotableObject & PaymentMethods<K>;
409
+ export type PaymentMethods<K extends AssetKind = AssetKind> = {
410
+ /**
411
+ * Get the allegedBrand, indicating
412
+ * the type of digital asset this payment purports to be, and which issuer to
413
+ * use. Because payments are not trusted, any method calls on payments should
414
+ * be treated with suspicion and verified elsewhere.
415
+ */
416
+ getAllegedBrand: () => Brand<K>;
417
+ };
418
+ /**
419
+ * All of the difference in how digital asset
420
+ * amount are manipulated can be reduced to the behavior of the math on
421
+ * values. We extract this custom logic into mathHelpers. MathHelpers are
422
+ * about value arithmetic, whereas AmountMath is about amounts, which are the
423
+ * values labeled with a brand. AmountMath use mathHelpers to do their value
424
+ * arithmetic, and then brand the results, making a new amount.
425
+ *
426
+ * The MathHelpers are designed to be called only from AmountMath, and so all
427
+ * methods but coerce can assume their inputs are valid. They only need to do
428
+ * output validation, and only when there is a possibility of invalid output.
429
+ */
430
+ export type MathHelpers<V extends AmountValue> = {
431
+ /**
432
+ * Check the kind of this value and
433
+ * throw if it is not the expected kind.
434
+ */
435
+ doCoerce: (allegedValue: V) => V;
436
+ /**
437
+ * Get the representation for the identity
438
+ * element (often 0 or an empty array)
439
+ */
440
+ doMakeEmpty: () => V;
441
+ /**
442
+ * Is the value the identity
443
+ * element?
444
+ */
445
+ doIsEmpty: (value: V) => boolean;
446
+ /**
447
+ * Is the left greater than
448
+ * or equal to the right?
449
+ */
450
+ doIsGTE: (left: V, right: V) => boolean;
451
+ /**
452
+ * Does left equal right?
453
+ */
454
+ doIsEqual: (left: V, right: V) => boolean;
455
+ /**
456
+ * Return the left combined with the
457
+ * right.
458
+ */
459
+ doAdd: (left: V, right: V) => V;
460
+ /**
461
+ * Return what remains after
462
+ * removing the right from the left. If something in the right was not in the
463
+ * left, we throw an error.
464
+ */
465
+ doSubtract: (left: V, right: V) => V;
466
+ };
467
+ export type NatValue = bigint;
468
+ export type SetValue<K extends Key = Key> = K[];