Skip to content
Permalink
Browse files

feat(wallet): improve fee estimation for Pay form

resolves #1972
  • Loading branch information...
korhaliv committed Apr 23, 2019
1 parent c0b5584 commit 0ab2de195e632ac6eaae347b23b680211ddd5b0a
Showing with 31 additions and 5 deletions.
  1. +13 −0 renderer/components/Pay/Pay.js
  2. +2 −2 renderer/components/Pay/PaySummaryOnChain.js
  3. +16 −3 renderer/reducers/pay.js
@@ -1,6 +1,7 @@
import React from 'react'
import PropTypes from 'prop-types'
import config from 'config'
import debounce from 'lodash.debounce'
import { Box, Flex } from 'rebass'
import { animated, Keyframes, Transition } from 'react-spring/renderprops.cjs'
import { FormattedMessage, injectIntl, intlShape } from 'react-intl'
@@ -138,6 +139,17 @@ class Pay extends React.Component {
amountInput = React.createRef()
payReqInput = React.createRef()

updateFees = debounce(() => {
const { isOnchain } = this.state
if (!isOnchain) {
return
}
const { queryFees } = this.props
const formState = this.formApi.getState()
const { payReq: address } = formState.values
queryFees(address, this.amountInSats())
}, 500)

// Set a flag so that we can trigger form submission in componentDidUpdate once the form is loaded.
componentDidMount() {
const { fetchTickers, payReq, queryFees } = this.props
@@ -522,6 +534,7 @@ class Pay extends React.Component {
initialAmountFiat={initialAmountFiat}
isDisabled={currentStep !== 'amount' || isCoinSweep}
isRequired
onChange={this.updateFees}
/>

{isOnchain && (
@@ -40,8 +40,8 @@ class PaySummaryOnChain extends React.Component {
}

componentDidMount() {
const { queryFees } = this.props
queryFees()
const { queryFees, address, amount } = this.props
queryFees(address, amount)
}

render() {
@@ -1,6 +1,7 @@
import get from 'lodash.get'
import { grpcService } from 'workers'
import { requestFees } from '@zap/utils/api'
import { estimateFeeRange } from '@zap/utils/fee'
import config from 'config'

// ------------------------------------
// Constants
@@ -18,10 +19,22 @@ export const SET_PAY_REQ = 'SET_PAY_REQ'
// ------------------------------------
// Actions
// ------------------------------------
export const queryFees = () => async dispatch => {

/**
* Estimates on-chain fee
*
* @param {string} [address]
* @param {number} [amountInSats] desired amount in satoshis
*/
export const queryFees = (address, amountInSats) => async dispatch => {
dispatch({ type: QUERY_FEES })
try {
const onchainFees = await requestFees()
const onchainFees = await estimateFeeRange({
address,
amountInSats,
range: config.lndTargetConfirmations,
})

dispatch({ type: QUERY_FEES_SUCCESS, onchainFees })
} catch (e) {
const error = get(e, 'response.statusText', e)

0 comments on commit 0ab2de1

Please sign in to comment.
You can’t perform that action at this time.