Skip to content

Commit

Permalink
fix: many typing improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Jun 23, 2024
1 parent efdf39f commit 777eb21
Show file tree
Hide file tree
Showing 21 changed files with 112 additions and 74 deletions.
2 changes: 1 addition & 1 deletion packages/governance/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ export {};
* Akin to StartedInstanceKit but designed for the results of starting governed contracts. Used in bootstrap space.
* @property {AdminFacet} adminFacet of the governed contract
* @property {LimitedCF<SF>} creatorFacet creator-like facet within the governed contract (without the powers the governor needs)
* @property {Guarded<GovernorCreatorFacet<SF>>} governorCreatorFacet of the governing contract
* @property {Guarded<GovernorCreatorFacet<SF>> | GovernorCreatorFacet<SF>} governorCreatorFacet of the governing contract
* @property {AdminFacet} governorAdminFacet of the governing contract
* @property {Awaited<ReturnType<SF>>['publicFacet']} publicFacet
* @property {Instance} instance
Expand Down
1 change: 0 additions & 1 deletion packages/inter-protocol/src/proposals/add-auction.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ export const addAuction = async ({
);

newAuctioneerKit.resolve(
// @ts-expect-error XXX governance types
harden({
label: 'auctioneer',
creatorFacet: governedCreatorFacet,
Expand Down
3 changes: 0 additions & 3 deletions packages/inter-protocol/src/proposals/econ-behaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ export const setupReserve = async ({
]);

reserveKit.resolve(
// @ts-expect-error XXX
harden({
label: 'AssetReserve',
instance,
Expand Down Expand Up @@ -349,7 +348,6 @@ export const startVaultFactory = async (
);

vaultFactoryKit.resolve(
// @ts-expect-error XXX
harden({
label: 'VaultFactory',
creatorFacet: vaultFactoryCreator,
Expand Down Expand Up @@ -624,7 +622,6 @@ export const startAuctioneer = async (
]);

auctioneerKit.resolve(
// @ts-expect-error XXX
harden({
label: 'auctioneer',
creatorFacet: governedCreatorFacet,
Expand Down
4 changes: 2 additions & 2 deletions packages/inter-protocol/test/smartWallet/contexts.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { makeHeapZone } from '@agoric/zone';
import { E } from '@endo/far';
import path from 'path';
import { makeScopedBridge } from '@agoric/vats';
import { oracleBrandFeedName } from '../../src/proposals/utils.js';
import { createPriceFeed } from '../../src/proposals/price-feed-proposal.js';
import { withAmountUtils } from '../supports.js';
Expand Down Expand Up @@ -108,9 +109,8 @@ export const makeDefaultTestContext = async (t, makeSpace) => {
* @type {undefined
* | import('@agoric/vats').ScopedBridgeManager<'wallet'>}
*/
// @ts-expect-error XXX EProxy
const walletBridgeManager = await (bridgeManager &&
E(bridgeManager).register(BridgeId.WALLET));
makeScopedBridge(bridgeManager, BridgeId.WALLET));
const walletFactory = await E(zoe).startInstance(
installation,
{},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ export const buildRootObject = async () => {
* @param {any} devices
*/
bootstrap: async (vats, devices) => {
// @ts-expect-error XXX adminNode
vatAdmin = await E(vats.vatAdmin).createVatAdminService(devices.vatAdmin);
({ feeMintAccess, zoeService } = await E(vats.zoe).buildZoe(
vatAdmin,
Expand Down
5 changes: 2 additions & 3 deletions packages/smart-wallet/test/contexts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { unsafeMakeBundleCache } from '@agoric/swingset-vat/tools/bundleTool.js'
import { makeStorageNodeChild } from '@agoric/internal/src/lib-chainStorage.js';
import { E } from '@endo/far';
import path from 'path';
import { makeScopedBridge } from '@agoric/vats';
import { withAmountUtils } from './supports.js';

/**
Expand Down Expand Up @@ -38,10 +39,8 @@ export const makeDefaultTestContext = async (t, makeSpace) => {
'anyAddress',
);
const bridgeManager = await consume.bridgeManager;
/** @type {import('@agoric/vats').ScopedBridgeManager<'wallet'>} */
// @ts-expect-error XXX generics through EProxy
const walletBridgeManager = await (bridgeManager &&
E(bridgeManager).register(BridgeId.WALLET));
makeScopedBridge(bridgeManager, BridgeId.WALLET));
const walletFactory = await E(zoe).startInstance(
installation,
{},
Expand Down
15 changes: 11 additions & 4 deletions packages/smart-wallet/test/supports.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,12 @@ const makeFakeBridgeManager = () =>
export const makeMockTestSpace = async log => {
const space = /** @type {any} */ (makePromiseSpace(log));
/**
* @type {BootstrapPowers}
* @type {BootstrapPowers & {
* produce: {
* loadVat: Producer<VatLoader>;
* loadCriticalVat: Producer<VatLoader>;
* };
* }}
*/
const { consume, produce } = space;
const { agoricNames, agoricNamesAdmin, spaces } =
Expand All @@ -112,13 +117,15 @@ export const makeMockTestSpace = async log => {
produce.zoe.resolve(zoe);
produce.feeMintAccess.resolve(feeMintAccessP);

const vatLoader = name => {
/** @type {VatLoader<'mints' | 'board'>} */
const vatLoader = async name => {
/** @typedef {Awaited<WellKnownVats[typeof name]>} ReturnedVat */
switch (name) {
case 'mints':
return mintsRoot();
return /** @type {ReturnedVat} */ (mintsRoot());
case 'board': {
const baggage = makeScalarBigMapStore('baggage');
return boardRoot({}, {}, baggage);
return /** @type {ReturnedVat} */ (boardRoot({}, {}, baggage));
}
default:
throw Error('unknown loadVat name');
Expand Down
9 changes: 7 additions & 2 deletions packages/swingset-liveslots/src/vatDataTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
} from '@agoric/store';
import type { Amplify, IsInstance, ReceivePower, StateShape } from '@endo/exo';
import type { RemotableObject } from '@endo/pass-style';
import type { RemotableBrand } from '@endo/eventual-send';
import type { InterfaceGuard, Pattern } from '@endo/patterns';
import type { makeWatchedPromiseManager } from './watchedPromises.js';

Expand All @@ -38,9 +39,13 @@ type OmitFirstArg<F> = F extends (x: any, ...args: infer P) => infer R
? (...args: P) => R
: never;

export type KindFacet<O> = RemotableObject & {
// The type of a passable local object with methods.
// An internal helper to avoid having to repeat `O`.
type PrimaryRemotable<O> = O & RemotableObject & RemotableBrand<{}, O>;

export type KindFacet<O> = PrimaryRemotable<{
[K in keyof O]: OmitFirstArg<O[K]>; // omit the 'context' parameter
};
}>;

export type KindFacets<B> = {
[FacetKey in keyof B]: KindFacet<B[FacetKey]>;
Expand Down
21 changes: 21 additions & 0 deletions packages/vats/src/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@ import { E } from '@endo/far';

const { Fail, details: X } = assert;

/**
* Helper to type the registered scoped bridge correctly.
*
* FIXME: This is needed because `register` is not an async function, so
* `E(x).register` needs to reconstruct its return value as a Promise, which
* loses the function's genericity. If `register` was async, we could use its
* type directly, and it would remain generic.
*
* @template {import('@agoric/internal').BridgeIdValue} BridgeId
* @param {ERef<import('./types.js').BridgeManager>} bridgeManager
* @param {BridgeId} bridgeIdValue
* @param {import('@agoric/internal').Remote<
* import('./types.js').BridgeHandler
* >} [handler]
* @returns {Promise<import('./types.js').ScopedBridgeManager<BridgeId>>}
*/
export const makeScopedBridge = (bridgeManager, bridgeIdValue, handler) =>
/** @type {Promise<import('./types.js').ScopedBridgeManager<BridgeId>>} */ (
E(bridgeManager).register(bridgeIdValue, handler)
);

export const BridgeHandlerI = M.interface('BridgeHandler', {
fromBridge: M.call(M.any()).returns(M.promise()),
});
Expand Down
15 changes: 8 additions & 7 deletions packages/vats/src/core/basic-behaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { Fail, NonNullish } from '@agoric/assert';
import { makeNameHubKit } from '../nameHub.js';
import { PowerFlags } from '../walletFlags.js';
import { feeIssuerConfig, makeMyAddressNameAdminKit } from './utils.js';
import { makeScopedBridge } from '../bridge.js';

/** @import {GovernableStartFn, GovernanceFacetKit} from '@agoric/governance/src/types.js'; */

Expand Down Expand Up @@ -52,7 +53,12 @@ const bootMsgEx = {
/** @typedef {MapStore<string, CreateVatResults>} VatStore */

/**
* @param {BootstrapPowers & {}} powers
* @param {BootstrapPowers & {
* produce: {
* loadVat: Producer<VatLoader>;
* loadCriticalVat: Producer<VatLoader>;
* };
* }} powers
* @import {CreateVatResults} from '@agoric/swingset-vat'
* as from createVatByName
*/
Expand All @@ -65,7 +71,6 @@ export const makeVatsFromBundles = async ({
// NOTE: we rely on multiple createVatAdminService calls
// to return cooperating services.
const svc = E(vats.vatAdmin).createVatAdminService(devices.vatAdmin);
// @ts-expect-error XXX
vatAdminSvc.resolve(svc);

const durableStore = await vatStore;
Expand All @@ -80,7 +85,6 @@ export const makeVatsFromBundles = async ({
if (bundleName) {
console.info(`createVatByName(${bundleName})`);
/** @type {Promise<CreateVatResults>} */
// @ts-expect-error XXX
const vatInfo = E(svc).createVatByName(bundleName, {
...defaultVatCreationOptions,
name: vatName,
Expand All @@ -91,7 +95,6 @@ export const makeVatsFromBundles = async ({
assert(bundleID);
const bcap = await E(svc).getBundleCap(bundleID);
/** @type {Promise<CreateVatResults>} */
// @ts-expect-error XXX
const vatInfo = E(svc).createVat(bcap, {
...defaultVatCreationOptions,
name: vatName,
Expand Down Expand Up @@ -647,10 +650,8 @@ export const addBankAssets = async ({
const assetAdmin = E(agoricNamesAdmin).lookupAdmin('vbankAsset');

const bridgeManager = await bridgeManagerP;
/** @type {import('../types.js').ScopedBridgeManager<'bank'> | undefined} */
// @ts-expect-error XXX EProxy
const bankBridgeManager =
bridgeManager && E(bridgeManager).register(BridgeId.BANK);
bridgeManager && makeScopedBridge(bridgeManager, BridgeId.BANK);
const bankMgr = await E(E(loadCriticalVat)('bank')).makeBankManager(
bankBridgeManager,
assetAdmin,
Expand Down
24 changes: 12 additions & 12 deletions packages/vats/src/core/chain-behaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { makePromiseKit } from '@endo/promise-kit';
import { PowerFlags } from '../walletFlags.js';
import { BASIC_BOOTSTRAP_PERMITS } from './basic-behaviors.js';
import { agoricNamesReserved, callProperties, extractPowers } from './utils.js';
import { makeScopedBridge } from '../bridge.js';

const { Fail } = assert;
const { keys } = Object;
Expand Down Expand Up @@ -104,7 +105,7 @@ export const bridgeCoreEval = async allPowers => {
// Not running with a bridge.
return;
}
await E(bridgeManager).register(BRIDGE_ID.CORE, handler);
await makeScopedBridge(bridgeManager, BRIDGE_ID.CORE, handler);
};
harden(bridgeCoreEval);

Expand Down Expand Up @@ -327,15 +328,14 @@ export const makeBridgeManager = async ({
const bridgeManager = E(vat).provideManagerForBridge(bridge);
bridgeManagerP.resolve(bridgeManager);
provisionBridgeManager.resolve(
// @ts-expect-error XXX EProxy
E(bridgeManager).register(BRIDGE_ID.PROVISION),
makeScopedBridge(bridgeManager, BRIDGE_ID.PROVISION),
);
provisionWalletBridgeManager.resolve(
// @ts-expect-error XXX EProxy
E(bridgeManager).register(BRIDGE_ID.PROVISION_SMART_WALLET),
makeScopedBridge(bridgeManager, BRIDGE_ID.PROVISION_SMART_WALLET),
);
walletBridgeManager.resolve(
makeScopedBridge(bridgeManager, BRIDGE_ID.WALLET),
);
// @ts-expect-error XXX EProxy
walletBridgeManager.resolve(E(bridgeManager).register(BRIDGE_ID.WALLET));
};
harden(makeBridgeManager);

Expand All @@ -353,14 +353,14 @@ export const makeChainStorage = async ({
if (!bridgeManager) {
console.warn('Cannot support chainStorage without an actual chain.');
chainStorageP.resolve(null);
// @ts-expect-error expects value or undefined
storageBridgeManagerP.resolve(null);
storageBridgeManagerP.resolve(undefined);
return;
}

/** @type {import('../types.js').ScopedBridgeManager<'storage'>} */
// @ts-expect-error XXX EProxy
const storageBridgeManager = E(bridgeManager).register(BRIDGE_ID.STORAGE);
const storageBridgeManager = makeScopedBridge(
bridgeManager,
BRIDGE_ID.STORAGE,
);
storageBridgeManagerP.resolve(storageBridgeManager);

const vat = E(loadCriticalVat)('bridge');
Expand Down
2 changes: 1 addition & 1 deletion packages/vats/src/core/demoIssuers.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ const mintRunPayment = async (

/**
* @param {string} name
* @param {MintsVat} mints
* @param {ERef<MintsVat>} mints
*/
const provideCoin = async (name, mints) => {
return E(mints).provideIssuerKit(name, AssetKind.NAT, {
Expand Down
32 changes: 20 additions & 12 deletions packages/vats/src/core/types-ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ type Producer<T> = {
};

type VatSourceRef = { bundleName?: string; bundleID?: string };
type VatLoader = <K extends keyof WellKnownVats>(
name: K,
type VatLoader<Names extends string = keyof WellKnownVats> = <N extends Names>(
name: N,
sourceRef?: VatSourceRef,
) => WellKnownVats[K];
) => Promise<Awaited<WellKnownVats[N]>>;

/** callback to assign a property onto the `home` object of the client */
type PropertyMaker = (addr: string, flags: string[]) => Record<string, unknown>;
Expand Down Expand Up @@ -437,14 +437,22 @@ type BootstrapSpace = WellKnownSpaces &
PromiseSpaceOf<
ChainBootstrapSpaceT & {
vatAdminSvc: VatAdminSvc;
} & {
},
{
loadVat: VatLoader;
loadCriticalVat: VatLoader;
},
{},
{}
>;

type LocalChainVat = ERef<
ReturnType<typeof import('../vat-localchain.js').buildRootObject>
>;

type TransferVat = ERef<
ReturnType<typeof import('../vat-transfer.js').buildRootObject>
>;

type ProvisioningVat = ERef<
ReturnType<typeof import('../vat-provisioning.js').buildRootObject>
>;
Expand All @@ -468,6 +476,9 @@ type NamedVatPowers = {
}>;
};

type OrchestrationVat = ERef<import('@agoric/orchestration').OrchestrationVat>;
type ZoeVat = ERef<import('../vat-zoe.js').ZoeVat>;

type RemoteIssuerKit = {
mint: ERef<Mint>;
issuer: ERef<Issuer>;
Expand Down Expand Up @@ -515,16 +526,13 @@ type WellKnownVats = SwingsetVats & {
bank: BankVat;
board: BoardVat;
bridge: ChainStorageVat;
localchain: LocalChainVat;
ibc: IBCVat;
localchain: LocalChainVat;
mints: MintsVat;
network: NetworkVat;
orchestration: ERef<
ReturnType<
typeof import('@agoric/orchestration/src/vat-orchestration.js').buildRootObject
>
>;
orchestration: OrchestrationVat;
priceAuthority: PriceAuthorityVat;
provisioning: ProvisioningVat;
zoe: ERef<ReturnType<typeof import('../vat-zoe.js').buildRootObject>>;
transfer: TransferVat;
zoe: ZoeVat;
};
1 change: 0 additions & 1 deletion packages/vats/src/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,6 @@ export const makeVatSpace = (
{
get: (_target, name, _rx) => {
assert.typeof(name, 'string');
// @ts-expect-error XXX
return provideAsync(name, createVatByName).then(vat => {
if (!durableStore.has(name)) {
durableStore.init(name, vat);
Expand Down
8 changes: 5 additions & 3 deletions packages/vats/src/proposals/localchain-proposal.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @ts-check
import { E } from '@endo/far';
import { BridgeId as BRIDGE_ID } from '@agoric/internal';
import { makeScopedBridge } from '../bridge.js';

/**
* @param {BootstrapPowers & {
Expand Down Expand Up @@ -57,9 +58,10 @@ export const setupLocalChainVat = async (
/** @type {import('../types').ScopedBridgeManager<'vlocalchain'>} */
let scopedManager;
try {
/** @type {import('../types.js').ScopedBridgeManager<'vlocalchain'>} */
// @ts-expect-error XXX EProxy
scopedManager = await E(bridgeManager).register(BRIDGE_ID.VLOCALCHAIN);
scopedManager = await makeScopedBridge(
bridgeManager,
BRIDGE_ID.VLOCALCHAIN,
);
localchainBridgeManager.reset();
localchainBridgeManager.resolve(scopedManager);
} catch (e) {
Expand Down
Loading

0 comments on commit 777eb21

Please sign in to comment.