Skip to content

Commit

Permalink
test: makeFakeBankManagerKit
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed May 23, 2024
1 parent 3448cd8 commit da1314a
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 46 deletions.
16 changes: 6 additions & 10 deletions packages/inter-protocol/test/provisionPool.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ import { CONTRACT_ELECTORATE, ParamTypes } from '@agoric/governance';
import committeeBundle from '@agoric/governance/bundles/bundle-committee.js';
import { WalletName } from '@agoric/internal';
import { eventLoopIteration } from '@agoric/internal/src/testing-utils.js';
import { publishDepositFacet } from '@agoric/smart-wallet/src/walletFactory.js';
import { unsafeMakeBundleCache } from '@agoric/swingset-vat/tools/bundleTool.js';
import { makeScalarBigMapStore } from '@agoric/vat-data';
import centralSupplyBundle from '@agoric/vats/bundles/bundle-centralSupply.js';
import { makeNameHubKit } from '@agoric/vats/src/nameHub.js';
import { buildRootObject as buildBankRoot } from '@agoric/vats/src/vat-bank.js';
import { PowerFlags } from '@agoric/vats/src/walletFlags.js';
import { makeFakeBankKit } from '@agoric/vats/tools/bank-utils.js';
import {
makeFakeBankKit,
makeFakeBankManagerKit,
} from '@agoric/vats/tools/bank-utils.js';
import { makeFakeBoard } from '@agoric/vats/tools/board-utils.js';
import { makeRatio } from '@agoric/zoe/src/contractSupport/ratio.js';
import { E, Far } from '@endo/far';
import path from 'path';
import { publishDepositFacet } from '@agoric/smart-wallet/src/walletFactory.js';
import { makeBridgeProvisionTool } from '../src/provisionPoolKit.js';
import {
makeMockChainStorageRoot,
Expand Down Expand Up @@ -275,12 +276,7 @@ test('provisionPool trades provided assets for IST', async t => {
* @param {string[]} addresses
*/
const makeWalletFactoryKitForAddresses = async addresses => {
const baggage = makeScalarBigMapStore('bank baggage');
const bankManager = await buildBankRoot(
undefined,
undefined,
baggage,
).makeBankManager();
const { bankManager } = await makeFakeBankManagerKit();

const feeKit = makeIssuerKit('FEE');
const fees = withAmountUtils(feeKit);
Expand Down
9 changes: 2 additions & 7 deletions packages/orchestration/test/examples/swapExample.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import path from 'path';
import { makeFakeStorageKit } from '@agoric/internal/src/storage-test-utils.js';
import { makeHeapZone } from '@agoric/zone';
import { prepareLocalChainTools } from '@agoric/vats/src/localchain.js';
import { buildRootObject as buildBankVatRoot } from '@agoric/vats/src/vat-bank.js';
import { withAmountUtils } from '@agoric/zoe/tools/test-utils.js';
import { makeFakeBankBridge } from '@agoric/vats/tools/fake-bridge.js';
import { makeFakeBankManagerKit } from '@agoric/vats/tools/bank-utils.js';
import { makeFakeLocalchainBridge } from '../supports.js';

const dirname = path.dirname(new URL(import.meta.url).pathname);
Expand All @@ -28,11 +27,7 @@ test('start', async t => {
const zone = makeHeapZone();
const { makeLocalChain } = prepareLocalChainTools(zone.subZone('localchain'));

const bankManager = await buildBankVatRoot(
undefined,
undefined,
zone.mapStore('bankManager'),
).makeBankManager(makeFakeBankBridge(zone));
const { bankManager } = await makeFakeBankManagerKit();

await E(bankManager).addAsset('uist', 'IST', 'Inter Stable Token', issuerKit);

Expand Down
7 changes: 2 additions & 5 deletions packages/orchestration/test/examples/unbondExample.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { withAmountUtils } from '@agoric/zoe/tools/test-utils.js';
import { makeHeapZone } from '@agoric/zone';
import { E } from '@endo/far';
import path from 'path';
import { makeFakeBankManagerKit } from '@agoric/vats/tools/bank-utils.js';
import { makeFakeLocalchainBridge } from '../supports.js';

const dirname = path.dirname(new URL(import.meta.url).pathname);
Expand All @@ -26,11 +27,7 @@ test('start', async t => {

const zone = makeHeapZone();
const { makeLocalChain } = prepareLocalChainTools(zone.subZone('localchain'));
const bankManager = await buildBankVatRoot(
undefined,
undefined,
zone.mapStore('bankManager'),
).makeBankManager();
const { bankManager } = await makeFakeBankManagerKit();

await E(bankManager).addAsset('uist', 'IST', 'Inter Stable Token', issuerKit);

Expand Down
10 changes: 3 additions & 7 deletions packages/smart-wallet/test/addAsset.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
/* eslint @typescript-eslint/no-floating-promises: "warn" */
import { test as anyTest } from '@agoric/zoe/tools/prepare-test-env-ava.js';
import { E, Far } from '@endo/far';
import { buildRootObject as buildBankVatRoot } from '@agoric/vats/src/vat-bank.js';
import { AmountMath, makeIssuerKit } from '@agoric/ertp';
import { eventLoopIteration } from '@agoric/internal/src/testing-utils.js';
import { makeCopyBag, makeScalarMapStore } from '@agoric/store';
import { makeCopyBag } from '@agoric/store';
import { makePromiseKit } from '@endo/promise-kit';
import bundleSource from '@endo/bundle-source';
import { makeMarshal } from '@endo/marshal';
import { resolve as importMetaResolve } from 'import-meta-resolve';
import { makeFakeBankManagerKit } from '@agoric/vats/tools/bank-utils.js';
import { makeDefaultTestContext } from './contexts.js';
import { ActionType, headValue, makeMockTestSpace } from './supports.js';
import { makeImportContext } from '../src/marshal-contexts.js';
Expand All @@ -24,11 +24,7 @@ const test = anyTest;

test.before(async t => {
const withBankManager = async () => {
const noBridge = undefined;
const baggage = makeScalarMapStore('baggage');
const bankManager = E(
buildBankVatRoot(undefined, undefined, baggage),
).makeBankManager(noBridge);
const { bankManager } = await makeFakeBankManagerKit();
const noop = () => {};
const space0 = await makeMockTestSpace(noop);
space0.produce.bankManager.reset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { makeTracer } from '@agoric/internal';
import { makeFakeStorageKit } from '@agoric/internal/src/storage-test-utils.js';
import { makeNameHubKit } from '@agoric/vats';
import { makeAgoricNamesAccess } from '@agoric/vats/src/core/utils.js';
import { makeFakeBankManagerKit } from '@agoric/vats/tools/bank-utils.js';
import { makeFakeBoard } from '@agoric/vats/tools/board-utils.js';
import { makeFakeBankKit } from '@agoric/vats/tools/bank-utils.js';
import { E } from '@endo/eventual-send';
import { Far } from '@endo/marshal';
import { makePromiseKit } from '@endo/promise-kit';
Expand All @@ -20,7 +20,7 @@ const walletAddr = 'agoric1whatever';
const moolaKit = makeIssuerKit('moola');

export const buildRootObject = async () => {
const { bank, addAsset } = makeFakeBankKit([]);
const { bankManager } = await makeFakeBankManagerKit();
const storageKit = makeFakeStorageKit('walletFactoryUpgradeTest');
const statusPath = `walletFactoryUpgradeTest.${walletAddr}`;
const currentPath = `${statusPath}.current`;
Expand All @@ -40,6 +40,8 @@ export const buildRootObject = async () => {
/** @type {import('../../../src/smartWallet.js').SmartWallet} */
let wallet;

const bank = await E(bankManager).getBankForAddress(walletAddr);

// for startInstance
/** @type {Installation<import('../../../src/walletFactory.js').start>} */
let installation;
Expand Down Expand Up @@ -104,7 +106,7 @@ export const buildRootObject = async () => {
).storagePath;
currentStoragePath === currentPath || Fail`bad storage path`;

addAsset('umoola', 'moola', 'moola', moolaKit);
await E(bankManager).addAsset('umoola', 'moola', 'moola', moolaKit);
const depositFacet = E(wallet).getDepositFacet();
const payment = moolaKit.mint.mintPayment(
AmountMath.make(moolaKit.brand, 100n),
Expand Down
13 changes: 2 additions & 11 deletions packages/vats/test/localchain.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { E } from '@endo/far';
import { getInterfaceOf } from '@endo/marshal';
import { M } from '@endo/patterns';
import { prepareLocalChainTools } from '../src/localchain.js';
import { buildRootObject as buildBankVatRoot } from '../src/vat-bank.js';
import { makeFakeLocalchainBridge } from '../tools/fake-bridge.js';
import { makeFakeBankManagerKit } from '../tools/bank-utils.js';

/**
* @import {LocalChainAccount, LocalChainPowers} from '../src/localchain.js';
Expand All @@ -32,16 +32,7 @@ const makeTestContext = async _t => {
makeDurableZone(provideBaggage('localchain')),
);

const makeBankManager = () => {
const zone = makeDurableZone(provideBaggage('bank'));
return buildBankVatRoot(
undefined,
undefined,
zone.mapStore('bankManager'),
).makeBankManager();
};

const bankManager = await makeBankManager();
const { bankManager } = await makeFakeBankManagerKit();

/** @param {LocalChainPowers} powers */
const makeLocalChain = async powers => {
Expand Down
35 changes: 34 additions & 1 deletion packages/vats/tools/bank-utils.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { makeSubscriptionKit } from '@agoric/notifier';
import { makeScalarMapStore } from '@agoric/vat-data';
import { makeScalarBigMapStore, makeScalarMapStore } from '@agoric/vat-data';
import { makeDurableZone } from '@agoric/zone/durable.js';
import { E } from '@endo/far';
import { Far } from '@endo/marshal';
import { buildRootObject as buildBankVatRoot } from '../src/vat-bank.js';
import { FAUCET_ADDRESS, makeFakeBankBridge } from './fake-bridge.js';

/**
* @deprecated use makeFakeBankManagerKit
* @param {Pick<IssuerKit<'nat'>, 'brand' | 'issuer'>[]} issuerKits
*/
export const makeFakeBankKit = issuerKits => {
Expand Down Expand Up @@ -57,3 +61,32 @@ export const makeFakeBankKit = issuerKits => {

return { addAsset, assetPublication: publication, bank };
};

/**
* @param {object} [opts]
* @param {import('./fake-bridge.js').Balances} opts.balances initial balances
*/
export const makeFakeBankManagerKit = async opts => {
const baggage = makeScalarBigMapStore('baggage');
const zone = makeDurableZone(baggage);

const bankManager = await buildBankVatRoot(
undefined,
undefined,
zone.mapStore('bankManager'),
).makeBankManager(makeFakeBankBridge(zone, opts));

/**
* Get a payment from the faucet
*
* @param {Amount<'nat'>} amount
* @returns {Promise<Payment<'nat'>>}
*/
const pourPayment = async amount => {
const faucet = await E(bankManager).getBankForAddress(FAUCET_ADDRESS);
const purse = await E(faucet).getPurse(amount.brand);
return E(purse).withdraw(amount);
};

return { bankManager, pourPayment };
};
4 changes: 2 additions & 2 deletions packages/vats/tools/fake-bridge.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Fail } from '@agoric/assert';
import { assert, Fail } from '@agoric/assert';
import { makeTracer, VBankAccount } from '@agoric/internal';
import assert from 'node:assert/strict';
import { E } from '@endo/far';
import { makeWhen } from '@agoric/vow/src/when.js';

Expand All @@ -27,6 +26,7 @@ const INFINITE_AMOUNT = '99999999999';
* @param {object} opts
* @param {Balances} opts.balances initial balances
* @returns {ScopedBridgeManager<'bank'>}
* @see {makeFakeBankManagerKit} and its `pourPayment` for a helper
*/
export const makeFakeBankBridge = (zone, opts = { balances: {} }) => {
const { balances } = opts;
Expand Down

0 comments on commit da1314a

Please sign in to comment.