Skip to content

Commit

Permalink
refactor(chainStorage): getChildNode helper
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed Jul 5, 2022
1 parent 18618f3 commit 9a0dd4a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 25 deletions.
17 changes: 5 additions & 12 deletions packages/run-protocol/src/proposals/econ-behaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { makeRunStakeTerms } from '../runStake/params.js';
import { liquidationDetailTerms } from '../vaultFactory/liquidation.js';
import { makeStakeReporter } from '../my-lien.js';
import { makeTracer } from '../makeTracer.js';
import { getChildNode } from '@agoric/vats/src/lib-chainStorage.js';

const trace = makeTracer('RunEconBehaviors', false);

Expand Down Expand Up @@ -216,9 +217,7 @@ export const setupAmm = async (
AmountMath.make(runBrand, minInitialPoolLiquidity),
);

const chainStoragePresence = await chainStorage;
const storageNode = await (chainStoragePresence &&
E(chainStoragePresence).getChildNode(AMM_STORAGE_PATH));
const storageNode = await getChildNode(chainStorage, AMM_STORAGE_PATH);
const marshaller = await E(board).getPublishingMarshaller();

const ammGovernorTerms = {
Expand Down Expand Up @@ -312,9 +311,7 @@ export const setupReserve = async ({

const feeMintAccess = await feeMintAccessP;

const chainStoragePresence = await chainStorage;
const storageNode = await (chainStoragePresence &&
E(chainStoragePresence).getChildNode(STORAGE_PATH));
const storageNode = await getChildNode(chainStorage, STORAGE_PATH);
const marshaller = E(board).getReadonlyMarshaller();

const reserveGovernorTerms = {
Expand Down Expand Up @@ -444,9 +441,7 @@ export const startVaultFactory = async (
const reservePublicFacet = await E(zoe).getPublicFacet(reserveInstance);
const timer = await chainTimerService;

const chainStoragePresence = await chainStorage;
const storageNode = await (chainStoragePresence &&
E(chainStoragePresence).getChildNode(STORAGE_PATH));
const storageNode = await getChildNode(chainStorage, STORAGE_PATH);
const marshaller = E(board).getReadonlyMarshaller();

const vaultFactoryTerms = makeGovernedTerms(
Expand Down Expand Up @@ -851,9 +846,7 @@ export const startRunStake = async (
},
);

const chainStoragePresence = await chainStorage;
const storageNode = await (chainStoragePresence &&
E(chainStoragePresence).getChildNode(STORAGE_PATH));
const storageNode = await getChildNode(chainStorage, STORAGE_PATH);
const marshaller = await E(board).getReadonlyMarshaller();

/** @type {{ publicFacet: GovernorPublic, creatorFacet: GovernedContractFacetAccess<unknown>}} */
Expand Down
5 changes: 2 additions & 3 deletions packages/run-protocol/src/proposals/startPSM.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { AmountMath, AssetKind } from '@agoric/ertp';
import { CONTRACT_ELECTORATE, ParamTypes } from '@agoric/governance';
import { makeRatio } from '@agoric/zoe/src/contractSupport/index.js';
import { reserveThenGetNamePaths } from './utils.js';
import { getChildNode } from '@agoric/vats/src/lib-chainStorage.js';

const BASIS_POINTS = 10000n;
const { details: X } = assert;
Expand Down Expand Up @@ -107,9 +108,7 @@ export const startPSM = async (
},
};

const chainStoragePresence = await chainStorage;
const storageNode = await (chainStoragePresence &&
E(chainStoragePresence).getChildNode('psm'));
const storageNode = await getChildNode(chainStorage, 'psm');
const marshaller = E(board).getPublishingMarshaller();

const governorFacets = await E(zoe).startInstance(
Expand Down
7 changes: 3 additions & 4 deletions packages/vats/src/core/basic-behaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { provide } from '@agoric/store/src/stores/store-utils.js';
import { makeNameHubKit } from '../nameHub.js';

import { feeIssuerConfig } from './utils.js';
import { getChildNode } from '../lib-chainStorage.js';

const { details: X } = assert;

Expand Down Expand Up @@ -243,9 +244,7 @@ export const makeClientBanks = async ({
consume: { smartWallet },
},
}) => {
const chainStoragePresence = await chainStorage;
const walletChainStorage = await (chainStoragePresence &&
E(chainStoragePresence).getChildNode(WALLET_STORAGE_PATH));
const storageNode = await getChildNode(chainStorage, WALLET_STORAGE_PATH);
const marshaller = E(board).getPublishingMarshaller();
return E(client).assignBundle([
address => {
Expand All @@ -255,7 +254,7 @@ export const makeClientBanks = async ({
smartWallet,
{},
{ agoricNames, bank, namesByAddress, myAddressNameAdmin, board },
{ storageNode: walletChainStorage, marshaller },
{ storageNode, marshaller },
);

// sets these values in REPL home by way of registerWallet
Expand Down
15 changes: 14 additions & 1 deletion packages/vats/src/lib-chainStorage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-check

import { Far } from '@endo/far';
import { E, Far } from '@endo/far';

const { details: X } = assert;

Expand Down Expand Up @@ -59,3 +59,16 @@ export function makeChainStorageRoot(toStorage, storeName, rootPath) {
const rootNode = makeChainStorageNode(rootPath);
return rootNode;
}

/**
*
* @param {ERef<ChainStorageNode | undefined>} chainStorage
* @param {string} childName
*/
export async function getChildNode(chainStorage, childName) {
const chainStoragePresence = await chainStorage;
const storageNode = await (chainStoragePresence &&
E(chainStoragePresence).getChildNode(childName));
return storageNode;
}
harden(getChildNode);
11 changes: 6 additions & 5 deletions packages/wallet/contract/test/test-contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ import {
makeAgoricNamesAccess,
makePromiseSpace,
} from '@agoric/vats/src/core/utils.js';
import { makeChainStorageRoot } from '@agoric/vats/src/lib-chainStorage.js';
import {
getChildNode,
makeChainStorageRoot,
} from '@agoric/vats/src/lib-chainStorage.js';
import { makeNameHubKit } from '@agoric/vats/src/nameHub.js';
import { buildRootObject as boardRoot } from '@agoric/vats/src/vat-board.js';
import { buildRootObject as mintsRoot } from '@agoric/vats/src/vat-mints.js';
Expand Down Expand Up @@ -155,9 +158,7 @@ const makeTestContext = async t => {
);

// copied from makeClientBanks()
const chainStoragePresence = await consume.chainStorage;
const walletChainStorage = await (chainStoragePresence &&
E(chainStoragePresence).getChildNode('wallet'));
const storageNode = await getChildNode(consume.chainStorage, 'wallet');
const marshaller = E(consume.board).getPublishingMarshaller();

// FIXME not exactly...
Expand All @@ -172,7 +173,7 @@ const makeTestContext = async t => {
myAddressNameAdmin,
board: consume.board,
},
{ storageNode: walletChainStorage, marshaller },
{ storageNode, marshaller },
);

return {
Expand Down

0 comments on commit 9a0dd4a

Please sign in to comment.