1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-15 12:59:09 +00:00
trezor-wallet/src/reducers/BlockchainReducer.js

178 lines
4.7 KiB
JavaScript
Raw Normal View History

2018-09-12 09:49:49 +00:00
/* @flow */
import { BLOCKCHAIN as BLOCKCHAIN_EVENT } from 'trezor-connect';
import * as BLOCKCHAIN_ACTION from 'actions/constants/blockchain';
2018-09-12 09:49:49 +00:00
import type { Action } from 'flowtype';
import type { BlockchainConnect, BlockchainError, BlockchainBlock } from 'trezor-connect';
2018-09-12 09:49:49 +00:00
export type BlockchainFeeLevel = {
name: string,
value: string,
};
2018-09-12 09:49:49 +00:00
export type BlockchainNetwork = {
+shortcut: string,
feeTimestamp: number,
feeLevels: Array<BlockchainFeeLevel>,
connected: boolean,
connecting: boolean,
block: number,
};
2018-09-12 09:49:49 +00:00
export type State = Array<BlockchainNetwork>;
2018-10-09 13:20:57 +00:00
export const initialState: State = [];
2018-09-12 09:49:49 +00:00
const onStartSubscribe = (state: State, shortcut: string): State => {
const network = state.find(b => b.shortcut === shortcut);
if (network) {
const others = state.filter(b => b !== network);
2019-03-04 12:33:02 +00:00
return others.concat([
{
...network,
connecting: true,
},
]);
}
2019-03-04 12:33:02 +00:00
return state.concat([
{
shortcut,
connected: false,
connecting: true,
block: 0,
feeTimestamp: 0,
feeLevels: [],
},
]);
};
2019-04-23 15:02:25 +00:00
const onFailSubscribe = (state: State, shortcut: string): State => {
const network = state.find(b => b.shortcut === shortcut);
if (network) {
const others = state.filter(b => b !== network);
return others.concat([
{
...network,
connecting: false,
},
]);
}
return state.concat([
{
shortcut,
connected: false,
connecting: false,
block: 0,
feeTimestamp: 0,
feeLevels: [],
},
]);
};
const onConnect = (state: State, action: BlockchainConnect): State => {
2018-10-15 13:44:10 +00:00
const shortcut = action.payload.coin.shortcut.toLowerCase();
const network = state.find(b => b.shortcut === shortcut);
const { info } = action.payload;
if (network) {
const others = state.filter(b => b !== network);
2019-03-04 12:33:02 +00:00
return others.concat([
{
...network,
block: info.block,
connected: true,
connecting: false,
},
]);
}
2019-03-04 12:33:02 +00:00
return state.concat([
{
shortcut,
connected: true,
connecting: false,
block: info.block,
feeTimestamp: 0,
feeLevels: [],
},
]);
};
const onError = (state: State, action: BlockchainError): 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);
2019-03-04 12:33:02 +00:00
return others.concat([
{
...network,
connected: false,
connecting: false,
},
]);
2018-09-12 09:49:49 +00:00
}
2019-03-04 12:33:02 +00:00
return state.concat([
{
shortcut,
connected: false,
connecting: false,
block: 0,
feeTimestamp: 0,
feeLevels: [],
},
]);
2018-09-12 09:49:49 +00:00
};
const onBlock = (state: State, action: BlockchainBlock): State => {
2018-10-15 13:44:10 +00:00
const shortcut = action.payload.coin.shortcut.toLowerCase();
const network = state.find(b => b.shortcut === shortcut);
if (network) {
const others = state.filter(b => b !== network);
2019-03-04 12:33:02 +00:00
return others.concat([
{
...network,
block: action.payload.block,
},
]);
2018-09-12 09:49:49 +00:00
}
return state;
2018-09-12 09:49:49 +00:00
};
const updateFee = (state: State, shortcut: string, feeLevels: Array<BlockchainFeeLevel>): State => {
const network = state.find(b => b.shortcut === shortcut);
if (!network) return state;
const others = state.filter(b => b !== network);
2019-03-04 12:33:02 +00:00
return others.concat([
{
...network,
feeTimestamp: new Date().getTime(),
feeLevels,
},
]);
};
2018-09-12 09:49:49 +00:00
export default (state: State = initialState, action: Action): State => {
switch (action.type) {
case BLOCKCHAIN_ACTION.START_SUBSCRIBE:
return onStartSubscribe(state, action.shortcut);
2019-04-23 15:02:25 +00:00
case BLOCKCHAIN_ACTION.FAIL_SUBSCRIBE:
return onFailSubscribe(state, action.shortcut);
case BLOCKCHAIN_EVENT.CONNECT:
return onConnect(state, action);
case BLOCKCHAIN_EVENT.ERROR:
return onError(state, action);
case BLOCKCHAIN_EVENT.BLOCK:
return onBlock(state, action);
case BLOCKCHAIN_ACTION.UPDATE_FEE:
return updateFee(state, action.shortcut, action.feeLevels);
2018-09-12 09:49:49 +00:00
default:
return state;
}
2019-03-04 12:33:02 +00:00
};