Skip to content

Commit

Permalink
chore(types): parameterize makeIssuerKit
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed Jan 10, 2025
1 parent 61df50e commit 361855b
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 25 deletions.
42 changes: 26 additions & 16 deletions packages/ERTP/src/issuerKit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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<K>}
* @param {O} [options]
* @returns {O['elementShape'] extends TypedPattern<infer T>
* ? IssuerKit<K, T>
* : IssuerKit<K>}
*/
export const prepareIssuerKit = (
issuerBaggage,
Expand All @@ -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)) {
Expand Down Expand Up @@ -285,6 +292,7 @@ export const prepareIssuerKit = (
optShutdownWithFailure,
options,
);
// @ts-expect-error cast to the type parameter
return issuerKit;
}
};
Expand All @@ -299,39 +307,41 @@ 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
* provided, then the failed atomic action will call it, so that some larger
* 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<K>}
* @param {O} [options]
* @returns {O['elementShape'] extends TypedPattern<infer T>
* ? IssuerKit<K, T>
* : IssuerKit<K>}
*/
export const makeIssuerKit = (
name,
// @ts-expect-error K could be instantiated with a different subtype of AssetKind
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,
Expand Down
7 changes: 6 additions & 1 deletion packages/ERTP/test/unitTests/mintObj.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,18 @@ test('mint.mintPayment default nat AssetKind', async t => {
t.assert(AmountMath.isEqual(paymentBalance2, fungible1000));
});

/** @type {import('@agoric/internal').TypedPattern<string>} */
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -21,6 +30,7 @@ test('findInvitationAmount', async t => {

const { findInvitationAmount } = makeOfferAndFindInvitationAmount(
walletAdmin,
// @ts-expect-error mock
zoe,
zoeInvitationPurse,
);
Expand Down
1 change: 1 addition & 0 deletions packages/zoe/src/typeGuards.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export const isAfterDeadlineExitRule = exit => {
};
harden(isAfterDeadlineExitRule);

/** @type {TypedPattern<InvitationDetails>} */
export const InvitationElementShape = M.splitRecord({
description: M.string(),
handle: InvitationHandleShape,
Expand Down
1 change: 0 additions & 1 deletion packages/zoe/src/zoeService/makeInvitation.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export const prepareInvitationKit = (baggage, shutdownZoeVat = undefined) => {
}

/** @type {IssuerKit<'set', InvitationDetails>} */
// @ts-expect-error cast
const invitationKit = prepareIssuerKit(
invitationKitBaggage,
'Zoe Invitation',
Expand Down
2 changes: 1 addition & 1 deletion packages/zoe/tools/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { makeRatio } from '../src/contractSupport/ratio.js';
* @import {Amount, Brand, DepositFacet, Issuer, IssuerKit, NatValue} from '@agoric/ertp';
*/

/** @param {Pick<IssuerKit<'nat'>, 'brand' | 'issuer' | 'mint'>} kit */
/** @param {Pick<IssuerKit<'nat', any>, 'brand' | 'issuer' | 'mint'>} kit */
export const withAmountUtils = kit => {
const decimalPlaces = kit.issuer.getDisplayInfo?.()?.decimalPlaces ?? 6;
return {
Expand Down

0 comments on commit 361855b

Please sign in to comment.