From da1314a2e032d0975c3466202c5b0a9b31c7e933 Mon Sep 17 00:00:00 2001
From: Turadg Aleahmad <turadg@agoric.com>
Date: Thu, 23 May 2024 13:33:42 -0700
Subject: [PATCH] test: makeFakeBankManagerKit

---
 .../inter-protocol/test/provisionPool.test.js | 16 ++++-----
 .../test/examples/swapExample.test.ts         |  9 ++---
 .../test/examples/unbondExample.test.ts       |  7 ++--
 packages/smart-wallet/test/addAsset.test.js   | 10 ++----
 ...bootstrap-walletFactory-service-upgrade.js |  8 +++--
 packages/vats/test/localchain.test.js         | 13 ++-----
 packages/vats/tools/bank-utils.js             | 35 ++++++++++++++++++-
 packages/vats/tools/fake-bridge.js            |  4 +--
 8 files changed, 56 insertions(+), 46 deletions(-)

diff --git a/packages/inter-protocol/test/provisionPool.test.js b/packages/inter-protocol/test/provisionPool.test.js
index c907e3527ed0..7f2d37986092 100644
--- a/packages/inter-protocol/test/provisionPool.test.js
+++ b/packages/inter-protocol/test/provisionPool.test.js
@@ -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,
@@ -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);
diff --git a/packages/orchestration/test/examples/swapExample.test.ts b/packages/orchestration/test/examples/swapExample.test.ts
index e4b826f2728e..49282636069d 100644
--- a/packages/orchestration/test/examples/swapExample.test.ts
+++ b/packages/orchestration/test/examples/swapExample.test.ts
@@ -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);
@@ -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);
 
diff --git a/packages/orchestration/test/examples/unbondExample.test.ts b/packages/orchestration/test/examples/unbondExample.test.ts
index eebe0bcb9306..6af004538346 100644
--- a/packages/orchestration/test/examples/unbondExample.test.ts
+++ b/packages/orchestration/test/examples/unbondExample.test.ts
@@ -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);
@@ -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);
 
diff --git a/packages/smart-wallet/test/addAsset.test.js b/packages/smart-wallet/test/addAsset.test.js
index 0ba36801ba45..48018096a94c 100644
--- a/packages/smart-wallet/test/addAsset.test.js
+++ b/packages/smart-wallet/test/addAsset.test.js
@@ -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';
@@ -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();
diff --git a/packages/smart-wallet/test/swingsetTests/upgradeWalletFactory/bootstrap-walletFactory-service-upgrade.js b/packages/smart-wallet/test/swingsetTests/upgradeWalletFactory/bootstrap-walletFactory-service-upgrade.js
index 127a465cdbe4..9a556dd926e3 100644
--- a/packages/smart-wallet/test/swingsetTests/upgradeWalletFactory/bootstrap-walletFactory-service-upgrade.js
+++ b/packages/smart-wallet/test/swingsetTests/upgradeWalletFactory/bootstrap-walletFactory-service-upgrade.js
@@ -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';
@@ -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`;
@@ -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;
@@ -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),
diff --git a/packages/vats/test/localchain.test.js b/packages/vats/test/localchain.test.js
index b960aac08cde..630642a792d4 100644
--- a/packages/vats/test/localchain.test.js
+++ b/packages/vats/test/localchain.test.js
@@ -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';
@@ -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 => {
diff --git a/packages/vats/tools/bank-utils.js b/packages/vats/tools/bank-utils.js
index 57b1d1334170..eff6af2fbdee 100644
--- a/packages/vats/tools/bank-utils.js
+++ b/packages/vats/tools/bank-utils.js
@@ -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 => {
@@ -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 };
+};
diff --git a/packages/vats/tools/fake-bridge.js b/packages/vats/tools/fake-bridge.js
index adec1e28a8cb..df32d43d8d5f 100644
--- a/packages/vats/tools/fake-bridge.js
+++ b/packages/vats/tools/fake-bridge.js
@@ -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';
 
@@ -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;