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

BlockchainReducer: add fee and block fields

This commit is contained in:
Szymon Lesisz 2018-11-29 21:09:07 +01:00
parent 95d1bdbd9e
commit f28aa760fd

View File

@ -3,57 +3,78 @@
import { BLOCKCHAIN } from 'trezor-connect'; import { BLOCKCHAIN } from 'trezor-connect';
import type { Action } from 'flowtype'; import type { Action } from 'flowtype';
import type { BlockchainConnect, BlockchainError, BlockchainBlock } from 'trezor-connect';
export type BlockchainNetwork = { export type BlockchainNetwork = {
+shortcut: string; +shortcut: string,
connected: boolean; connected: boolean,
} fee: string,
block: number,
};
export type State = Array<BlockchainNetwork>; export type State = Array<BlockchainNetwork>;
export const initialState: State = []; export const initialState: State = [];
const find = (state: State, shortcut: string): number => state.findIndex(b => b.shortcut === shortcut); const onConnect = (state: State, action: BlockchainConnect): State => {
const connect = (state: State, action: any): State => {
const shortcut = action.payload.coin.shortcut.toLowerCase(); const shortcut = action.payload.coin.shortcut.toLowerCase();
const network: BlockchainNetwork = { const network = state.find(b => b.shortcut === shortcut);
const { info } = action.payload;
if (network) {
const others = state.filter(b => b !== network);
return others.concat([{
...network,
connected: true,
fee: info.fee,
block: info.block,
}]);
}
return state.concat([{
shortcut, shortcut,
connected: true, connected: true,
}; fee: info.fee,
const newState: State = [...state]; block: info.block,
const index: number = find(newState, shortcut); }]);
if (index >= 0) {
newState[index] = network;
} else {
newState.push(network);
}
return newState;
}; };
const disconnect = (state: State, action: any): State => { const onError = (state: State, action: BlockchainError): State => {
const shortcut = action.payload.coin.shortcut.toLowerCase(); const shortcut = action.payload.coin.shortcut.toLowerCase();
const network: BlockchainNetwork = { const network = state.find(b => b.shortcut === shortcut);
shortcut, if (network) {
connected: false, const others = state.filter(b => b !== network);
}; return others.concat([{
const newState: State = [...state]; ...network,
const index: number = find(newState, shortcut); connected: false,
if (index >= 0) { }]);
newState[index] = network;
} else {
newState.push(network);
} }
return newState;
return state;
};
const onBlock = (state: State, action: BlockchainBlock): State => {
const shortcut = action.payload.coin.shortcut.toLowerCase();
const network = state.find(b => b.shortcut === shortcut);
if (network) {
const others = state.filter(b => b !== network);
return others.concat([{
...network,
block: action.payload.block,
}]);
}
return state;
}; };
export default (state: State = initialState, action: Action): State => { export default (state: State = initialState, action: Action): State => {
switch (action.type) { switch (action.type) {
case BLOCKCHAIN.CONNECT: case BLOCKCHAIN.CONNECT:
return connect(state, action); return onConnect(state, action);
case BLOCKCHAIN.ERROR: case BLOCKCHAIN.ERROR:
return disconnect(state, action); return onError(state, action);
case BLOCKCHAIN.BLOCK:
return onBlock(state, action);
default: default:
return state; return state;