Skip to content

Commit 60f7ea0

Browse files
committed
feat: create a feeCollectorDepositFacet
1 parent b56fa7f commit 60f7ea0

File tree

2 files changed

+53
-22
lines changed

2 files changed

+53
-22
lines changed

packages/vats/src/vat-bank.js

+31-21
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,6 @@ export function buildRootObject(_vatPowers) {
172172
/** @type {Store<string, Bank>} */
173173
const addressToBank = makeStore('address');
174174

175-
/** @type {NotifierRecord<string[]>} */
176-
const {
177-
notifier: accountsNotifier,
178-
updater: accountsUpdater,
179-
} = makeNotifierKit([...addressToBank.keys()]);
180-
181175
/**
182176
* Create a new personal bank interface for a given address.
183177
*
@@ -257,22 +251,9 @@ export function buildRootObject(_vatPowers) {
257251
},
258252
});
259253
addressToBank.init(address, bank);
260-
accountsUpdater.updateState([...addressToBank.keys()]);
261254
return bank;
262255
};
263256

264-
const bankDepositFacet = Far('bankDepositFacet', {
265-
getAccountsNotifier() {
266-
return accountsNotifier;
267-
},
268-
deposit(brand, account, payment) {
269-
// The purse will do the proper verification as part of deposit.
270-
const bank = getBankForAddress(account);
271-
const purse = bank.getPurse(brand);
272-
return E(purse).deposit(payment);
273-
},
274-
});
275-
276257
return Far('bankManager', {
277258
/**
278259
* Returns assets as they are added to the bank.
@@ -282,9 +263,38 @@ export function buildRootObject(_vatPowers) {
282263
getAssetSubscription() {
283264
return harden(assetSubscription);
284265
},
285-
getDepositFacet() {
286-
return bankDepositFacet;
266+
/**
267+
* @param {string} denom
268+
* @param {AssetIssuerKit} feeKit
269+
* @returns {import('@agoric/eventual-send').EOnly<DepositFacet>}
270+
*/
271+
getFeeCollectorDepositFacet(denom, feeKit) {
272+
if (!bankCall) {
273+
throw Error(`Bank doesn't implement fee collectors`);
274+
}
275+
276+
/** @type {VirtualPurseController} */
277+
const feeVpc = harden({
278+
async *getBalances(_brand) {
279+
// Never resolve!
280+
yield new Promise(_ => {});
281+
},
282+
async pullAmount(_amount) {
283+
throw Error(`Cannot pull from fee collector`);
284+
},
285+
async pushAmount(amount) {
286+
const value = AmountMath.getValue(feeKit.brand, amount);
287+
await bankCall({
288+
type: 'VPURSE_GIVE_TO_FEE_COLLECTOR',
289+
denom,
290+
amount: `${value}`,
291+
});
292+
},
293+
});
294+
const vp = makeVirtualPurse(feeVpc, feeKit);
295+
return E(vp).getDepositFacet();
287296
},
297+
288298
/**
289299
* Add an asset to the bank, and publish it to the subscriptions.
290300
*

packages/vats/test/test-vat-bank.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Far } from '@agoric/marshal';
88
import { buildRootObject } from '../src/vat-bank';
99

1010
test('communication', async t => {
11-
t.plan(24);
11+
t.plan(29);
1212
const bankVat = E(buildRootObject)();
1313

1414
/** @type {undefined | { fromBridge: (srcID: string, obj: any) => void }} */
@@ -54,6 +54,15 @@ test('communication', async t => {
5454
break;
5555
}
5656

57+
case 'VPURSE_GIVE_TO_FEE_COLLECTOR': {
58+
const { amount, denom, type: _type, ...rest } = obj;
59+
t.is(denom, 'ufee');
60+
t.is(amount, '12');
61+
t.deepEqual(rest, {});
62+
ret = true;
63+
break;
64+
}
65+
5766
default: {
5867
t.is(obj, null);
5968
}
@@ -131,4 +140,16 @@ test('communication', async t => {
131140
AmountMath.make(kit.brand, BigInt(balance.amount)),
132141
),
133142
);
143+
144+
const { mint, ...feeKit } = makeIssuerKit('fee', AssetKind.NAT, {
145+
decimalPlaces: 6,
146+
});
147+
148+
// Try sending in some fees.
149+
const feeAmount = AmountMath.make(feeKit.brand, 12n);
150+
const feePayment = mint.mintPayment(feeAmount);
151+
const feeReceived = await E(
152+
E(bankMgr).getFeeCollectorDepositFacet('ufee', feeKit),
153+
).receive(feePayment);
154+
t.assert(AmountMath.isEqual(feeReceived, feeAmount));
134155
});

0 commit comments

Comments
 (0)