From 01ac7fced743961ea235f75e7b7b77711f0b455d Mon Sep 17 00:00:00 2001 From: Tom Kirkpatrick Date: Mon, 16 Dec 2019 08:59:31 +0100 Subject: [PATCH] feat(wallet): handle fees using msats --- .../components/Pay/PaySummaryLightning.js | 13 ++++---- utils/btc.js | 2 +- utils/crypto.js | 30 +++++-------------- utils/fee.js | 3 +- 4 files changed, 17 insertions(+), 31 deletions(-) diff --git a/renderer/components/Pay/PaySummaryLightning.js b/renderer/components/Pay/PaySummaryLightning.js index d2eb8e890d6..ec1a3ef729a 100644 --- a/renderer/components/Pay/PaySummaryLightning.js +++ b/renderer/components/Pay/PaySummaryLightning.js @@ -40,9 +40,9 @@ class PaySummaryLightning extends React.Component { renderFee() { const { exactFee, maxFee, minFee } = this.props - const hasExactFee = Number.isFinite(exactFee) - const hasMinFee = Number.isFinite(minFee) - const hasMaxFee = Number.isFinite(maxFee) + const hasExactFee = CoinBig(exactFee).isFinite() + const hasMinFee = CoinBig(minFee).isFinite() + const hasMaxFee = CoinBig(maxFee).isFinite() if (hasExactFee) { return ( @@ -111,10 +111,9 @@ class PaySummaryLightning extends React.Component { } const nodeAlias = getNodeAlias(payeeNodeKey, nodes) - - const totalAmountInSatoshis = Number.isFinite(exactFee) - ? CoinBig.sum(amountInSatoshis, exactFee).toString() - : CoinBig.sum(amountInSatoshis, maxFee || 0).toString() + const totalAmountInSatoshis = CoinBig(exactFee).isFinite() + ? CoinBig.sum(amountInSatoshis, convert('msats', 'sats', exactFee)).toString() + : CoinBig.sum(amountInSatoshis, convert('msats', 'sats', maxFee)).toString() return ( diff --git a/utils/btc.js b/utils/btc.js index 54d1f378761..27c6073ca22 100644 --- a/utils/btc.js +++ b/utils/btc.js @@ -44,7 +44,7 @@ export function btcToSatoshis(btc) { export function btcToMillisatoshis(btc) { if (isEmptyAmount(btc)) return null - return Coin(btc, 11) + return Coin(btc) .multiply(100000000000) .toString() } diff --git a/utils/crypto.js b/utils/crypto.js index b532413a030..88b07af73ee 100644 --- a/utils/crypto.js +++ b/utils/crypto.js @@ -5,6 +5,7 @@ import { address } from 'bitcoinjs-lib' import lightningRequestReq from 'bolt11' import coininfo from 'coininfo' import { CoinBig } from '@zap/utils/coin' +import { convert } from '@zap/utils/btc' export const networks = { bitcoin: { @@ -207,15 +208,8 @@ export const getMinFee = (routes = []) => { if (!routes || !routes.length) { return null } - const fee = routes.reduce( - (min, b) => CoinBig.min(min, b.totalFees).toString(), - routes[0].totalFees - ) - - // Add one to the fee to add room for accuracy error when using as a fee limit. - return CoinBig(fee) - .plus(1) - .toString() + const fee = routes.reduce((min, b) => CoinBig.min(min, b.totalFeesMsat), routes[0].totalFeesMsat) + return convert('msats', 'sats', fee) } /** @@ -228,15 +222,8 @@ export const getMaxFee = routes => { if (!routes || !routes.length) { return null } - const fee = routes.reduce( - (max, b) => CoinBig.max(max, b.totalFees).toString(), - routes[0].totalFees - ) - - // Add one to the fee to add room for accuracy error when using as a fee limit. - return CoinBig(fee) - .plus(1) - .toString() + const fee = routes.reduce((max, b) => CoinBig.max(max, b.totalFeesMsat), routes[0].totalFeesMsat) + return convert('msats', 'sats', fee) } /** @@ -250,7 +237,7 @@ export const getExactFee = routes => { return null } const route = routes.find(r => r.isExact) - return route ? route.total_fees : null + return route ? convert('msats', 'sats', route.totalFeesMsat) : null } /** @@ -269,9 +256,8 @@ export const getMaxFeeInclusive = routes => { } = config let fee = getMaxFee(routes) - fee = range(retryCount).reduce(max => Math.ceil(max * feeIncrementExponent), fee) - - return CoinBig(fee).toString() + fee = range(retryCount).reduce(max => CoinBig(max).times(feeIncrementExponent), fee) + return fee.toString() } /** diff --git a/utils/fee.js b/utils/fee.js index b9b55627b43..bb2943fad5e 100644 --- a/utils/fee.js +++ b/utils/fee.js @@ -3,6 +3,7 @@ import mapValues from 'lodash/mapValues' import { grpc } from 'workers' import { requestFees } from '@zap/utils/api' import { mainLog } from '@zap/utils/log' +import { CoinBig } from '@zap/utils/coin' import { createError, UNSUPPORTED } from '@zap/utils/error' /** @@ -11,7 +12,7 @@ import { createError, UNSUPPORTED } from '@zap/utils/error' * @param {object} fees Fee rate object * @returns {object} Sanitized fee rate object */ -const sanitizeFeeRange = fees => mapValues(fees, fee => Math.max(1, fee)) +const sanitizeFeeRange = fees => mapValues(fees, fee => CoinBig.max(1, fee).toString()) /** * estimateLndFee - Returns fee estimation for the specified @address @amount & @targetConf using LND gRPC API.