Skip to content

Commit

Permalink
refactor: Use new M.splitArray and M.splitRecord
Browse files Browse the repository at this point in the history
  • Loading branch information
erights committed Dec 1, 2022
1 parent fcc2ef7 commit f7e1c23
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 71 deletions.
11 changes: 6 additions & 5 deletions packages/ERTP/src/typeGuards.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,19 @@ export const MAX_ABSOLUTE_DECIMAL_PLACES = 100;

export const AssetKindShape = M.or('nat', 'set', 'copySet', 'copyBag');

export const DisplayInfoShape = M.partial(
harden({
export const DisplayInfoShape = M.splitRecord(
{},
{
decimalPlaces: M.and(
M.gte(-MAX_ABSOLUTE_DECIMAL_PLACES),
M.lte(MAX_ABSOLUTE_DECIMAL_PLACES),
),
assetKind: AssetKindShape,
}),
harden({
},
{
// Including this empty `rest` ensures that there are no other
// properties beyond those in the `base` record.
}),
},
);

// //////////////////////// Interfaces /////////////////////////////////////////
Expand Down
31 changes: 16 additions & 15 deletions packages/SwingSet/src/typeGuards.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ import { M } from '@agoric/store';

export const ManagerType = M.or('xs-worker', 'local'); // TODO: others

const Bundle = M.split({ moduleType: M.string() }, M.partial({}));
const Bundle = M.splitRecord({ moduleType: M.string() });

const SwingsetConfigOptions = harden({
creationOptions: M.splitRecord({}, { critial: M.boolean() }),
parameters: M.recordOf(M.string(), M.any()),
});

const p1 = M.and(
M.partial({ creationOptions: M.partial({ critial: M.boolean() }) }),
M.partial({ parameters: M.recordOf(M.string(), M.any()) }),
);
const SwingSetConfigProperties = M.or(
M.split({ sourceSpec: M.string() }, p1),
M.split({ bundleSpec: M.string() }, p1),
M.split({ bundle: Bundle }, p1),
M.splitRecord({ sourceSpec: M.string() }, SwingsetConfigOptions),
M.splitRecord({ bundleSpec: M.string() }, SwingsetConfigOptions),
M.splitRecord({ bundle: Bundle }, SwingsetConfigOptions),
);
const SwingSetConfigDescriptor = M.recordOf(
M.string(),
Expand All @@ -30,11 +31,11 @@ const SwingSetConfigDescriptor = M.recordOf(
* in ./types-external.js
*/
export const SwingSetConfig = M.and(
M.partial({ defaultManagerType: ManagerType }),
M.partial({ includeDevDependencies: M.boolean() }),
M.partial({ defaultReapInterval: M.number() }), // not in type decl
M.partial({ snapshotInterval: M.number() }),
M.partial({ vats: SwingSetConfigDescriptor }),
M.partial({ bootstrap: M.string() }),
M.partial({ bundles: SwingSetConfigDescriptor }),
M.splitRecord({}, { defaultManagerType: ManagerType }),
M.splitRecord({}, { includeDevDependencies: M.boolean() }),
M.splitRecord({}, { defaultReapInterval: M.number() }), // not in type decl
M.splitRecord({}, { snapshotInterval: M.number() }),
M.splitRecord({}, { vats: SwingSetConfigDescriptor }),
M.splitRecord({}, { bootstrap: M.string() }),
M.splitRecord({}, { bundles: SwingSetConfigDescriptor }),
);
8 changes: 4 additions & 4 deletions packages/casting/src/netconfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import { fit, M } from '@agoric/store';
* @property {string[]} [peers] - a list of nodes used to start the p2p gossip (stored in a per-node “address book”, which is a file stored in that node’s data directory)
* @property {string[]} [seeds] - nodes which tell you about other peers but don't gossip actual data
*/
export const NetworkConfigShape = M.split(
{
export const NetworkConfigShape = M.splitRecord(
harden({
chainName: M.string(),
rpcAddrs: M.arrayOf(M.string()),
},
M.partial({
}),
harden({
apiAddrs: M.arrayOf(M.string()),
gci: M.string(),
peers: M.arrayOf(M.string()),
Expand Down
4 changes: 2 additions & 2 deletions packages/inter-protocol/src/psm/psm.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ export const start = async (zcf, privateArgs, baggage) => {
wantmintedHook,
'wantMinted',
undefined,
M.split({
M.splitRecord({
give: { In: anchorAmountShape },
want: M.or({ Out: stableAmountShape }, {}),
}),
Expand All @@ -287,7 +287,7 @@ export const start = async (zcf, privateArgs, baggage) => {
giveMintedHook,
'giveMinted',
undefined,
M.split({
M.splitRecord({
give: { In: stableAmountShape },
want: M.or({ Out: anchorAmountShape }, {}),
}),
Expand Down
20 changes: 9 additions & 11 deletions packages/inter-protocol/src/psm/psmCharter.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@ import { E } from '@endo/far';
* @property {Record<string, unknown>} params
* @property {{paramPath: { key: string }}} [path]
*/
const ParamChangesOfferArgsShape = harden(
M.split(
{
deadline: TimestampShape,
instance: InstanceHandleShape,
params: M.recordOf(M.string(), M.any()),
},
M.partial({
path: { paramPath: { key: M.string() } },
}),
),
const ParamChangesOfferArgsShape = M.splitRecord(
{
deadline: TimestampShape,
instance: InstanceHandleShape,
params: M.recordOf(M.string(), M.any()),
},
{
path: { paramPath: { key: M.string() } },
},
);

/**
Expand Down
17 changes: 7 additions & 10 deletions packages/inter-protocol/src/vaultFactory/params.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,13 @@ const makeVaultParamManager = (publisherKit, initial) =>
});
/** @typedef {ReturnType<typeof makeVaultParamManager>} VaultParamManager */

export const vaultParamPattern = M.split(
{
liquidationMargin: ratioPattern,
liquidationPenalty: ratioPattern,
interestRate: ratioPattern,
loanFee: ratioPattern,
debtLimit: amountPattern,
},
M.any(),
);
export const vaultParamPattern = M.splitRecord({
liquidationMargin: ratioPattern,
liquidationPenalty: ratioPattern,
interestRate: ratioPattern,
loanFee: ratioPattern,
debtLimit: amountPattern,
});

/**
* @param {import('@agoric/notifier').StoredPublisherKit<GovernanceSubscriptionState>} publisherKit
Expand Down
19 changes: 10 additions & 9 deletions packages/smart-wallet/src/typeGuards.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@ export const shape = {
},

// invitations
ContractInvitationSpec: M.split(
ContractInvitationSpec: M.splitRecord(
{
source: 'contract',
instance: InstanceHandleShape,
publicInvitationMaker: M.string(),
},
M.partial({
{
invitationArgs: M.array(),
}),
},
),
ContinuingInvitationSpec: M.split(
ContinuingInvitationSpec: M.splitRecord(
{
source: 'continuing',
previousOffer: M.or(M.number(), M.string()),
invitationMakerName: M.string(),
},
M.partial({
{
invitationArgs: M.array(),
}),
},
),
PurseInvitationSpec: {
source: 'purse',
Expand All @@ -39,24 +39,25 @@ export const shape = {
},

// offers
OfferSpec: M.split(
OfferSpec: M.splitRecord(
{
id: M.or(M.number(), M.string()),
// TODO M.unknown() to defer validation
invitationSpec: M.any(),
proposal: ProposalShape,
},
M.partial({ offerArgs: M.any() }),
{ offerArgs: M.any() },
),

// walletFactory
WalletBridgeMsg: M.split(
WalletBridgeMsg: M.splitRecord(
{
owner: M.string(),
type: M.string(),
blockHeight: M.number(),
blockTime: M.number(),
},
{},
M.or({ action: M.string() }, { spendAction: M.string() }),
),
};
Expand Down
4 changes: 2 additions & 2 deletions packages/smart-wallet/src/walletFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import { shape } from './typeGuards.js';
import '@agoric/vats/exported.js';

const PrivateArgsShape = harden(
M.split(
M.splitRecord(
{ storageNode: M.eref(M.any()) },
M.partial({ bridgeManager: M.eref(M.any()) }),
{ bridgeManager: M.eref(M.any()) },
),
);

Expand Down
17 changes: 10 additions & 7 deletions packages/vats/src/core/boot-psm.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,22 @@ export const agoricNamesReserved = harden(
* decimalPlaces?: number
* }} AnchorOptions
*/
const AnchorOptionsShape = M.split(
const AnchorOptionsShape = M.splitRecord(
{ denom: M.string() },
M.partial({
{
keyword: M.string(),
proposedName: M.string(),
decimalPlaces: M.number(),
}),
},
);

export const ParametersShape = M.partial({
anchorAssets: M.arrayOf(AnchorOptionsShape),
economicCommitteeAddresses: M.recordOf(M.string(), M.string()),
});
export const ParametersShape = M.splitRecord(
{},
{
anchorAssets: M.arrayOf(AnchorOptionsShape),
economicCommitteeAddresses: M.recordOf(M.string(), M.string()),
},
);

/**
* Build root object of the PSM-only bootstrap vat.
Expand Down
2 changes: 1 addition & 1 deletion packages/zoe/src/contracts/coveredCall-durable.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const start = async (zcf, _privateArgs, instanceBaggage) => {
const makeOption = sellSeat => {
fit(
sellSeat.getProposal(),
M.split({ exit: { afterDeadline: M.any() } }),
M.splitRecord({ exit: { afterDeadline: M.any() } }),
'exit afterDeadline',
);
const sellSeatExitRule = sellSeat.getProposal().exit;
Expand Down
2 changes: 1 addition & 1 deletion packages/zoe/src/contracts/coveredCall.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const start = zcf => {
const makeOption = sellSeat => {
fit(
sellSeat.getProposal(),
M.split({ exit: { afterDeadline: M.any() } }),
M.splitRecord({ exit: { afterDeadline: M.any() } }),
'exit afterDeadline',
);
const sellSeatExitRule = sellSeat.getProposal().exit;
Expand Down
9 changes: 5 additions & 4 deletions packages/zoe/src/typeGuards.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ export const TimerShape = makeHandleShape('timer');
export const FullProposalShape = harden({
want: AmountPatternKeywordRecordShape,
give: AmountKeywordRecordShape,
// To accept only one, we could use M.or rather than M.partial,
// To accept only one, we could use M.or rather than M.splitRecord,
// but the error messages would have been worse. Rather,
// cleanProposal's assertExit checks that there's exactly one.
exit: M.partial(
exit: M.splitRecord(
{},
{
onDemand: null,
waived: null,
Expand All @@ -43,7 +44,7 @@ export const FullProposalShape = harden({
),
});
/** @see {Proposal} type */
export const ProposalShape = M.partial(FullProposalShape);
export const ProposalShape = M.splitRecord({}, FullProposalShape, {});

export const isOnDemandExitRule = exit => {
const [exitKey] = Object.getOwnPropertyNames(exit);
Expand All @@ -68,7 +69,7 @@ export const isAfterDeadlineExitRule = exit => {
return exitKey === 'afterDeadline';
};

export const InvitationElementShape = M.split({
export const InvitationElementShape = M.splitRecord({
description: M.string(),
handle: InvitationHandleShape,
instance: InstanceHandleShape,
Expand Down

0 comments on commit f7e1c23

Please sign in to comment.