Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
feat(wallet): handle fees using msats
Browse files Browse the repository at this point in the history
  • Loading branch information
mrfelton committed Mar 29, 2020
1 parent d1da8ee commit 01ac7fc
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 31 deletions.
13 changes: 6 additions & 7 deletions renderer/components/Pay/PaySummaryLightning.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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 (
<Box {...rest}>
Expand Down
2 changes: 1 addition & 1 deletion utils/btc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
30 changes: 8 additions & 22 deletions utils/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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)
}

/**
Expand All @@ -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)
}

/**
Expand All @@ -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
}

/**
Expand All @@ -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()
}

/**
Expand Down
3 changes: 2 additions & 1 deletion utils/fee.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'

/**
Expand All @@ -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.
Expand Down

0 comments on commit 01ac7fc

Please sign in to comment.