add BlockchainFeeLevel to BlockchainReducer

pull/288/head
Szymon Lesisz 5 years ago
parent 2639aab093
commit bb140b8bfe

@ -8,6 +8,7 @@ import type {
Dispatch, Dispatch,
GetState, GetState,
PromiseAction, PromiseAction,
BlockchainFeeLevel,
} from 'flowtype'; } from 'flowtype';
import type { BlockchainBlock, BlockchainNotification, BlockchainError } from 'trezor-connect'; import type { BlockchainBlock, BlockchainNotification, BlockchainError } from 'trezor-connect';
@ -17,7 +18,7 @@ export type BlockchainAction = {
} | { } | {
type: typeof BLOCKCHAIN.UPDATE_FEE, type: typeof BLOCKCHAIN.UPDATE_FEE,
shortcut: string, shortcut: string,
fee: string, feeLevels: Array<BlockchainFeeLevel>,
} }
// Conditionally subscribe to blockchain backend // Conditionally subscribe to blockchain backend
@ -100,7 +101,7 @@ export const onNotification = (payload: $ElementType<BlockchainNotification, 'pa
}; };
// Handle BLOCKCHAIN.ERROR event from TrezorConnect // Handle BLOCKCHAIN.ERROR event from TrezorConnect
// disconnect and remove Web3 webscocket instance if exists // disconnect and remove Web3 websocket instance if exists
export const onError = (payload: $ElementType<BlockchainError, 'payload'>): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => { export const onError = (payload: $ElementType<BlockchainError, 'payload'>): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
const shortcut = payload.coin.shortcut.toLowerCase(); const shortcut = payload.coin.shortcut.toLowerCase();
const { config } = getState().localStorage; const { config } = getState().localStorage;

@ -2,6 +2,7 @@
import * as ACCOUNT from 'actions/constants/account'; import * as ACCOUNT from 'actions/constants/account';
import * as SEND from 'actions/constants/send'; import * as SEND from 'actions/constants/send';
import * as WEB3 from 'actions/constants/web3'; import * as WEB3 from 'actions/constants/web3';
import * as BLOCKCHAIN from 'actions/constants/blockchain';
import type { import type {
Dispatch, Dispatch,
@ -36,6 +37,7 @@ export type SendFormAction = {
const actions = [ const actions = [
ACCOUNT.UPDATE_SELECTED_ACCOUNT, ACCOUNT.UPDATE_SELECTED_ACCOUNT,
WEB3.GAS_PRICE_UPDATED, WEB3.GAS_PRICE_UPDATED,
BLOCKCHAIN.UPDATE_FEE,
...Object.values(SEND).filter(v => typeof v === 'string'), ...Object.values(SEND).filter(v => typeof v === 'string'),
]; ];

@ -159,6 +159,7 @@ export type { Account } from 'reducers/AccountsReducer';
export type { Discovery } from 'reducers/DiscoveryReducer'; export type { Discovery } from 'reducers/DiscoveryReducer';
export type { Token } from 'reducers/TokensReducer'; export type { Token } from 'reducers/TokensReducer';
export type { Web3Instance } from 'reducers/Web3Reducer'; export type { Web3Instance } from 'reducers/Web3Reducer';
export type { BlockchainFeeLevel } from 'reducers/BlockchainReducer';
export type Accounts = $ElementType<State, 'accounts'>; export type Accounts = $ElementType<State, 'accounts'>;
export type LocalStorage = $ElementType<State, 'localStorage'>; export type LocalStorage = $ElementType<State, 'localStorage'>;

@ -6,12 +6,17 @@ import * as BLOCKCHAIN_ACTION from 'actions/constants/blockchain';
import type { Action } from 'flowtype'; import type { Action } from 'flowtype';
import type { BlockchainConnect, BlockchainError, BlockchainBlock } from 'trezor-connect'; import type { BlockchainConnect, BlockchainError, BlockchainBlock } from 'trezor-connect';
export type BlockchainFeeLevel = {
name: string,
value: string,
};
export type BlockchainNetwork = { export type BlockchainNetwork = {
+shortcut: string, +shortcut: string,
feeTimestamp: number,
feeLevels: Array<BlockchainFeeLevel>,
connected: boolean, connected: boolean,
block: number, block: number,
reserved: string, // xrp specific
fee: string,
}; };
export type State = Array<BlockchainNetwork>; export type State = Array<BlockchainNetwork>;
@ -27,8 +32,6 @@ const onConnect = (state: State, action: BlockchainConnect): State => {
return others.concat([{ return others.concat([{
...network, ...network,
connected: true, connected: true,
fee: info.fee,
block: info.block,
}]); }]);
} }
@ -36,8 +39,8 @@ const onConnect = (state: State, action: BlockchainConnect): State => {
shortcut, shortcut,
connected: true, connected: true,
block: info.block, block: info.block,
fee: info.fee, feeTimestamp: 0,
reserved: info.reserved || '0', feeLevels: [],
}]); }]);
}; };
@ -56,8 +59,8 @@ const onError = (state: State, action: BlockchainError): State => {
shortcut, shortcut,
connected: false, connected: false,
block: 0, block: 0,
fee: '0', feeTimestamp: 0,
reserved: '0', feeLevels: [],
}]); }]);
}; };
@ -75,14 +78,15 @@ const onBlock = (state: State, action: BlockchainBlock): State => {
return state; return state;
}; };
const updateFee = (state: State, shortcut: string, fee: string): State => { const updateFee = (state: State, shortcut: string, feeLevels: Array<BlockchainFeeLevel>): State => {
const network = state.find(b => b.shortcut === shortcut); const network = state.find(b => b.shortcut === shortcut);
if (!network) return state; if (!network) return state;
const others = state.filter(b => b !== network); const others = state.filter(b => b !== network);
return others.concat([{ return others.concat([{
...network, ...network,
fee, feeTimestamp: new Date().getTime(),
feeLevels,
}]); }]);
}; };
@ -96,7 +100,7 @@ export default (state: State = initialState, action: Action): State => {
case BLOCKCHAIN_EVENT.BLOCK: case BLOCKCHAIN_EVENT.BLOCK:
return onBlock(state, action); return onBlock(state, action);
case BLOCKCHAIN_ACTION.UPDATE_FEE: case BLOCKCHAIN_ACTION.UPDATE_FEE:
return updateFee(state, action.shortcut, action.fee); return updateFee(state, action.shortcut, action.feeLevels);
default: default:
return state; return state;

Loading…
Cancel
Save