2018-12-10 09:10:06 -08:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import eres from 'eres';
|
2018-12-15 07:08:18 -08:00
|
|
|
import flow from 'lodash.flow';
|
|
|
|
import groupBy from 'lodash.groupby';
|
2018-12-13 12:21:08 -08:00
|
|
|
import dateFns from 'date-fns';
|
2019-01-29 07:02:34 -08:00
|
|
|
import { BigNumber } from 'bignumber.js';
|
2018-12-10 09:10:06 -08:00
|
|
|
import { DashboardView } from '../views/dashboard';
|
|
|
|
import rpc from '../../services/api';
|
2019-02-11 05:50:10 -08:00
|
|
|
import { listShieldedTransactions } from '../../services/shielded-transactions';
|
2018-12-13 13:37:15 -08:00
|
|
|
import store from '../../config/electron-store';
|
2018-12-15 07:08:18 -08:00
|
|
|
import {
|
|
|
|
loadWalletSummary,
|
|
|
|
loadWalletSummarySuccess,
|
|
|
|
loadWalletSummaryError,
|
|
|
|
} from '../redux/modules/wallet';
|
2019-02-11 11:36:29 -08:00
|
|
|
import { sortByDescend } from '../utils/sort-by-descend';
|
2018-12-10 09:10:06 -08:00
|
|
|
|
|
|
|
import type { AppState } from '../types/app-state';
|
|
|
|
import type { Dispatch } from '../types/redux';
|
|
|
|
|
|
|
|
const mapStateToProps = ({ walletSummary }: AppState) => ({
|
|
|
|
total: walletSummary.total,
|
|
|
|
shielded: walletSummary.shielded,
|
|
|
|
transparent: walletSummary.transparent,
|
|
|
|
error: walletSummary.error,
|
|
|
|
isLoading: walletSummary.isLoading,
|
2018-12-13 13:37:15 -08:00
|
|
|
zecPrice: walletSummary.zecPrice,
|
2018-12-10 16:11:53 -08:00
|
|
|
addresses: walletSummary.addresses,
|
2018-12-13 12:21:08 -08:00
|
|
|
transactions: walletSummary.transactions,
|
2018-12-10 09:10:06 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
const mapDispatchToProps = (dispatch: Dispatch) => ({
|
|
|
|
getSummary: async () => {
|
|
|
|
dispatch(loadWalletSummary());
|
|
|
|
|
2019-01-29 07:02:34 -08:00
|
|
|
const [walletErr, walletSummary] = await eres(rpc.z_gettotalbalance());
|
2019-01-28 16:34:07 -08:00
|
|
|
const [zAddressesErr, zAddresses = []] = await eres(rpc.z_listaddresses());
|
|
|
|
const [tAddressesErr, tAddresses = []] = await eres(rpc.getaddressesbyaccount(''));
|
2018-12-19 10:29:45 -08:00
|
|
|
const [transactionsErr, transactions] = await eres(rpc.listtransactions());
|
2018-12-13 12:21:08 -08:00
|
|
|
|
2019-01-29 07:02:34 -08:00
|
|
|
if (walletErr || zAddressesErr || tAddressesErr || transactionsErr) {
|
2019-01-28 16:34:07 -08:00
|
|
|
return dispatch(
|
|
|
|
loadWalletSummaryError({
|
2019-01-29 07:02:34 -08:00
|
|
|
error: 'Something went wrong!',
|
2019-01-28 16:34:07 -08:00
|
|
|
}),
|
|
|
|
);
|
2018-12-15 07:08:18 -08:00
|
|
|
}
|
2018-12-13 12:21:08 -08:00
|
|
|
|
2019-02-11 05:50:10 -08:00
|
|
|
const formattedTransactions: Array<Object> = flow([
|
2019-01-08 08:00:40 -08:00
|
|
|
arr => arr.map(transaction => ({
|
2019-02-19 08:06:34 -08:00
|
|
|
transactionId: transaction.txid || 'N/A',
|
2019-01-08 08:00:40 -08:00
|
|
|
type: transaction.category,
|
|
|
|
date: new Date(transaction.time * 1000).toISOString(),
|
|
|
|
address: transaction.address,
|
|
|
|
amount: Math.abs(transaction.amount),
|
2019-02-19 08:06:34 -08:00
|
|
|
fees: transaction.fee ? new BigNumber(transaction.fee).abs().toFormat(4) : 'N/A',
|
2019-01-08 08:00:40 -08:00
|
|
|
})),
|
|
|
|
arr => groupBy(arr, obj => dateFns.format(obj.date, 'MMM DD, YYYY')),
|
|
|
|
obj => Object.keys(obj).map(day => ({
|
|
|
|
day,
|
2019-02-11 05:50:10 -08:00
|
|
|
jsDay: new Date(day),
|
|
|
|
list: sortByDescend('date')(obj[day]),
|
2019-01-08 08:00:40 -08:00
|
|
|
})),
|
2019-02-11 05:50:10 -08:00
|
|
|
sortByDescend('jsDay'),
|
|
|
|
])([...transactions, ...listShieldedTransactions()]);
|
2019-01-08 08:00:40 -08:00
|
|
|
|
2019-01-10 05:02:28 -08:00
|
|
|
if (!zAddresses.length) {
|
2019-01-29 07:02:34 -08:00
|
|
|
const [, newZAddress] = await eres(rpc.z_getnewaddress());
|
2019-01-10 05:02:28 -08:00
|
|
|
|
2019-01-29 07:02:34 -08:00
|
|
|
if (newZAddress) zAddresses.push(newZAddress);
|
2019-01-10 05:02:28 -08:00
|
|
|
}
|
|
|
|
|
2019-01-28 16:34:07 -08:00
|
|
|
if (!tAddresses.length) {
|
2019-01-29 07:02:34 -08:00
|
|
|
const [, newTAddress] = await eres(rpc.getnewaddress(''));
|
2019-01-10 05:02:28 -08:00
|
|
|
|
2019-01-29 07:02:34 -08:00
|
|
|
if (newTAddress) tAddresses.push(newTAddress);
|
2019-01-10 05:02:28 -08:00
|
|
|
}
|
|
|
|
|
2018-12-10 09:10:06 -08:00
|
|
|
dispatch(
|
|
|
|
loadWalletSummarySuccess({
|
|
|
|
transparent: walletSummary.transparent,
|
|
|
|
total: walletSummary.total,
|
|
|
|
shielded: walletSummary.private,
|
2019-01-28 16:34:07 -08:00
|
|
|
addresses: [...zAddresses, ...tAddresses],
|
2019-01-08 08:00:40 -08:00
|
|
|
transactions: formattedTransactions,
|
2019-01-29 07:02:34 -08:00
|
|
|
zecPrice: new BigNumber(store.get('ZEC_DOLLAR_PRICE')).toNumber(),
|
2018-12-10 09:10:06 -08:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2019-01-21 16:08:35 -08:00
|
|
|
// $FlowFixMe
|
2018-12-10 09:10:06 -08:00
|
|
|
export const DashboardContainer = connect(
|
|
|
|
mapStateToProps,
|
|
|
|
mapDispatchToProps,
|
|
|
|
)(DashboardView);
|