From b63bf9f635e3e66e1d6dcc4303019aad12e87dc1 Mon Sep 17 00:00:00 2001 From: Turadg Aleahmad Date: Sat, 3 Sep 2022 16:32:10 -0700 Subject: [PATCH] switch discriminate UpdateRecord type --- packages/smart-wallet/src/smartWallet.js | 27 +++++++++-------- packages/smart-wallet/src/utils.js | 29 ++++++++++++------- .../smart-wallet/test/test-psm-integration.js | 1 - .../smart-wallet/test/test-walletFactory.js | 3 +- 4 files changed, 36 insertions(+), 24 deletions(-) diff --git a/packages/smart-wallet/src/smartWallet.js b/packages/smart-wallet/src/smartWallet.js index b664f36ae8d7..7a7232c42c07 100644 --- a/packages/smart-wallet/src/smartWallet.js +++ b/packages/smart-wallet/src/smartWallet.js @@ -38,9 +38,9 @@ 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. @@ -48,9 +48,9 @@ import { * 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? @@ -144,7 +144,8 @@ export const makeSmartWallet = async ( purseBalances.set(purse, balance); } updatePublishKit.publisher.publish({ - latestBalance: { currentAmount: balance }, + updated: 'balance', + currentAmount: balance, }); }; @@ -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 @@ -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 @@ -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), diff --git a/packages/smart-wallet/src/utils.js b/packages/smart-wallet/src/utils.js index 77f1fcba58fc..093438744a48 100644 --- a/packages/smart-wallet/src/utils.js +++ b/packages/smart-wallet/src/utils.js @@ -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}`); } }, }); diff --git a/packages/smart-wallet/test/test-psm-integration.js b/packages/smart-wallet/test/test-psm-integration.js index bac7cee4738b..32c7ff39c150 100644 --- a/packages/smart-wallet/test/test-psm-integration.js +++ b/packages/smart-wallet/test/test-psm-integration.js @@ -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'; /** diff --git a/packages/smart-wallet/test/test-walletFactory.js b/packages/smart-wallet/test/test-walletFactory.js index c1f3284f8174..a4e7f90e8b56 100644 --- a/packages/smart-wallet/test/test-walletFactory.js +++ b/packages/smart-wallet/test/test-walletFactory.js @@ -64,7 +64,8 @@ test('bridge', async t => { t.is(res, undefined); t.deepEqual(await lastUpdate(), { - latestOfferStatus: { + updated: 'offerStatus', + status: { ...offerSpec, state: 'error', },