Skip to content

Commit

Permalink
switch discriminate UpdateRecord type
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed Sep 3, 2022
1 parent dafed5b commit b63bf9f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 24 deletions.
27 changes: 15 additions & 12 deletions packages/smart-wallet/src/smartWallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ import {
*/

/**
* @typedef {{ latestOfferStatus: import('./offers.js').OfferStatus } |
* { latestBalance: { currentAmount: Amount } } |
* { latestBrand: BrandDescriptor }
* @typedef {{ updated: 'offerStatus', status: import('./offers.js').OfferStatus } |
* { updated: 'balance'; currentAmount: Amount } |
* { updated: 'brand', descriptor: BrandDescriptor }
* } UpdateRecord Record of an update to the state of this wallet.
*
* Client is responsible for coalescing updates into a current state. See `coalesceUpdates` utility.
*
* The reason for this burden on the client is that transferring the full state is untenable
* (because it would grow monotonically).
*
* `latestBalance` amount is nested for forward-compatibility with supporting
* more than one purse per brand. An additional key will be needed to
* disambiguate. For now the brand in the amount suffices.
* `balance` update supports forward-compatibility for more than one purse per
* brand. An additional key will be needed to disambiguate. For now the brand in
* the amount suffices.
*/

// TODO remove petname? what will UI show then? look up in agoricNames?
Expand Down Expand Up @@ -144,7 +144,8 @@ export const makeSmartWallet = async (
purseBalances.set(purse, balance);
}
updatePublishKit.publisher.publish({
latestBalance: { currentAmount: balance },
updated: 'balance',
currentAmount: balance,
});
};

Expand Down Expand Up @@ -183,8 +184,8 @@ export const makeSmartWallet = async (
// relevant purse. when it's time to make an offer, you know how to make
// payments. REMEMBER when doing that, need to handle every exception to
// put the money back in the purse if anything fails.
const fullDesc = { ...desc, displayInfo };
brandDescriptors.init(desc.brand, fullDesc);
const descriptor = { ...desc, displayInfo };
brandDescriptors.init(desc.brand, descriptor);
brandPurses.init(desc.brand, purse);

// publish purse's balance and changes
Expand All @@ -202,8 +203,7 @@ export const makeSmartWallet = async (
},
});

console.log('DEBUG WALLET PUBLISHING BRAND', fullDesc);
updatePublishKit.publisher.publish({ latestBrand: fullDesc });
updatePublishKit.publisher.publish({ updated: 'brand', descriptor });
};

// Ensure a purse for each issuer
Expand Down Expand Up @@ -277,7 +277,10 @@ export const makeSmartWallet = async (
invitationFromSpec,
sufficientPayments,
offerStatus =>
updatePublishKit.publisher.publish({ latestOfferStatus: offerStatus }),
updatePublishKit.publisher.publish({
updated: 'offerStatus',
status: offerStatus,
}),
payouts => depositPaymentsIntoPurses(payouts, brandPurses.get),
(offerId, invitationMakers) =>
offerToInvitationMakers.init(offerId, invitationMakers),
Expand Down
29 changes: 19 additions & 10 deletions packages/smart-wallet/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,25 @@ export const coalesceUpdates = updates => {
const balances = new Map();
observeIteration(subscribeEach(updates), {
updateState: updateRecord => {
// XXX use discriminated union
if ('latestBalance' in updateRecord) {
const { currentAmount } = updateRecord.latestBalance;
balances.set(currentAmount.brand, currentAmount);
} else if ('latestOfferStatus' in updateRecord) {
const status = updateRecord.latestOfferStatus;
offerStatuses[status.id] = status;
} else if ('latestBrand' in updateRecord) {
const descriptor = updateRecord.latestBrand;
brands.set(descriptor.brand, descriptor);
const { updated } = updateRecord;
switch (updateRecord.updated) {
case 'balance': {
const { currentAmount } = updateRecord;
balances.set(currentAmount.brand, currentAmount);
break;
}
case 'offerStatus': {
const { status } = updateRecord;
offerStatuses[status.id] = status;
break;
}
case 'brand': {
const { descriptor } = updateRecord;
brands.set(descriptor.brand, descriptor);
break;
}
default:
throw new Error(`unknown record updated ${updated}`);
}
},
});
Expand Down
1 change: 0 additions & 1 deletion packages/smart-wallet/test/test-psm-integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
import { eventLoopIteration } from '@agoric/zoe/tools/eventLoopIteration.js';
import { E } from '@endo/far';
import { makeDefaultTestContext } from './contexts.js';
import { currentState } from './supports.js';
import { coalesceUpdates } from '../src/utils.js';

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/smart-wallet/test/test-walletFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ test('bridge', async t => {
t.is(res, undefined);

t.deepEqual(await lastUpdate(), {
latestOfferStatus: {
updated: 'offerStatus',
status: {
...offerSpec,
state: 'error',
},
Expand Down

0 comments on commit b63bf9f

Please sign in to comment.