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

121 lines
4.4 KiB
JavaScript
Raw Normal View History

2018-09-12 09:49:49 +00:00
/* @flow */
import * as BLOCKCHAIN from 'actions/constants/blockchain';
import * as EthereumBlockchainActions from 'actions/ethereum/BlockchainActions';
import * as RippleBlockchainActions from 'actions/ripple/BlockchainActions';
2018-09-12 09:49:49 +00:00
import type {
Dispatch,
GetState,
PromiseAction,
BlockchainFeeLevel,
2018-09-12 09:49:49 +00:00
} from 'flowtype';
import type { BlockchainBlock, BlockchainNotification, BlockchainError } from 'trezor-connect';
2018-09-12 09:49:49 +00:00
export type BlockchainAction = {
2018-09-13 12:40:41 +00:00
type: typeof BLOCKCHAIN.READY,
} | {
type: typeof BLOCKCHAIN.UPDATE_FEE,
2018-12-05 13:11:03 +00:00
shortcut: string,
feeLevels: Array<BlockchainFeeLevel>,
2018-09-13 12:40:41 +00:00
}
2018-09-12 09:49:49 +00:00
2018-09-13 12:40:41 +00:00
// Conditionally subscribe to blockchain backend
// called after TrezorConnect.init successfully emits TRANSPORT.START event
// checks if there are discovery processes loaded from LocalStorage
// if so starts subscription to proper networks
export const init = (): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
if (getState().discovery.length > 0) {
// get unique networks
const networks: Array<string> = [];
2018-09-22 17:21:33 +00:00
getState().discovery.forEach((discovery) => {
2018-09-13 12:40:41 +00:00
if (networks.indexOf(discovery.network) < 0) {
networks.push(discovery.network);
}
});
// subscribe
2018-09-22 17:21:33 +00:00
const results = networks.map(n => dispatch(subscribe(n)));
// wait for all subscriptions
await Promise.all(results);
2018-09-13 12:40:41 +00:00
}
// continue wallet initialization
dispatch({
2018-09-22 17:21:33 +00:00
type: BLOCKCHAIN.READY,
2018-09-13 12:40:41 +00:00
});
2018-09-22 17:21:33 +00:00
};
2018-09-13 12:40:41 +00:00
export const subscribe = (networkName: string): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
const { config } = getState().localStorage;
const network = config.networks.find(c => c.shortcut === networkName);
if (!network) return;
2018-12-11 14:08:02 +00:00
switch (network.type) {
case 'ethereum':
await dispatch(EthereumBlockchainActions.subscribe(networkName));
break;
case 'ripple':
await dispatch(RippleBlockchainActions.subscribe(networkName));
break;
default: break;
}
};
export const onBlockMined = (payload: $ElementType<BlockchainBlock, 'payload'>): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
const shortcut = payload.coin.shortcut.toLowerCase();
if (getState().router.location.state.network !== shortcut) return;
const { config } = getState().localStorage;
const network = config.networks.find(c => c.shortcut === shortcut);
if (!network) return;
2018-12-11 14:08:02 +00:00
switch (network.type) {
case 'ethereum':
await dispatch(EthereumBlockchainActions.onBlockMined(shortcut));
break;
case 'ripple':
await dispatch(RippleBlockchainActions.onBlockMined(shortcut));
break;
default: break;
}
};
export const onNotification = (payload: $ElementType<BlockchainNotification, 'payload'>): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
const shortcut = payload.coin.shortcut.toLowerCase();
const { config } = getState().localStorage;
const network = config.networks.find(c => c.shortcut === shortcut);
if (!network) return;
2018-12-11 14:08:02 +00:00
switch (network.type) {
case 'ethereum':
// this is not working until blockchain-link will start support blockbook backends
await dispatch(EthereumBlockchainActions.onNotification(payload));
2018-12-11 14:08:02 +00:00
break;
case 'ripple':
await dispatch(RippleBlockchainActions.onNotification(payload));
break;
default: break;
}
};
2018-09-13 12:40:41 +00:00
// Handle BLOCKCHAIN.ERROR event from TrezorConnect
// 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;
const network = config.networks.find(c => c.shortcut === shortcut);
if (!network) return;
2018-12-11 14:08:02 +00:00
switch (network.type) {
case 'ethereum':
await dispatch(EthereumBlockchainActions.onError(shortcut));
break;
case 'ripple':
// this error is handled in BlockchainReducer
// await dispatch(RippleBlockchainActions.onBlockMined(shortcut));
break;
default: break;
}
2018-09-22 17:21:33 +00:00
};