From 3146886cbad75e773b0dd0520d0d88ef0f12540f Mon Sep 17 00:00:00 2001 From: Turadg Aleahmad Date: Thu, 22 Feb 2024 13:29:40 -0800 Subject: [PATCH 1/4] fix: handle promises in repairWalletForIncarnation2 --- packages/smart-wallet/src/smartWallet.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/smart-wallet/src/smartWallet.js b/packages/smart-wallet/src/smartWallet.js index 0721f21a31d..267f5d5bd37 100644 --- a/packages/smart-wallet/src/smartWallet.js +++ b/packages/smart-wallet/src/smartWallet.js @@ -1102,9 +1102,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 */ @@ -1115,8 +1115,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}`); }, }, From c70ee0113cd7e6b0df5e33bd934d33a588ef0b3d Mon Sep 17 00:00:00 2001 From: Turadg Aleahmad Date: Thu, 22 Feb 2024 13:39:10 -0800 Subject: [PATCH 2/4] fix: burn invitation made for repair --- packages/smart-wallet/src/smartWallet.js | 34 ++++++++++++------------ 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/packages/smart-wallet/src/smartWallet.js b/packages/smart-wallet/src/smartWallet.js index 267f5d5bd37..fd1f969d760 100644 --- a/packages/smart-wallet/src/smartWallet.js +++ b/packages/smart-wallet/src/smartWallet.js @@ -643,24 +643,24 @@ 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 () => { + // To infer the invitation amount for the offer + const invitation = invitationFromSpec(offerSpec.invitationSpec); + const invitationAmount = + await E(invitationIssuer).getAmountOf(invitation); + const watcher = makeOfferWatcher( + facets.helper, + facets.deposit, + offerSpec, + address, + invitationAmount, + seat, + ); + void E(invitationIssuer).burn(invitation); + return watchOfferOutcomes(watcher, seat); + })(); trace(`Repaired seat ${seatId} for wallet ${address}`); + watcherPromises.push(watchOutcome); } await Promise.all(watcherPromises); From 71bb1c76d47da15242e7eaf54899869f9d5976aa Mon Sep 17 00:00:00 2001 From: Turadg Aleahmad Date: Thu, 22 Feb 2024 14:12:55 -0800 Subject: [PATCH 3/4] fix: re-use invitation from offerToUsedInvitation --- packages/smart-wallet/src/smartWallet.js | 28 +++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/packages/smart-wallet/src/smartWallet.js b/packages/smart-wallet/src/smartWallet.js index fd1f969d760..e90ade7c5cc 100644 --- a/packages/smart-wallet/src/smartWallet.js +++ b/packages/smart-wallet/src/smartWallet.js @@ -644,10 +644,29 @@ export const prepareSmartWallet = (baggage, shared) => { const seat = liveOfferSeats.get(seatId); const watchOutcome = (async () => { - // To infer the invitation amount for the offer - const invitation = invitationFromSpec(offerSpec.invitationSpec); - const invitationAmount = - await E(invitationIssuer).getAmountOf(invitation); + 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, @@ -656,7 +675,6 @@ export const prepareSmartWallet = (baggage, shared) => { invitationAmount, seat, ); - void E(invitationIssuer).burn(invitation); return watchOfferOutcomes(watcher, seat); })(); trace(`Repaired seat ${seatId} for wallet ${address}`); From a3f0fc5d2cd48fa398491237b683d091b1003309 Mon Sep 17 00:00:00 2001 From: Turadg Aleahmad Date: Thu, 22 Feb 2024 14:14:26 -0800 Subject: [PATCH 4/4] chore(types): offerToUsedInvitation --- packages/deploy-script-support/src/offer.js | 4 ++-- packages/smart-wallet/src/offerWatcher.js | 16 ++++++++++++++-- packages/smart-wallet/src/smartWallet.js | 2 +- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/packages/deploy-script-support/src/offer.js b/packages/deploy-script-support/src/offer.js index 8ba945f20e4..8a7ae23b130 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 269830cb73f..affcc45984a 100644 --- a/packages/smart-wallet/src/offerWatcher.js +++ b/packages/smart-wallet/src/offerWatcher.js @@ -104,17 +104,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 e90ade7c5cc..61577e0a29b 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,