1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-27 18:58:08 +00:00

handle trezor-connect blockchain events

This commit is contained in:
Szymon Lesisz 2018-11-29 21:08:41 +01:00
parent e64b44d632
commit 95d1bdbd9e
4 changed files with 64 additions and 61 deletions

View File

@ -10,7 +10,7 @@ import type {
GetState,
PromiseAction,
} from 'flowtype';
import type { EthereumAccount } from 'trezor-connect';
import type { EthereumAccount, BlockchainBlock, BlockchainNotification } from 'trezor-connect';
import type { Token } from 'reducers/TokensReducer';
import type { NetworkToken } from 'reducers/LocalStorageReducer';
import * as Web3Actions from './Web3Actions';
@ -18,6 +18,9 @@ import * as AccountsActions from './AccountsActions';
export type BlockchainAction = {
type: typeof BLOCKCHAIN.READY,
} | {
type: typeof BLOCKCHAIN.UPDATE_FEE,
fee: string,
}
export const discoverAccount = (device: TrezorDevice, address: string, network: string): PromiseAction<EthereumAccount> => async (dispatch: Dispatch): Promise<EthereumAccount> => {
@ -89,11 +92,24 @@ export const estimateGasLimit = (network: string, data: string, value: string, g
return result;
};
export const onBlockMined = (coinInfo: any): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
// incoming "coinInfo" from TrezorConnect is CoinInfo | EthereumNetwork type
const network: string = coinInfo.shortcut.toLowerCase();
export const onBlockMined = (payload: $ElementType<BlockchainBlock, 'payload'>): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
const network: string = payload.coin.shortcut.toLowerCase();
if (payload.coin.type === 'misc') {
if (getState().router.location.state.network === network) {
const fee = await TrezorConnect.blockchainGetFee({
coin: network,
});
if (!fee.success) return;
dispatch({
type: BLOCKCHAIN.UPDATE_FEE,
fee: fee.payload,
});
}
return;
}
if (network !== 'ethereum') return;
// try to resolve pending transactions
await dispatch(Web3Actions.resolvePendingTransactions(network));
@ -127,46 +143,45 @@ export const onBlockMined = (coinInfo: any): PromiseAction<void> => async (dispa
// not used for now, waiting for fix in blockbook
/*
export const onNotification = (payload: any): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
export const onNotification = (payload: $ElementType<BlockchainNotification, 'payload'>): PromiseAction<void> => async (/*dispatch: Dispatch, getState: GetState*/): Promise<void> => {
console.warn('OnBL', payload);
// this event can be triggered multiple times
// 1. check if pair [txid + address] is already in reducer
const network: string = payload.coin.shortcut.toLowerCase();
const address: string = EthereumjsUtil.toChecksumAddress(payload.tx.address);
const txInfo = await dispatch(Web3Actions.getPendingInfo(network, payload.tx.txid));
// // 1. check if pair [txid + address] is already in reducer
// const network: string = payload.coin.shortcut.toLowerCase();
// const address: string = EthereumjsUtil.toChecksumAddress(payload.tx.address);
// const txInfo = await dispatch(Web3Actions.getPendingInfo(network, payload.tx.txid));
// const exists = getState().pending.filter(p => p.id === payload.tx.txid && p.address === address);
const exists = getState().pending.filter(p => p.address === address);
if (exists.length < 1) {
if (txInfo) {
dispatch({
type: PENDING.ADD,
payload: {
type: 'send',
id: payload.tx.txid,
network,
currency: 'tETH',
amount: txInfo.value,
total: '0',
tx: {},
nonce: txInfo.nonce,
address,
rejected: false,
},
});
} else {
// tx info not found (yet?)
// dispatch({
// type: PENDING.ADD_UNKNOWN,
// payload: {
// network,
// ...payload.tx,
// }
// });
}
}
// // const exists = getState().pending.filter(p => p.id === payload.tx.txid && p.address === address);
// const exists = getState().pending.filter(p => p.address === address);
// if (exists.length < 1) {
// if (txInfo) {
// dispatch({
// type: PENDING.ADD,
// payload: {
// type: 'send',
// id: payload.tx.txid,
// network,
// currency: 'tETH',
// amount: txInfo.value,
// total: '0',
// tx: {},
// nonce: txInfo.nonce,
// address,
// rejected: false,
// },
// });
// } else {
// // tx info not found (yet?)
// // dispatch({
// // type: PENDING.ADD_UNKNOWN,
// // payload: {
// // network,
// // ...payload.tx,
// // }
// // });
// }
// }
};
*/
export const subscribe = (network: string): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => {

View File

@ -17,8 +17,7 @@ import type {
UiMessageType,
TransportMessage,
TransportMessageType,
BlockchainMessage,
BlockchainMessageType,
BlockchainEvent,
} from 'trezor-connect';
import type {
@ -114,13 +113,9 @@ export const init = (): AsyncAction => async (dispatch: Dispatch, getState: GetS
});
});
TrezorConnect.on(BLOCKCHAIN_EVENT, (event: BlockchainMessage): void => {
// post event to reducers
const type: BlockchainMessageType = event.type; // eslint-disable-line prefer-destructuring
dispatch({
type,
payload: event.payload,
});
// post event to reducers
TrezorConnect.on(BLOCKCHAIN_EVENT, (event: BlockchainEvent): void => {
dispatch(event);
});
if (buildUtils.isDev()) {

View File

@ -1,6 +1,4 @@
/* @flow */
export const READY: 'blockchain__ready' = 'blockchain__ready';
export const CONNECTING: 'blockchain__connecting' = 'blockchain__connecting';
export const CONNECTED: 'blockchain__connected' = 'blockchain__connected';
export const DISCONNECTED: 'blockchain__disconnected' = 'blockchain__disconnected';
export const UPDATE_FEE: 'blockchain__update_fee' = 'blockchain__update_fee';

View File

@ -42,8 +42,8 @@ import type {
DeviceMode,
DeviceMessageType,
TransportMessageType,
BlockchainMessageType,
UiMessageType,
BlockchainEvent,
} from 'trezor-connect';
import type { RouterAction, LocationState } from 'react-router-redux';
@ -111,11 +111,6 @@ type UiEventAction = {
// },
}
type BlockchainEventAction = {
type: BlockchainMessageType,
payload: any,
}
// TODO: join this message with uiMessage
type IFrameHandshake = {
type: 'iframe_handshake',
@ -128,7 +123,7 @@ export type Action =
| TransportEventAction
| DeviceEventAction
| UiEventAction
| BlockchainEventAction
| BlockchainEvent
| SelectedAccountAction
| AccountAction