Skip to content

Commit

Permalink
use default eth addr when calculated eth balance for erc20 tx fee
Browse files Browse the repository at this point in the history
  • Loading branch information
schnogz committed May 7, 2019
1 parent 1737adb commit 4802a88
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ export default ({ api, coreSagas, networks }) => {
// ensure for sufficient eth balance for erc20 swap
if (isSourceErc20) {
const ethBalanceInWei = (yield select(
selectors.core.data.eth.getBalance
selectors.core.data.eth.getDefaultAddressBalance
)).getOrElse(0)
let ethBalance = Exchange.convertEtherToEther({
value: ethBalanceInWei,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class FirstStep extends React.PureComponent {
balanceStatus={value.balanceStatus}
excludeLockbox={value.excludeLockbox}
hasErc20Balance={value.hasErc20Balance}
isFeeSufficientForTx={value.isFeeSufficientForTx}
isSufficientEthForErc20={value.isSufficientEthForErc20}
/>
),
Failure: message => <Error>{message}</Error>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { FormattedMessage } from 'react-intl'
import { head, gt, prop, propOr, path, includes, isEmpty } from 'ramda'
import { model, selectors } from 'data'
import { createDeepEqualSelector } from 'services/ReselectHelper'
import BigNumber from 'bignumber.js'

import { Remote } from 'blockchain-wallet-v4/src'

Expand All @@ -19,7 +20,7 @@ export const getData = createDeepEqualSelector(
? selectors.core.data.eth.getErc20CurrentBalance(state, coin)
: selectors.core.data.eth.getCurrentBalance(state)
},
state => selectors.core.data.eth.getBalance(state),
state => selectors.core.data.eth.getDefaultAddressBalance(state),
state =>
selectors.core.common.eth.getErc20AccountBalances(state, 'PAX').map(head),
selectors.core.kvStore.lockbox.getDevices,
Expand All @@ -40,7 +41,6 @@ export const getData = createDeepEqualSelector(
) => {
const enableToggle = !isEmpty(lockboxDevicesR.getOrElse([]))
const excludeLockbox = !prop('lockbox', coinAvailability.getOrElse({}))
const ethBalance = ethBalanceR.getOrElse(0)
// TODO: include any/all ERC20 balances in future
const hasErc20Balance = gt(prop('balance', paxBalanceR.getOrElse(0)), 0)

Expand All @@ -53,7 +53,9 @@ export const getData = createDeepEqualSelector(
const priorityFee = path(['fees', 'priority'], payment)
const minFee = path(['fees', 'limits', 'min'], payment)
const maxFee = path(['fees', 'limits', 'max'], payment)
const isFeeSufficientForTx = ethBalance >= fee
const isSufficientEthForErc20 = new BigNumber(
ethBalanceR.getOrElse(0)
).isGreaterThan(new BigNumber(fee))
const isContract = isContractR.getOrElse(false)
const isContractChecked = Remote.Success.is(isContractR)
const feeElements = [
Expand Down Expand Up @@ -87,7 +89,7 @@ export const getData = createDeepEqualSelector(
unconfirmedTx,
isContract,
isContractChecked,
isFeeSufficientForTx,
isSufficientEthForErc20,
fee,
feeToggled,
enableToggle,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ const FirstStep = props => {
balanceStatus,
excludeLockbox,
hasErc20Balance,
isFeeSufficientForTx
isSufficientEthForErc20
} = props
const isFromLockbox = from && from.type === 'LOCKBOX'
const disableLockboxSend =
isFromLockbox && !(bowser.name === 'Chrome' || bowser.name === 'Chromium')
const isFeeSufficientForErc20Tx = coin === 'ETH' || isFeeSufficientForTx
const disableDueToLowEth = coin !== 'ETH' && !isSufficientEthForErc20

return (
<Form onSubmit={handleSubmit}>
Expand Down Expand Up @@ -315,7 +315,7 @@ const FirstStep = props => {
</Text>
</CustomFeeAlertBanner>
) : null}
{!isFeeSufficientForErc20Tx && <LowEthWarningForErc20 />}
{disableDueToLowEth && <LowEthWarningForErc20 />}
<SubmitFormGroup>
<Button
type='submit'
Expand All @@ -328,7 +328,7 @@ const FirstStep = props => {
invalid ||
isContract ||
!isContractChecked ||
!isFeeSufficientForErc20Tx ||
disableDueToLowEth ||
Remote.Loading.is(balanceStatus)
}
data-e2e={`${coin}SendContinue`}
Expand Down
6 changes: 6 additions & 0 deletions packages/blockchain-wallet-v4/src/redux/data/eth/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ export const getDefaultAddress = state =>
getAddresses(state).map(addr => head(keys(addr)))
export const getAddress = (state, address) =>
getAddresses(state).map(prop(address))
export const getDefaultAddressBalance = state => {
const defaultAddr = getDefaultAddress(state)
.map(x => x)
.getOrElse('')
return getAddress(state, defaultAddr).map(prop('balance'))
}
export const getLegacyBalance = path([dataPath, 'eth', 'legacy_balance'])
export const getRates = path([dataPath, 'eth', 'rates', 'eth'])
export const getHeight = state => getLatestBlock(state).map(path(['number']))
Expand Down

0 comments on commit 4802a88

Please sign in to comment.