Skip to content

Commit

Permalink
chore: add getUINotifier to wallet bridge (Agoric#2172)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
katelynsills authored Jan 9, 2021
1 parent 7b07d89 commit 0dde1bc
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 46 deletions.
43 changes: 28 additions & 15 deletions packages/dapp-svelte-wallet/api/src/lib-wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { makeIssuerTable } from '@agoric/zoe/src/issuerTable';

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

import { makeMarshal } from '@agoric/marshal';
import { makeMarshal, passStyleOf } from '@agoric/marshal';
import {
makeNotifierKit,
observeIteration,
Expand Down Expand Up @@ -88,6 +88,11 @@ export function makeWallet({
// Offers that the wallet knows about (the inbox).
const idToOffer = makeStore('offerId');
const idToNotifierP = makeStore('offerId');
/** @type {Store<string, any>} */
const idToOfferResult = makeStore('id');

/** @type {WeakStore<Handle<'invitation'>, any>} */
const invitationHandleToOfferResult = makeWeakStore('invitationHandle');

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

function makeId(dappOrigin, rawId) {
return `${dappOrigin}#${rawId}`;
}

async function addOffer(rawOffer, requestContext = {}) {
const dappOrigin =
requestContext.dappOrigin || requestContext.origin || 'unknown';
const { id: rawId } = rawOffer;
const id = `${dappOrigin}#${rawId}`;
const id = makeId(dappOrigin, rawId);
const offer = harden({
...rawOffer,
id,
Expand Down Expand Up @@ -898,7 +907,7 @@ export function makeWallet({
return undefined;
}

/** @type {{ outcome?: any, depositedP?: Promise<any[]>, dappContext?: any }} */
/** @type {{ depositedP?: Promise<any[]>, dappContext?: any }} */
let ret = {};
let alreadyResolved = false;
const rejected = e => {
Expand Down Expand Up @@ -940,16 +949,10 @@ export function makeWallet({
}
});

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

ret = {
outcome,
depositedP,
dappContext: offer.dappContext,
};
Expand Down Expand Up @@ -1311,14 +1314,23 @@ export function makeWallet({
return issuerManager;
}

const offerResultStore = makeWeakStore('invitationHandle');

async function saveOfferResult(invitationHandle, offerResult) {
offerResultStore.init(invitationHandle, offerResult);
invitationHandleToOfferResult.init(invitationHandle, offerResult);
}

async function getOfferResult(invitationHandle) {
return offerResultStore.get(invitationHandle);
return invitationHandleToOfferResult.get(invitationHandle);
}

async function getUINotifier(rawId, dappOrigin = 'unknown') {
const id = makeId(dappOrigin, rawId);
const offerResult = idToOfferResult.get(id);
assert(
passStyleOf(offerResult) === 'copyRecord',
`offerResult must be a record to have a uiNotifier`,
);
assert(offerResult.uiNotifier, `offerResult does not have a uiNotifier`);
return offerResult.uiNotifier;
}

const wallet = harden({
Expand Down Expand Up @@ -1396,6 +1408,7 @@ export function makeWallet({
getPaymentsNotifier() {
return paymentsNotifier;
},
getUINotifier,
});

const initialize = async () => {
Expand Down
3 changes: 3 additions & 0 deletions packages/dapp-svelte-wallet/api/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@
* @property {(petname: Petname, instanceBoardId: string) => Promise<void>}
* suggestInstance Introduce a Zoe contract instance to the wallet, with
* suggested petname.
* @property {(rawId: string) => Promise<Notifier<any>>} getUINotifier Get the UI notifier from the offerResult
* for a particular offer, identified by id. This notifier should only
* contain information that is safe to pass to the dapp UI.
*/

/**
Expand Down
33 changes: 7 additions & 26 deletions packages/dapp-svelte-wallet/api/src/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ export function buildRootObject(_vatPowers) {
await approve();
return walletAdmin.suggestInstance(petname, boardId, dappOrigin);
},
async getUINotifier(rawId) {
await approve();
return walletAdmin.getUINotifier(rawId, dappOrigin);
},
};
return harden(bridge);
};
Expand Down Expand Up @@ -195,6 +199,9 @@ export function buildRootObject(_vatPowers) {
suggestIssuer(petname, issuerBoardId) {
return walletAdmin.suggestIssuer(petname, issuerBoardId);
},
getUINotifier(rawId) {
return walletAdmin.getUINotifier(rawId);
},
};
harden(preapprovedBridge);

Expand Down Expand Up @@ -386,32 +393,6 @@ export function buildRootObject(_vatPowers) {
case 'walletAddOffer': {
let handled = false;
const actions = harden({
result(offer, outcome) {
httpSend(
{
type: 'walletOfferResult',
data: {
id: offer.id,
dappContext: offer.dappContext,
outcome,
},
},
[meta.channelHandle],
);
},
error(offer, reason) {
httpSend(
{
type: 'walletOfferResult',
data: {
id: offer.id,
dappContext: offer.dappContext,
error: `${(reason && reason.stack) || reason}`,
},
},
[meta.channelHandle],
);
},
handled(offer) {
if (handled) {
return;
Expand Down
12 changes: 7 additions & 5 deletions packages/dapp-svelte-wallet/api/test/test-lib-wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ test('lib-wallet dapp suggests issuer, instance, installation petnames', async t
});

test('lib-wallet offer methods', async t => {
t.plan(8);
t.plan(7);
const {
moolaBundle,
wallet,
Expand Down Expand Up @@ -630,8 +630,7 @@ test('lib-wallet offer methods', async t => {
);
const accepted = await wallet.acceptOffer(id);
assert(accepted);
const { outcome, depositedP } = accepted;
t.is(await outcome, 'The offer was accepted', `offer was accepted`);
const { depositedP } = accepted;
await depositedP;
const seats = wallet.getSeats(harden([id]));
const seat = wallet.getSeat(id);
Expand Down Expand Up @@ -881,8 +880,11 @@ test('lib-wallet addOffer for autoswap swap', async t => {

const accepted = await wallet.acceptOffer(id);
assert(accepted);
const { outcome, depositedP } = accepted;
t.is(await outcome, 'Swap successfully completed.', `offer was accepted`);
const { depositedP } = accepted;
await t.throwsAsync(() => wallet.getUINotifier(rawId, `unknown`), {
message: 'offerResult must be a record to have a uiNotifier',
});

await depositedP;
const seats = wallet.getSeats(harden([id]));
const seat = wallet.getSeat(id);
Expand Down

0 comments on commit 0dde1bc

Please sign in to comment.