don't use emsc directly in wallet; show more reserve creation detail

This commit is contained in:
Florian Dold 2016-02-22 23:13:28 +01:00
parent 268ca99244
commit 82742861d2
5 changed files with 91 additions and 93 deletions

View File

@ -26,22 +26,6 @@ System.config({
defaultJSExtensions: true, 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") System.import("../lib/wallet/wxMessaging")
.then((wxMessaging) => { .then((wxMessaging) => {

View File

@ -21,7 +21,6 @@
* @author Florian Dold * @author Florian Dold
*/ */
import * as native from "./emscriptif";
import {AmountJson, CreateReserveResponse, IMintInfo, Denomination, Notifier} from "./types"; import {AmountJson, CreateReserveResponse, IMintInfo, Denomination, Notifier} from "./types";
import {HttpResponse, RequestException} from "./http"; import {HttpResponse, RequestException} from "./http";
import {Query} from "./query"; 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) * Get a list of denominations (with repetitions possible)
* whose total value is as close as possible to the available * 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, function getWithdrawDenomList(amountAvailable: AmountJson,
denoms: Denomination[]): Denomination[] { denoms: Denomination[]): Denomination[] {
let remaining = new native.Amount(amountAvailable); let remaining = Amounts.copy(amountAvailable);
let ds: Denomination[] = []; let ds: Denomination[] = [];
denoms = denoms.filter(isWithdrawableDenom); 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 // This is an arbitrary number of coins
// we can withdraw in one go. It's not clear if this limit // 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++) { for (let i = 0; i < 1000; i++) {
let found = false; let found = false;
for (let d of denoms) { for (let d of denoms) {
let cost = new native.Amount(d.value); let cost = Amounts.add(d.value, d.fee_withdraw).amount;
cost.add(new native.Amount(d.fee_withdraw)); if (Amounts.cmp(remaining, cost) < 0) {
if (remaining.cmp(cost) < 0) {
continue; continue;
} }
found = true; found = true;
remaining.sub(cost); remaining = Amounts.sub(remaining, cost).amount;
ds.push(d); ds.push(d);
break;
} }
if (!found) { if (!found) {
console.log("did not find coins for remaining ", remaining.toJson()); console.log("did not find coins for remaining ", remaining);
break; break;
} }
} }
@ -466,34 +460,32 @@ export class Wallet {
nextMint: nextMint:
for (let key in m) { for (let key in m) {
let coins = m[key].map((x) => ({ let coins = m[key];
a: new native.Amount(x.denom.fee_deposit),
c: x
}));
// Sort by ascending deposit fee // Sort by ascending deposit fee
coins.sort((o1, o2) => o1.a.cmp(o2.a)); coins.sort((o1, o2) => Amounts.cmp(o1.denom.fee_deposit,
let maxFee = new native.Amount(depositFeeLimit); o2.denom.fee_deposit));
let minAmount = new native.Amount(paymentAmount); let maxFee = Amounts.copy(depositFeeLimit);
let accFee = new native.Amount(coins[0].c.denom.fee_deposit); let minAmount = Amounts.copy(paymentAmount);
let accAmount = native.Amount.getZero(coins[0].c.coin.currentAmount.currency); let accFee = Amounts.copy(coins[0].denom.fee_deposit);
let accAmount = Amounts.getZero(coins[0].coin.currentAmount.currency);
let usableCoins: CoinWithDenom[] = []; let usableCoins: CoinWithDenom[] = [];
nextCoin: nextCoin:
for (let i = 0; i < coins.length; i++) { for (let i = 0; i < coins.length; i++) {
let coinAmount = new native.Amount(coins[i].c.coin.currentAmount); let coinAmount = Amounts.copy(coins[i].coin.currentAmount);
let coinFee = coins[i].a; let coinFee = coins[i].denom.fee_deposit;
if (coinAmount.cmp(coinFee) <= 0) { if (Amounts.cmp(coinAmount, coinFee) <= 0) {
continue nextCoin; continue nextCoin;
} }
accFee.add(coinFee); accFee = Amounts.add(accFee, coinFee).amount;
accAmount.add(coinAmount); accAmount = Amounts.add(accAmount, coinAmount).amount;
if (accFee.cmp(maxFee) >= 0) { if (Amounts.cmp(accFee, maxFee) >= 0) {
// FIXME: if the fees are too high, we have // FIXME: if the fees are too high, we have
// to cover them ourselves .... // to cover them ourselves ....
console.log("too much fees"); console.log("too much fees");
continue nextMint; continue nextMint;
} }
usableCoins.push(coins[i].c); usableCoins.push(coins[i]);
if (accAmount.cmp(minAmount) >= 0) { if (Amounts.cmp(accAmount, minAmount) >= 0) {
ret[key] = usableCoins; ret[key] = usableCoins;
continue nextMint; continue nextMint;
} }
@ -848,14 +840,21 @@ export class Wallet {
let selectedDenoms = getWithdrawDenomList(amount, let selectedDenoms = getWithdrawDenomList(amount,
mintInfo.denoms); mintInfo.denoms);
let acc = native.Amount.getZero(amount.currency); let acc = Amounts.getZero(amount.currency);
for (let d of selectedDenoms) { 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 = { let ret: ReserveCreationInfo = {
mintInfo, mintInfo,
selectedDenoms, selectedDenoms,
withdrawFee: acc.toJson(), withdrawFee: acc,
overhead: Amounts.sub(amount, actualCoinCost).amount,
}; };
return ret; return ret;
}); });
@ -911,11 +910,10 @@ export class Wallet {
function collectBalances(c: Coin, byCurrency) { function collectBalances(c: Coin, byCurrency) {
let acc: AmountJson = byCurrency[c.currentAmount.currency]; let acc: AmountJson = byCurrency[c.currentAmount.currency];
if (!acc) { if (!acc) {
acc = native.Amount.getZero(c.currentAmount.currency).toJson(); acc = Amounts.getZero(c.currentAmount.currency);
} }
let am = new native.Amount(c.currentAmount); byCurrency[c.currentAmount.currency] = Amounts.add(c.currentAmount,
am.add(new native.Amount(acc)); acc).amount;
byCurrency[c.currentAmount.currency] = am.toJson();
return byCurrency; return byCurrency;
} }

View File

@ -43,7 +43,6 @@
"scripts": [ "scripts": [
"lib/vendor/URI.js", "lib/vendor/URI.js",
"lib/vendor/lodash.core.min.js", "lib/vendor/lodash.core.min.js",
"lib/emscripten/libwrapper.js",
"lib/vendor/system-csp-production.src.js", "lib/vendor/system-csp-production.src.js",
"background/main.js" "background/main.js"
] ]

View File

@ -16,7 +16,7 @@
System.register(["../lib/wallet/helpers", "../lib/wallet/types", "mithril", "../lib/wallet/wxApi"], function(exports_1, context_1) { System.register(["../lib/wallet/helpers", "../lib/wallet/types", "mithril", "../lib/wallet/wxApi"], function(exports_1, context_1) {
"use strict"; "use strict";
var __moduleName = context_1 && context_1.id; 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; var DelayTimer, Controller;
function view(ctrl) { function view(ctrl) {
var controls = []; var controls = [];
@ -46,28 +46,29 @@ System.register(["../lib/wallet/helpers", "../lib/wallet/types", "mithril", "../
mx("p", "Checking URL, please wait ..."); mx("p", "Checking URL, please wait ...");
} }
if (ctrl.reserveCreationInfo) { if (ctrl.reserveCreationInfo) {
var withdrawFeeStr = helpers_1.amountToPretty(ctrl.reserveCreationInfo.withdrawFee); var totalCost = types_2.Amounts.add(ctrl.reserveCreationInfo.overhead, ctrl.reserveCreationInfo.withdrawFee).amount;
mx("p", "Fee for withdrawal: " + withdrawFeeStr); mx("p", "Withdraw cost: " + helpers_1.amountToPretty(totalCost));
if (ctrl.detailCollapsed()) { if (ctrl.detailCollapsed()) {
mx("button.linky", { mx("button.linky", {
onclick: function () { onclick: function () {
ctrl.detailCollapsed(false); ctrl.detailCollapsed(false);
} }
}, "show more"); }, "show more details");
} }
else { else {
mx("button.linky", { mx("button.linky", {
onclick: function () { onclick: function () {
ctrl.detailCollapsed(true); ctrl.detailCollapsed(true);
} }
}, "show less"); }, "hide details");
mx("div", {}, renderCoinTable(ctrl.reserveCreationInfo.selectedDenoms)); mx("div", {}, renderReserveCreationDetails(ctrl.reserveCreationInfo));
} }
} }
return mithril_1.default("div", controls); return mithril_1.default("div", controls);
var _a; var _a;
} }
function renderCoinTable(denoms) { function renderReserveCreationDetails(rci) {
var denoms = rci.selectedDenoms;
function row(denom) { function row(denom) {
return mithril_1.default("tr", [ return mithril_1.default("tr", [
mithril_1.default("td", denom.pub_hash.substr(0, 5) + "..."), 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)), mithril_1.default("td", helpers_1.amountToPretty(denom.fee_deposit)),
]); ]);
} }
return mithril_1.default("table", [ var withdrawFeeStr = helpers_1.amountToPretty(rci.withdrawFee);
mithril_1.default("tr", [ var overheadStr = helpers_1.amountToPretty(rci.overhead);
mithril_1.default("th", "Key Hash"), return [
mithril_1.default("th", "Value"), mithril_1.default("p", "Fee for withdrawal: " + withdrawFeeStr),
mithril_1.default("th", "Withdraw Fee"), mithril_1.default("p", "Overhead: " + overheadStr),
mithril_1.default("th", "Refresh Fee"), mithril_1.default("table", [
mithril_1.default("th", "Deposit Fee"), mithril_1.default("tr", [
]), mithril_1.default("th", "Key Hash"),
denoms.map(row) 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) { function probeMint(mintBaseUrl) {
throw Error("not implemented"); throw Error("not implemented");
@ -131,6 +138,7 @@ System.register(["../lib/wallet/helpers", "../lib/wallet/types", "mithril", "../
}, },
function (types_1_1) { function (types_1_1) {
types_1 = types_1_1; types_1 = types_1_1;
types_2 = types_1_1;
}, },
function (mithril_1_1) { function (mithril_1_1) {
mithril_1 = mithril_1_1; mithril_1 = mithril_1_1;

View File

@ -20,7 +20,7 @@ import {amountToPretty, canonicalizeBaseUrl} from "../lib/wallet/helpers";
import {AmountJson, CreateReserveResponse} from "../lib/wallet/types"; import {AmountJson, CreateReserveResponse} from "../lib/wallet/types";
import m from "mithril"; import m from "mithril";
import {IMintInfo} from "../lib/wallet/types"; import {IMintInfo} from "../lib/wallet/types";
import {ReserveCreationInfo} from "../lib/wallet/types"; import {ReserveCreationInfo, Amounts} from "../lib/wallet/types";
import MithrilComponent = _mithril.MithrilComponent; import MithrilComponent = _mithril.MithrilComponent;
import {Denomination} from "../lib/wallet/types"; import {Denomination} from "../lib/wallet/types";
import {getReserveCreationInfo} from "../lib/wallet/wxApi"; import {getReserveCreationInfo} from "../lib/wallet/wxApi";
@ -201,22 +201,22 @@ function view(ctrl: Controller) {
} }
if (ctrl.reserveCreationInfo) { if (ctrl.reserveCreationInfo) {
let withdrawFeeStr = amountToPretty(ctrl.reserveCreationInfo.withdrawFee); let totalCost = Amounts.add(ctrl.reserveCreationInfo.overhead,
mx("p", `Fee for withdrawal: ${withdrawFeeStr}`); ctrl.reserveCreationInfo.withdrawFee).amount;
mx("p", `Withdraw cost: ${amountToPretty(totalCost)}`);
if (ctrl.detailCollapsed()) { if (ctrl.detailCollapsed()) {
mx("button.linky", { mx("button.linky", {
onclick: () => { onclick: () => {
ctrl.detailCollapsed(false); ctrl.detailCollapsed(false);
} }
}, "show more"); }, "show more details");
} else { } else {
mx("button.linky", { mx("button.linky", {
onclick: () => { onclick: () => {
ctrl.detailCollapsed(true); ctrl.detailCollapsed(true);
} }
}, "show less"); }, "hide details");
mx("div", {}, renderCoinTable(ctrl.reserveCreationInfo.selectedDenoms)) 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) { function row(denom: Denomination) {
return m("tr", [ return m("tr", [
m("td", denom.pub_hash.substr(0, 5) + "..."), m("td", denom.pub_hash.substr(0, 5) + "..."),
@ -234,16 +236,23 @@ function renderCoinTable(denoms: Denomination[]) {
m("td", amountToPretty(denom.fee_deposit)), m("td", amountToPretty(denom.fee_deposit)),
]); ]);
} }
return m("table", [
m("tr", [ let withdrawFeeStr = amountToPretty(rci.withdrawFee);
m("th", "Key Hash"), let overheadStr = amountToPretty(rci.overhead);
m("th", "Value"), return [
m("th", "Withdraw Fee"), m("p", `Fee for withdrawal: ${withdrawFeeStr}`),
m("th", "Refresh Fee"), m("p", `Overhead: ${overheadStr}`),
m("th", "Deposit Fee"), m("table", [
]), m("tr", [
denoms.map(row) m("th", "Key Hash"),
]); m("th", "Value"),
m("th", "Withdraw Fee"),
m("th", "Refresh Fee"),
m("th", "Deposit Fee"),
]),
denoms.map(row)
])
];
} }