add BlockchainFeeLevel to BlockchainReducer

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

@ -8,6 +8,7 @@ import type {
Dispatch,
GetState,
PromiseAction,
BlockchainFeeLevel,
} from 'flowtype';
import type { BlockchainBlock, BlockchainNotification, BlockchainError } from 'trezor-connect';
@ -17,7 +18,7 @@ export type BlockchainAction = {
} | {
type: typeof BLOCKCHAIN.UPDATE_FEE,
shortcut: string,
fee: string,
feeLevels: Array<BlockchainFeeLevel>,
}
// Conditionally subscribe to blockchain backend
@ -100,7 +101,7 @@ export const onNotification = (payload: $ElementType<BlockchainNotification, 'pa
};
// 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> => {
const shortcut = payload.coin.shortcut.toLowerCase();
const { config } = getState().localStorage;

@ -2,6 +2,7 @@
import * as ACCOUNT from 'actions/constants/account';
import * as SEND from 'actions/constants/send';
import * as WEB3 from 'actions/constants/web3';
import * as BLOCKCHAIN from 'actions/constants/blockchain';
import type {
Dispatch,
@ -36,6 +37,7 @@ export type SendFormAction = {
const actions = [
ACCOUNT.UPDATE_SELECTED_ACCOUNT,
WEB3.GAS_PRICE_UPDATED,
BLOCKCHAIN.UPDATE_FEE,
...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 { Token } from 'reducers/TokensReducer';
export type { Web3Instance } from 'reducers/Web3Reducer';
export type { BlockchainFeeLevel } from 'reducers/BlockchainReducer';
export type Accounts = $ElementType<State, 'accounts'>;
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 { BlockchainConnect, BlockchainError, BlockchainBlock } from 'trezor-connect';
export type BlockchainFeeLevel = {
name: string,
value: string,
};
export type BlockchainNetwork = {
+shortcut: string,
feeTimestamp: number,
feeLevels: Array<BlockchainFeeLevel>,
connected: boolean,
block: number,
reserved: string, // xrp specific
fee: string,
};
export type State = Array<BlockchainNetwork>;
@ -27,8 +32,6 @@ const onConnect = (state: State, action: BlockchainConnect): State => {
return others.concat([{
...network,
connected: true,
fee: info.fee,
block: info.block,
}]);
}
@ -36,8 +39,8 @@ const onConnect = (state: State, action: BlockchainConnect): State => {
shortcut,
connected: true,
block: info.block,
fee: info.fee,
reserved: info.reserved || '0',
feeTimestamp: 0,
feeLevels: [],
}]);
};
@ -56,8 +59,8 @@ const onError = (state: State, action: BlockchainError): State => {
shortcut,
connected: false,
block: 0,
fee: '0',
reserved: '0',
feeTimestamp: 0,
feeLevels: [],
}]);
};
@ -75,14 +78,15 @@ const onBlock = (state: State, action: BlockchainBlock): 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);
if (!network) return state;
const others = state.filter(b => b !== network);
return others.concat([{
...network,
fee,
feeTimestamp: new Date().getTime(),
feeLevels,
}]);
};
@ -96,7 +100,7 @@ export default (state: State = initialState, action: Action): State => {
case BLOCKCHAIN_EVENT.BLOCK:
return onBlock(state, action);
case BLOCKCHAIN_ACTION.UPDATE_FEE:
return updateFee(state, action.shortcut, action.fee);
return updateFee(state, action.shortcut, action.feeLevels);
default:
return state;

Loading…
Cancel
Save