Skip to content

Commit

Permalink
feat: create a feeCollectorDepositFacet
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed May 21, 2021
1 parent b56fa7f commit 60f7ea0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 22 deletions.
52 changes: 31 additions & 21 deletions packages/vats/src/vat-bank.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,6 @@ export function buildRootObject(_vatPowers) {
/** @type {Store<string, Bank>} */
const addressToBank = makeStore('address');

/** @type {NotifierRecord<string[]>} */
const {
notifier: accountsNotifier,
updater: accountsUpdater,
} = makeNotifierKit([...addressToBank.keys()]);

/**
* Create a new personal bank interface for a given address.
*
Expand Down Expand Up @@ -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.
Expand All @@ -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<DepositFacet>}
*/
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.
*
Expand Down
23 changes: 22 additions & 1 deletion packages/vats/test/test-vat-bank.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }} */
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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));
});

0 comments on commit 60f7ea0

Please sign in to comment.