You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
trezor-wallet/src/reducers/PendingTxReducer.js

79 lines
2.0 KiB

6 years ago
/* @flow */
import * as CONNECT from 'actions/constants/TrezorConnect';
import * as PENDING from 'actions/constants/pendingTx';
6 years ago
import type { Action } from 'flowtype';
6 years ago
// TODO: import them from trezor-connect
type Input = {
addresses: Array<string>,
amount: string,
fee: string,
total: string,
};
type Output = {
addresses: Array<string>,
amount: string,
}
6 years ago
export type PendingTx = {
+type: 'send' | 'recv' | 'self',
+address: string,
+deviceState: string,
+inputs: Array<Input>,
+outputs: Array<Output>,
+sequence: number,
+hash: string,
+network: string,
+currency: string,
+amount: string,
+total: string,
+fee: string,
rejected?: boolean,
};
6 years ago
6 years ago
export type State = Array<PendingTx>;
const initialState: State = [];
6 years ago
const add = (state: State, payload: PendingTx): State => {
const newState = [...state];
6 years ago
newState.push(payload);
6 years ago
return newState;
};
6 years ago
const removeByDeviceState = (state: State, deviceState: ?string): State => state.filter(tx => tx.deviceState !== deviceState);
const removeByHash = (state: State, hash: string): State => state.filter(tx => tx.hash !== hash);
6 years ago
const reject = (state: State, hash: string): State => state.map((tx) => {
if (tx.hash === hash && !tx.rejected) {
return { ...tx, rejected: true };
}
return tx;
});
6 years ago
export default function pending(state: State = initialState, action: Action): State {
6 years ago
switch (action.type) {
case CONNECT.FORGET:
case CONNECT.FORGET_SINGLE:
case CONNECT.FORGET_SILENT:
case CONNECT.RECEIVE_WALLET_TYPE:
return removeByDeviceState(state, action.device.state);
case PENDING.ADD:
return add(state, action.payload);
case PENDING.TX_RESOLVED:
return removeByHash(state, action.hash);
6 years ago
case PENDING.TX_REJECTED:
return reject(state, action.hash);
6 years ago
case PENDING.FROM_STORAGE:
6 years ago
return action.payload;
default:
return state;
}
}