From 82742861d29a9d75da4de90322a128bcad5da503 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Mon, 22 Feb 2016 23:13:28 +0100 Subject: don't use emsc directly in wallet; show more reserve creation detail --- extension/background/main.ts | 16 ------ extension/lib/wallet/wallet.ts | 80 +++++++++++++++--------------- extension/manifest.json | 1 - extension/pages/confirm-create-reserve.js | 42 +++++++++------- extension/pages/confirm-create-reserve.tsx | 45 ++++++++++------- 5 files changed, 91 insertions(+), 93 deletions(-) diff --git a/extension/background/main.ts b/extension/background/main.ts index 4ec2c4d5d..746d81a60 100644 --- a/extension/background/main.ts +++ b/extension/background/main.ts @@ -26,22 +26,6 @@ System.config({ defaultJSExtensions: true, }); -// We expect that in the manifest, the emscripten js is loaded -// becore the background page. -// Currently it is not possible to use SystemJS to load the emscripten js. -declare var Module: any; -if ("object" !== typeof Module) { - throw Error("emscripten not loaded, no 'Module' defined"); -} - -// Manually register the emscripten js as a SystemJS, so that -// we can use it from TypeScript by importing it. -{ - let mod = System.newModule({Module: Module}); - let modName = System.normalizeSync("../lib/emscripten/emsc"); - console.log("registering", modName); - System.set(modName, mod); -} System.import("../lib/wallet/wxMessaging") .then((wxMessaging) => { diff --git a/extension/lib/wallet/wallet.ts b/extension/lib/wallet/wallet.ts index 8446d7194..76339fe5d 100644 --- a/extension/lib/wallet/wallet.ts +++ b/extension/lib/wallet/wallet.ts @@ -21,7 +21,6 @@ * @author Florian Dold */ -import * as native from "./emscriptif"; import {AmountJson, CreateReserveResponse, IMintInfo, Denomination, Notifier} from "./types"; import {HttpResponse, RequestException} from "./http"; import {Query} from "./query"; @@ -344,15 +343,6 @@ function copy(o) { } -/** - * Rank two denomination by how desireable it is to withdraw them, - * based on their fees and value. - */ -function rankDenom(denom1: Denomination, denom2: Denomination) { - return (-1) * Amounts.cmp(denom1.value, denom2.value); -} - - /** * Get a list of denominations (with repetitions possible) * whose total value is as close as possible to the available @@ -360,11 +350,15 @@ function rankDenom(denom1: Denomination, denom2: Denomination) { */ function getWithdrawDenomList(amountAvailable: AmountJson, denoms: Denomination[]): Denomination[] { - let remaining = new native.Amount(amountAvailable); + let remaining = Amounts.copy(amountAvailable); let ds: Denomination[] = []; denoms = denoms.filter(isWithdrawableDenom); - denoms.sort(rankDenom); + denoms.sort((d1, d2) => Amounts.cmp(d2.value, d1.value)); + + console.log("ranked denoms"); + console.dir(denoms); + // This is an arbitrary number of coins // we can withdraw in one go. It's not clear if this limit @@ -372,17 +366,17 @@ function getWithdrawDenomList(amountAvailable: AmountJson, for (let i = 0; i < 1000; i++) { let found = false; for (let d of denoms) { - let cost = new native.Amount(d.value); - cost.add(new native.Amount(d.fee_withdraw)); - if (remaining.cmp(cost) < 0) { + let cost = Amounts.add(d.value, d.fee_withdraw).amount; + if (Amounts.cmp(remaining, cost) < 0) { continue; } found = true; - remaining.sub(cost); + remaining = Amounts.sub(remaining, cost).amount; ds.push(d); + break; } if (!found) { - console.log("did not find coins for remaining ", remaining.toJson()); + console.log("did not find coins for remaining ", remaining); break; } } @@ -466,34 +460,32 @@ export class Wallet { nextMint: for (let key in m) { - let coins = m[key].map((x) => ({ - a: new native.Amount(x.denom.fee_deposit), - c: x - })); + let coins = m[key]; // Sort by ascending deposit fee - coins.sort((o1, o2) => o1.a.cmp(o2.a)); - let maxFee = new native.Amount(depositFeeLimit); - let minAmount = new native.Amount(paymentAmount); - let accFee = new native.Amount(coins[0].c.denom.fee_deposit); - let accAmount = native.Amount.getZero(coins[0].c.coin.currentAmount.currency); + coins.sort((o1, o2) => Amounts.cmp(o1.denom.fee_deposit, + o2.denom.fee_deposit)); + let maxFee = Amounts.copy(depositFeeLimit); + let minAmount = Amounts.copy(paymentAmount); + let accFee = Amounts.copy(coins[0].denom.fee_deposit); + let accAmount = Amounts.getZero(coins[0].coin.currentAmount.currency); let usableCoins: CoinWithDenom[] = []; nextCoin: for (let i = 0; i < coins.length; i++) { - let coinAmount = new native.Amount(coins[i].c.coin.currentAmount); - let coinFee = coins[i].a; - if (coinAmount.cmp(coinFee) <= 0) { + let coinAmount = Amounts.copy(coins[i].coin.currentAmount); + let coinFee = coins[i].denom.fee_deposit; + if (Amounts.cmp(coinAmount, coinFee) <= 0) { continue nextCoin; } - accFee.add(coinFee); - accAmount.add(coinAmount); - if (accFee.cmp(maxFee) >= 0) { + accFee = Amounts.add(accFee, coinFee).amount; + accAmount = Amounts.add(accAmount, coinAmount).amount; + if (Amounts.cmp(accFee, maxFee) >= 0) { // FIXME: if the fees are too high, we have // to cover them ourselves .... console.log("too much fees"); continue nextMint; } - usableCoins.push(coins[i].c); - if (accAmount.cmp(minAmount) >= 0) { + usableCoins.push(coins[i]); + if (Amounts.cmp(accAmount, minAmount) >= 0) { ret[key] = usableCoins; continue nextMint; } @@ -848,14 +840,21 @@ export class Wallet { let selectedDenoms = getWithdrawDenomList(amount, mintInfo.denoms); - let acc = native.Amount.getZero(amount.currency); + let acc = Amounts.getZero(amount.currency); for (let d of selectedDenoms) { - acc.add(new native.Amount(d.fee_withdraw)); + acc = Amounts.add(acc, d.fee_withdraw).amount; } + let actualCoinCost = selectedDenoms + .map((d: Denomination) => Amounts.add(d.value, + d.fee_withdraw).amount) + .reduce((a, b) => Amounts.add(a, b).amount); + console.log("actual coin cost", actualCoinCost); + console.log("amount", amount); let ret: ReserveCreationInfo = { mintInfo, selectedDenoms, - withdrawFee: acc.toJson(), + withdrawFee: acc, + overhead: Amounts.sub(amount, actualCoinCost).amount, }; return ret; }); @@ -911,11 +910,10 @@ export class Wallet { function collectBalances(c: Coin, byCurrency) { let acc: AmountJson = byCurrency[c.currentAmount.currency]; if (!acc) { - acc = native.Amount.getZero(c.currentAmount.currency).toJson(); + acc = Amounts.getZero(c.currentAmount.currency); } - let am = new native.Amount(c.currentAmount); - am.add(new native.Amount(acc)); - byCurrency[c.currentAmount.currency] = am.toJson(); + byCurrency[c.currentAmount.currency] = Amounts.add(c.currentAmount, + acc).amount; return byCurrency; } diff --git a/extension/manifest.json b/extension/manifest.json index c4c2de987..6ed8f0948 100644 --- a/extension/manifest.json +++ b/extension/manifest.json @@ -43,7 +43,6 @@ "scripts": [ "lib/vendor/URI.js", "lib/vendor/lodash.core.min.js", - "lib/emscripten/libwrapper.js", "lib/vendor/system-csp-production.src.js", "background/main.js" ] diff --git a/extension/pages/confirm-create-reserve.js b/extension/pages/confirm-create-reserve.js index 610697246..97b2314ff 100644 --- a/extension/pages/confirm-create-reserve.js +++ b/extension/pages/confirm-create-reserve.js @@ -16,7 +16,7 @@ System.register(["../lib/wallet/helpers", "../lib/wallet/types", "mithril", "../lib/wallet/wxApi"], function(exports_1, context_1) { "use strict"; var __moduleName = context_1 && context_1.id; - var helpers_1, types_1, mithril_1, wxApi_1; + var helpers_1, types_1, mithril_1, types_2, wxApi_1; var DelayTimer, Controller; function view(ctrl) { var controls = []; @@ -46,28 +46,29 @@ System.register(["../lib/wallet/helpers", "../lib/wallet/types", "mithril", "../ mx("p", "Checking URL, please wait ..."); } if (ctrl.reserveCreationInfo) { - var withdrawFeeStr = helpers_1.amountToPretty(ctrl.reserveCreationInfo.withdrawFee); - mx("p", "Fee for withdrawal: " + withdrawFeeStr); + var totalCost = types_2.Amounts.add(ctrl.reserveCreationInfo.overhead, ctrl.reserveCreationInfo.withdrawFee).amount; + mx("p", "Withdraw cost: " + helpers_1.amountToPretty(totalCost)); if (ctrl.detailCollapsed()) { mx("button.linky", { onclick: function () { ctrl.detailCollapsed(false); } - }, "show more"); + }, "show more details"); } else { mx("button.linky", { onclick: function () { ctrl.detailCollapsed(true); } - }, "show less"); - mx("div", {}, renderCoinTable(ctrl.reserveCreationInfo.selectedDenoms)); + }, "hide details"); + mx("div", {}, renderReserveCreationDetails(ctrl.reserveCreationInfo)); } } return mithril_1.default("div", controls); var _a; } - function renderCoinTable(denoms) { + function renderReserveCreationDetails(rci) { + var denoms = rci.selectedDenoms; function row(denom) { return mithril_1.default("tr", [ mithril_1.default("td", denom.pub_hash.substr(0, 5) + "..."), @@ -77,16 +78,22 @@ System.register(["../lib/wallet/helpers", "../lib/wallet/types", "mithril", "../ mithril_1.default("td", helpers_1.amountToPretty(denom.fee_deposit)), ]); } - return mithril_1.default("table", [ - mithril_1.default("tr", [ - mithril_1.default("th", "Key Hash"), - mithril_1.default("th", "Value"), - mithril_1.default("th", "Withdraw Fee"), - mithril_1.default("th", "Refresh Fee"), - mithril_1.default("th", "Deposit Fee"), - ]), - denoms.map(row) - ]); + var withdrawFeeStr = helpers_1.amountToPretty(rci.withdrawFee); + var overheadStr = helpers_1.amountToPretty(rci.overhead); + return [ + mithril_1.default("p", "Fee for withdrawal: " + withdrawFeeStr), + mithril_1.default("p", "Overhead: " + overheadStr), + mithril_1.default("table", [ + mithril_1.default("tr", [ + mithril_1.default("th", "Key Hash"), + mithril_1.default("th", "Value"), + mithril_1.default("th", "Withdraw Fee"), + mithril_1.default("th", "Refresh Fee"), + mithril_1.default("th", "Deposit Fee"), + ]), + denoms.map(row) + ]) + ]; } function probeMint(mintBaseUrl) { throw Error("not implemented"); @@ -131,6 +138,7 @@ System.register(["../lib/wallet/helpers", "../lib/wallet/types", "mithril", "../ }, function (types_1_1) { types_1 = types_1_1; + types_2 = types_1_1; }, function (mithril_1_1) { mithril_1 = mithril_1_1; diff --git a/extension/pages/confirm-create-reserve.tsx b/extension/pages/confirm-create-reserve.tsx index 9ae2938f3..2c42813a1 100644 --- a/extension/pages/confirm-create-reserve.tsx +++ b/extension/pages/confirm-create-reserve.tsx @@ -20,7 +20,7 @@ import {amountToPretty, canonicalizeBaseUrl} from "../lib/wallet/helpers"; import {AmountJson, CreateReserveResponse} from "../lib/wallet/types"; import m from "mithril"; import {IMintInfo} from "../lib/wallet/types"; -import {ReserveCreationInfo} from "../lib/wallet/types"; +import {ReserveCreationInfo, Amounts} from "../lib/wallet/types"; import MithrilComponent = _mithril.MithrilComponent; import {Denomination} from "../lib/wallet/types"; import {getReserveCreationInfo} from "../lib/wallet/wxApi"; @@ -201,22 +201,22 @@ function view(ctrl: Controller) { } if (ctrl.reserveCreationInfo) { - let withdrawFeeStr = amountToPretty(ctrl.reserveCreationInfo.withdrawFee); - mx("p", `Fee for withdrawal: ${withdrawFeeStr}`); - + let totalCost = Amounts.add(ctrl.reserveCreationInfo.overhead, + ctrl.reserveCreationInfo.withdrawFee).amount; + mx("p", `Withdraw cost: ${amountToPretty(totalCost)}`); if (ctrl.detailCollapsed()) { mx("button.linky", { onclick: () => { ctrl.detailCollapsed(false); } - }, "show more"); + }, "show more details"); } else { mx("button.linky", { onclick: () => { ctrl.detailCollapsed(true); } - }, "show less"); - mx("div", {}, renderCoinTable(ctrl.reserveCreationInfo.selectedDenoms)) + }, "hide details"); + mx("div", {}, renderReserveCreationDetails(ctrl.reserveCreationInfo)) } } @@ -224,7 +224,9 @@ function view(ctrl: Controller) { } -function renderCoinTable(denoms: Denomination[]) { +function renderReserveCreationDetails(rci: ReserveCreationInfo) { + let denoms = rci.selectedDenoms; + function row(denom: Denomination) { return m("tr", [ m("td", denom.pub_hash.substr(0, 5) + "..."), @@ -234,16 +236,23 @@ function renderCoinTable(denoms: Denomination[]) { m("td", amountToPretty(denom.fee_deposit)), ]); } - return m("table", [ - m("tr", [ - m("th", "Key Hash"), - m("th", "Value"), - m("th", "Withdraw Fee"), - m("th", "Refresh Fee"), - m("th", "Deposit Fee"), - ]), - denoms.map(row) - ]); + + let withdrawFeeStr = amountToPretty(rci.withdrawFee); + let overheadStr = amountToPretty(rci.overhead); + return [ + m("p", `Fee for withdrawal: ${withdrawFeeStr}`), + m("p", `Overhead: ${overheadStr}`), + m("table", [ + m("tr", [ + m("th", "Key Hash"), + m("th", "Value"), + m("th", "Withdraw Fee"), + m("th", "Refresh Fee"), + m("th", "Deposit Fee"), + ]), + denoms.map(row) + ]) + ]; } -- cgit v1.2.3