From 361855b6acbed4dcda010204893ef0a5879ecc38 Mon Sep 17 00:00:00 2001 From: Turadg Aleahmad Date: Thu, 9 Jan 2025 18:01:33 -0800 Subject: [PATCH] chore(types): parameterize makeIssuerKit --- packages/ERTP/src/issuerKit.js | 42 ++++++++++++------- packages/ERTP/test/unitTests/mintObj.test.js | 7 +++- .../test/unitTests/depositInvitation.test.js | 14 +++++-- .../unitTests/findInvitationAmount.test.js | 14 ++++++- packages/zoe/src/typeGuards.js | 1 + packages/zoe/src/zoeService/makeInvitation.js | 1 - packages/zoe/tools/test-utils.js | 2 +- 7 files changed, 56 insertions(+), 25 deletions(-) diff --git a/packages/ERTP/src/issuerKit.js b/packages/ERTP/src/issuerKit.js index fc3311a4b2e..0493f44ebfa 100644 --- a/packages/ERTP/src/issuerKit.js +++ b/packages/ERTP/src/issuerKit.js @@ -9,8 +9,11 @@ import { AssetKind, assertAssetKind } from './amountMath.js'; import { coerceDisplayInfo } from './displayInfo.js'; import { preparePaymentLedger } from './paymentLedger.js'; -/** @import {AdditionalDisplayInfo, RecoverySetsOption, IssuerKit, PaymentLedger} from './types.js' */ -/** @import {ShutdownWithFailure} from '@agoric/swingset-vat' */ +/** + * @import {AdditionalDisplayInfo, RecoverySetsOption, IssuerKit, PaymentLedger} from './types.js'; + * @import {ShutdownWithFailure} from '@agoric/swingset-vat'; + * @import {TypedPattern} from '@agoric/internal'; + */ /** * @template {AssetKind} K @@ -224,6 +227,7 @@ harden(makeDurableIssuerKit); * Used to either revive a predecessor issuerKit, or to make a new durable one * if it is absent, and to place it in baggage for the next successor. * + * @template {IssuerOptionsRecord} O * @template {AssetKind} K The name becomes part of the brand in asset * descriptions. The name is useful for debugging and double-checking * assumptions, but should not be trusted wrt any external namespace. For @@ -247,8 +251,10 @@ harden(makeDurableIssuerKit); * unit of computation, like the enclosing vat, can be shutdown before * anything else is corrupted by that corrupted state. See * https://github.com/Agoric/agoric-sdk/issues/3434 - * @param {IssuerOptionsRecord} [options] - * @returns {IssuerKit} + * @param {O} [options] + * @returns {O['elementShape'] extends TypedPattern + * ? IssuerKit + * : IssuerKit} */ export const prepareIssuerKit = ( issuerBaggage, @@ -257,6 +263,7 @@ export const prepareIssuerKit = ( assetKind = AssetKind.NAT, displayInfo = harden({}), optShutdownWithFailure = undefined, + // @ts-expect-error could have a different subtype of IssuerOptionsRecord options = {}, ) => { if (hasIssuer(issuerBaggage)) { @@ -285,6 +292,7 @@ export const prepareIssuerKit = ( optShutdownWithFailure, options, ); + // @ts-expect-error cast to the type parameter return issuerKit; } }; @@ -299,21 +307,19 @@ harden(prepareIssuerKit); * Currently used for testing only. Should probably continue to be used for * testing only. * - * @template {AssetKind} [K='nat'] The name becomes part of the brand in asset + * @template {AssetKind} [K='nat'] + * @template {IssuerOptionsRecord} [O={}] + * @param {string} name The name becomes part of the brand in asset * descriptions. The name is useful for debugging and double-checking * assumptions, but should not be trusted wrt any external namespace. For * example, anyone could create a new issuer kit with name 'BTC', but it is * not bitcoin or even related. It is only the name according to that issuer * and brand. - * - * The assetKind will be used to import a specific mathHelpers from the - * mathHelpers library. For example, natMathHelpers, the default, is used for - * basic fungible tokens. - * - * `displayInfo` gives information to the UI on how to display the amount. - * @param {string} name - * @param {K} [assetKind] - * @param {AdditionalDisplayInfo} [displayInfo] + * @param {K} [assetKind] The assetKind will be used to import a specific + * mathHelpers from the mathHelpers library. For example, natMathHelpers, the + * default, is used for basic fungible tokens. + * @param {AdditionalDisplayInfo} [displayInfo] `displayInfo` gives information + * to the UI on how to display the amount. * @param {ShutdownWithFailure} [optShutdownWithFailure] If this issuer fails in * the middle of an atomic action (which btw should never happen), it * potentially leaves its ledger in a corrupted state. If this function was @@ -321,8 +327,10 @@ harden(prepareIssuerKit); * unit of computation, like the enclosing vat, can be shutdown before * anything else is corrupted by that corrupted state. See * https://github.com/Agoric/agoric-sdk/issues/3434 - * @param {IssuerOptionsRecord} [options] - * @returns {IssuerKit} + * @param {O} [options] + * @returns {O['elementShape'] extends TypedPattern + * ? IssuerKit + * : IssuerKit} */ export const makeIssuerKit = ( name, @@ -330,8 +338,10 @@ export const makeIssuerKit = ( assetKind = AssetKind.NAT, displayInfo = harden({}), optShutdownWithFailure = undefined, + // @ts-expect-error O could be instantiated with a different subtype { elementShape = undefined, recoverySetsOption = undefined } = {}, ) => + // @ts-expect-error cast makeDurableIssuerKit( makeScalarBigMapStore('dropped issuer kit', { durable: true }), name, diff --git a/packages/ERTP/test/unitTests/mintObj.test.js b/packages/ERTP/test/unitTests/mintObj.test.js index eb64eefba1f..0b03fc4b413 100644 --- a/packages/ERTP/test/unitTests/mintObj.test.js +++ b/packages/ERTP/test/unitTests/mintObj.test.js @@ -28,13 +28,18 @@ test('mint.mintPayment default nat AssetKind', async t => { t.assert(AmountMath.isEqual(paymentBalance2, fungible1000)); }); +/** @type {import('@agoric/internal').TypedPattern} */ +const StringPattern = M.string(); + test('mint.mintPayment set w strings AssetKind', async t => { const { mint, issuer, brand } = makeIssuerKit( 'items', AssetKind.SET, undefined, undefined, - { elementShape: M.string() }, + { + elementShape: StringPattern, + }, ); const items1and2and4 = AmountMath.make(brand, harden(['1', '2', '4'])); const payment1 = mint.mintPayment(items1and2and4); diff --git a/packages/deploy-script-support/test/unitTests/depositInvitation.test.js b/packages/deploy-script-support/test/unitTests/depositInvitation.test.js index 0c89a468939..e0af858f5d1 100644 --- a/packages/deploy-script-support/test/unitTests/depositInvitation.test.js +++ b/packages/deploy-script-support/test/unitTests/depositInvitation.test.js @@ -3,13 +3,19 @@ import { test } from '@agoric/zoe/tools/prepare-test-env-ava.js'; import { makeIssuerKit, AssetKind, AmountMath } from '@agoric/ertp'; +import { InvitationElementShape } from '@agoric/zoe/src/typeGuards.js'; import { makeDepositInvitation } from '../../src/depositInvitation.js'; test('depositInvitation', async t => { - const { mint, issuer, brand } = - /** @type {IssuerKit<'set', InvitationDetails>} */ ( - makeIssuerKit('invitations', AssetKind.SET) - ); + const { mint, issuer, brand } = makeIssuerKit( + 'invitations', + AssetKind.SET, + undefined, + undefined, + { + elementShape: InvitationElementShape, + }, + ); const purse = issuer.makeEmptyPurse(); const paymentAmount = AmountMath.make(brand, harden([{ instance: {} }])); const payment = mint.mintPayment(paymentAmount); diff --git a/packages/deploy-script-support/test/unitTests/findInvitationAmount.test.js b/packages/deploy-script-support/test/unitTests/findInvitationAmount.test.js index bb2e7f9e982..d9bf073135b 100644 --- a/packages/deploy-script-support/test/unitTests/findInvitationAmount.test.js +++ b/packages/deploy-script-support/test/unitTests/findInvitationAmount.test.js @@ -1,15 +1,24 @@ -// @ts-nocheck import { test } from '@agoric/zoe/tools/prepare-test-env-ava.js'; import { makeIssuerKit, AssetKind, AmountMath } from '@agoric/ertp'; +import { InvitationElementShape } from '@agoric/zoe/src/typeGuards.js'; import { makeOfferAndFindInvitationAmount } from '../../src/offer.js'; test('findInvitationAmount', async t => { - const { mint, issuer, brand } = makeIssuerKit('invitations', AssetKind.SET); + const { mint, issuer, brand } = makeIssuerKit( + 'invitations', + AssetKind.SET, + undefined, + undefined, + { + elementShape: InvitationElementShape, + }, + ); const zoeInvitationPurse = issuer.makeEmptyPurse(); const walletAdmin = {}; + const zoe = {}; const paymentAmount = AmountMath.make( @@ -21,6 +30,7 @@ test('findInvitationAmount', async t => { const { findInvitationAmount } = makeOfferAndFindInvitationAmount( walletAdmin, + // @ts-expect-error mock zoe, zoeInvitationPurse, ); diff --git a/packages/zoe/src/typeGuards.js b/packages/zoe/src/typeGuards.js index 3690c681102..f2ba085c7e8 100644 --- a/packages/zoe/src/typeGuards.js +++ b/packages/zoe/src/typeGuards.js @@ -133,6 +133,7 @@ export const isAfterDeadlineExitRule = exit => { }; harden(isAfterDeadlineExitRule); +/** @type {TypedPattern} */ export const InvitationElementShape = M.splitRecord({ description: M.string(), handle: InvitationHandleShape, diff --git a/packages/zoe/src/zoeService/makeInvitation.js b/packages/zoe/src/zoeService/makeInvitation.js index b6e5c20242f..8558695db98 100644 --- a/packages/zoe/src/zoeService/makeInvitation.js +++ b/packages/zoe/src/zoeService/makeInvitation.js @@ -30,7 +30,6 @@ export const prepareInvitationKit = (baggage, shutdownZoeVat = undefined) => { } /** @type {IssuerKit<'set', InvitationDetails>} */ - // @ts-expect-error cast const invitationKit = prepareIssuerKit( invitationKitBaggage, 'Zoe Invitation', diff --git a/packages/zoe/tools/test-utils.js b/packages/zoe/tools/test-utils.js index bb50ababc94..51975aa13d9 100644 --- a/packages/zoe/tools/test-utils.js +++ b/packages/zoe/tools/test-utils.js @@ -5,7 +5,7 @@ import { makeRatio } from '../src/contractSupport/ratio.js'; * @import {Amount, Brand, DepositFacet, Issuer, IssuerKit, NatValue} from '@agoric/ertp'; */ -/** @param {Pick, 'brand' | 'issuer' | 'mint'>} kit */ +/** @param {Pick, 'brand' | 'issuer' | 'mint'>} kit */ export const withAmountUtils = kit => { const decimalPlaces = kit.issuer.getDisplayInfo?.()?.decimalPlaces ?? 6; return {