@agoric/ertp 0.16.3-other-dev-1f26562.0 → 0.16.3-other-dev-3eb1a1d.0
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/exported.d.ts +37 -0
- package/exported.js +2 -1
- package/package.json +35 -35
- package/src/amountMath.d.ts +42 -38
- package/src/amountMath.d.ts.map +1 -1
- package/src/amountMath.js +131 -127
- package/src/amountStore.d.ts +9 -0
- package/src/amountStore.d.ts.map +1 -0
- package/src/amountStore.js +34 -0
- package/src/displayInfo.d.ts +3 -0
- package/src/displayInfo.d.ts.map +1 -1
- package/src/displayInfo.js +3 -1
- package/src/index.d.ts +1 -0
- package/src/index.js +4 -0
- package/src/issuerKit.d.ts +30 -5
- package/src/issuerKit.d.ts.map +1 -1
- package/src/issuerKit.js +218 -76
- package/src/legacy-payment-helpers.d.ts +6 -1
- package/src/legacy-payment-helpers.d.ts.map +1 -1
- package/src/legacy-payment-helpers.js +34 -34
- package/src/mathHelpers/copyBagMathHelpers.d.ts +3 -4
- package/src/mathHelpers/copyBagMathHelpers.d.ts.map +1 -1
- package/src/mathHelpers/copyBagMathHelpers.js +4 -5
- package/src/mathHelpers/copySetMathHelpers.d.ts +3 -3
- package/src/mathHelpers/copySetMathHelpers.d.ts.map +1 -1
- package/src/mathHelpers/copySetMathHelpers.js +6 -4
- package/src/mathHelpers/natMathHelpers.d.ts +8 -7
- package/src/mathHelpers/natMathHelpers.d.ts.map +1 -1
- package/src/mathHelpers/natMathHelpers.js +8 -9
- package/src/mathHelpers/setMathHelpers.d.ts +2 -0
- package/src/mathHelpers/setMathHelpers.d.ts.map +1 -1
- package/src/mathHelpers/setMathHelpers.js +2 -1
- package/src/payment.d.ts +4 -2
- package/src/payment.d.ts.map +1 -1
- package/src/payment.js +6 -7
- package/src/paymentLedger.d.ts +6 -2
- package/src/paymentLedger.d.ts.map +1 -1
- package/src/paymentLedger.js +76 -95
- package/src/purse.d.ts +19 -9
- package/src/purse.d.ts.map +1 -1
- package/src/purse.js +86 -26
- package/src/transientNotifier.d.ts +1 -1
- package/src/transientNotifier.d.ts.map +1 -1
- package/src/transientNotifier.js +5 -0
- package/src/typeGuards.d.ts +60 -13
- package/src/typeGuards.d.ts.map +1 -1
- package/src/typeGuards.js +69 -57
- package/src/types-index.d.ts +2 -0
- package/src/types-index.js +2 -0
- package/src/types.d.ts +253 -219
- package/src/types.d.ts.map +1 -1
- package/src/types.ts +468 -0
- package/CHANGELOG.md +0 -743
- package/src/types-ambient.d.ts +0 -376
- package/src/types-ambient.d.ts.map +0 -1
- package/src/types-ambient.js +0 -440
- package/src/types.js +0 -441
package/src/amountMath.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { q, Fail } from '@endo/errors';
|
|
1
2
|
import { passStyleOf, assertRemotable, assertRecord } from '@endo/marshal';
|
|
2
3
|
|
|
3
4
|
import { M, matches } from '@agoric/store';
|
|
@@ -6,67 +7,65 @@ import { setMathHelpers } from './mathHelpers/setMathHelpers.js';
|
|
|
6
7
|
import { copySetMathHelpers } from './mathHelpers/copySetMathHelpers.js';
|
|
7
8
|
import { copyBagMathHelpers } from './mathHelpers/copyBagMathHelpers.js';
|
|
8
9
|
|
|
9
|
-
|
|
10
|
+
/**
|
|
11
|
+
* @import {CopyBag, CopySet} from '@endo/patterns';
|
|
12
|
+
* @import {Amount, AssetValueForKind, Brand, CopyBagAmount, CopySetAmount, MathHelpers, NatAmount, NatValue, SetAmount, SetValue} from './types.js';
|
|
13
|
+
*/
|
|
10
14
|
|
|
15
|
+
// NB: AssetKind is both a constant for enumerated values and a type for those values.
|
|
11
16
|
/**
|
|
12
17
|
* Constants for the kinds of assets we support.
|
|
13
18
|
*
|
|
14
|
-
* @
|
|
19
|
+
* @enum {(typeof AssetKind)[keyof typeof AssetKind]}
|
|
15
20
|
*/
|
|
16
|
-
const AssetKind =
|
|
21
|
+
export const AssetKind = /** @type {const} */ ({
|
|
17
22
|
NAT: 'nat',
|
|
18
23
|
SET: 'set',
|
|
19
24
|
COPY_SET: 'copySet',
|
|
20
25
|
COPY_BAG: 'copyBag',
|
|
21
26
|
});
|
|
27
|
+
harden(AssetKind);
|
|
22
28
|
const assetKindNames = harden(Object.values(AssetKind).sort());
|
|
23
29
|
|
|
24
|
-
/**
|
|
25
|
-
|
|
26
|
-
*/
|
|
27
|
-
const assertAssetKind = allegedAK => {
|
|
30
|
+
/** @param {AssetKind} allegedAK */
|
|
31
|
+
export const assertAssetKind = allegedAK => {
|
|
28
32
|
assetKindNames.includes(allegedAK) ||
|
|
29
33
|
Fail`The assetKind ${allegedAK} must be one of ${q(assetKindNames)}`;
|
|
30
34
|
};
|
|
31
35
|
harden(assertAssetKind);
|
|
32
36
|
|
|
33
37
|
/**
|
|
34
|
-
* Amounts describe digital assets. From an amount, you can learn the
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
* the
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
* anyone.
|
|
38
|
+
* Amounts describe digital assets. From an amount, you can learn the brand of
|
|
39
|
+
* digital asset as well as "how much" or "how many". Amounts have two parts: a
|
|
40
|
+
* brand (loosely speaking, the type of digital asset) and the value (the answer
|
|
41
|
+
* to "how much"). For example, in the phrase "5 bucks", "bucks" takes the role
|
|
42
|
+
* of the brand and the value is 5. Amounts can describe fungible and
|
|
43
|
+
* non-fungible digital assets. Amounts are pass-by-copy and can be made by and
|
|
44
|
+
* sent to anyone.
|
|
42
45
|
*
|
|
43
|
-
* The issuer is the authoritative source of the amount in payments
|
|
44
|
-
*
|
|
45
|
-
* digital assets
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
* which we call `AmountMath`. These math functions include concepts
|
|
46
|
+
* The issuer is the authoritative source of the amount in payments and purses.
|
|
47
|
+
* The issuer must be able to do things such as add digital assets to a purse
|
|
48
|
+
* and withdraw digital assets from a purse. To do so, it must know how to add
|
|
49
|
+
* and subtract digital assets. Rather than hard-coding a particular solution,
|
|
50
|
+
* we chose to parameterize the issuer with a collection of polymorphic
|
|
51
|
+
* functions, which we call `AmountMath`. These math functions include concepts
|
|
50
52
|
* like addition, subtraction, and greater than or equal to.
|
|
51
53
|
*
|
|
52
|
-
* We also want to make sure there is no confusion as to what kind of
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
* operation does not succeed.
|
|
54
|
+
* We also want to make sure there is no confusion as to what kind of asset we
|
|
55
|
+
* are using. Thus, AmountMath includes checks of the `brand`, the unique
|
|
56
|
+
* identifier for the type of digital asset. If the wrong brand is used in
|
|
57
|
+
* AmountMath, an error is thrown and the operation does not succeed.
|
|
57
58
|
*
|
|
58
|
-
* AmountMath uses mathHelpers to do most of the work, but then adds
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
* returning an amount (amount -> amount).
|
|
59
|
+
* AmountMath uses mathHelpers to do most of the work, but then adds the brand
|
|
60
|
+
* to the result. The function `value` gets the value from the amount by
|
|
61
|
+
* removing the brand (amount -> value), and the function `make` adds the brand
|
|
62
|
+
* to produce an amount (value -> amount). The function `coerce` takes an amount
|
|
63
|
+
* and checks it, returning an amount (amount -> amount).
|
|
64
64
|
*
|
|
65
|
-
* Each issuer of digital assets has an associated brand in a
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
* each other.
|
|
65
|
+
* Each issuer of digital assets has an associated brand in a one-to-one
|
|
66
|
+
* mapping. In untrusted contexts, such as in analyzing payments and amounts, we
|
|
67
|
+
* can get the brand and find the issuer which matches the brand. The issuer and
|
|
68
|
+
* the brand mutually validate each other.
|
|
70
69
|
*/
|
|
71
70
|
|
|
72
71
|
const helpers = {
|
|
@@ -76,26 +75,19 @@ const helpers = {
|
|
|
76
75
|
copyBag: copyBagMathHelpers,
|
|
77
76
|
};
|
|
78
77
|
|
|
79
|
-
/**
|
|
80
|
-
* @template {AmountValue} V
|
|
81
|
-
* @type {(value: V) => AssetKindForValue<V>}
|
|
82
|
-
*/
|
|
78
|
+
/** @type {(value: unknown) => 'nat' | 'set' | 'copySet' | 'copyBag'} } */
|
|
83
79
|
const assertValueGetAssetKind = value => {
|
|
84
80
|
const passStyle = passStyleOf(value);
|
|
85
81
|
if (passStyle === 'bigint') {
|
|
86
|
-
// @ts-expect-error cast
|
|
87
82
|
return 'nat';
|
|
88
83
|
}
|
|
89
84
|
if (passStyle === 'copyArray') {
|
|
90
|
-
// @ts-expect-error cast
|
|
91
85
|
return 'set';
|
|
92
86
|
}
|
|
93
87
|
if (matches(value, M.set())) {
|
|
94
|
-
// @ts-expect-error cast
|
|
95
88
|
return 'copySet';
|
|
96
89
|
}
|
|
97
90
|
if (matches(value, M.bag())) {
|
|
98
|
-
// @ts-expect-error cast
|
|
99
91
|
return 'copyBag';
|
|
100
92
|
}
|
|
101
93
|
// TODO This isn't quite the right error message, in case valuePassStyle
|
|
@@ -113,7 +105,7 @@ const assertValueGetAssetKind = value => {
|
|
|
113
105
|
*
|
|
114
106
|
* Made available only for testing, but it is harmless for other uses.
|
|
115
107
|
*
|
|
116
|
-
* @template
|
|
108
|
+
* @template V
|
|
117
109
|
* @param {V} value
|
|
118
110
|
* @returns {MathHelpers<V>}
|
|
119
111
|
*/
|
|
@@ -137,7 +129,7 @@ const optionalBrandCheck = (allegedBrand, brand) => {
|
|
|
137
129
|
* @param {Amount<K>} leftAmount
|
|
138
130
|
* @param {Amount<K>} rightAmount
|
|
139
131
|
* @param {Brand<K> | undefined} brand
|
|
140
|
-
* @returns {MathHelpers
|
|
132
|
+
* @returns {MathHelpers<any>}
|
|
141
133
|
*/
|
|
142
134
|
const checkLRAndGetHelpers = (leftAmount, rightAmount, brand = undefined) => {
|
|
143
135
|
assertRecord(leftAmount, 'leftAmount');
|
|
@@ -172,11 +164,11 @@ const coerceLR = (h, leftAmount, rightAmount) => {
|
|
|
172
164
|
};
|
|
173
165
|
|
|
174
166
|
/**
|
|
175
|
-
* Returns true if the leftAmount is greater than or equal to the
|
|
176
|
-
*
|
|
177
|
-
*
|
|
178
|
-
* whether rectangle A
|
|
179
|
-
*
|
|
167
|
+
* Returns true if the leftAmount is greater than or equal to the rightAmount.
|
|
168
|
+
* The notion of "greater than or equal to" depends on the kind of amount, as
|
|
169
|
+
* defined by the MathHelpers. For example, whether rectangle A is greater than
|
|
170
|
+
* rectangle B depends on whether rectangle A includes rectangle B as defined by
|
|
171
|
+
* the logic in MathHelpers.
|
|
180
172
|
*
|
|
181
173
|
* @template {AssetKind} K
|
|
182
174
|
* @param {Amount<K>} leftAmount
|
|
@@ -194,34 +186,48 @@ const isGTE = (leftAmount, rightAmount, brand = undefined) => {
|
|
|
194
186
|
*
|
|
195
187
|
* Amounts are the canonical description of tradable goods. They are manipulated
|
|
196
188
|
* by issuers and mints, and represent the goods and currency carried by purses
|
|
197
|
-
* and
|
|
198
|
-
*
|
|
199
|
-
* abstract right to participate in a particular exchange.
|
|
189
|
+
* and payments. They can be used to represent things like currency, stock, and
|
|
190
|
+
* the abstract right to participate in a particular exchange.
|
|
200
191
|
*/
|
|
201
|
-
const AmountMath = {
|
|
192
|
+
export const AmountMath = {
|
|
193
|
+
// TODO use overloading to handle when Brand has an AssetKind and when it doesn't.
|
|
194
|
+
// a AmountForValue utility could help DRY those cases.
|
|
202
195
|
/**
|
|
203
196
|
* Make an amount from a value by adding the brand.
|
|
204
197
|
*
|
|
205
|
-
*
|
|
206
|
-
*
|
|
207
|
-
* @
|
|
208
|
-
* @
|
|
198
|
+
* Does not verify that the Brand's AssetKind matches the value's.
|
|
199
|
+
*
|
|
200
|
+
* @template {Brand} B
|
|
201
|
+
* @template {NatValue | CopySet | CopyBag | SetValue} V
|
|
202
|
+
* @param {B} brand
|
|
203
|
+
* @param {V} allegedValue
|
|
204
|
+
* @returns {B extends Brand<'nat'>
|
|
205
|
+
* ? NatAmount
|
|
206
|
+
* : V extends NatValue
|
|
207
|
+
* ? NatAmount
|
|
208
|
+
* : V extends CopySet
|
|
209
|
+
* ? CopySetAmount<V['payload'][0]>
|
|
210
|
+
* : V extends CopyBag
|
|
211
|
+
* ? CopyBagAmount<V['payload'][0][0]>
|
|
212
|
+
* : V extends SetValue
|
|
213
|
+
* ? SetAmount<V[0]>
|
|
214
|
+
* : never}
|
|
209
215
|
*/
|
|
210
|
-
// allegedValue has a conditional expression for type widening, to prevent V being bound to a a literal like 1n
|
|
211
216
|
make: (brand, allegedValue) => {
|
|
212
217
|
assertRemotable(brand, 'brand');
|
|
213
218
|
const h = assertValueGetHelpers(allegedValue);
|
|
214
219
|
const value = h.doCoerce(allegedValue);
|
|
220
|
+
// @ts-expect-error cast
|
|
215
221
|
return harden({ brand, value });
|
|
216
222
|
},
|
|
217
223
|
/**
|
|
218
|
-
* Make sure this amount is valid enough, and return a corresponding
|
|
219
|
-
*
|
|
224
|
+
* Make sure this amount is valid enough, and return a corresponding valid
|
|
225
|
+
* amount if so.
|
|
220
226
|
*
|
|
221
|
-
* @template {
|
|
222
|
-
* @param {Brand
|
|
223
|
-
* @param {
|
|
224
|
-
* @returns {
|
|
227
|
+
* @template {Amount} A
|
|
228
|
+
* @param {Brand} brand
|
|
229
|
+
* @param {A} allegedAmount
|
|
230
|
+
* @returns {A}
|
|
225
231
|
*/
|
|
226
232
|
coerce: (brand, allegedAmount) => {
|
|
227
233
|
assertRemotable(brand, 'brand');
|
|
@@ -230,46 +236,47 @@ const AmountMath = {
|
|
|
230
236
|
brand === allegedBrand ||
|
|
231
237
|
Fail`The brand in the allegedAmount ${allegedAmount} in 'coerce' didn't match the specified brand ${brand}.`;
|
|
232
238
|
// Will throw on inappropriate value
|
|
239
|
+
// @ts-expect-error cast
|
|
233
240
|
return AmountMath.make(brand, allegedValue);
|
|
234
241
|
},
|
|
235
242
|
/**
|
|
236
243
|
* Extract and return the value.
|
|
237
244
|
*
|
|
238
|
-
* @template {
|
|
239
|
-
* @param {Brand
|
|
240
|
-
* @param {
|
|
241
|
-
* @returns {
|
|
245
|
+
* @template {Amount} A
|
|
246
|
+
* @param {Brand} brand
|
|
247
|
+
* @param {A} amount
|
|
248
|
+
* @returns {A['value']}
|
|
242
249
|
*/
|
|
243
250
|
getValue: (brand, amount) => AmountMath.coerce(brand, amount).value,
|
|
244
251
|
/**
|
|
245
|
-
* Return the amount representing an empty amount. This is the
|
|
246
|
-
*
|
|
252
|
+
* Return the amount representing an empty amount. This is the identity
|
|
253
|
+
* element for MathHelpers.add and MatHelpers.subtract.
|
|
247
254
|
*
|
|
248
255
|
* @type {{
|
|
249
256
|
* (brand: Brand): Amount<'nat'>;
|
|
250
|
-
* <K extends AssetKind>(brand: Brand
|
|
257
|
+
* <K extends AssetKind>(brand: Brand<K>, assetKind: K): Amount<K>;
|
|
251
258
|
* }}
|
|
252
259
|
*/
|
|
253
260
|
makeEmpty: (brand, assetKind = /** @type {const} */ ('nat')) => {
|
|
254
261
|
assertRemotable(brand, 'brand');
|
|
255
262
|
assertAssetKind(assetKind);
|
|
256
263
|
const value = helpers[assetKind].doMakeEmpty();
|
|
264
|
+
// @ts-expect-error XXX narrowing from function overload
|
|
257
265
|
return harden({ brand, value });
|
|
258
266
|
},
|
|
259
267
|
/**
|
|
260
|
-
* Return the amount representing an empty amount, using another
|
|
261
|
-
*
|
|
268
|
+
* Return the amount representing an empty amount, using another amount as the
|
|
269
|
+
* template for the brand and assetKind.
|
|
262
270
|
*
|
|
263
|
-
* @template {
|
|
264
|
-
* @param {
|
|
265
|
-
* @returns {
|
|
271
|
+
* @template {Amount} A
|
|
272
|
+
* @param {A} amount
|
|
273
|
+
* @returns {A}
|
|
266
274
|
*/
|
|
267
275
|
makeEmptyFromAmount: amount => {
|
|
268
276
|
assertRecord(amount, 'amount');
|
|
269
277
|
const { brand, value } = amount;
|
|
270
|
-
// @ts-expect-error cast
|
|
271
278
|
const assetKind = assertValueGetAssetKind(value);
|
|
272
|
-
// @ts-expect-error
|
|
279
|
+
// @ts-expect-error different subtype
|
|
273
280
|
return AmountMath.makeEmpty(brand, assetKind);
|
|
274
281
|
},
|
|
275
282
|
/**
|
|
@@ -289,13 +296,13 @@ const AmountMath = {
|
|
|
289
296
|
},
|
|
290
297
|
isGTE,
|
|
291
298
|
/**
|
|
292
|
-
* Returns true if the leftAmount equals the rightAmount. We assume
|
|
293
|
-
*
|
|
299
|
+
* Returns true if the leftAmount equals the rightAmount. We assume that if
|
|
300
|
+
* isGTE is true in both directions, isEqual is also true
|
|
294
301
|
*
|
|
295
|
-
* @template {
|
|
296
|
-
* @param {
|
|
297
|
-
* @param {
|
|
298
|
-
* @param {Brand
|
|
302
|
+
* @template {Amount} A
|
|
303
|
+
* @param {A} leftAmount
|
|
304
|
+
* @param {A} rightAmount
|
|
305
|
+
* @param {Brand} [brand]
|
|
299
306
|
* @returns {boolean}
|
|
300
307
|
*/
|
|
301
308
|
isEqual: (leftAmount, rightAmount, brand = undefined) => {
|
|
@@ -306,83 +313,80 @@ const AmountMath = {
|
|
|
306
313
|
* Returns a new amount that is the union of both leftAmount and rightAmount.
|
|
307
314
|
*
|
|
308
315
|
* For fungible amount this means adding the values. For other kinds of
|
|
309
|
-
* amount, it usually means including all of the elements from both
|
|
310
|
-
*
|
|
316
|
+
* amount, it usually means including all of the elements from both left and
|
|
317
|
+
* right.
|
|
311
318
|
*
|
|
312
|
-
* @template {
|
|
313
|
-
* @param {
|
|
314
|
-
* @param {
|
|
315
|
-
* @param {Brand
|
|
316
|
-
* @returns {
|
|
319
|
+
* @template {Amount} A
|
|
320
|
+
* @param {A} leftAmount
|
|
321
|
+
* @param {A} rightAmount
|
|
322
|
+
* @param {Brand} [brand]
|
|
323
|
+
* @returns {A}
|
|
317
324
|
*/
|
|
318
325
|
add: (leftAmount, rightAmount, brand = undefined) => {
|
|
319
326
|
const h = checkLRAndGetHelpers(leftAmount, rightAmount, brand);
|
|
320
327
|
const value = h.doAdd(...coerceLR(h, leftAmount, rightAmount));
|
|
328
|
+
// @ts-expect-error different subtype
|
|
321
329
|
return harden({ brand: leftAmount.brand, value });
|
|
322
330
|
},
|
|
323
331
|
/**
|
|
324
|
-
* Returns a new amount that is the leftAmount minus the rightAmount
|
|
325
|
-
*
|
|
326
|
-
*
|
|
327
|
-
*
|
|
328
|
-
*
|
|
329
|
-
* to set subtraction.
|
|
332
|
+
* Returns a new amount that is the leftAmount minus the rightAmount (i.e.
|
|
333
|
+
* everything in the leftAmount that is not in the rightAmount). If leftAmount
|
|
334
|
+
* doesn't include rightAmount (subtraction results in a negative), throw an
|
|
335
|
+
* error. Because the left amount must include the right amount, this is NOT
|
|
336
|
+
* equivalent to set subtraction.
|
|
330
337
|
*
|
|
331
|
-
* @template {
|
|
332
|
-
* @
|
|
333
|
-
* @param {
|
|
334
|
-
* @param {
|
|
335
|
-
* @
|
|
338
|
+
* @template {Amount} L
|
|
339
|
+
* @template {Amount} R
|
|
340
|
+
* @param {L} leftAmount
|
|
341
|
+
* @param {R} rightAmount
|
|
342
|
+
* @param {Brand} [brand]
|
|
343
|
+
* @returns {L extends R ? L : never}
|
|
336
344
|
*/
|
|
337
345
|
subtract: (leftAmount, rightAmount, brand = undefined) => {
|
|
338
346
|
const h = checkLRAndGetHelpers(leftAmount, rightAmount, brand);
|
|
339
347
|
const value = h.doSubtract(...coerceLR(h, leftAmount, rightAmount));
|
|
348
|
+
// @ts-expect-error different subtype
|
|
340
349
|
return harden({ brand: leftAmount.brand, value });
|
|
341
350
|
},
|
|
342
351
|
/**
|
|
343
352
|
* Returns the min value between x and y using isGTE
|
|
344
353
|
*
|
|
345
|
-
* @template {
|
|
346
|
-
* @param {
|
|
347
|
-
* @param {
|
|
348
|
-
* @param {Brand
|
|
349
|
-
* @returns {
|
|
354
|
+
* @template {Amount} A
|
|
355
|
+
* @param {A} x
|
|
356
|
+
* @param {A} y
|
|
357
|
+
* @param {Brand} [brand]
|
|
358
|
+
* @returns {A}
|
|
350
359
|
*/
|
|
351
360
|
min: (x, y, brand = undefined) =>
|
|
352
361
|
// eslint-disable-next-line no-nested-ternary
|
|
353
362
|
isGTE(x, y, brand)
|
|
354
363
|
? y
|
|
355
364
|
: isGTE(y, x, brand)
|
|
356
|
-
|
|
357
|
-
|
|
365
|
+
? x
|
|
366
|
+
: Fail`${x} and ${y} are incomparable`,
|
|
358
367
|
/**
|
|
359
368
|
* Returns the max value between x and y using isGTE
|
|
360
369
|
*
|
|
361
|
-
* @template {
|
|
362
|
-
* @param {
|
|
363
|
-
* @param {
|
|
364
|
-
* @param {Brand
|
|
365
|
-
* @returns {
|
|
370
|
+
* @template {Amount} A
|
|
371
|
+
* @param {A} x
|
|
372
|
+
* @param {A} y
|
|
373
|
+
* @param {Brand} [brand]
|
|
374
|
+
* @returns {A}
|
|
366
375
|
*/
|
|
367
376
|
max: (x, y, brand = undefined) =>
|
|
368
377
|
// eslint-disable-next-line no-nested-ternary
|
|
369
378
|
isGTE(x, y, brand)
|
|
370
379
|
? x
|
|
371
380
|
: isGTE(y, x)
|
|
372
|
-
|
|
373
|
-
|
|
381
|
+
? y
|
|
382
|
+
: Fail`${x} and ${y} are incomparable`,
|
|
374
383
|
};
|
|
375
384
|
harden(AmountMath);
|
|
376
385
|
|
|
377
|
-
/**
|
|
378
|
-
|
|
379
|
-
*/
|
|
380
|
-
const getAssetKind = amount => {
|
|
386
|
+
/** @param {Amount} amount */
|
|
387
|
+
export const getAssetKind = amount => {
|
|
381
388
|
assertRecord(amount, 'amount');
|
|
382
389
|
const { value } = amount;
|
|
383
|
-
// @ts-expect-error cast (ignore b/c erroring in CI but not my IDE)
|
|
384
390
|
return assertValueGetAssetKind(value);
|
|
385
391
|
};
|
|
386
392
|
harden(getAssetKind);
|
|
387
|
-
|
|
388
|
-
export { AmountMath, AssetKind, getAssetKind, assertAssetKind };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export function makeAmountStore<K extends AssetKind = AssetKind>(state: object, key: string): AmountStore<K>;
|
|
2
|
+
export type AmountStore<K extends AssetKind = AssetKind> = {
|
|
3
|
+
getAmount: () => Amount<K>;
|
|
4
|
+
increment: (delta: Amount<K>) => void;
|
|
5
|
+
decrement: (delta: Amount<K>) => boolean;
|
|
6
|
+
};
|
|
7
|
+
import type { AssetKind } from './types.js';
|
|
8
|
+
import type { Amount } from './types.js';
|
|
9
|
+
//# sourceMappingURL=amountStore.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"amountStore.d.ts","sourceRoot":"","sources":["amountStore.js"],"names":[],"mappings":"AAkBO,gCALmB,CAAC,SAAd,SAAW,qBACb,MAAM,OACN,MAAM,GACJ,WAAW,CAAC,CAAC,CAAC,CAgB1B;wBA3ByB,CAAC,SAAd,SAAW;eAEV,MAAM,OAAO,CAAC,CAAC;eACf,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,KAAK,IAAI;eAC1B,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,KAAK,OAAO;;+BAPN,YAAY;4BAAZ,YAAY"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { AmountMath } from './amountMath.js';
|
|
2
|
+
|
|
3
|
+
/** @import {Amount, AssetKind} from './types.js' */
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @template {AssetKind} [K=AssetKind]
|
|
7
|
+
* @typedef {object} AmountStore
|
|
8
|
+
* @property {() => Amount<K>} getAmount
|
|
9
|
+
* @property {(delta: Amount<K>) => void} increment
|
|
10
|
+
* @property {(delta: Amount<K>) => boolean} decrement
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @template {AssetKind} [K=AssetKind]
|
|
15
|
+
* @param {object} state
|
|
16
|
+
* @param {string} key
|
|
17
|
+
* @returns {AmountStore<K>}
|
|
18
|
+
*/
|
|
19
|
+
export const makeAmountStore = (state, key) => {
|
|
20
|
+
return harden({
|
|
21
|
+
getAmount: () => state[key],
|
|
22
|
+
increment: delta => {
|
|
23
|
+
state[key] = AmountMath.add(state[key], delta);
|
|
24
|
+
},
|
|
25
|
+
decrement: delta => {
|
|
26
|
+
if (AmountMath.isGTE(state[key], delta)) {
|
|
27
|
+
state[key] = AmountMath.subtract(state[key], delta);
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
return false;
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
harden(makeAmountStore);
|
package/src/displayInfo.d.ts
CHANGED
|
@@ -1,2 +1,5 @@
|
|
|
1
1
|
export function coerceDisplayInfo(allegedDisplayInfo: AdditionalDisplayInfo, assetKind: AssetKind): DisplayInfo;
|
|
2
|
+
import type { AdditionalDisplayInfo } from './types.js';
|
|
3
|
+
import type { AssetKind } from './types.js';
|
|
4
|
+
import type { DisplayInfo } from './types.js';
|
|
2
5
|
//# sourceMappingURL=displayInfo.d.ts.map
|
package/src/displayInfo.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"displayInfo.d.ts","sourceRoot":"","sources":["displayInfo.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"displayInfo.d.ts","sourceRoot":"","sources":["displayInfo.js"],"names":[],"mappings":"AAcO,sDAJI,qBAAqB,aACrB,SAAS,GACP,WAAW,CAiBvB;2CAtBgE,YAAY;+BAAZ,YAAY;iCAAZ,YAAY"}
|
package/src/displayInfo.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
// @jessie-check
|
|
2
2
|
|
|
3
|
-
import { Fail } from '@
|
|
3
|
+
import { Fail } from '@endo/errors';
|
|
4
4
|
import { mustMatch } from '@agoric/store';
|
|
5
5
|
|
|
6
6
|
import { DisplayInfoShape } from './typeGuards.js';
|
|
7
7
|
|
|
8
|
+
/** @import {AdditionalDisplayInfo, AssetKind, DisplayInfo} from './types.js' */
|
|
9
|
+
|
|
8
10
|
/**
|
|
9
11
|
* @param {AdditionalDisplayInfo} allegedDisplayInfo
|
|
10
12
|
* @param {AssetKind} assetKind
|
package/src/index.d.ts
CHANGED
package/src/index.js
CHANGED
package/src/issuerKit.d.ts
CHANGED
|
@@ -1,19 +1,44 @@
|
|
|
1
|
-
export function
|
|
2
|
-
export function hasIssuer(baggage:
|
|
3
|
-
export function makeDurableIssuerKit<K extends AssetKind>(issuerBaggage:
|
|
1
|
+
export function upgradeIssuerKit<K extends AssetKind>(issuerBaggage: import("@agoric/vat-data").Baggage, optShutdownWithFailure?: ShutdownWithFailure | undefined, recoverySetsOption?: RecoverySetsOption | undefined): IssuerKit<K>;
|
|
2
|
+
export function hasIssuer(baggage: import("@agoric/vat-data").Baggage): boolean;
|
|
3
|
+
export function makeDurableIssuerKit<K extends AssetKind>(issuerBaggage: import("@agoric/vat-data").Baggage, name: string, assetKind?: K | undefined, displayInfo?: AdditionalDisplayInfo | undefined, optShutdownWithFailure?: ShutdownWithFailure | undefined, { elementShape, recoverySetsOption }?: Partial<{
|
|
4
4
|
elementShape: Pattern;
|
|
5
|
+
recoverySetsOption: RecoverySetsOption;
|
|
5
6
|
}> | undefined): IssuerKit<K>;
|
|
6
|
-
export function
|
|
7
|
+
export function prepareIssuerKit<K extends AssetKind>(issuerBaggage: import("@agoric/vat-data").Baggage, name: string, assetKind?: K | undefined, displayInfo?: AdditionalDisplayInfo | undefined, optShutdownWithFailure?: ShutdownWithFailure | undefined, options?: Partial<{
|
|
7
8
|
elementShape: Pattern;
|
|
9
|
+
recoverySetsOption: RecoverySetsOption;
|
|
8
10
|
}> | undefined): IssuerKit<K>;
|
|
9
|
-
export
|
|
11
|
+
export function makeIssuerKit<K extends AssetKind = "nat">(name: string, assetKind?: K | undefined, displayInfo?: AdditionalDisplayInfo | undefined, optShutdownWithFailure?: ShutdownWithFailure | undefined, { elementShape, recoverySetsOption }?: Partial<{
|
|
12
|
+
elementShape: Pattern;
|
|
13
|
+
recoverySetsOption: RecoverySetsOption;
|
|
14
|
+
}> | undefined): IssuerKit<K, any>;
|
|
10
15
|
export type IssuerRecord<K extends AssetKind> = {
|
|
11
16
|
name: string;
|
|
12
17
|
assetKind: K;
|
|
13
18
|
displayInfo: AdditionalDisplayInfo;
|
|
14
19
|
elementShape: Pattern;
|
|
15
20
|
};
|
|
21
|
+
/**
|
|
22
|
+
* `elementShape`, may only be present for collection-style amounts. If present,
|
|
23
|
+
* it is a `Pattern` that every element of this issuerKits's amounts must
|
|
24
|
+
* satisfy. For example, the Zoe Invitation issuerKit uses an elementShape
|
|
25
|
+
* describing the invitation details for an individual invitation. An invitation
|
|
26
|
+
* purse or payment has an amount that can only be a set of these. (Though
|
|
27
|
+
* typically, the amount of an invitation payment is a singleton set. Such a
|
|
28
|
+
* payment is often referred to in the singular as "an invitation".)
|
|
29
|
+
*
|
|
30
|
+
* `recoverySetsOption` added in upgrade. Note that `IssuerOptionsRecord` is
|
|
31
|
+
* never stored, so we never need to worry about inheriting one from a
|
|
32
|
+
* predecessor predating the introduction of recovery sets. See
|
|
33
|
+
* `RecoverySetsOption` for defaulting behavior.
|
|
34
|
+
*/
|
|
16
35
|
export type IssuerOptionsRecord = Partial<{
|
|
17
36
|
elementShape: Pattern;
|
|
37
|
+
recoverySetsOption: RecoverySetsOption;
|
|
18
38
|
}>;
|
|
39
|
+
import { AssetKind } from './amountMath.js';
|
|
40
|
+
import type { ShutdownWithFailure } from '@agoric/swingset-vat';
|
|
41
|
+
import type { RecoverySetsOption } from './types.js';
|
|
42
|
+
import type { IssuerKit } from './types.js';
|
|
43
|
+
import type { AdditionalDisplayInfo } from './types.js';
|
|
19
44
|
//# sourceMappingURL=issuerKit.d.ts.map
|
package/src/issuerKit.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"issuerKit.d.ts","sourceRoot":"","sources":["issuerKit.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"issuerKit.d.ts","sourceRoot":"","sources":["issuerKit.js"],"names":[],"mappings":"AA4GO,iCAbkB,CAAC,SAAZ,SAAU,iBACb,OAAO,kBAAkB,EAAE,OAAO,kHAUhC,UAAU,CAAC,CAAC,CA4BxB;AAQM,mCAFI,OAAO,kBAAkB,EAAE,OAAO,WAEgB;AAoDtD,qCA1BkB,CAAC,SAAZ,SAAU,iBAYb,OAAO,kBAAkB,EAAE,OAAO,QAClC,MAAM;kBAtBE,OAAO;wBACD,kBAAkB;iBAgC9B,UAAU,CAAC,CAAC,CA2BxB;AAiCM,iCA1BkB,CAAC,SAAZ,SAAU,iBAYb,OAAO,kBAAkB,EAAE,OAAO,QAClC,MAAM;kBAhFE,OAAO;wBACD,kBAAkB;iBA0F9B,UAAU,CAAC,CAAC,CAuCxB;AAqCM,8BAzBmB,CAAC,SAAb,SAAU,gBAYb,MAAM;kBA1JE,OAAO;wBACD,kBAAkB;iBAoK9B,UAAU,CAAC,EAAE,GAAG,CAAC,CAiB3B;yBAtUsB,CAAC,SAAZ,SAAU;UAEV,MAAM;eACN,CAAC;iBACD,qBAAqB;kBACrB,OAAO;;;;;;;;;;;;;;;;kCA0IR,OAAO,CAAC;IAChB,YAAY,EAAE,OAAO,CAAC;IACtB,kBAAkB,EAAE,kBAAkB,CAAC;CACxC,CAAC;0BA1JsC,iBAAiB;yCAKrB,sBAAsB;wCAD0B,YAAY;+BAAZ,YAAY;2CAAZ,YAAY"}
|