Skip to content

Commit

Permalink
fix: make wallet more robust, and handle decimals fully
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Nov 6, 2020
1 parent 97c10d0 commit 9c29e10
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 18 deletions.
42 changes: 31 additions & 11 deletions packages/dapp-svelte-wallet/api/src/lib-wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export async function makeWallet({
depositBoardId = brandToDepositFacetId.get(brand);
}

const issuerRecord = brandTable.getByBrand(brand);
const issuerRecord = brandTable.hasByBrand(brand) && brandTable.getByBrand(brand);
/**
* @type {PursesJSONState}
*/
Expand Down Expand Up @@ -213,7 +213,7 @@ export async function makeWallet({

const displayProposal = proposalTemplate => {
const { want, give, exit = { onDemand: null } } = proposalTemplate;
const compile = pursePetnameValueKeywordRecord => {
const displayRecord = pursePetnameValueKeywordRecord => {
if (pursePetnameValueKeywordRecord === undefined) {
return undefined;
}
Expand All @@ -228,7 +228,9 @@ export async function makeWallet({
pursePetname = purseMapping.valToPetname.get(purse);
}

amount = harden({ ...amount, brand: purseToBrand.get(purse) });
const brand = purseToBrand.get(purse);
const issuerRecord = brandTable.hasByBrand(brand) && brandTable.getByBrand(brand);
amount = harden({ ...amount, brand, displayInfo: issuerRecord && issuerRecord.displayInfo });
const displayAmount = display(amount);
return [
keyword,
Expand All @@ -242,12 +244,12 @@ export async function makeWallet({
),
);
};
const proposal = {
want: compile(want),
give: compile(give),
const proposalForDisplay = {
want: displayRecord(want),
give: displayRecord(give),
exit,
};
return proposal;
return proposalForDisplay;
};

async function updateInboxState(id, offer, doPush = true) {
Expand Down Expand Up @@ -431,7 +433,16 @@ export async function makeWallet({
return undefined;
}),
);
});
})
.finally(() =>
// Try reclaiming any payments that were left on the table.
keywords.map((keyword, i) => {
const purse = purseKeywordRecord[keyword];
if (purse && payments[i]) {
return addPayment(payments[i], purse);
}
})
);

return { depositedP, seat };
}
Expand Down Expand Up @@ -602,7 +613,6 @@ export async function makeWallet({
const amount = {
brand,
value,
displayInfo: brandTable.getByBrand(brand).displayInfo,
};
return [keyword, amount];
},
Expand Down Expand Up @@ -924,7 +934,6 @@ export async function makeWallet({
})
.catch(rejected);
} catch (e) {
console.error('Have error', e);
if (offer.actions) {
E(offer.actions).error(offer, e);
}
Expand Down Expand Up @@ -968,6 +977,17 @@ export async function makeWallet({
const brand = await E(payment).getAllegedBrand();
const depositedPK = makePromiseKit();

/** @type {ERef<boolean>} */
let isAliveP = true;
if (brandTable.hasByBrand(brand)) {
isAliveP = E(brandTable.getByBrand(brand).issuer).isLive(payment);
}
const isAlive = await isAliveP;
if (!isAlive) {
// Nothing to do.
return;
}

/** @type {PaymentRecord} */
let paymentRecord = {
payment,
Expand All @@ -993,7 +1013,7 @@ export async function makeWallet({
} else {
purse = purseOrPetname;
}
const brandRecord = brandTable.getByBrand(brand);
const brandRecord = brandTable.hasByBrand(brand) && brandTable.getByBrand(brand);
paymentRecord = {
...paymentRecord,
...brandRecord,
Expand Down
6 changes: 6 additions & 0 deletions packages/dapp-svelte-wallet/api/test/test-lib-wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,9 @@ test('lib-wallet offer methods', async t => {
petname: 'moola',
},
value: 1,
displayInfo: {
amountMathKind: 'nat',
},
},
},
},
Expand Down Expand Up @@ -740,6 +743,9 @@ test('lib-wallet offer methods', async t => {
petname: 'moola',
},
value: 1,
displayInfo: {
amountMathKind: 'nat',
},
},
},
},
Expand Down
10 changes: 5 additions & 5 deletions packages/dapp-svelte-wallet/ui/src/Transaction.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
const makeRejected = context =>
function rejected(e) {
// TODO: Do something less blatant.
alert(`${context}: ${e}`);
// We expect our caller to see this result, so just log an error.
console.error(context, e);
};
const statusText = {
Expand Down Expand Up @@ -99,16 +99,16 @@
<Debug title="Offer Detail" target={item} />
</div>
<div>
{#each Object.entries(give) as [role, { amount, displayInfo, pursePetname }], i}
{#each Object.entries(give) as [role, { amount, pursePetname }], i}
<div>
<h6>Give</h6>
<Amount {amount} /> from <Petname name={pursePetname} {displayInfo} />
<Amount {amount} displayInfo={amount.displayInfo} /> from <Petname name={pursePetname} />
</div>
{/each}
{#each Object.entries(want) as [role, { amount, displayInfo, pursePetname }], i}
<div>
<h6>Want</h6>
<Amount {amount} /> into <Petname name={pursePetname} {displayInfo} />
<Amount {amount} displayInfo={amount.displayInfo} /> into <Petname name={pursePetname} />
</div>
{/each}
</div>
Expand Down
12 changes: 10 additions & 2 deletions packages/dapp-svelte-wallet/ui/src/captp.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,16 @@ export function makeCapTPConnection(makeConnection, { onReset }) {
abort = ctpAbort;
dispatch = ctpDispatch;

// Wait for the other side to finish loading.
await E.G(getBootstrap()).LOADING;
// Wait for the wallet to finish loading.
let lastUpdateCount;
while (true) {
const update = await E(E.G(getBootstrap()).loadingNotifier).getUpdateSince(lastUpdateCount);
console.log('waiting for wallet');
lastUpdateCount = update.updateCount;
if (!update.value.includes('wallet')) {
break;
}
}

// Begin the flow of messages to our wallet, which
// we refetch from the new, loaded, bootstrap promise.
Expand Down

0 comments on commit 9c29e10

Please sign in to comment.