1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-30 20:28:09 +00:00

fix pending tx for ethereum networkType

This commit is contained in:
Szymon Lesisz 2018-12-03 20:22:49 +01:00
parent 2e89624b27
commit 90cc406773
5 changed files with 37 additions and 38 deletions

View File

@ -6,6 +6,7 @@ import BigNumber from 'bignumber.js';
import * as ACCOUNT from 'actions/constants/account'; import * as ACCOUNT from 'actions/constants/account';
import * as NOTIFICATION from 'actions/constants/notification'; import * as NOTIFICATION from 'actions/constants/notification';
import * as SEND from 'actions/constants/send'; import * as SEND from 'actions/constants/send';
import * as PENDING from 'actions/constants/pendingTx';
import * as WEB3 from 'actions/constants/web3'; import * as WEB3 from 'actions/constants/web3';
import { initialState } from 'reducers/SendFormEthereumReducer'; import { initialState } from 'reducers/SendFormEthereumReducer';
import { findToken } from 'reducers/TokensReducer'; import { findToken } from 'reducers/TokensReducer';
@ -480,7 +481,7 @@ export const onSend = (): AsyncAction => async (dispatch: Dispatch, getState: Ge
const currentState: State = getState().sendFormEthereum; const currentState: State = getState().sendFormEthereum;
const isToken: boolean = currentState.currency !== currentState.networkSymbol; const isToken: boolean = currentState.currency !== currentState.networkSymbol;
const pendingNonce: number = reducerUtils.getPendingNonce(pending); const pendingNonce: number = reducerUtils.getPendingSequence(pending);
const nonce = pendingNonce > 0 && pendingNonce >= account.nonce ? pendingNonce : account.nonce; const nonce = pendingNonce > 0 && pendingNonce >= account.nonce ? pendingNonce : account.nonce;
const txData = await dispatch(prepareEthereumTx({ const txData = await dispatch(prepareEthereumTx({
@ -553,6 +554,22 @@ export const onSend = (): AsyncAction => async (dispatch: Dispatch, getState: Ge
txData, txData,
}); });
dispatch({
type: PENDING.ADD,
payload: {
type: 'send',
deviceState: account.deviceState,
sequence: nonce,
hash: txid,
network: account.network,
address: account.address,
currency: currentState.currency,
amount: currentState.amount,
total: currentState.total,
fee: '0', // TODO: calculate fee
},
});
// clear session storage // clear session storage
dispatch(SessionStorageActions.clear()); dispatch(SessionStorageActions.clear());

View File

@ -44,6 +44,8 @@ export const onNotification = (payload: $ElementType<BlockchainNotification, 'pa
type: PENDING.ADD, type: PENDING.ADD,
payload: { payload: {
type: notification.type, type: notification.type,
deviceState: account.deviceState,
sequence: account.sequence,
hash: notification.hash, hash: notification.hash,
network: account.network, network: account.network,
address: account.address, address: account.address,

View File

@ -71,6 +71,8 @@ const start = (state: State, action: DiscoveryStartAction): State => {
instance.hdKey = hdKey; instance.hdKey = hdKey;
instance.publicKey = action.publicKey; instance.publicKey = action.publicKey;
instance.chainCode = action.chainCode; instance.chainCode = action.chainCode;
instance.basePath = action.basePath;
} }
const newState: State = [...state]; const newState: State = [...state];

View File

@ -1,12 +1,13 @@
/* @flow */ /* @flow */
import * as CONNECT from 'actions/constants/TrezorConnect'; import * as CONNECT from 'actions/constants/TrezorConnect';
import * as PENDING from 'actions/constants/pendingTx'; import * as PENDING from 'actions/constants/pendingTx';
import * as SEND from 'actions/constants/send';
import type { TrezorDevice, Action } from 'flowtype'; import type { Action } from 'flowtype';
export type PendingTx = { export type PendingTx = {
+type: 'send' | 'recv', +type: 'send' | 'recv',
+deviceState: string,
+sequence: number,
+hash: string, +hash: string,
+network: string, +network: string,
+address: string, +address: string,
@ -21,35 +22,15 @@ export type State = Array<PendingTx>;
const initialState: State = []; const initialState: State = [];
// const add = (state: State, action: SendTxAction): State => { const add = (state: State, payload: PendingTx): State => {
// const newState = [...state];
// newState.push({
// type: 'send',
// id: action.txid,
// network: action.account.network,
// address: action.account.address,
// deviceState: action.account.deviceState,
// currency: action.selectedCurrency,
// amount: action.amount,
// total: action.total,
// tx: action.tx,
// nonce: action.nonce,
// rejected: false,
// });
// return newState;
// };
const addFromNotification = (state: State, payload: PendingTx): State => {
const newState = [...state]; const newState = [...state];
newState.push(payload); newState.push(payload);
return newState; return newState;
}; };
//const clear = (state: State, device: TrezorDevice): State => state.filter(tx => tx.deviceState !== device.state); const removeByDeviceState = (state: State, deviceState: ?string): State => state.filter(tx => tx.deviceState !== deviceState);
const remove = (state: State, hash: string): State => state.filter(tx => tx.hash !== hash); const removeByHash = (state: State, hash: string): State => state.filter(tx => tx.hash !== hash);
const reject = (state: State, hash: string): State => state.map((tx) => { const reject = (state: State, hash: string): State => state.map((tx) => {
if (tx.hash === hash && !tx.rejected) { if (tx.hash === hash && !tx.rejected) {
@ -60,19 +41,16 @@ const reject = (state: State, hash: string): State => state.map((tx) => {
export default function pending(state: State = initialState, action: Action): State { export default function pending(state: State = initialState, action: Action): State {
switch (action.type) { switch (action.type) {
// case SEND.TX_COMPLETE: case CONNECT.FORGET:
// return add(state, action); case CONNECT.FORGET_SINGLE:
case CONNECT.FORGET_SILENT:
// case CONNECT.FORGET: case CONNECT.RECEIVE_WALLET_TYPE:
// case CONNECT.FORGET_SINGLE: return removeByDeviceState(state, action.device.state);
// case CONNECT.FORGET_SILENT:
// case CONNECT.RECEIVE_WALLET_TYPE:
// return clear(state, action.device);
case PENDING.ADD: case PENDING.ADD:
return addFromNotification(state, action.payload); return add(state, action.payload);
case PENDING.TX_RESOLVED: case PENDING.TX_RESOLVED:
return remove(state, action.hash); return removeByHash(state, action.hash);
case PENDING.TX_REJECTED: case PENDING.TX_REJECTED:
return reject(state, action.hash); return reject(state, action.hash);

View File

@ -89,9 +89,9 @@ export const getAccountPendingTx = (pending: Array<PendingTx>, account: ?Account
return pending.filter(p => p.network === a.network && p.address === a.address); return pending.filter(p => p.network === a.network && p.address === a.address);
}; };
export const getPendingNonce = (pending: Array<PendingTx>): number => pending.reduce((value: number, tx: PendingTx) => { export const getPendingSequence = (pending: Array<PendingTx>): number => pending.reduce((value: number, tx: PendingTx) => {
if (tx.rejected) return value; if (tx.rejected) return value;
return Math.max(value, tx.nonce + 1); return Math.max(value, tx.sequence + 1);
}, 0); }, 0);
export const getPendingAmount = (pending: Array<PendingTx>, currency: string, token: boolean = false): BigNumber => pending.reduce((value: BigNumber, tx: PendingTx) => { export const getPendingAmount = (pending: Array<PendingTx>, currency: string, token: boolean = false): BigNumber => pending.reduce((value: BigNumber, tx: PendingTx) => {