From 0ce647fca81f6a9aeaf13c1d3435b2e4e3f9cba8 Mon Sep 17 00:00:00 2001 From: Turadg Aleahmad Date: Thu, 23 May 2024 16:05:03 -0700 Subject: [PATCH] fix(purse): amountShape param --- packages/ERTP/src/typeGuards.js | 6 ++++++ .../ERTP/test/unitTests/typeGuards.test.js | 18 ++++++++++++++++++ packages/vats/src/virtual-purse.js | 7 +++++-- 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 packages/ERTP/test/unitTests/typeGuards.test.js diff --git a/packages/ERTP/src/typeGuards.js b/packages/ERTP/src/typeGuards.js index 1b2ec30fcea9..f47d52ed9916 100644 --- a/packages/ERTP/src/typeGuards.js +++ b/packages/ERTP/src/typeGuards.js @@ -77,6 +77,12 @@ export const AmountShape = harden({ value: AmountValueShape, }); +/** For parameters that are themselves an AmountShape pattern. */ +export const AmountShapeShape = harden({ + brand: M.pattern(), + value: M.pattern(), +}); + export const RatioShape = harden({ numerator: AmountShape, denominator: AmountShape, diff --git a/packages/ERTP/test/unitTests/typeGuards.test.js b/packages/ERTP/test/unitTests/typeGuards.test.js new file mode 100644 index 000000000000..6eec846b28a3 --- /dev/null +++ b/packages/ERTP/test/unitTests/typeGuards.test.js @@ -0,0 +1,18 @@ +import test from 'ava'; + +import { M, matches } from '@endo/patterns'; +import { Far } from '@endo/far'; +import { AmountShape, AmountShapeShape } from '../../src/typeGuards.js'; + +test('AmountShape', t => { + t.truthy(matches(harden({ brand: Far('brand'), value: 123n }), AmountShape)); +}); + +test('AmountShapeShape', t => { + t.truthy( + matches( + harden({ brand: M.remotable('brand'), value: M.nat() }), + AmountShapeShape, + ), + ); +}); diff --git a/packages/vats/src/virtual-purse.js b/packages/vats/src/virtual-purse.js index c08b39751ae8..5ef083701f9e 100644 --- a/packages/vats/src/virtual-purse.js +++ b/packages/vats/src/virtual-purse.js @@ -4,6 +4,7 @@ import { isPromise } from '@endo/promise-kit'; import { AmountShape, + AmountShapeShape, BrandShape, DepositFacetShape, NotifierShape, @@ -37,7 +38,7 @@ export const makeVirtualPurseKitIKit = ( // to this raw method to validate that this remotable is actually // a live payment of the correct brand with sufficient funds. deposit: M.callWhen(PaymentShape) - .optional(M.pattern()) + .optional(AmountShapeShape) .returns(amountShape), getDepositFacet: M.callWhen().returns(DepositFacetShape), withdraw: M.callWhen(amountShape).returns(PaymentShape), @@ -50,7 +51,9 @@ export const makeVirtualPurseKitIKit = ( }); const RetainRedeemI = M.interface('RetainRedeem', { - retain: M.callWhen(PaymentShape).optional(amountShape).returns(amountShape), + retain: M.callWhen(PaymentShape) + .optional(AmountShapeShape) + .returns(amountShape), redeem: M.callWhen(amountShape).returns(PaymentShape), });