From 86b166cd7152f01c4382d430798db96a91e6e2d2 Mon Sep 17 00:00:00 2001 From: Chris Hibbert Date: Tue, 5 Dec 2023 17:18:22 -0800 Subject: [PATCH] chore: cleanups from review --- packages/smart-wallet/src/offerWatcher.js | 35 ++-------- packages/smart-wallet/src/smartWallet.js | 84 ++++++++++++++--------- 2 files changed, 57 insertions(+), 62 deletions(-) diff --git a/packages/smart-wallet/src/offerWatcher.js b/packages/smart-wallet/src/offerWatcher.js index d085ad18315d..16a761fcf33a 100644 --- a/packages/smart-wallet/src/offerWatcher.js +++ b/packages/smart-wallet/src/offerWatcher.js @@ -8,7 +8,7 @@ import { SeatShape, } from '@agoric/zoe/src/typeGuards.js'; import { AmountShape } from '@agoric/ertp/src/typeGuards.js'; -import { objectMap, deeplyFulfilledObject } from '@agoric/internal'; +import { deeplyFulfilledObject, objectMap } from '@agoric/internal'; import { UNPUBLISHED_RESULT } from './offers.js'; @@ -88,7 +88,6 @@ const offerWatcherGuard = harden({ .optional(M.record()) .returns(), publishResult: M.call(M.any()).returns(), - exitOpenSeat: M.call().returns(), }), paymentWatcher: M.interface('paymentWatcher', { onFulfilled: M.call(PaymentPKeywordRecordShape, SeatShape).returns( @@ -106,7 +105,7 @@ const offerWatcherGuard = harden({ }), }); -export const makeOfferWatcherMaker = baggage => { +export const prepareOfferWatcher = baggage => { return prepareExoClassKit( baggage, 'OfferWatcher', @@ -125,7 +124,7 @@ export const makeOfferWatcherMaker = baggage => { const { state } = this; state.status = harden({ ...state.status, ...offerStatusUpdates }); - state.walletHelper.updateStatus(state.status, state.address); + state.walletHelper.updateStatus(state.status); }, onNewContinuingOffer( offerId, @@ -140,7 +139,6 @@ export const makeOfferWatcherMaker = baggage => { invitationAmount, invitationMakers, publicSubscribers, - state.address, ); }, @@ -177,15 +175,6 @@ export const makeOfferWatcherMaker = baggage => { facets.helper.updateStatus({ result: UNPUBLISHED_RESULT }); } }, - - exitOpenSeat() { - const { state } = this; - void E.when(E(state.seatRef).hasExited(), hasExited => { - if (!hasExited) { - void E(state.seatRef).tryExit(); - } - }); - }, }, /** @type {OutcomeWatchers['paymentWatcher']} */ @@ -207,12 +196,8 @@ export const makeOfferWatcherMaker = baggage => { */ onRejected(err, seat) { const { facets } = this; - if (isUpgradeDisconnection(err)) { void watchForPayout(facets, seat); - } else { - facets.helper.updateStatus({ error: err.toString() }); - facets.helper.exitOpenSeat(); } }, }, @@ -231,9 +216,6 @@ export const makeOfferWatcherMaker = baggage => { const { facets } = this; if (isUpgradeDisconnection(err)) { void watchForOfferResult(facets, seat); - } else { - facets.helper.updateStatus({ error: err.toString() }); - facets.helper.exitOpenSeat(); } }, }, @@ -251,17 +233,12 @@ export const makeOfferWatcherMaker = baggage => { */ onRejected(err, seat) { const { facets } = this; - if (isUpgradeDisconnection(err)) { - void watchForNumWants(facets, seat); - } else { - facets.helper.updateStatus({ error: err.toString() }); - facets.helper.exitOpenSeat(); - } + void watchForNumWants(facets, seat); }, }, }, ); }; -harden(makeOfferWatcherMaker); +harden(prepareOfferWatcher); -/** @typedef {ReturnType} MakeOfferWatcher */ +/** @typedef {ReturnType} MakeOfferWatcher */ diff --git a/packages/smart-wallet/src/smartWallet.js b/packages/smart-wallet/src/smartWallet.js index 9ec79d6d4378..dad6f47454f9 100644 --- a/packages/smart-wallet/src/smartWallet.js +++ b/packages/smart-wallet/src/smartWallet.js @@ -38,7 +38,7 @@ import { import { makeInvitationsHelper } from './invitations.js'; import { shape } from './typeGuards.js'; import { objectMapStoragePath } from './utils.js'; -import { makeOfferWatcherMaker, watchOfferOutcomes } from './offerWatcher.js'; +import { prepareOfferWatcher, watchOfferOutcomes } from './offerWatcher.js'; const { Fail, quote: q } = assert; @@ -65,7 +65,7 @@ const trace = makeTracer('SmrtWlt'); * @typedef {{ * logger: {info: (...args: any[]) => void, error: (...args: any[]) => void}, * makeOfferWatcher: import('./offerWatcher.js').MakeOfferWatcher, - * invitationFromSpec: import('./invitations.js').InvitationFromSpec, + * invitationFromSpec: ERef, * }} ExecutorPowers */ @@ -264,7 +264,7 @@ export const prepareSmartWallet = (baggage, shared) => { return store; }); - const makeOfferWatcher = makeOfferWatcherMaker(baggage); + const makeOfferWatcher = prepareOfferWatcher(baggage); /** * @param {UniqueParams} unique @@ -352,15 +352,16 @@ export const prepareSmartWallet = (baggage, shared) => { publishCurrentState: M.call().returns(), watchPurse: M.call(M.eref(PurseShape)).returns(M.promise()), repairUnwatchedSeats: M.call().returns(), - updateStatus: M.call(M.any(), M.string()).returns(), + updateStatus: M.call(M.any()).returns(), addContinuingOffer: M.call( M.or(M.number(), M.string()), AmountShape, M.remotable('InvitationMaker'), M.or(M.record(), M.undefined()), - M.string(), ).returns(M.promise()), purseForBrand: M.call(BrandShape).returns(M.promise()), + consoleInfo: M.call(M.any()).returns(), + consoleError: M.call(M.any()).returns(), }), deposit: M.interface('depositFacetI', { @@ -467,7 +468,8 @@ export const prepareSmartWallet = (baggage, shared) => { /** @type {(purse: ERef) => Promise} */ async watchPurse(purseRef) { - const { address } = this.state; + const { state, facets } = this; + const { address } = state; const purse = await purseRef; // promises don't fit in durable storage @@ -477,7 +479,7 @@ export const prepareSmartWallet = (baggage, shared) => { E(purse).getCurrentAmount(), balance => helper.updateBalance(purse, balance), err => - console.error( + facets.helper.consoleError( address, 'initial purse balance publish failed', err, @@ -488,7 +490,7 @@ export const prepareSmartWallet = (baggage, shared) => { helper.updateBalance(purse, balance); }, fail(reason) { - console.error( + facets.helper.consoleError( `⚠️ ${address} failed updateState observer, ${reason}`, ); }, @@ -562,7 +564,7 @@ export const prepareSmartWallet = (baggage, shared) => { if (baggage.has(REPAIRED_UNWATCHED_SEATS)) { return; } - baggage.init(REPAIRED_UNWATCHED_SEATS, true); + baggage.init(REPAIRED_UNWATCHED_SEATS, 'present`'); const invitationFromSpec = makeInvitationsHelper( zoe, @@ -588,9 +590,11 @@ export const prepareSmartWallet = (baggage, shared) => { void watchOfferOutcomes(watcher, seat); } }, - updateStatus(offerStatus, address) { + + /** @param {import('./offers.js').OfferStatus} offerStatus */ + updateStatus(offerStatus) { const { state, facets } = this; - console.info('wallet', address, 'offerStatus', offerStatus); + facets.helper.consoleInfo('offerStatus', offerStatus); void state.updateRecorderKit.recorder.write({ updated: 'offerStatus', @@ -619,7 +623,6 @@ export const prepareSmartWallet = (baggage, shared) => { invitationAmount, invitationMakers, publicSubscribers, - address, ) { const { state, facets } = this; @@ -627,7 +630,7 @@ export const prepareSmartWallet = (baggage, shared) => { state.offerToInvitationMakers.init(offerId, invitationMakers); const pathMap = await objectMapStoragePath(publicSubscribers); if (pathMap) { - console.info('wallet', address, 'recording pathMap', pathMap); + facets.helper.consoleInfo('recording pathMap', pathMap); state.offerToPublicSubscriberPaths.init(offerId, pathMap); } facets.helper.publishCurrentState(); @@ -657,6 +660,14 @@ export const prepareSmartWallet = (baggage, shared) => { } throw Fail`cannot find/make purse for ${brand}`; }, + consoleInfo(...args) { + const { state } = this; + console.info('wallet', state.address, ...args); + }, + consoleError(...args) { + const { state } = this; + console.error('wallet', state.address, ...args); + }, }, /** * Similar to {DepositFacet} but async because it has to look up the purse. @@ -725,13 +736,20 @@ export const prepareSmartWallet = (baggage, shared) => { return objectMap(give, amount => { /** @type {Promise} */ const purseP = facets.helper.purseForBrand(amount.brand); + debugger; const paymentP = E(purseP).withdraw(amount); void E.when( paymentP, payment => brandPaymentRecord.init(amount.brand, payment), e => { + debugger; + // recovery will be handled by tryReclaimingWithdrawnPayments() - console.log(`Payment withdrawal failed.`, offerId, e); + facets.helper.consoleInfo( + `⚠️ Payment withdrawal failed.`, + offerId, + e, + ); }, ); return paymentP; @@ -792,12 +810,7 @@ export const prepareSmartWallet = (baggage, shared) => { state.offerToInvitationMakers.get, ); - console.info( - 'wallet', - address, - 'starting executeOffer', - offerSpec.id, - ); + facets.helper.consoleInfo('starting executeOffer', offerSpec.id); // 1. Prepare values and validate synchronously. const { proposal } = offerSpec; @@ -823,7 +836,7 @@ export const prepareSmartWallet = (baggage, shared) => { paymentKeywordRecord, offerSpec.offerArgs, ); - console.info('wallet', address, offerSpec.id, 'seated'); + facets.helper.consoleInfo(offerSpec.id, 'seated'); watcher = makeOfferWatcher( facets.helper, @@ -843,25 +856,25 @@ export const prepareSmartWallet = (baggage, shared) => { // await so that any errors are caught and handled below await watchOfferOutcomes(watcher, seatRef); } catch (err) { - console.error('OFFER ERROR:', err); + facets.helper.consoleError('OFFER ERROR:', err); // Notify the user if (watcher) { watcher.helper.updateStatus({ error: err.toString() }); } else { - facets.helper.updateStatus( - { - error: err.toString(), - ...offerSpec, - }, - address, - ); + facets.helper.updateStatus({ + error: err.toString(), + ...offerSpec, + }); } if (offerSpec?.proposal?.give) { facets.payments .tryReclaimingWithdrawnPayments(offerSpec.id) .catch(e => - console.error('recovery failed reclaiming payments', e), + facets.helper.consoleError( + 'recovery failed reclaiming payments', + e, + ), ); } @@ -897,14 +910,19 @@ export const prepareSmartWallet = (baggage, shared) => { * @returns {Promise} */ handleBridgeAction(actionCapData, canSpend = false) { + const { facets } = this; + const { offers } = facets; const { publicMarshaller } = shared; - const { offers } = this.facets; - /** @param {Error} err */ const recordError = err => { const { address, updateRecorderKit } = this.state; - console.error('wallet', address, 'handleBridgeAction error:', err); + facets.helper.consoleError( + 'wallet', + address, + 'handleBridgeAction error:', + err, + ); void updateRecorderKit.recorder.write({ updated: 'walletAction', status: { error: err.message },