Skip to content

Commit

Permalink
feat: add wallet offer publicID querying API to the bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Mar 23, 2020
1 parent eba7dff commit 4010226
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
26 changes: 20 additions & 6 deletions packages/cosmic-swingset/lib/ag-solo/vats/lib-wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function makeWallet(
const brandToMath = makeStore();

// Offers that the wallet knows about (the inbox).
const idToOffer = new Map();
const idToOffer = makeStore();

// Compiled offers (all ready to execute).
const idToCompiledOfferP = new Map();
Expand Down Expand Up @@ -193,12 +193,25 @@ export async function makeWallet(
return petnameToPurse.entries();
}

function getPendingPublicIDsByOrigin(origin) {
return idToOffer
.values()
.filter(
offer =>
offer.status === 'pending' &&
offer.publicID &&
offer.requestContext.origin === origin,
)
.map(offer => offer.publicID);
}

function getOffers() {
// return the offers sorted by id
return Array.from(idToOffer)
.filter(p => p[1].status === 'accept')
.sort((p1, p2) => p1[0] > p2[0])
.map(p => harden(p[1]));
return idToOffer
.entries()
.filter(([_id, offer]) => offer.status === 'accept')
.sort(([id1], [id2]) => id1 > id2)
.map(([_id, offer]) => harden(offer));
}

const compileOffer = makeOfferCompiler({
Expand Down Expand Up @@ -228,7 +241,7 @@ export async function makeWallet(
requestContext,
status: undefined,
};
idToOffer.set(id, offer);
idToOffer.init(id, offer);
updateInboxState(id, offer);

// Start compiling the template, saving a promise for it.
Expand Down Expand Up @@ -415,6 +428,7 @@ export async function makeWallet(
cancelOffer,
acceptOffer,
getOffers,
getPendingPublicIDsByOrigin,
});

return wallet;
Expand Down
13 changes: 11 additions & 2 deletions packages/cosmic-swingset/lib/ag-solo/vats/vat-wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import harden from '@agoric/harden';
import { makeWallet } from './lib-wallet';
import pubsub from './pubsub';

function build(E, D, _log) {
function build(E, _D, _log) {
let wallet;
let pursesState;
let inboxState;
Expand All @@ -25,7 +25,7 @@ function build(E, D, _log) {
http = o;
}

async function adminOnMessage(obj, meta) {
async function adminOnMessage(obj, meta = { origin: 'unknown' }) {
const { type, data } = obj;
switch (type) {
case 'walletGetPurses': {
Expand Down Expand Up @@ -76,6 +76,14 @@ function build(E, D, _log) {
};
}

case 'walletGetOfferPublicIDs': {
const result = await wallet.getPendingPublicIDsByOrigin(meta.origin);
return {
type: 'walletOfferPublicIDs',
data: result,
};
}

default: {
return false;
}
Expand Down Expand Up @@ -151,6 +159,7 @@ function build(E, D, _log) {
switch (type) {
case 'walletGetPurses':
case 'walletAddOffer':
case 'walletGetOfferDescriptions':
// Override the origin since we got it from the bridge.
return adminOnMessage(obj, {
...meta,
Expand Down

0 comments on commit 4010226

Please sign in to comment.