@agoric/zoe 0.26.3-dev-34632ea.0 → 0.26.3-dev-f0367d7.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agoric/zoe",
3
- "version": "0.26.3-dev-34632ea.0+34632ea",
3
+ "version": "0.26.3-dev-f0367d7.0+f0367d7",
4
4
  "description": "Zoe: the Smart Contract Framework for Offer Enforcement",
5
5
  "type": "module",
6
6
  "main": "./src/zoeService/zoe.js",
@@ -43,17 +43,17 @@
43
43
  },
44
44
  "homepage": "https://github.com/Agoric/agoric-sdk#readme",
45
45
  "dependencies": {
46
- "@agoric/base-zone": "0.1.1-dev-34632ea.0+34632ea",
47
- "@agoric/ertp": "0.16.3-dev-34632ea.0+34632ea",
48
- "@agoric/internal": "0.3.3-dev-34632ea.0+34632ea",
49
- "@agoric/notifier": "0.6.3-dev-34632ea.0+34632ea",
50
- "@agoric/store": "0.9.3-dev-34632ea.0+34632ea",
51
- "@agoric/swingset-liveslots": "0.10.3-dev-34632ea.0+34632ea",
52
- "@agoric/swingset-vat": "0.32.3-dev-34632ea.0+34632ea",
53
- "@agoric/time": "0.3.3-dev-34632ea.0+34632ea",
54
- "@agoric/vat-data": "0.5.3-dev-34632ea.0+34632ea",
55
- "@agoric/vow": "0.1.1-dev-34632ea.0+34632ea",
56
- "@agoric/zone": "0.2.3-dev-34632ea.0+34632ea",
46
+ "@agoric/base-zone": "0.1.1-dev-f0367d7.0+f0367d7",
47
+ "@agoric/ertp": "0.16.3-dev-f0367d7.0+f0367d7",
48
+ "@agoric/internal": "0.3.3-dev-f0367d7.0+f0367d7",
49
+ "@agoric/notifier": "0.6.3-dev-f0367d7.0+f0367d7",
50
+ "@agoric/store": "0.9.3-dev-f0367d7.0+f0367d7",
51
+ "@agoric/swingset-liveslots": "0.10.3-dev-f0367d7.0+f0367d7",
52
+ "@agoric/swingset-vat": "0.32.3-dev-f0367d7.0+f0367d7",
53
+ "@agoric/time": "0.3.3-dev-f0367d7.0+f0367d7",
54
+ "@agoric/vat-data": "0.5.3-dev-f0367d7.0+f0367d7",
55
+ "@agoric/vow": "0.1.1-dev-f0367d7.0+f0367d7",
56
+ "@agoric/zone": "0.2.3-dev-f0367d7.0+f0367d7",
57
57
  "@endo/bundle-source": "^3.5.1",
58
58
  "@endo/captp": "^4.4.4",
59
59
  "@endo/common": "^1.2.9",
@@ -70,7 +70,7 @@
70
70
  "yargs-parser": "^21.1.1"
71
71
  },
72
72
  "devDependencies": {
73
- "@agoric/kmarshal": "0.1.1-dev-34632ea.0+34632ea",
73
+ "@agoric/kmarshal": "0.1.1-dev-f0367d7.0+f0367d7",
74
74
  "@endo/init": "^1.1.8",
75
75
  "ava": "^5.3.0",
76
76
  "c8": "^10.1.2",
@@ -100,5 +100,5 @@
100
100
  "typeCoverage": {
101
101
  "atLeast": 85.15
102
102
  },
103
- "gitHead": "34632ea968d02a9e35edd3f7ca3b6edd59e4ef52"
103
+ "gitHead": "f0367d7ec66e1b662c39811381d4ff5f525b1fd2"
104
104
  }
@@ -2,7 +2,7 @@
2
2
 
3
3
  import { Nat } from '@endo/nat';
4
4
  import { Fail } from '@endo/errors';
5
- import { natSafeMath } from './safeMath.js';
5
+ import { natSafeMath } from '@agoric/ertp/src/safeMath.js';
6
6
 
7
7
  const { subtract, add, multiply, floorDivide } = natSafeMath;
8
8
 
@@ -1,5 +1,6 @@
1
- export { natSafeMath } from "./safeMath.js";
1
+ export { natSafeMath } from "@agoric/ertp/src/safeMath.js";
2
2
  export { makeStateMachine } from "./stateMachine.js";
3
+ export * from "@agoric/ertp/src/ratio.js";
3
4
  export * from "./durability.js";
4
5
  export * from "./prepare-ownable.js";
5
6
  export * from "./priceAuthority.js";
@@ -10,5 +11,4 @@ export * from "./topics.js";
10
11
  export { getInputPrice, getOutputPrice, calcLiqValueToMint, calcValueToRemove, calcSecondaryRequired } from "./bondingCurves.js";
11
12
  export { atomicRearrange, atomicTransfer, fromOnly, toOnly } from "./atomicTransfer.js";
12
13
  export { defaultAcceptanceMsg, swap, fitProposalShape, assertProposalShape, assertIssuerKeywords, satisfies, assertNatAssetKind, swapExact, depositToSeat, withdrawFromSeat, saveAllIssuers, offerTo } from "./zoeHelpers.js";
13
- export { makeRatio, makeRatioFromAmounts, floorMultiplyBy, floorDivideBy, ceilMultiplyBy, ceilDivideBy, assertIsRatio, invertRatio, oneMinus, addRatios, multiplyRatios, ratiosSame, quantize, ratioGTE, subtractRatios, ratioToNumber } from "./ratio.js";
14
14
  //# sourceMappingURL=index.d.ts.map
@@ -8,7 +8,7 @@ export {
8
8
  calcSecondaryRequired,
9
9
  } from './bondingCurves.js';
10
10
 
11
- export { natSafeMath } from './safeMath.js';
11
+ export { natSafeMath } from '@agoric/ertp/src/safeMath.js';
12
12
 
13
13
  export { makeStateMachine } from './stateMachine.js';
14
14
 
@@ -34,24 +34,7 @@ export {
34
34
  offerTo,
35
35
  } from './zoeHelpers.js';
36
36
 
37
- export {
38
- makeRatio,
39
- makeRatioFromAmounts,
40
- floorMultiplyBy,
41
- floorDivideBy,
42
- ceilMultiplyBy,
43
- ceilDivideBy,
44
- assertIsRatio,
45
- invertRatio,
46
- oneMinus,
47
- addRatios,
48
- multiplyRatios,
49
- ratiosSame,
50
- quantize,
51
- ratioGTE,
52
- subtractRatios,
53
- ratioToNumber,
54
- } from './ratio.js';
37
+ export * from '@agoric/ertp/src/ratio.js';
55
38
 
56
39
  export * from './durability.js';
57
40
  export * from './prepare-ownable.js';
@@ -7,7 +7,7 @@ import { Far } from '@endo/marshal';
7
7
  import { makeNotifier } from '@agoric/notifier';
8
8
  import { AmountMath } from '@agoric/ertp';
9
9
 
10
- import { multiplyBy } from './ratio.js';
10
+ import { multiplyBy } from '@agoric/ertp/src/ratio.js';
11
11
  import { mintQuote } from './priceAuthorityTransform.js';
12
12
 
13
13
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"priceAggregator.d.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AC,WAAW,CAAC,KAAK,CAAC,CAAC;;QA/ExC;;;;;;;;;WASG;2CADQ,QAAQ,GAAG,MAAM;mBAaG,WAAW,CAAC,KAAK,CAAC;8BAAoB;gBAAC,SAAS,EAAE,CAAC,KAAK,EAAE,cAAc,KAAK,OAAO,CAAC,WAAW,IAAI,CAAC,CAAC,CAAA;aAAC;;;;;QA0CtI,mCAAmC;kCAAvB,SAAS;QAarB;;;;WAIG;sCAHQ,QAAQ,GAAG,MAAM,UACjB,WAAW,KACT,OAAO,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;;;;;QAqHxC,sBAAsB;;;;;QAAtB,sBAAsB;;;GAOzB;gCA3lB4B,wBAAwB;yBAEV,aAAa;sCADqB,4BAA4B;gCAC9D,aAAa"}
@@ -12,23 +12,21 @@ import { Fail, q } from '@endo/errors';
12
12
  import { E } from '@endo/eventual-send';
13
13
  import { Far } from '@endo/marshal';
14
14
 
15
- import {
16
- calculateMedian,
17
- makeOnewayPriceAuthorityKit,
18
- makePriceQuoteIssuer,
19
- } from '../contractSupport/index.js';
20
15
  import {
21
16
  addRatios,
22
17
  assertParsableNumber,
18
+ calculateMedian,
23
19
  ceilDivideBy,
24
20
  floorMultiplyBy,
21
+ makeOnewayPriceAuthorityKit,
22
+ makePriceQuoteIssuer,
25
23
  makeRatio,
26
24
  makeRatioFromAmounts,
27
25
  multiplyRatios,
28
26
  parseRatio,
29
27
  ratioGTE,
30
28
  ratiosSame,
31
- } from '../contractSupport/ratio.js';
29
+ } from '../contractSupport/index.js';
32
30
 
33
31
  /**
34
32
  * @import {LegacyMap} from '@agoric/store'
@@ -6,7 +6,7 @@ export function withAmountUtils(kit: Pick<IssuerKit<"nat", any>, "brand" | "issu
6
6
  * @param {NatValue} n
7
7
  * @param {NatValue} [d]
8
8
  */
9
- makeRatio: (n: NatValue, d?: NatValue) => Ratio;
9
+ makeRatio: (n: NatValue, d?: NatValue) => import("@agoric/ertp/src/ratio.js").Ratio;
10
10
  /** @param {number} n */
11
11
  units: (n: number) => import("@agoric/ertp").NatAmount;
12
12
  /** The intact Exo remotable */
@@ -1,5 +1,5 @@
1
1
  import { AmountMath } from '@agoric/ertp';
2
- import { makeRatio } from '../src/contractSupport/ratio.js';
2
+ import { makeRatio } from '../src/contractSupport/index.js';
3
3
 
4
4
  /**
5
5
  * @import {Amount, Brand, DepositFacet, Issuer, IssuerKit, NatValue} from '@agoric/ertp';
@@ -1,39 +0,0 @@
1
- export function assertIsRatio(ratio: any): void;
2
- export function makeRatio(numerator: bigint, numeratorBrand: Brand, denominator?: bigint, denominatorBrand?: Brand): Ratio;
3
- export function makeRatioFromAmounts(numeratorAmount: Amount, denominatorAmount: Amount): Ratio;
4
- /** @type {ScaleAmount} */
5
- export const floorMultiplyBy: ScaleAmount;
6
- /** @type {ScaleAmount} */
7
- export const ceilMultiplyBy: ScaleAmount;
8
- /** @type {ScaleAmount} */
9
- export const multiplyBy: ScaleAmount;
10
- /**
11
- * Divide the amount by the ratio, truncating the remainder.
12
- * @type {ScaleAmount}
13
- */
14
- export const floorDivideBy: ScaleAmount;
15
- /**
16
- * Divide the amount by the ratio, rounding up the remainder.
17
- * @type {ScaleAmount}
18
- */
19
- export const ceilDivideBy: ScaleAmount;
20
- /**
21
- * Divide the amount by the ratio, rounding to nearest with ties to even (aka Banker's Rounding) as in IEEE 754 default rounding.
22
- * @type {ScaleAmount}
23
- */
24
- export const divideBy: ScaleAmount;
25
- export function invertRatio(ratio: Ratio): Ratio;
26
- export function addRatios(left: Ratio, right: Ratio): Ratio;
27
- export function subtractRatios(left: Ratio, right: Ratio): Ratio;
28
- export function multiplyRatios(left: Ratio, right: Ratio): Ratio;
29
- export function oneMinus(ratio: Ratio): Ratio;
30
- export function ratioGTE(left: Ratio, right: Ratio): boolean;
31
- export function ratiosSame(left: Ratio, right: Ratio): boolean;
32
- export function quantize(ratio: Ratio, newDen: bigint): Ratio;
33
- export function parseRatio(numeric: ParsableNumber, numeratorBrand: Brand<"nat">, denominatorBrand?: Brand<"nat">): Ratio;
34
- export function assertParsableNumber(specimen: unknown): asserts specimen is ParsableNumber;
35
- export function ratioToNumber(ratio: Ratio): number;
36
- export type ParsableNumber = bigint | number | string;
37
- import type { Brand } from '@agoric/ertp';
38
- import type { Amount } from '@agoric/ertp';
39
- //# sourceMappingURL=ratio.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ratio.d.ts","sourceRoot":"","sources":["ratio.js"],"names":[],"mappings":"AA4CO,gDAcN;AASM,qCANI,MAAM,kBACN,KAAK,gBACL,MAAM,qBACN,KAAK,GACH,KAAK,CAejB;AAOM,sDAJI,MAAM,qBACN,MAAM,GACJ,KAAK,CAYjB;AA2BD,0BAA0B;AAC1B,8BADW,WAAW,CAGpB;AAEF,0BAA0B;AAC1B,6BADW,WAAW,CAGpB;AAEF,0BAA0B;AAC1B,yBADW,WAAW,CAGpB;AA2BF;;;GAGG;AACH,4BAFU,WAAW,CAInB;AAEF;;;GAGG;AACH,2BAFU,WAAW,CAInB;AAEF;;;GAGG;AACH,uBAFU,WAAW,CAInB;AAMK,mCAHI,KAAK,GACH,KAAK,CAWjB;AAOM,gCAJI,KAAK,SACL,KAAK,GACH,KAAK,CAwBjB;AAOM,qCAJI,KAAK,SACL,KAAK,GACH,KAAK,CAmBjB;AAOM,qCAJI,KAAK,SACL,KAAK,GACH,KAAK,CA8BjB;AAQM,gCAHI,KAAK,GACH,KAAK,CAcjB;AAOM,+BAJI,KAAK,SACL,KAAK,GACH,OAAO,CAkBnB;AASM,iCAJI,KAAK,SACL,KAAK,GACH,OAAO,CAOnB;AAUM,gCAJI,KAAK,UACL,MAAM,GACJ,KAAK,CAiBjB;AAaM,oCALI,cAAc,kBACd,MAAM,KAAK,CAAC,qBACZ,MAAM,KAAK,CAAC,GACV,KAAK,CAmBjB;AAMM,+CAHI,OAAO,GACL,QAAQ,QAAQ,IAAI,cAAc,CAK9C;AAQM,qCAHI,KAAK,GACH,MAAM,CAMlB;6BAhDa,MAAM,GAAG,MAAM,GAAG,MAAM;2BA7WkB,cAAc;4BAAd,cAAc"}
@@ -1,423 +0,0 @@
1
- /// <reference path="./types-ambient.js" />
2
- import { q, Fail } from '@endo/errors';
3
- import { AmountMath } from '@agoric/ertp';
4
- import { assertRecord } from '@endo/marshal';
5
- import { isNat } from '@endo/nat';
6
-
7
- import { natSafeMath } from './safeMath.js';
8
-
9
- /**
10
- * @import {Amount, Brand, Issuer, Payment, Purse} from '@agoric/ertp';
11
- */
12
-
13
- const { multiply, floorDivide, ceilDivide, bankersDivide, add, subtract } =
14
- natSafeMath;
15
-
16
- // make a Ratio, which represents a fraction. It is a pass-by-copy record.
17
- //
18
- // The natural syntax for the most common operations we want to support
19
- // are Amount * Ratio and Amount / Ratio. Since the operations want to adhere to
20
- // the ratio rather than the amount, we settled on a calling convention of
21
- // [ceil|floor]MultiplyBy(Amount, Ratio) and [ceil|floor]DivideBy(Amount, Ratio)
22
- //
23
- // The most common kind of Ratio can be applied to Amounts of a particular
24
- // brand, and produces results of the same brand. This represents a multiplier
25
- // that is only applicable to that brand. The less common kind of Ratio can be
26
- // applied to one particular brand of amounts, and produces results of another
27
- // particular brand. This represents some kind of exchange rate. The
28
- // brand-checking helps us ensure that normal Ratios aren't applied to amounts
29
- // of the wrong brand, and that exchange rates are only used in the appropriate
30
- // direction.
31
- //
32
- // Since the ratios are represented by a numerator and a denominator, every
33
- // multiplication or division operation that produces an amount ends with a
34
- // division of the underlying bigints, and integer division requires a mode
35
- // of [rounding to integer](https://en.wikipedia.org/wiki/Rounding#Rounding_to_integer).
36
- // Because `Ratio` only work with Natural numbers, just three modes suffice:
37
- // - floor rounds down
38
- // - ceil rounds up
39
- // - default (without prefix) minimizes bias by rounding half to even
40
-
41
- const PERCENT = 100n;
42
-
43
- const ratioPropertyNames = ['numerator', 'denominator'];
44
-
45
- export const assertIsRatio = ratio => {
46
- assertRecord(ratio, 'ratio');
47
- const keys = Object.keys(ratio);
48
- keys.length === 2 || Fail`Ratio ${ratio} must be a record with 2 fields.`;
49
- for (const name of keys) {
50
- ratioPropertyNames.includes(name) ||
51
- Fail`Parameter must be a Ratio record, but ${ratio} has ${q(name)}`;
52
- }
53
- const numeratorValue = ratio.numerator.value;
54
- const denominatorValue = ratio.denominator.value;
55
- isNat(numeratorValue) ||
56
- Fail`The numerator value must be a NatValue, not ${numeratorValue}`;
57
- isNat(denominatorValue) ||
58
- Fail`The denominator value must be a NatValue, not ${denominatorValue}`;
59
- };
60
-
61
- /**
62
- * @param {bigint} numerator
63
- * @param {Brand} numeratorBrand
64
- * @param {bigint} [denominator] The default denominator is 100
65
- * @param {Brand} [denominatorBrand] The default is to reuse the numeratorBrand
66
- * @returns {Ratio}
67
- */
68
- export const makeRatio = (
69
- numerator,
70
- numeratorBrand,
71
- denominator = PERCENT,
72
- denominatorBrand = numeratorBrand,
73
- ) => {
74
- denominator > 0n ||
75
- Fail`No infinite ratios! Denominator was 0 ${q(denominatorBrand)}`;
76
-
77
- return harden({
78
- numerator: AmountMath.make(numeratorBrand, numerator),
79
- denominator: AmountMath.make(denominatorBrand, denominator),
80
- });
81
- };
82
-
83
- /**
84
- * @param {Amount} numeratorAmount
85
- * @param {Amount} denominatorAmount
86
- * @returns {Ratio}
87
- */
88
- export const makeRatioFromAmounts = (numeratorAmount, denominatorAmount) => {
89
- AmountMath.coerce(numeratorAmount.brand, numeratorAmount);
90
- AmountMath.coerce(denominatorAmount.brand, denominatorAmount);
91
- return makeRatio(
92
- // @ts-expect-error value can be any AmountValue but makeRatio() supports only bigint
93
- numeratorAmount.value,
94
- numeratorAmount.brand,
95
- denominatorAmount.value,
96
- denominatorAmount.brand,
97
- );
98
- };
99
-
100
- /**
101
- * @param {Amount<'nat'>} amount
102
- * @param {Ratio} ratio
103
- * @param {*} divideOp
104
- * @returns {Amount<'nat'>}
105
- */
106
- const multiplyHelper = (amount, ratio, divideOp) => {
107
- AmountMath.coerce(amount.brand, amount);
108
- assertIsRatio(ratio);
109
- amount.brand === ratio.denominator.brand ||
110
- Fail`amount's brand ${q(amount.brand)} must match ratio's denominator ${q(
111
- ratio.denominator.brand,
112
- )}`;
113
-
114
- return /** @type {Amount<'nat'>} */ (
115
- AmountMath.make(
116
- ratio.numerator.brand,
117
- divideOp(
118
- multiply(amount.value, ratio.numerator.value),
119
- ratio.denominator.value,
120
- ),
121
- )
122
- );
123
- };
124
-
125
- /** @type {ScaleAmount} */
126
- export const floorMultiplyBy = (amount, ratio) => {
127
- return multiplyHelper(amount, ratio, floorDivide);
128
- };
129
-
130
- /** @type {ScaleAmount} */
131
- export const ceilMultiplyBy = (amount, ratio) => {
132
- return multiplyHelper(amount, ratio, ceilDivide);
133
- };
134
-
135
- /** @type {ScaleAmount} */
136
- export const multiplyBy = (amount, ratio) => {
137
- return multiplyHelper(amount, ratio, bankersDivide);
138
- };
139
-
140
- /**
141
- * @param {Amount<'nat'>} amount
142
- * @param {Ratio} ratio
143
- * @param {*} divideOp
144
- * @returns {Amount<'nat'>}
145
- */
146
- const divideHelper = (amount, ratio, divideOp) => {
147
- AmountMath.coerce(amount.brand, amount);
148
- assertIsRatio(ratio);
149
- amount.brand === ratio.numerator.brand ||
150
- Fail`amount's brand ${q(amount.brand)} must match ratio's numerator ${q(
151
- ratio.numerator.brand,
152
- )}`;
153
-
154
- return /** @type {Amount<'nat'>} */ (
155
- AmountMath.make(
156
- ratio.denominator.brand,
157
- divideOp(
158
- multiply(amount.value, ratio.denominator.value),
159
- ratio.numerator.value,
160
- ),
161
- )
162
- );
163
- };
164
-
165
- /**
166
- * Divide the amount by the ratio, truncating the remainder.
167
- * @type {ScaleAmount}
168
- */
169
- export const floorDivideBy = (amount, ratio) => {
170
- return divideHelper(amount, ratio, floorDivide);
171
- };
172
-
173
- /**
174
- * Divide the amount by the ratio, rounding up the remainder.
175
- * @type {ScaleAmount}
176
- */
177
- export const ceilDivideBy = (amount, ratio) => {
178
- return divideHelper(amount, ratio, ceilDivide);
179
- };
180
-
181
- /**
182
- * Divide the amount by the ratio, rounding to nearest with ties to even (aka Banker's Rounding) as in IEEE 754 default rounding.
183
- * @type {ScaleAmount}
184
- */
185
- export const divideBy = (amount, ratio) => {
186
- return divideHelper(amount, ratio, bankersDivide);
187
- };
188
-
189
- /**
190
- * @param {Ratio} ratio
191
- * @returns {Ratio}
192
- */
193
- export const invertRatio = ratio => {
194
- assertIsRatio(ratio);
195
-
196
- return makeRatio(
197
- /** @type {NatValue} */ (ratio.denominator.value),
198
- ratio.denominator.brand,
199
- /** @type {NatValue} */ (ratio.numerator.value),
200
- ratio.numerator.brand,
201
- );
202
- };
203
-
204
- /**
205
- * @param {Ratio} left
206
- * @param {Ratio} right
207
- * @returns {Ratio}
208
- */
209
- export const addRatios = (left, right) => {
210
- assertIsRatio(right);
211
- assertIsRatio(left);
212
- left.numerator.brand === right.numerator.brand ||
213
- Fail`numerator brands must match: ${q(left)} ${q(right)}`;
214
- left.denominator.brand === right.denominator.brand ||
215
- Fail`denominator brands must match: ${q(left)} ${q(right)}`;
216
-
217
- // Simplifying the expression:
218
- // (and + bnd) / y d**2
219
- // (a + b) nd / y d**2
220
- // ((a + b) n / y d) * (d / d)
221
- // (a + b) n / yd
222
- return makeRatio(
223
- add(
224
- multiply(left.numerator.value, right.denominator.value), // a nd
225
- multiply(left.denominator.value, right.numerator.value), // b nd
226
- ), // (a + b) nd
227
- left.numerator.brand,
228
- multiply(left.denominator.value, right.denominator.value), // y d**2
229
- left.denominator.brand,
230
- );
231
- };
232
-
233
- /**
234
- * @param {Ratio} left
235
- * @param {Ratio} right
236
- * @returns {Ratio}
237
- */
238
- export const subtractRatios = (left, right) => {
239
- assertIsRatio(right);
240
- assertIsRatio(left);
241
- left.numerator.brand === right.numerator.brand ||
242
- Fail`numerator brands must match: ${q(left)} ${q(right)}`;
243
- left.denominator.brand === right.denominator.brand ||
244
- Fail`denominator brands must match: ${q(left)} ${q(right)}`;
245
-
246
- return makeRatio(
247
- subtract(
248
- multiply(left.numerator.value, right.denominator.value), // a nd
249
- multiply(left.denominator.value, right.numerator.value), // b nd
250
- ), // (a - b) nd
251
- left.numerator.brand,
252
- multiply(left.denominator.value, right.denominator.value), // y d**2
253
- left.denominator.brand,
254
- );
255
- };
256
-
257
- /**
258
- * @param {Ratio} left
259
- * @param {Ratio} right
260
- * @returns {Ratio}
261
- */
262
- export const multiplyRatios = (left, right) => {
263
- assertIsRatio(right);
264
- assertIsRatio(left);
265
-
266
- const getRemainingBrands = () => {
267
- // Prefer results that have the same brand as the left operand.
268
- if (right.numerator.brand === right.denominator.brand) {
269
- return [left.numerator.brand, left.denominator.brand];
270
- }
271
- if (right.numerator.brand === left.denominator.brand) {
272
- return [left.numerator.brand, right.denominator.brand];
273
- }
274
- if (left.numerator.brand === right.denominator.brand) {
275
- return [right.numerator.brand, left.denominator.brand];
276
- }
277
- if (left.numerator.brand === left.denominator.brand) {
278
- return [right.numerator.brand, right.denominator.brand];
279
- }
280
- throw Fail`at least one brand must cancel out: ${q(left)} ${q(right)}`;
281
- };
282
-
283
- const [numeratorBrand, denominatorBrand] = getRemainingBrands();
284
- return makeRatio(
285
- multiply(left.numerator.value, right.numerator.value),
286
- numeratorBrand,
287
- multiply(left.denominator.value, right.denominator.value),
288
- denominatorBrand,
289
- );
290
- };
291
-
292
- /**
293
- * If ratio is between 0 and 1, subtract from 1.
294
- *
295
- * @param {Ratio} ratio
296
- * @returns {Ratio}
297
- */
298
- export const oneMinus = ratio => {
299
- assertIsRatio(ratio);
300
- ratio.numerator.brand === ratio.denominator.brand ||
301
- Fail`oneMinus only supports ratios with a single brand, but ${ratio.numerator.brand} doesn't match ${ratio.denominator.brand}`;
302
- ratio.numerator.value <= ratio.denominator.value ||
303
- Fail`Parameter must be less than or equal to 1: ${ratio.numerator.value}/${ratio.denominator.value}`;
304
- return makeRatio(
305
- subtract(ratio.denominator.value, ratio.numerator.value),
306
- ratio.numerator.brand,
307
- ratio.denominator.value,
308
- ratio.numerator.brand,
309
- );
310
- };
311
-
312
- /**
313
- * @param {Ratio} left
314
- * @param {Ratio} right
315
- * @returns {boolean}
316
- */
317
- export const ratioGTE = (left, right) => {
318
- if (left.numerator.brand === right.numerator.brand) {
319
- left.denominator.brand === right.denominator.brand ||
320
- Fail`numerator brands match, but denominator brands don't: ${q(left)} ${q(
321
- right,
322
- )}`;
323
- } else if (left.numerator.brand === left.denominator.brand) {
324
- right.numerator.brand === right.denominator.brand ||
325
- Fail`lefthand brands match, but righthand brands don't: ${q(left)} ${q(
326
- right,
327
- )}`;
328
- }
329
- return natSafeMath.isGTE(
330
- multiply(left.numerator.value, right.denominator.value),
331
- multiply(right.numerator.value, left.denominator.value),
332
- );
333
- };
334
-
335
- /**
336
- * True iff the ratios are the same values (equal or equivalant may return false)
337
- *
338
- * @param {Ratio} left
339
- * @param {Ratio} right
340
- * @returns {boolean}
341
- */
342
- export const ratiosSame = (left, right) => {
343
- return (
344
- AmountMath.isEqual(left.numerator, right.numerator) &&
345
- AmountMath.isEqual(left.denominator, right.denominator)
346
- );
347
- };
348
-
349
- /**
350
- * Make a new ratio with a smaller denominator that approximates the ratio. If
351
- * the proposed denominator is larger than the current one, return the original.
352
- *
353
- * @param {Ratio} ratio
354
- * @param {bigint} newDen
355
- * @returns {Ratio}
356
- */
357
- export const quantize = (ratio, newDen) => {
358
- const oldDen = ratio.denominator.value;
359
- const oldNum = ratio.numerator.value;
360
- if (newDen > oldDen) {
361
- return ratio;
362
- }
363
-
364
- const newNum =
365
- newDen === oldDen ? oldNum : bankersDivide(oldNum * newDen, oldDen);
366
- return makeRatio(
367
- newNum,
368
- ratio.numerator.brand,
369
- newDen,
370
- ratio.denominator.brand,
371
- );
372
- };
373
-
374
- const NUMERIC_RE = /^(\d\d*)(?:\.(\d*))?$/;
375
- /** @typedef {bigint | number | string} ParsableNumber */
376
-
377
- /**
378
- * Create a ratio from a given numeric value.
379
- *
380
- * @param {ParsableNumber} numeric
381
- * @param {Brand<'nat'>} numeratorBrand
382
- * @param {Brand<'nat'>} [denominatorBrand]
383
- * @returns {Ratio}
384
- */
385
- export const parseRatio = (
386
- numeric,
387
- numeratorBrand,
388
- denominatorBrand = numeratorBrand,
389
- ) => {
390
- const match = `${numeric}`.match(NUMERIC_RE);
391
- if (!match) {
392
- throw Fail`Invalid numeric data: ${numeric}`;
393
- }
394
-
395
- const [_, whole, part = ''] = match;
396
- return makeRatio(
397
- BigInt(`${whole}${part}`),
398
- numeratorBrand,
399
- 10n ** BigInt(part.length),
400
- denominatorBrand,
401
- );
402
- };
403
-
404
- /**
405
- * @param {unknown} specimen
406
- * @returns {asserts specimen is ParsableNumber}
407
- */
408
- export const assertParsableNumber = specimen => {
409
- const match = `${specimen}`.match(NUMERIC_RE);
410
- match || Fail`Invalid numeric data: ${specimen}`;
411
- };
412
-
413
- /**
414
- * Ratios might be greater or less than one.
415
- *
416
- * @param {Ratio} ratio
417
- * @returns {number}
418
- */
419
- export const ratioToNumber = ratio => {
420
- const n = Number(ratio.numerator.value);
421
- const d = Number(ratio.denominator.value);
422
- return n / d;
423
- };
@@ -1,11 +0,0 @@
1
- export namespace natSafeMath {
2
- let add: NatOp;
3
- let subtract: NatOp;
4
- let multiply: NatOp;
5
- let floorDivide: NatOp;
6
- let ceilDivide: NatOp;
7
- let bankersDivide: NatOp;
8
- let isGTE: (x: number | bigint, y: number | bigint) => boolean;
9
- }
10
- export type NatOp = (x: number | bigint, y: number | bigint) => NatValue;
11
- //# sourceMappingURL=safeMath.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"safeMath.d.ts","sourceRoot":"","sources":["safeMath.js"],"names":[],"mappings":";aAYa,KAAK;kBAGL,KAAK;kBAEL,KAAK;qBAEL,KAAK;oBAEL,KAAK;uBAQN,KAAK;eAiBJ,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,KAAK,OAAO;;oBA5ClD,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,KAAK,QAAQ"}