Skip to content

Commit

Permalink
chore: cleanups from review
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris-Hibbert committed Dec 6, 2023
1 parent 8619d8c commit 86b166c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 62 deletions.
35 changes: 6 additions & 29 deletions packages/smart-wallet/src/offerWatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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(
Expand All @@ -106,7 +105,7 @@ const offerWatcherGuard = harden({
}),
});

export const makeOfferWatcherMaker = baggage => {
export const prepareOfferWatcher = baggage => {
return prepareExoClassKit(
baggage,
'OfferWatcher',
Expand All @@ -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,
Expand All @@ -140,7 +139,6 @@ export const makeOfferWatcherMaker = baggage => {
invitationAmount,
invitationMakers,
publicSubscribers,
state.address,
);
},

Expand Down Expand Up @@ -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']} */
Expand All @@ -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();
}
},
},
Expand All @@ -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();
}
},
},
Expand All @@ -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<typeof makeOfferWatcherMaker>} MakeOfferWatcher */
/** @typedef {ReturnType<typeof prepareOfferWatcher>} MakeOfferWatcher */
84 changes: 51 additions & 33 deletions packages/smart-wallet/src/smartWallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<Invitation>,
* }} ExecutorPowers
*/

Expand Down Expand Up @@ -264,7 +264,7 @@ export const prepareSmartWallet = (baggage, shared) => {
return store;
});

const makeOfferWatcher = makeOfferWatcherMaker(baggage);
const makeOfferWatcher = prepareOfferWatcher(baggage);

/**
* @param {UniqueParams} unique
Expand Down Expand Up @@ -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', {
Expand Down Expand Up @@ -467,7 +468,8 @@ export const prepareSmartWallet = (baggage, shared) => {

/** @type {(purse: ERef<Purse>) => Promise<void>} */
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

Expand All @@ -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,
Expand All @@ -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}`,
);
},
Expand Down Expand Up @@ -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,
Expand All @@ -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',
Expand Down Expand Up @@ -619,15 +623,14 @@ export const prepareSmartWallet = (baggage, shared) => {
invitationAmount,
invitationMakers,
publicSubscribers,
address,
) {
const { state, facets } = this;

state.offerToUsedInvitation.init(offerId, invitationAmount);
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();
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -725,13 +736,20 @@ export const prepareSmartWallet = (baggage, shared) => {
return objectMap(give, amount => {
/** @type {Promise<Purse>} */
const purseP = facets.helper.purseForBrand(amount.brand);
debugger;

Check failure on line 739 in packages/smart-wallet/src/smartWallet.js

View workflow job for this annotation

GitHub Actions / lint-rest

Unexpected 'debugger' statement
const paymentP = E(purseP).withdraw(amount);
void E.when(
paymentP,
payment => brandPaymentRecord.init(amount.brand, payment),
e => {
debugger;

Check failure on line 745 in packages/smart-wallet/src/smartWallet.js

View workflow job for this annotation

GitHub Actions / lint-rest

Unexpected 'debugger' statement

// recovery will be handled by tryReclaimingWithdrawnPayments()
console.log(`Payment withdrawal failed.`, offerId, e);
facets.helper.consoleInfo(
`⚠️ Payment withdrawal failed.`,
offerId,
e,
);
},
);
return paymentP;
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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,
),
);
}

Expand Down Expand Up @@ -897,14 +910,19 @@ export const prepareSmartWallet = (baggage, shared) => {
* @returns {Promise<void>}
*/
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 },
Expand Down

0 comments on commit 86b166c

Please sign in to comment.