2018-09-12 09:49:49 +00:00
|
|
|
/* @flow */
|
|
|
|
|
|
|
|
import * as BLOCKCHAIN from 'actions/constants/blockchain';
|
2018-12-03 18:04:57 +00:00
|
|
|
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,
|
|
|
|
} from 'flowtype';
|
2018-12-03 18:04:57 +00:00
|
|
|
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,
|
2018-11-29 20:08:41 +00:00
|
|
|
} | {
|
|
|
|
type: typeof BLOCKCHAIN.UPDATE_FEE,
|
|
|
|
fee: string,
|
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
|
|
|
|
2018-12-03 18:04:57 +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;
|
|
|
|
|
|
|
|
if (network.type === 'ethereum') {
|
|
|
|
await dispatch(EthereumBlockchainActions.subscribe(networkName));
|
|
|
|
} else if (network.type === 'ripple') {
|
|
|
|
await dispatch(RippleBlockchainActions.subscribe(networkName));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (network.type === 'ethereum') {
|
|
|
|
await dispatch(EthereumBlockchainActions.onBlockMined(shortcut));
|
|
|
|
} else if (network.type === 'ripple') {
|
|
|
|
await dispatch(RippleBlockchainActions.onBlockMined(shortcut));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (network.type === 'ethereum') {
|
|
|
|
await dispatch(EthereumBlockchainActions.onNotification());
|
|
|
|
} else if (network.type === 'ripple') {
|
|
|
|
await dispatch(RippleBlockchainActions.onNotification(payload));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-09-13 12:40:41 +00:00
|
|
|
// Handle BLOCKCHAIN.ERROR event from TrezorConnect
|
|
|
|
// disconnect and remove Web3 webscocket instance if exists
|
2018-12-03 18:04:57 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
if (network.type === 'ethereum') {
|
|
|
|
await dispatch(EthereumBlockchainActions.onError(shortcut));
|
|
|
|
} else if (network.type === 'ripple') {
|
|
|
|
// await dispatch(RippleBlockchainActions.onError(shortcut));
|
|
|
|
}
|
2018-09-22 17:21:33 +00:00
|
|
|
};
|