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 18, 2024
1 parent 7c04012 commit cffe478
Show file tree
Hide file tree
Showing 21 changed files with 85 additions and 66 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
9 changes: 6 additions & 3 deletions packages/swingset-liveslots/src/vatDataTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ type OmitFirstArg<F> = F extends (x: any, ...args: infer P) => infer R
? (...args: P) => R
: never;

export type KindFacet<O> = RemotableObject & {
[K in keyof O]: OmitFirstArg<O[K]>; // omit the 'context' parameter
};
export type KindFacet<O> = RemotableObject &
import('@endo/eventual-send').RemotableBrand<{
[K in keyof O]: OmitFirstArg<O[K]>; // omit the 'context' parameter
}> & {
[K in keyof O]: OmitFirstArg<O[K]>; // omit the 'context' parameter
};

export type KindFacets<B> = {
[FacetKey in keyof B]: KindFacet<B[FacetKey]>;
Expand Down
4 changes: 0 additions & 4 deletions packages/vat-data/src/exo-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ export const makeExoUtils = VatData => {
* @returns {(...args: Parameters<I>) => import('@endo/exo').Guarded<T>}
*/
const defineVirtualExoClass = (tag, interfaceGuard, init, methods, options) =>
// @ts-expect-error cast
defineKind(tag, init, methods, {
...options,
thisfulMethods: true,
Expand Down Expand Up @@ -138,7 +137,6 @@ export const makeExoUtils = VatData => {
facets,
options,
) =>
// @ts-expect-error cast
defineKindMulti(tag, init, facets, {
...options,
thisfulMethods: true,
Expand Down Expand Up @@ -169,7 +167,6 @@ export const makeExoUtils = VatData => {
methods,
options,
) =>
// @ts-expect-error cast
defineDurableKind(kindHandle, init, methods, {
...options,
thisfulMethods: true,
Expand Down Expand Up @@ -200,7 +197,6 @@ export const makeExoUtils = VatData => {
facets,
options,
) =>
// @ts-expect-error cast
defineDurableKindMulti(kindHandle, init, facets, {
...options,
thisfulMethods: true,
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
10 changes: 3 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 @@ -65,7 +66,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 +80,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 +90,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 @@ -368,7 +366,7 @@ export const startPriceAuthorityRegistry = async ({
vats.priceAuthority,
).getRegistry();

produce.priceAuthorityVat.resolve(vats.priceAuthority);
produce.priceAuthorityVat.resolve(await vats.priceAuthority);
produce.priceAuthority.resolve(priceAuthority);
produce.priceAuthorityAdmin.resolve(adminFacet);

Expand Down Expand Up @@ -647,10 +645,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
13 changes: 11 additions & 2 deletions packages/vats/src/core/types-ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ type ClientProvider = {
};

type Producer<T> = {
resolve: (v: ERef<T>) => void;
resolve: (v: ERef<T | import('@endo/exo').Guarded<T>>) => void;
reject: (r: unknown) => void;
reset: (reason?: unknown) => void;
};
Expand All @@ -112,7 +112,7 @@ type VatSourceRef = { bundleName?: string; bundleID?: string };
type VatLoader = <K extends keyof WellKnownVats>(
name: K,
sourceRef?: VatSourceRef,
) => WellKnownVats[K];
) => Promise<Awaited<WellKnownVats[K]>>;

/** 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 @@ -445,6 +445,14 @@ type BootstrapSpace = WellKnownSpaces &
{}
>;

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 Down Expand Up @@ -526,5 +534,6 @@ type WellKnownVats = SwingsetVats & {
>;
priceAuthority: PriceAuthorityVat;
provisioning: ProvisioningVat;
transfer: TransferVat;
zoe: ERef<ReturnType<typeof import('../vat-zoe.js').buildRootObject>>;
};
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
9 changes: 2 additions & 7 deletions packages/vats/src/proposals/network-proposal.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { makeScalarBigMapStore } from '@agoric/vat-data';
// Heap-based vow resolution is used for this module because the
// bootstrap vat can't yet be upgraded.
import { when } from '@agoric/vow/vat.js';
import { makeScopedBridge } from '../bridge.js';

/**
* @import {ProtocolHandler} from '@agoric/network';
Expand Down Expand Up @@ -81,10 +82,6 @@ export const registerNetworkProtocols = async (vats, dibcBridgeManager) => {
* }} powers
* @param {object} options
* @param {{ networkRef: VatSourceRef; ibcRef: VatSourceRef }} options.options
* // TODO: why doesn't overloading VatLoader work???
*
* @typedef {((name: 'network') => NetworkVat) & ((name: 'ibc') => IBCVat)} VatLoader2
*
*
* @typedef {{
* network: ERef<NetworkVat>;
Expand Down Expand Up @@ -130,10 +127,8 @@ export const setupNetworkProtocols = async (
const allocator = await portAllocatorP;

const bridgeManager = await bridgeManagerP;
/** @type {import('../types.js').ScopedBridgeManager<'dibc'> | undefined} */
// @ts-expect-error XXX EProxy
const dibcBridgeManager =
bridgeManager && E(bridgeManager).register(BRIDGE_ID.DIBC);
bridgeManager && makeScopedBridge(bridgeManager, BRIDGE_ID.DIBC);

// The Interchain Account (ICA) Controller must be bound to a port that starts
// with 'icacontroller', so we provide one such port to each client.
Expand Down
Loading

0 comments on commit cffe478

Please sign in to comment.