Skip to content

Commit

Permalink
fix: proper sorting of wallet entries
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Mar 23, 2020
1 parent 4010226 commit 24627eb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 28 deletions.
36 changes: 19 additions & 17 deletions packages/cosmic-swingset/lib/ag-solo/vats/lib-wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,21 @@ export async function makeWallet(
const inboxState = new Map();

function getPursesState() {
return JSON.stringify([...pursesState.values()]);
const entries = [...pursesState.entries()];
const values = entries
.sort(([id1], [id2]) => id1 > id2)
.map(([_id, value]) => value);

return JSON.stringify(values);
}

function getInboxState() {
return JSON.stringify([...inboxState.values()]);
const entries = [...inboxState.entries()];
const values = entries
.sort(([id1], [id2]) => id1 > id2)
.map(([_id, value]) => value);

return JSON.stringify(values);
}

async function updatePursesState(pursePetname, purse) {
Expand Down Expand Up @@ -193,23 +203,16 @@ 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() {
function getOffers({ status = 'accept', origin = null } = {}) {
// return the offers sorted by id
return idToOffer
.entries()
.filter(([_id, offer]) => offer.status === 'accept')
.filter(
([_id, offer]) =>
(status === null || offer.status === status) &&
(origin === null ||
(offer.requestContext && offer.requestContext.origin === origin)),
)
.sort(([id1], [id2]) => id1 > id2)
.map(([_id, offer]) => harden(offer));
}
Expand Down Expand Up @@ -428,7 +431,6 @@ export async function makeWallet(
cancelOffer,
acceptOffer,
getOffers,
getPendingPublicIDsByOrigin,
});

return wallet;
Expand Down
29 changes: 18 additions & 11 deletions packages/cosmic-swingset/lib/ag-solo/vats/vat-wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ function build(E, _D, _log) {
data: true,
};
}
// TODO: Maybe rename to walletGetOffers

case 'walletGetOffers':
case 'walletGetOfferDescriptions': {
const result = await wallet.getOffers(data);
return {
Expand All @@ -76,14 +77,6 @@ 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 @@ -154,17 +147,31 @@ function build(E, _D, _log) {
onClose(_obj, meta) {
bridgeHandles.delete(meta.channelHandle);
},
onMessage(obj, meta) {

async onMessage(obj, meta) {
const { type } = obj;
switch (type) {
case 'walletGetPurses':
case 'walletAddOffer':
case 'walletGetOfferDescriptions':
// Override the origin since we got it from the bridge.
return adminOnMessage(obj, {
...meta,
origin: obj.dappOrigin,
});

case 'walletGetOffers': {
const { status = null } = obj;
// Override the origin since we got it from the bridge.
const result = await wallet.getOffers({
origin: obj.dappOrigin,
status,
});
return {
type: 'walletOfferDescriptions',
data: result,
};
}

default:
return Promise.resolve(false);
}
Expand Down

0 comments on commit 24627eb

Please sign in to comment.