diff --git a/packages/deploy-script-support/src/offer.js b/packages/deploy-script-support/src/offer.js index 6096fe6b454..d6db3ccdc80 100644 --- a/packages/deploy-script-support/src/offer.js +++ b/packages/deploy-script-support/src/offer.js @@ -19,7 +19,7 @@ import { AmountMath } from '@agoric/ertp/src/amountMath.js'; * @param {ERef} walletAdmin - an internal type of the * wallet, not defined here * @param {ERef} zoe - * @param {ERef} zoeInvitationPurse + * @param {ERef>} zoeInvitationPurse */ export const makeOfferAndFindInvitationAmount = ( walletAdmin, @@ -28,7 +28,7 @@ export const makeOfferAndFindInvitationAmount = ( ) => { /** * @param {Record} invitationDetailsCriteria - * @returns {Promise} invitationAmount + * @returns {Promise>} invitationAmount */ const findInvitationAmount = async invitationDetailsCriteria => { const invitationAmount = await E(zoeInvitationPurse).getCurrentAmount(); diff --git a/packages/smart-wallet/src/offerWatcher.js b/packages/smart-wallet/src/offerWatcher.js index 5e14abe06cc..1d31a7ad511 100644 --- a/packages/smart-wallet/src/offerWatcher.js +++ b/packages/smart-wallet/src/offerWatcher.js @@ -107,17 +107,29 @@ const offerWatcherGuard = harden({ }), }); +/** + * @param {import('@agoric/vat-data').Baggage} baggage + */ export const prepareOfferWatcher = baggage => { return prepareExoClassKit( baggage, 'OfferWatcher', offerWatcherGuard, - (walletHelper, deposit, offerSpec, address, iAmount, seatRef) => ({ + /** + * + * @param {*} walletHelper + * @param {*} deposit + * @param {import('./offers.js').OfferSpec} offerSpec + * @param {string} address + * @param {Amount<'set'>} invitationAmount + * @param {UserSeat} seatRef + */ + (walletHelper, deposit, offerSpec, address, invitationAmount, seatRef) => ({ walletHelper, deposit, status: offerSpec, address, - invitationAmount: iAmount, + invitationAmount, seatRef, }), { diff --git a/packages/smart-wallet/src/smartWallet.js b/packages/smart-wallet/src/smartWallet.js index db1186d2f4f..23ee14af182 100644 --- a/packages/smart-wallet/src/smartWallet.js +++ b/packages/smart-wallet/src/smartWallet.js @@ -173,7 +173,7 @@ const trace = makeTracer('SmrtWlt'); * paymentQueues: MapStore>, * offerToInvitationMakers: MapStore, * offerToPublicSubscriberPaths: MapStore>, - * offerToUsedInvitation: MapStore, + * offerToUsedInvitation: MapStore>, * purseBalances: MapStore, * updateRecorderKit: import('@agoric/zoe/src/contractSupport/recorder.js').RecorderKit, * currentRecorderKit: import('@agoric/zoe/src/contractSupport/recorder.js').RecorderKit, @@ -645,24 +645,43 @@ export const prepareSmartWallet = (baggage, shared) => { const offerSpec = liveOffers.get(seatId); const seat = liveOfferSeats.get(seatId); - const invitation = invitationFromSpec(offerSpec.invitationSpec); - watcherPromises.push( - E.when( - E(invitationIssuer).getAmountOf(invitation), - invitationAmount => { - const watcher = makeOfferWatcher( - facets.helper, - facets.deposit, - offerSpec, - address, - invitationAmount, - seat, - ); - return watchOfferOutcomes(watcher, seat); - }, - ), - ); + const watchOutcome = (async () => { + await null; + let invitationAmount = state.offerToUsedInvitation.get( + // @ts-expect-error older type allowed number + offerSpec.id, + ); + if (invitationAmount) { + facets.helper.logWalletInfo( + 'recovered invitation amount for offer', + offerSpec.id, + ); + } else { + facets.helper.logWalletInfo( + 'inferring invitation amount for offer', + offerSpec.id, + ); + const tempInvitation = invitationFromSpec( + offerSpec.invitationSpec, + ); + invitationAmount = await E(invitationIssuer).getAmountOf( + tempInvitation, + ); + void E(invitationIssuer).burn(tempInvitation); + } + + const watcher = makeOfferWatcher( + facets.helper, + facets.deposit, + offerSpec, + address, + invitationAmount, + seat, + ); + return watchOfferOutcomes(watcher, seat); + })(); trace(`Repaired seat ${seatId} for wallet ${address}`); + watcherPromises.push(watchOutcome); } await Promise.all(watcherPromises); @@ -1104,9 +1123,9 @@ export const prepareSmartWallet = (baggage, shared) => { }, }); }, + // TODO remove this and repairUnwatchedSeats once the repair has taken place. /** - * one-time use function. Remove this and repairUnwatchedSeats once the - * repair has taken place. + * To be called once ever per wallet. * * @param {object} key */ @@ -1117,8 +1136,12 @@ export const prepareSmartWallet = (baggage, shared) => { return; } - void facets.helper.repairUnwatchedSeats(); - void facets.helper.repairUnwatchedPurses(); + facets.helper.repairUnwatchedSeats().catch(e => { + console.error('repairUnwatchedSeats rejection', e); + }); + facets.helper.repairUnwatchedPurses().catch(e => { + console.error('repairUnwatchedPurses rejection', e); + }); trace(`repaired wallet ${state.address}`); }, },