diff --git a/packages/vats/src/vat-bank.js b/packages/vats/src/vat-bank.js index 8511ef40b2e..765702fe903 100644 --- a/packages/vats/src/vat-bank.js +++ b/packages/vats/src/vat-bank.js @@ -172,12 +172,6 @@ export function buildRootObject(_vatPowers) { /** @type {Store} */ const addressToBank = makeStore('address'); - /** @type {NotifierRecord} */ - const { - notifier: accountsNotifier, - updater: accountsUpdater, - } = makeNotifierKit([...addressToBank.keys()]); - /** * Create a new personal bank interface for a given address. * @@ -257,22 +251,9 @@ export function buildRootObject(_vatPowers) { }, }); addressToBank.init(address, bank); - accountsUpdater.updateState([...addressToBank.keys()]); return bank; }; - const bankDepositFacet = Far('bankDepositFacet', { - getAccountsNotifier() { - return accountsNotifier; - }, - deposit(brand, account, payment) { - // The purse will do the proper verification as part of deposit. - const bank = getBankForAddress(account); - const purse = bank.getPurse(brand); - return E(purse).deposit(payment); - }, - }); - return Far('bankManager', { /** * Returns assets as they are added to the bank. @@ -282,9 +263,38 @@ export function buildRootObject(_vatPowers) { getAssetSubscription() { return harden(assetSubscription); }, - getDepositFacet() { - return bankDepositFacet; + /** + * @param {string} denom + * @param {AssetIssuerKit} feeKit + * @returns {import('@agoric/eventual-send').EOnly} + */ + getFeeCollectorDepositFacet(denom, feeKit) { + if (!bankCall) { + throw Error(`Bank doesn't implement fee collectors`); + } + + /** @type {VirtualPurseController} */ + const feeVpc = harden({ + async *getBalances(_brand) { + // Never resolve! + yield new Promise(_ => {}); + }, + async pullAmount(_amount) { + throw Error(`Cannot pull from fee collector`); + }, + async pushAmount(amount) { + const value = AmountMath.getValue(feeKit.brand, amount); + await bankCall({ + type: 'VPURSE_GIVE_TO_FEE_COLLECTOR', + denom, + amount: `${value}`, + }); + }, + }); + const vp = makeVirtualPurse(feeVpc, feeKit); + return E(vp).getDepositFacet(); }, + /** * Add an asset to the bank, and publish it to the subscriptions. * diff --git a/packages/vats/test/test-vat-bank.js b/packages/vats/test/test-vat-bank.js index 7601950a132..30a242a17d1 100644 --- a/packages/vats/test/test-vat-bank.js +++ b/packages/vats/test/test-vat-bank.js @@ -8,7 +8,7 @@ import { Far } from '@agoric/marshal'; import { buildRootObject } from '../src/vat-bank'; test('communication', async t => { - t.plan(24); + t.plan(29); const bankVat = E(buildRootObject)(); /** @type {undefined | { fromBridge: (srcID: string, obj: any) => void }} */ @@ -54,6 +54,15 @@ test('communication', async t => { break; } + case 'VPURSE_GIVE_TO_FEE_COLLECTOR': { + const { amount, denom, type: _type, ...rest } = obj; + t.is(denom, 'ufee'); + t.is(amount, '12'); + t.deepEqual(rest, {}); + ret = true; + break; + } + default: { t.is(obj, null); } @@ -131,4 +140,16 @@ test('communication', async t => { AmountMath.make(kit.brand, BigInt(balance.amount)), ), ); + + const { mint, ...feeKit } = makeIssuerKit('fee', AssetKind.NAT, { + decimalPlaces: 6, + }); + + // Try sending in some fees. + const feeAmount = AmountMath.make(feeKit.brand, 12n); + const feePayment = mint.mintPayment(feeAmount); + const feeReceived = await E( + E(bankMgr).getFeeCollectorDepositFacet('ufee', feeKit), + ).receive(feePayment); + t.assert(AmountMath.isEqual(feeReceived, feeAmount)); });