From 268ca9924435ec65dcb15da0b425b608d4407008 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Mon, 22 Feb 2016 23:12:57 +0100 Subject: fix amount arithmetic --- extension/lib/wallet/types.ts | 108 ++++++++++++++++++++++++++++-------------- 1 file changed, 72 insertions(+), 36 deletions(-) (limited to 'extension') diff --git a/extension/lib/wallet/types.ts b/extension/lib/wallet/types.ts index 77603e083..8151bf41a 100644 --- a/extension/lib/wallet/types.ts +++ b/extension/lib/wallet/types.ts @@ -105,6 +105,7 @@ export interface ReserveCreationInfo { mintInfo: IMintInfo; selectedDenoms: Denomination[]; withdrawFee: AmountJson; + overhead: AmountJson; } @@ -157,7 +158,7 @@ export namespace Amounts { saturated: boolean; } - function getMaxAmount(currency: string) { + function getMaxAmount(currency: string): AmountJson { return { currency, value: Number.MAX_SAFE_INTEGER, @@ -165,25 +166,39 @@ export namespace Amounts { } } - export function add(first: AmountJson, ...rest: AmountJson[]): Result { - let currency = first.currency; - let value = first.value + Math.floor(first.fraction / 1e6); - if (value > Number.MAX_SAFE_INTEGER) { - return {amount: getMaxAmount(currency), saturated: true}; + export function getZero(currency: string): AmountJson { + return { + currency, + value: 0, + fraction: 0, } - let fraction = first.fraction; - for (let x of rest) { - if (x.currency !== currency) { - throw Error(`Mismatched currency: ${x.currency} and ${currency}`); - } + } - fraction = (fraction + x.fraction) % 1e6; - value = value + x.value + (Math.floor(fraction + x.fraction) / 1e6); + export function add(first: AmountJson, ...rest: AmountJson[]): Result { + const doit = () => { + let currency = first.currency; + let value = first.value + Math.floor(first.fraction / 1e6); if (value > Number.MAX_SAFE_INTEGER) { return {amount: getMaxAmount(currency), saturated: true}; } - } - return {amount: {currency, value, fraction}, saturated: false}; + let fraction = first.fraction % 1e6; + for (let x of rest) { + if (x.currency !== currency) { + throw Error(`Mismatched currency: ${x.currency} and ${currency}`); + } + + value = value + x.value + Math.floor((fraction + x.fraction) / 1e6); + fraction = (fraction + x.fraction) % 1e6; + if (value > Number.MAX_SAFE_INTEGER) { + return {amount: getMaxAmount(currency), saturated: true}; + } + } + return {amount: {currency, value, fraction}, saturated: false}; + }; + console.log("adding", first, "and", rest); + let ret = doit(); + console.log("result is", ret); + return ret; } @@ -199,7 +214,7 @@ export namespace Amounts { return {amount: {currency, value: 0, fraction: 0}, saturated: true}; } value--; - fraction = +1e6; + fraction += 1e6; } console.assert(fraction >= b.fraction); fraction -= b.fraction; @@ -211,30 +226,51 @@ export namespace Amounts { } export function cmp(a: AmountJson, b: AmountJson): number { - if (a.currency !== b.currency) { - throw Error(`Mismatched currency: ${a.currency} and ${b.currency}`); - } - let av = a.value + Math.floor(a.fraction / 1e6); - let af = a.fraction % 1e6; - let bv = b.value + Math.floor(b.fraction / 1e6); - let bf = b.fraction % 1e6; - switch (true) { - case av < bv: - return -1; - case av > bv: - return 1; - case af < bf: - return -1; - case af > bf: - return 1; - case af == bf: - return 0; - default: - throw Error("assertion failed"); + const doit = () => { + if (a.currency !== b.currency) { + throw Error(`Mismatched currency: ${a.currency} and ${b.currency}`); + } + let av = a.value + Math.floor(a.fraction / 1e6); + let af = a.fraction % 1e6; + let bv = b.value + Math.floor(b.fraction / 1e6); + let bf = b.fraction % 1e6; + switch (true) { + case av < bv: + return -1; + case av > bv: + return 1; + case af < bf: + return -1; + case af > bf: + return 1; + case af == bf: + return 0; + default: + throw Error("assertion failed"); + } + }; + + console.log("comparing", a, "and", b); + let res = doit(); + console.log("result:", res); + return res; + + } + + export function copy(a: AmountJson): AmountJson { + return { + value: a.value, + fraction: a.fraction, + currency: a.currency, } } + + export function isNonZero(a: AmountJson) { + return a.value > 0 || a.fraction > 0; + } } + export interface Notifier { notify(); } \ No newline at end of file -- cgit v1.2.3