zepio/app/containers/send.js

76 lines
1.7 KiB
JavaScript
Raw Normal View History

2018-12-20 10:56:45 -08:00
// @flow
import { connect } from 'react-redux';
2018-12-21 07:44:11 -08:00
import eres from 'eres';
2019-01-09 05:14:02 -08:00
import { BigNumber } from 'bignumber.js';
2018-12-20 10:56:45 -08:00
2018-12-21 07:44:11 -08:00
import rpc from '../../services/api';
2018-12-20 10:56:45 -08:00
import { SendView } from '../views/send';
2018-12-21 07:44:11 -08:00
import {
sendTransaction,
sendTransactionSuccess,
sendTransactionError,
resetSendTransaction,
2018-12-21 07:44:11 -08:00
} from '../redux/modules/send';
2019-01-07 10:59:27 -08:00
import filterObjectNullKeys from '../utils/filterObjectNullKeys';
2018-12-20 10:56:45 -08:00
import type { AppState } from '../types/app-state';
2018-12-21 07:44:11 -08:00
import type { Dispatch } from '../types/redux';
export type SendTransactionInput = {
from: string,
to: string,
2019-01-07 10:59:27 -08:00
amount: string,
2018-12-21 07:44:11 -08:00
fee: number,
memo: string,
};
2018-12-20 10:56:45 -08:00
2018-12-21 07:44:11 -08:00
const mapStateToProps = ({ walletSummary, sendStatus }: AppState) => ({
2018-12-20 10:56:45 -08:00
balance: walletSummary.total,
zecPrice: walletSummary.zecPrice,
addresses: walletSummary.addresses,
2018-12-21 07:44:11 -08:00
error: sendStatus.error,
isSending: sendStatus.isSending,
operationId: sendStatus.operationId,
2018-12-21 07:44:11 -08:00
});
const mapDispatchToProps = (dispatch: Dispatch) => ({
sendTransaction: async ({
from,
to,
amount,
fee,
memo,
}: SendTransactionInput) => {
dispatch(sendTransaction());
const [sendErr, operationId] = await eres(
2019-01-07 10:59:27 -08:00
rpc.z_sendmany(
from,
// $FlowFixMe
[
filterObjectNullKeys({
address: to,
2019-01-09 05:14:02 -08:00
amount: new BigNumber(amount).toNumber(),
2019-01-07 10:59:27 -08:00
memo,
}),
],
1,
2019-01-09 05:14:02 -08:00
new BigNumber(fee).toNumber(),
2019-01-07 10:59:27 -08:00
),
2018-12-21 07:44:11 -08:00
);
// eslint-disable-next-line
if (sendErr || !operationId) return dispatch(sendTransactionError({ error: sendErr.message }));
2018-12-21 07:44:11 -08:00
dispatch(sendTransactionSuccess({ operationId }));
2018-12-21 07:44:11 -08:00
},
resetSendView: () => dispatch(resetSendTransaction()),
2018-12-20 10:56:45 -08:00
});
2018-12-21 07:44:11 -08:00
export const SendContainer = connect(
mapStateToProps,
mapDispatchToProps,
)(SendView);