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

Commit 324f514

Browse files
authored
Merge pull request #1750 from blockstack/feature/wallet-balance-update-fix
Wallet balance update fix
2 parents 66554f1 + d224c2f commit 324f514

5 files changed

Lines changed: 80 additions & 209 deletions

File tree

app/js/account/store/account/actions.js

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -433,62 +433,46 @@ function withdrawBitcoinFromCoreWallet(
433433
}
434434
}
435435

436-
function refreshBalances(insightUrl, addresses, coreAPIPassword) {
436+
function refreshBalances(balanceURL, addresses) {
437437
return dispatch => {
438438
const results = []
439439
return Promise.all(
440440
addresses.map(address => {
441-
logger.debug(
441+
logger.debug(
442442
`refreshBalances: refreshing balances for address ${address}`
443443
)
444-
const insightUrls = getInsightUrls(insightUrl, address, coreAPIPassword)
445-
const confirmedBalanceUrl = insightUrls.confirmedBalanceUrl
446-
const unconfirmedBalanceUrl = insightUrls.unconfirmedBalanceUrl
447-
return fetch(confirmedBalanceUrl)
444+
445+
return fetch(`${balanceURL}${address}`)
448446
.then(response => response.text())
449-
.then(responseText => {
450-
const confirmedBalance = parseInt(responseText, 10)
451-
fetch(unconfirmedBalanceUrl)
452-
.then(response => response.text())
453-
.then(balanceResponseText => {
454-
const unconfirmedBalance = parseInt(balanceResponseText, 10)
455-
results.push({
456-
address,
457-
balance: satoshisToBtc(unconfirmedBalance + confirmedBalance)
458-
})
459-
460-
if (results.length >= addresses.length) {
461-
let balances = {}
462-
let total = 0.0
463-
464-
for (let i = 0; i < results.length; i++) {
465-
const thisAddress = results[i].address
466-
if (!balances.hasOwnProperty(thisAddress)) {
467-
const balance = results[i].balance
468-
total = total + balance
469-
balances[thisAddress] = balance
470-
} else {
471-
logger.error(
472-
`refreshBalances: Duplicate address ${thisAddress} in addresses array`
473-
)
474-
}
475-
}
476-
477-
balances.total = total
478-
479-
dispatch(updateBalances(balances))
447+
.then(response => {
448+
results.push({
449+
address,
450+
balance: satoshisToBtc(parseInt(response))
451+
})
452+
453+
if (results.length >= addresses.length) {
454+
let balances = {}
455+
let total = 0.0
456+
457+
for (let i = 0; i < results.length; i++) {
458+
const thisAddress = results[i].address
459+
if (!balances.hasOwnProperty(thisAddress)) {
460+
const balance = results[i].balance
461+
total = total + balance
462+
balances[thisAddress] = balance
463+
} else {
464+
logger.error(
465+
`refreshBalances: Duplicate address ${thisAddress} in addresses array`
466+
)
480467
}
481-
})
482-
.catch(error => {
483-
logger.error(
484-
`refreshBalances: error fetching ${address} unconfirmed balance`,
485-
error
486-
)
487-
})
468+
}
469+
balances.total = total
470+
dispatch(updateBalances(balances))
471+
}
488472
})
489473
.catch(error => {
490474
logger.error(
491-
`refreshBalances: error fetching ${address} confirmed balance`,
475+
`refreshBalances: error fetching ${address} balance`,
492476
error
493477
)
494478
})

app/js/account/store/settings/default.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const DEFAULT_API = {
2121
bitcoinAddressLookupUrl: `${DEFAULT_CORE_API_ENDPOINT}/v1/addresses/bitcoin/{address}`,
2222
zeroConfBalanceUrl: `${DEFAULT_CORE_API_ENDPOINT}/v1/wallet/balance/0`,
2323
insightUrl: 'https://utxo.blockstack.org/insight-api/addr/{address}',
24+
btcBalanceUrl: 'https://blockchain.info/q/addressbalance/',
2425
broadcastUrl: 'https://utxo.blockstack.org/insight-api/tx/send',
2526
priceUrl: `${DEFAULT_CORE_API_ENDPOINT}/v1/prices/names/{name}`,
2627
networkFeeUrl: 'https://bitcoinfees.21.co/api/v1/fees/recommended',

app/js/profiles/components/registration/RegistrationSelectView.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function mapStateToProps(state) {
3030
balanceUrl: state.settings.api.zeroConfBalanceUrl,
3131
encryptedBackupPhrase: state.account.encryptedBackupPhrase,
3232
balances: state.account.bitcoinAccount.balances,
33-
insightUrl: state.settings.api.insightUrl
33+
btcBalanceUrl: state.settings.api.btcBalanceUrl
3434
}
3535
}
3636

@@ -54,7 +54,7 @@ class AddUsernameSelectPage extends Component {
5454
encryptedBackupPhrase: PropTypes.string.isRequired,
5555
addresses: PropTypes.array.isRequired,
5656
balances: PropTypes.object.isRequired,
57-
insightUrl: PropTypes.string.isRequired,
57+
btcBalanceUrl: PropTypes.string.isRequired,
5858
refreshBalances: PropTypes.func.isRequired
5959
}
6060

@@ -93,8 +93,7 @@ class AddUsernameSelectPage extends Component {
9393
if (!enoughMoney) {
9494
this.paymentTimer = setInterval(() => {
9595
logger.debug('paymentTimer: calling refreshBalances...')
96-
that.props.refreshBalances(that.props.insightUrl, that.props.addresses,
97-
that.props.api.coreAPIPassword)
96+
that.props.refreshBalances(that.props.btcBalanceUrl, that.props.addresses)
9897
}, CHECK_FOR_PAYMENT_INTERVAL)
9998
}
10099
this.state = {
@@ -114,8 +113,7 @@ class AddUsernameSelectPage extends Component {
114113

115114
componentDidMount() {
116115
logger.info('componentDidMount')
117-
this.props.refreshBalances(this.props.insightUrl, this.props.addresses,
118-
this.props.api.coreAPIPassword)
116+
this.props.refreshBalances(this.props.btcBalanceUrl, this.props.addresses)
119117

120118
// Only consider top level domains for showing the alert
121119
if (!isSubdomain(this.props.routeParams.name)) {

app/js/wallet/components/Balance.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import log4js from 'log4js'
1212

1313
const logger = log4js.getLogger(__filename)
1414

15-
const UPDATE_BALANCE_INTERVAL = 10000
15+
const UPDATE_BALANCE_INTERVAL = 30000
1616

1717
function mapStateToProps(state) {
1818
return {
1919
addresses: state.account.bitcoinAccount.addresses,
2020
balances: state.account.bitcoinAccount.balances,
21-
insightUrl: state.settings.api.insightUrl,
21+
btcBalanceUrl: state.settings.api.btcBalanceUrl,
2222
btcPriceUrl: state.settings.api.btcPriceUrl,
2323
btcPrice: state.settings.api.btcPrice,
2424
coreAPIPassword: state.settings.api.coreAPIPassword
@@ -34,7 +34,7 @@ class Balance extends Component {
3434
addresses: PropTypes.array.isRequired,
3535
balances: PropTypes.object.isRequired,
3636
refreshBalances: PropTypes.func.isRequired,
37-
insightUrl: PropTypes.string.isRequired,
37+
btcBalanceUrl: PropTypes.string.isRequired,
3838
btcPriceUrl: PropTypes.string.isRequired,
3939
btcPrice: PropTypes.string.isRequired,
4040
refreshBtcPrice: PropTypes.func.isRequired,
@@ -49,13 +49,11 @@ class Balance extends Component {
4949
}
5050

5151
componentDidMount() {
52-
this.props.refreshBalances(this.props.insightUrl, this.props.addresses,
53-
this.props.coreAPIPassword)
52+
this.props.refreshBalances(this.props.btcBalanceUrl, this.props.addresses)
5453
this.props.refreshBtcPrice(this.props.btcPriceUrl)
5554
this.balanceTimer = setInterval(() => {
5655
logger.debug('balanceTimer: calling refreshBalances...')
57-
this.props.refreshBalances(this.props.insightUrl, this.props.addresses,
58-
this.props.coreAPIPassword)
56+
this.props.refreshBalances(this.props.btcBalanceUrl, this.props.addresses)
5957
this.props.refreshBtcPrice(this.props.btcPriceUrl)
6058
}, UPDATE_BALANCE_INTERVAL)
6159
}

0 commit comments

Comments
 (0)