Skip to content

Commit

Permalink
refactor: move allValues, zip to internal
Browse files Browse the repository at this point in the history
 - prune collect.js
  • Loading branch information
dckc committed Feb 24, 2023
1 parent 7cba1e3 commit 97b8b57
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 33 deletions.
11 changes: 0 additions & 11 deletions packages/inter-protocol/src/collect.js

This file was deleted.

5 changes: 2 additions & 3 deletions packages/inter-protocol/src/proposals/demoIssuers.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
/* eslint-disable @jessie.js/no-nested-await -- demo file */
import { AmountMath, AssetKind } from '@agoric/ertp';
import { objectMap } from '@agoric/internal';
import { allValues, objectMap } from '@agoric/internal';
import {
makeRatio,
natSafeMath,
} from '@agoric/zoe/src/contractSupport/index.js';
import { E, Far } from '@endo/far';
import { Stake, Stable } from '@agoric/vats/src/tokens.js';
import { Nat } from '@endo/nat';
import * as Collect from '../collect.js';

const { Fail, quote: q } = assert;
const { multiply, floorDivide } = natSafeMath;
Expand Down Expand Up @@ -491,7 +490,7 @@ export const fundAMM = async ({

const vats = { mints, priceAuthority: priceAuthorityVat };

const kits = await Collect.allValues(
const kits = await allValues(
objectMap(
fromEntries([Stable.symbol, ...keys(AMMDemoState)].map(n => [n, n])),
async issuerName => {
Expand Down
5 changes: 2 additions & 3 deletions packages/inter-protocol/test/amm/vpool-xyk-amm/setup.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { makeLoopback } from '@endo/captp';
import { E } from '@endo/eventual-send';

import { makeTracer } from '@agoric/internal';
import { allValues, makeTracer } from '@agoric/internal';
import {
makeAgoricNamesAccess,
makePromiseSpace,
Expand All @@ -12,7 +12,6 @@ import { makeZoeKit } from '@agoric/zoe';
import { makeFakeVatAdmin } from '@agoric/zoe/tools/fakeVatAdmin.js';
import buildManualTimer from '@agoric/zoe/tools/manualTimer.js';

import * as Collect from '../../../src/collect.js';
import {
setupAmm,
setupReserve,
Expand Down Expand Up @@ -130,7 +129,7 @@ export const setupAmmServices = async (
]);
await setupReserve(space);

const installs = await Collect.allValues({
const installs = await allValues({
amm: installation.consume.amm,
governor: installation.consume.contractGovernor,
electorate: installation.consume.committee,
Expand Down
2 changes: 1 addition & 1 deletion packages/inter-protocol/test/psm/setupPsm.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { makeScalarMapStore } from '@agoric/vat-data';
import { makeZoeKit } from '@agoric/zoe';
import { makeFakeVatAdmin } from '@agoric/zoe/tools/fakeVatAdmin.js';
import buildManualTimer from '@agoric/zoe/tools/manualTimer.js';
import { allValues } from '@agoric/internal';
import { makeMockChainStorageRoot } from '@agoric/internal/src/storage-test-utils.js';
import { makeIssuerKit } from '@agoric/ertp';

Expand All @@ -21,7 +22,6 @@ import {
} from '../supports.js';
import { startEconomicCommittee } from '../../src/proposals/startEconCommittee.js';
import { startPSM, startEconCharter } from '../../src/proposals/startPSM.js';
import { allValues } from '../../src/collect.js';

const psmRoot = './src/psm/psm.js'; // package relative
const charterRoot = './src/econCommitteeCharter.js'; // package relative
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { eventLoopIteration } from '@agoric/zoe/tools/eventLoopIteration.js';
import buildManualTimer from '@agoric/zoe/tools/manualTimer.js';
import { E } from '@endo/far';
import { zip } from '../../src/collect.js';
import { zip } from '@agoric/internal';
import { INVITATION_MAKERS_DESC as EC_INVITATION_MAKERS_DESC } from '../../src/econCommitteeCharter.js';
import { INVITATION_MAKERS_DESC as ORACLE_INVITATION_MAKERS_DESC } from '../../src/price/fluxAggregator.js';
import { ensureOracleBrands } from '../../src/proposals/price-feed-proposal.js';
Expand Down
5 changes: 2 additions & 3 deletions packages/inter-protocol/test/vaultFactory/driver.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import '@agoric/zoe/exported.js';

import { AmountMath, AssetKind, makeIssuerKit } from '@agoric/ertp';
import { makeTracer, objectMap } from '@agoric/internal';
import { allValues, makeTracer, objectMap } from '@agoric/internal';
import { makeNotifierFromSubscriber } from '@agoric/notifier';
import { unsafeMakeBundleCache } from '@agoric/swingset-vat/tools/bundleTool.js';
import {
Expand All @@ -13,7 +13,6 @@ import buildManualTimer from '@agoric/zoe/tools/manualTimer.js';
import { E } from '@endo/eventual-send';
import { deeplyFulfilled } from '@endo/marshal';

import * as Collect from '../../src/collect.js';
import {
setupAmm,
setupReserve,
Expand Down Expand Up @@ -105,7 +104,7 @@ export const makeDriverContext = async () => {

// note that the liquidation might be a different bundle name
// objectMap(contractRoots, (root, k) => loader.load(root, k)),
const bundles = await Collect.allValues({
const bundles = await allValues({
faucet: bundleCache.load(contractRoots.faucet, 'faucet'),
liquidate: bundleCache.load(
contractRoots.liquidate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { test as unknownTest } from '@agoric/zoe/tools/prepare-test-env-ava.js';

import { AmountMath, AssetKind, makeIssuerKit } from '@agoric/ertp';
import { makeParamManagerBuilder } from '@agoric/governance';
import { makeTracer, objectMap } from '@agoric/internal';
import { allValues, makeTracer, objectMap } from '@agoric/internal';
import {
makeNotifierFromAsyncIterable,
makeNotifierFromSubscriber,
Expand All @@ -22,7 +22,6 @@ import buildManualTimer from '@agoric/zoe/tools/manualTimer.js';
import { makeScriptedPriceAuthority } from '@agoric/zoe/tools/scriptedPriceAuthority.js';
import { E } from '@endo/eventual-send';
import { deeplyFulfilled } from '@endo/marshal';
import * as Collect from '../../src/collect.js';
import { calculateCurrentDebt } from '../../src/interest-math.js';
import { SECONDS_PER_YEAR } from '../../src/interest.js';
import {
Expand Down Expand Up @@ -115,7 +114,7 @@ test.before(async t => {

const bundleCache = await unsafeMakeBundleCache('./bundles/'); // package-relative
// note that the liquidation might be a different bundle name
const bundles = await Collect.allValues({
const bundles = await allValues({
faucet: bundleCache.load(contractRoots.faucet, 'faucet'),
liquidate: bundleCache.load(contractRoots.liquidate, 'liquidateMinimum'),
VaultFactory: bundleCache.load(contractRoots.VaultFactory, 'VaultFactory'),
Expand Down
20 changes: 16 additions & 4 deletions packages/internal/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import { deeplyFulfilled, isObject } from '@endo/marshal';
import { isPromise } from '@endo/promise-kit';
import { asyncGenerate, makeSet } from 'jessie.js';

const { entries, fromEntries } = Object;
const { entries, fromEntries, keys, values } = Object;
const { ownKeys } = Reflect;

const { details: X, quote: q, Fail } = assert;

/** @template T @typedef {import('@endo/eventual-send').ERef<T>} ERef<T> */

/**
* Throws if multiple entries use the same property name. Otherwise acts
* like `Object.fromEntries` but hardens the result.
Expand Down Expand Up @@ -214,11 +216,11 @@ export const makeAggregateError = (errors, message) => {

/**
* @template T
* @param {readonly (T | PromiseLike<T>)[]} values
* @param {readonly (T | PromiseLike<T>)[]} items
* @returns {Promise<T[]>}
*/
export const PromiseAllOrErrors = async values => {
return Promise.allSettled(values).then(results => {
export const PromiseAllOrErrors = async items => {
return Promise.allSettled(items).then(results => {
const errors = /** @type {PromiseRejectedResult[]} */ (
results.filter(({ status }) => status === 'rejected')
).map(result => result.reason);
Expand Down Expand Up @@ -328,3 +330,13 @@ export const untilTrue = produce =>
value,
});
});

/** @type { <X, Y>(xs: X[], ys: Y[]) => [X, Y][]} */
export const zip = (xs, ys) => harden(xs.map((x, i) => [x, ys[+i]]));

/** @type { <T extends Record<string, ERef<any>>>(obj: T) => Promise<{ [K in keyof T]: Awaited<T[K]>}> } */
export const allValues = async obj => {
const resolved = await Promise.all(values(obj));
// @ts-expect-error cast
return harden(fromEntries(zip(keys(obj), resolved)));
};
6 changes: 2 additions & 4 deletions packages/vats/src/core/chain-behaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ import {
makeNonceMaker,
} from '@agoric/swingset-vat/src/vats/network/index.js';
import { importBundle } from '@endo/import-bundle';
// XXX use Collect stuff from internal
import * as Collect from '@agoric/inter-protocol/src/collect.js';
import { BridgeId as BRIDGE_ID } from '@agoric/internal';
import { allValues, BridgeId as BRIDGE_ID } from '@agoric/internal';
import * as STORAGE_PATH from '@agoric/internal/src/chain-storage-paths.js';

import { agoricNamesReserved, callProperties, extractPowers } from './utils.js';
Expand Down Expand Up @@ -278,7 +276,7 @@ export const setupClientManager = async (
/** @type {ClientFacet} */
const clientFacet = Far('chainProvisioner', {
getChainBundle: () =>
bundleReady.promise.then(_ => Collect.allValues(clientHome)),
bundleReady.promise.then(_ => allValues(clientHome)),
getConfiguration: () => notifier,
});

Expand Down

0 comments on commit 97b8b57

Please sign in to comment.