Browse Source

Fix a JS bug when you have no txs but have balance

master
Gaëtan Renaudeau 7 years ago
parent
commit
d4305b5b38
  1. 4
      src/bridge/EthereumJSBridge.js
  2. 3
      src/components/AccountPage/AccountHeaderActions.js
  3. 3
      src/components/AccountPage/index.js
  4. 9
      src/components/modals/AddAccounts/steps/03-step-import.js
  5. 10
      src/helpers/isAccountEmpty.js

4
src/bridge/EthereumJSBridge.js

@ -246,7 +246,9 @@ const EthereumBridge: WalletBridge<Transaction> = {
} }
for (let i = 0; i < 50; i++) { for (let i = 0; i < 50; i++) {
const api = apiForCurrency(account.currency) const api = apiForCurrency(account.currency)
const { block } = txs[txs.length - 1] const last = txs[txs.length - 1]
if (!last) break
const { block } = last
if (!block) break if (!block) break
const next = await api.getTransactions(account.freshAddress, block.hash) const next = await api.getTransactions(account.freshAddress, block.hash)
if (next.txs.length === 0) break if (next.txs.length === 0) break

3
src/components/AccountPage/AccountHeaderActions.js

@ -7,6 +7,7 @@ import { translate } from 'react-i18next'
import styled from 'styled-components' import styled from 'styled-components'
import type { Account } from '@ledgerhq/live-common/lib/types' import type { Account } from '@ledgerhq/live-common/lib/types'
import Tooltip from 'components/base/Tooltip' import Tooltip from 'components/base/Tooltip'
import isAccountEmpty from 'helpers/isAccountEmpty'
import { MODAL_SEND, MODAL_RECEIVE, MODAL_SETTINGS_ACCOUNT } from 'config/constants' import { MODAL_SEND, MODAL_RECEIVE, MODAL_SETTINGS_ACCOUNT } from 'config/constants'
@ -61,7 +62,7 @@ class AccountHeaderActions extends PureComponent<Props> {
const { account, openModal, t } = this.props const { account, openModal, t } = this.props
return ( return (
<Box horizontal alignItems="center" justifyContent="flex-end" flow={2}> <Box horizontal alignItems="center" justifyContent="flex-end" flow={2}>
{account.operations.length > 0 || !account.balance.isZero() ? ( {!isAccountEmpty(account) ? (
<Fragment> <Fragment>
<Button small primary onClick={() => openModal(MODAL_SEND, { account })}> <Button small primary onClick={() => openModal(MODAL_SEND, { account })}>
<Box horizontal flow={1} alignItems="center"> <Box horizontal flow={1} alignItems="center">

3
src/components/AccountPage/index.js

@ -8,6 +8,7 @@ import { Redirect } from 'react-router'
import type { Currency, Account } from '@ledgerhq/live-common/lib/types' import type { Currency, Account } from '@ledgerhq/live-common/lib/types'
import type { T } from 'types/common' import type { T } from 'types/common'
import { accountSelector } from 'reducers/accounts' import { accountSelector } from 'reducers/accounts'
import isAccountEmpty from 'helpers/isAccountEmpty'
import { import {
counterValueCurrencySelector, counterValueCurrencySelector,
localeSelector, localeSelector,
@ -82,7 +83,7 @@ class AccountPage extends PureComponent<Props> {
<AccountHeaderActions account={account} /> <AccountHeaderActions account={account} />
</Box> </Box>
{account.operations.length > 0 || !account.balance.isZero() ? ( {!isAccountEmpty(account) ? (
<Fragment> <Fragment>
<Box mb={7}> <Box mb={7}>
<BalanceSummary <BalanceSummary

9
src/components/modals/AddAccounts/steps/03-step-import.js

@ -10,6 +10,7 @@ import uniq from 'lodash/uniq'
import { urls } from 'config/urls' import { urls } from 'config/urls'
import ExternalLinkButton from 'components/base/ExternalLinkButton' import ExternalLinkButton from 'components/base/ExternalLinkButton'
import RetryButton from 'components/base/RetryButton' import RetryButton from 'components/base/RetryButton'
import isAccountEmpty from 'helpers/isAccountEmpty'
import { getBridgeForCurrency } from 'bridge' import { getBridgeForCurrency } from 'bridge'
@ -132,7 +133,7 @@ class StepImport extends PureComponent<StepProps> {
const { scannedAccounts, checkedAccountsIds, existingAccounts } = this.props const { scannedAccounts, checkedAccountsIds, existingAccounts } = this.props
const hasAlreadyBeenScanned = !!scannedAccounts.find(a => account.id === a.id) const hasAlreadyBeenScanned = !!scannedAccounts.find(a => account.id === a.id)
const hasAlreadyBeenImported = !!existingAccounts.find(a => account.id === a.id) const hasAlreadyBeenImported = !!existingAccounts.find(a => account.id === a.id)
const isNewAccount = account.operations.length === 0 const isNewAccount = isAccountEmpty(account)
if (!hasAlreadyBeenScanned) { if (!hasAlreadyBeenScanned) {
setScannedAccounts({ setScannedAccounts({
scannedAccounts: [...scannedAccounts, this.translateName(account)], scannedAccounts: [...scannedAccounts, this.translateName(account)],
@ -229,7 +230,7 @@ class StepImport extends PureComponent<StepProps> {
let alreadyEmptyAccount let alreadyEmptyAccount
scannedAccounts.forEach(acc => { scannedAccounts.forEach(acc => {
const existingAccount = existingAccounts.find(a => a.id === acc.id) const existingAccount = existingAccounts.find(a => a.id === acc.id)
const empty = acc.operations.length === 0 const empty = isAccountEmpty(acc)
if (existingAccount) { if (existingAccount) {
importedAccounts.push(existingAccount) importedAccounts.push(existingAccount)
if (empty) { if (empty) {
@ -343,12 +344,12 @@ export const StepImportFooter = ({
}: StepProps) => { }: StepProps) => {
const willCreateAccount = checkedAccountsIds.some(id => { const willCreateAccount = checkedAccountsIds.some(id => {
const account = scannedAccounts.find(a => a.id === id) const account = scannedAccounts.find(a => a.id === id)
return account && account.operations.length === 0 return account && isAccountEmpty(account)
}) })
const willAddAccounts = checkedAccountsIds.some(id => { const willAddAccounts = checkedAccountsIds.some(id => {
const account = scannedAccounts.find(a => a.id === id) const account = scannedAccounts.find(a => a.id === id)
return account && account.operations.length > 0 return account && !isAccountEmpty(account)
}) })
const count = checkedAccountsIds.length const count = checkedAccountsIds.length

10
src/helpers/isAccountEmpty.js

@ -0,0 +1,10 @@
// @flow
import type { Account } from '@ledgerhq/live-common/lib/types'
// TODO move this back in live-common
// An account is empty if there is no operations AND balance is zero.
// balance can be non-zero in edgecases, for instance:
// - Ethereum contract only funds (api limitations)
// - Ripple node that don't show all ledgers and if you have very old txs
export default (a: Account): boolean => a.operations.length === 0 && a.balance.isZero()
Loading…
Cancel
Save