Skip to content

Commit 0dde1bc

Browse files
authored
chore: add getUINotifier to wallet bridge (#2172)
* chore: add getUINotifier to wallet. Remove outcome from what is sent back to wallet. * chore: fix t.plan * chore: add error message for non-existent uiNotifier
1 parent 7b07d89 commit 0dde1bc

File tree

4 files changed

+45
-46
lines changed

4 files changed

+45
-46
lines changed

packages/dapp-svelte-wallet/api/src/lib-wallet.js

+28-15
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { makeIssuerTable } from '@agoric/zoe/src/issuerTable';
1717

1818
import { E } from '@agoric/eventual-send';
1919

20-
import { makeMarshal } from '@agoric/marshal';
20+
import { makeMarshal, passStyleOf } from '@agoric/marshal';
2121
import {
2222
makeNotifierKit,
2323
observeIteration,
@@ -88,6 +88,11 @@ export function makeWallet({
8888
// Offers that the wallet knows about (the inbox).
8989
const idToOffer = makeStore('offerId');
9090
const idToNotifierP = makeStore('offerId');
91+
/** @type {Store<string, any>} */
92+
const idToOfferResult = makeStore('id');
93+
94+
/** @type {WeakStore<Handle<'invitation'>, any>} */
95+
const invitationHandleToOfferResult = makeWeakStore('invitationHandle');
9196

9297
// Compiled offers (all ready to execute).
9398
const idToCompiledOfferP = new Map();
@@ -809,11 +814,15 @@ export function makeWallet({
809814
return dappOrigins.get(origin);
810815
}
811816

817+
function makeId(dappOrigin, rawId) {
818+
return `${dappOrigin}#${rawId}`;
819+
}
820+
812821
async function addOffer(rawOffer, requestContext = {}) {
813822
const dappOrigin =
814823
requestContext.dappOrigin || requestContext.origin || 'unknown';
815824
const { id: rawId } = rawOffer;
816-
const id = `${dappOrigin}#${rawId}`;
825+
const id = makeId(dappOrigin, rawId);
817826
const offer = harden({
818827
...rawOffer,
819828
id,
@@ -898,7 +907,7 @@ export function makeWallet({
898907
return undefined;
899908
}
900909

901-
/** @type {{ outcome?: any, depositedP?: Promise<any[]>, dappContext?: any }} */
910+
/** @type {{ depositedP?: Promise<any[]>, dappContext?: any }} */
902911
let ret = {};
903912
let alreadyResolved = false;
904913
const rejected = e => {
@@ -940,16 +949,10 @@ export function makeWallet({
940949
}
941950
});
942951

943-
// The outcome is most often a string that can be returned, but
944-
// it could be an object. We don't do anything currently if it
945-
// is an object, but we will store it here for future use.
946-
const outcome = await E(seat).getOfferResult();
947-
if (offer.actions) {
948-
E(offer.actions).result(offer, outcome);
949-
}
952+
const offerResult = await E(seat).getOfferResult();
953+
idToOfferResult.init(id, offerResult);
950954

951955
ret = {
952-
outcome,
953956
depositedP,
954957
dappContext: offer.dappContext,
955958
};
@@ -1311,14 +1314,23 @@ export function makeWallet({
13111314
return issuerManager;
13121315
}
13131316

1314-
const offerResultStore = makeWeakStore('invitationHandle');
1315-
13161317
async function saveOfferResult(invitationHandle, offerResult) {
1317-
offerResultStore.init(invitationHandle, offerResult);
1318+
invitationHandleToOfferResult.init(invitationHandle, offerResult);
13181319
}
13191320

13201321
async function getOfferResult(invitationHandle) {
1321-
return offerResultStore.get(invitationHandle);
1322+
return invitationHandleToOfferResult.get(invitationHandle);
1323+
}
1324+
1325+
async function getUINotifier(rawId, dappOrigin = 'unknown') {
1326+
const id = makeId(dappOrigin, rawId);
1327+
const offerResult = idToOfferResult.get(id);
1328+
assert(
1329+
passStyleOf(offerResult) === 'copyRecord',
1330+
`offerResult must be a record to have a uiNotifier`,
1331+
);
1332+
assert(offerResult.uiNotifier, `offerResult does not have a uiNotifier`);
1333+
return offerResult.uiNotifier;
13221334
}
13231335

13241336
const wallet = harden({
@@ -1396,6 +1408,7 @@ export function makeWallet({
13961408
getPaymentsNotifier() {
13971409
return paymentsNotifier;
13981410
},
1411+
getUINotifier,
13991412
});
14001413

14011414
const initialize = async () => {

packages/dapp-svelte-wallet/api/src/types.js

+3
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@
7575
* @property {(petname: Petname, instanceBoardId: string) => Promise<void>}
7676
* suggestInstance Introduce a Zoe contract instance to the wallet, with
7777
* suggested petname.
78+
* @property {(rawId: string) => Promise<Notifier<any>>} getUINotifier Get the UI notifier from the offerResult
79+
* for a particular offer, identified by id. This notifier should only
80+
* contain information that is safe to pass to the dapp UI.
7881
*/
7982

8083
/**

packages/dapp-svelte-wallet/api/src/wallet.js

+7-26
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ export function buildRootObject(_vatPowers) {
159159
await approve();
160160
return walletAdmin.suggestInstance(petname, boardId, dappOrigin);
161161
},
162+
async getUINotifier(rawId) {
163+
await approve();
164+
return walletAdmin.getUINotifier(rawId, dappOrigin);
165+
},
162166
};
163167
return harden(bridge);
164168
};
@@ -195,6 +199,9 @@ export function buildRootObject(_vatPowers) {
195199
suggestIssuer(petname, issuerBoardId) {
196200
return walletAdmin.suggestIssuer(petname, issuerBoardId);
197201
},
202+
getUINotifier(rawId) {
203+
return walletAdmin.getUINotifier(rawId);
204+
},
198205
};
199206
harden(preapprovedBridge);
200207

@@ -386,32 +393,6 @@ export function buildRootObject(_vatPowers) {
386393
case 'walletAddOffer': {
387394
let handled = false;
388395
const actions = harden({
389-
result(offer, outcome) {
390-
httpSend(
391-
{
392-
type: 'walletOfferResult',
393-
data: {
394-
id: offer.id,
395-
dappContext: offer.dappContext,
396-
outcome,
397-
},
398-
},
399-
[meta.channelHandle],
400-
);
401-
},
402-
error(offer, reason) {
403-
httpSend(
404-
{
405-
type: 'walletOfferResult',
406-
data: {
407-
id: offer.id,
408-
dappContext: offer.dappContext,
409-
error: `${(reason && reason.stack) || reason}`,
410-
},
411-
},
412-
[meta.channelHandle],
413-
);
414-
},
415396
handled(offer) {
416397
if (handled) {
417398
return;

packages/dapp-svelte-wallet/api/test/test-lib-wallet.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ test('lib-wallet dapp suggests issuer, instance, installation petnames', async t
555555
});
556556

557557
test('lib-wallet offer methods', async t => {
558-
t.plan(8);
558+
t.plan(7);
559559
const {
560560
moolaBundle,
561561
wallet,
@@ -630,8 +630,7 @@ test('lib-wallet offer methods', async t => {
630630
);
631631
const accepted = await wallet.acceptOffer(id);
632632
assert(accepted);
633-
const { outcome, depositedP } = accepted;
634-
t.is(await outcome, 'The offer was accepted', `offer was accepted`);
633+
const { depositedP } = accepted;
635634
await depositedP;
636635
const seats = wallet.getSeats(harden([id]));
637636
const seat = wallet.getSeat(id);
@@ -881,8 +880,11 @@ test('lib-wallet addOffer for autoswap swap', async t => {
881880

882881
const accepted = await wallet.acceptOffer(id);
883882
assert(accepted);
884-
const { outcome, depositedP } = accepted;
885-
t.is(await outcome, 'Swap successfully completed.', `offer was accepted`);
883+
const { depositedP } = accepted;
884+
await t.throwsAsync(() => wallet.getUINotifier(rawId, `unknown`), {
885+
message: 'offerResult must be a record to have a uiNotifier',
886+
});
887+
886888
await depositedP;
887889
const seats = wallet.getSeats(harden([id]));
888890
const seat = wallet.getSeat(id);

0 commit comments

Comments
 (0)