Browse Source

Fix sync

master
Loëck Vézien 7 years ago
parent
commit
9ef9af21a8
No known key found for this signature in database GPG Key ID: CBCDCE384E853AC4
  1. 2
      src/components/BalanceSummary/index.js
  2. 6
      src/components/CalculateBalance.js
  3. 9
      src/datas.json
  4. 9
      src/helpers/btc.js
  5. 13
      src/internals/accounts/sync.js
  6. 13
      src/reducers/accounts.js
  7. 22
      src/renderer/events.js

2
src/components/BalanceSummary/index.js

@ -50,7 +50,7 @@ const BalanceSummary = ({ accounts, selectedTime, daysCount }: Props) => (
}}
strokeWidth={2}
renderLabels={d =>
formatCurrencyUnit(getFiatUnit('USD'), d.y * 10, {
formatCurrencyUnit(getFiatUnit('USD'), d.y * 100, {
showCode: true,
})
}

6
src/components/CalculateBalance.js

@ -90,11 +90,7 @@ function calculateBalance(props) {
})
return {
allBalances: getAllBalances({
accounts: props.accounts,
counterValues: props.counterValues,
daysCount: props.daysCount,
}),
allBalances,
totalBalance: last(allBalances).value,
sinceBalance: first(allBalances).value,
}

9
src/datas.json

@ -2784,7 +2784,8 @@
"2018-02-26": 10326.5,
"2018-02-27": 10594.76,
"2018-02-28": 10334.44,
"2018-03-01": 10711.91
"2018-03-01": 10929.37,
"2018-03-02": 11130.09
},
"XRP-USD": {
"2015-01-21": 0.01523,
@ -3922,7 +3923,8 @@
"2018-02-26": 0.9296,
"2018-02-27": 0.9276,
"2018-02-28": 0.8853,
"2018-03-01": 0.907
"2018-03-01": 0.9155,
"2018-03-02": 0.9072
},
"LTC-USD": {
"2013-10-24": 3,
@ -5514,6 +5516,7 @@
"2018-02-26": 218.62,
"2018-02-27": 215.15,
"2018-02-28": 202.14,
"2018-03-01": 208.91
"2018-03-01": 209.32,
"2018-03-02": 208.01
}
}

9
src/helpers/btc.js

@ -68,6 +68,7 @@ export function getBalanceByDay(transactions: Transactions) {
export async function getAccount({
rootPath,
allAddresses = [],
allTxsHash = [],
currentIndex = 0,
hdnode,
segwit,
@ -78,6 +79,7 @@ export async function getAccount({
}: {
rootPath: string,
allAddresses?: Array<string>,
allTxsHash?: Array<string>,
currentIndex?: number,
hdnode: Object,
segwit: boolean,
@ -122,7 +124,7 @@ export async function getAccount({
new Promise(resolve => setTimeout(() => resolve(getAddress(params)), asyncDelay))
const getLastAddress = (addresses, txs) => {
const txsAddresses = [...txs.inputs.map(tx => tx.address), ...txs.outputs.map(tx => tx.address)]
const txsAddresses = [...txs.inputs.map(t => t.address), ...txs.outputs.map(t => t.address)]
return addresses.find(a => txsAddresses.includes(a.address)) || null
}
@ -146,9 +148,8 @@ export async function getAccount({
allAddresses = [...new Set([...allAddresses, ...listAddresses])]
const transactionsOpts = { coin_type: coinType }
const txs = await ledger.getTransactions(listAddresses, transactionsOpts)
txs.reverse()
let txs = await ledger.getTransactions(listAddresses, transactionsOpts)
txs.reverse().filter(t => !allTxsHash.includes(t.hash))
const hasTransactions = txs.length > 0

13
src/internals/accounts/sync.js

@ -4,12 +4,15 @@ import { getAccount, getHDNode, networks } from 'helpers/btc'
const network = networks[1]
function syncAccount({ id, ...currentAccount }) {
function syncAccount({ id, transactions, ...currentAccount }) {
const hdnode = getHDNode({ xpub58: id, network })
return getAccount({ hdnode, network, segwit: true, ...currentAccount }).then(account => ({
id,
...account,
}))
const allTxsHash = transactions.map(t => t.hash)
return getAccount({ hdnode, network, allTxsHash, segwit: true, ...currentAccount }).then(
account => ({
id,
...account,
}),
)
}
export default (send: Function) => ({

13
src/reducers/accounts.js

@ -53,17 +53,18 @@ const handlers: Object = {
return existingAccount
}
const { transactions, index } = account
const { balance, balanceByDay, transactions } = existingAccount
const updatedAccount = {
...existingAccount,
...account,
balance: transactions.reduce((result, v) => {
result += v.balance
balance: balance + account.balance,
balanceByDay: Object.keys(balanceByDay).reduce((result, k) => {
result[k] = balanceByDay[k] + (account.balanceByDay[k] || 0)
return result
}, 0),
index: index || get(existingAccount, 'index', 0),
transactions,
}, {}),
index: account.index || get(existingAccount, 'index', 0),
transactions: [...transactions, ...account.transactions],
}
return orderAccountsTransactions(updatedAccount)

22
src/renderer/events.js

@ -2,7 +2,6 @@
import { ipcRenderer } from 'electron'
import objectPath from 'object-path'
import get from 'lodash/get'
import debug from 'debug'
import type { Accounts } from 'types/common'
@ -52,13 +51,13 @@ export function startSyncAccounts(accounts: Accounts) {
syncAccounts = true
sendEvent('accounts', 'sync.all', {
accounts: accounts.map(account => {
const index = get(account, 'index', 0)
const addresses = get(account, 'addresses', [])
const { id, rootPath, addresses, index, transactions } = account
return {
id: account.id,
rootPath: account.rootPath,
id,
allAddresses: addresses,
currentIndex: index,
rootPath,
transactions,
}
}),
})
@ -92,20 +91,11 @@ export default ({ store, locked }: { store: Object, locked: boolean }) => {
return
}
const { name, balanceByDay } = currentAccount
const { name } = currentAccount
if (account.transactions.length > 0) {
d.sync(`Update account - ${name}`)
store.dispatch(
updateAccount({
...account,
balance: currentAccount.balance + account.balance,
balanceByDay: Object.keys(balanceByDay).reduce((result, k) => {
result[k] = balanceByDay[k] + (account.balanceByDay[k] || 0)
return result
}, {}),
}),
)
store.dispatch(updateAccount(account))
}
}
},

Loading…
Cancel
Save