2018-09-12 09:49:49 +00:00
|
|
|
/* @flow */
|
|
|
|
|
2018-12-05 13:12:53 +00:00
|
|
|
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';
|
2018-11-29 20:09:07 +00:00
|
|
|
import type { BlockchainConnect, BlockchainError, BlockchainBlock } from 'trezor-connect';
|
2018-09-12 09:49:49 +00:00
|
|
|
|
2018-12-28 15:02:50 +00:00
|
|
|
export type BlockchainFeeLevel = {
|
|
|
|
name: string,
|
|
|
|
value: string,
|
|
|
|
};
|
|
|
|
|
2018-09-12 09:49:49 +00:00
|
|
|
export type BlockchainNetwork = {
|
2018-11-29 20:09:07 +00:00
|
|
|
+shortcut: string,
|
2018-12-28 15:02:50 +00:00
|
|
|
feeTimestamp: number,
|
|
|
|
feeLevels: Array<BlockchainFeeLevel>,
|
2018-11-29 20:09:07 +00:00
|
|
|
connected: boolean,
|
2019-01-14 01:46:41 +00:00
|
|
|
connecting: boolean,
|
2018-11-29 20:09:07 +00:00
|
|
|
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
|
|
|
|
2019-01-14 01:46:41 +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-01-14 01:46:41 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 12:33:02 +00:00
|
|
|
return state.concat([
|
|
|
|
{
|
|
|
|
shortcut,
|
|
|
|
connected: false,
|
|
|
|
connecting: true,
|
|
|
|
block: 0,
|
|
|
|
feeTimestamp: 0,
|
|
|
|
feeLevels: [],
|
|
|
|
},
|
|
|
|
]);
|
2019-01-14 01:46:41 +00:00
|
|
|
};
|
|
|
|
|
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: [],
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
};
|
|
|
|
|
2018-11-29 20:09:07 +00:00
|
|
|
const onConnect = (state: State, action: BlockchainConnect): State => {
|
2018-10-15 13:44:10 +00:00
|
|
|
const shortcut = action.payload.coin.shortcut.toLowerCase();
|
2018-11-29 20:09:07 +00:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
]);
|
2018-11-29 20:09:07 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 12:33:02 +00:00
|
|
|
return state.concat([
|
|
|
|
{
|
|
|
|
shortcut,
|
|
|
|
connected: true,
|
|
|
|
connecting: false,
|
|
|
|
block: info.block,
|
|
|
|
feeTimestamp: 0,
|
|
|
|
feeLevels: [],
|
|
|
|
},
|
|
|
|
]);
|
2018-11-29 20:09:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2018-11-29 20:09:07 +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
|
|
|
};
|
|
|
|
|
2018-11-29 20:09:07 +00:00
|
|
|
const onBlock = (state: State, action: BlockchainBlock): State => {
|
2018-10-15 13:44:10 +00:00
|
|
|
const shortcut = action.payload.coin.shortcut.toLowerCase();
|
2018-11-29 20:09:07 +00:00
|
|
|
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
|
|
|
}
|
2018-11-29 20:09:07 +00:00
|
|
|
|
|
|
|
return state;
|
2018-09-12 09:49:49 +00:00
|
|
|
};
|
|
|
|
|
2018-12-28 15:02:50 +00:00
|
|
|
const updateFee = (state: State, shortcut: string, feeLevels: Array<BlockchainFeeLevel>): State => {
|
2018-12-05 13:12:53 +00:00
|
|
|
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-12-05 13:12:53 +00:00
|
|
|
};
|
|
|
|
|
2018-09-12 09:49:49 +00:00
|
|
|
export default (state: State = initialState, action: Action): State => {
|
|
|
|
switch (action.type) {
|
2019-01-14 01:46:41 +00:00
|
|
|
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);
|
2018-12-05 13:12:53 +00:00
|
|
|
case BLOCKCHAIN_EVENT.CONNECT:
|
2018-11-29 20:09:07 +00:00
|
|
|
return onConnect(state, action);
|
2018-12-05 13:12:53 +00:00
|
|
|
case BLOCKCHAIN_EVENT.ERROR:
|
2018-11-29 20:09:07 +00:00
|
|
|
return onError(state, action);
|
2018-12-05 13:12:53 +00:00
|
|
|
case BLOCKCHAIN_EVENT.BLOCK:
|
2018-11-29 20:09:07 +00:00
|
|
|
return onBlock(state, action);
|
2018-12-05 13:12:53 +00:00
|
|
|
case BLOCKCHAIN_ACTION.UPDATE_FEE:
|
2018-12-28 15:02:50 +00:00
|
|
|
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
|
|
|
};
|