From 95d1bdbd9e3d56c2e43486923df2c1bdd97ae31b Mon Sep 17 00:00:00 2001 From: Szymon Lesisz Date: Thu, 29 Nov 2018 21:08:41 +0100 Subject: [PATCH] handle trezor-connect blockchain events --- src/actions/BlockchainActions.js | 99 +++++++++++++++++------------ src/actions/TrezorConnectActions.js | 13 ++-- src/actions/constants/blockchain.js | 4 +- src/flowtype/index.js | 9 +-- 4 files changed, 64 insertions(+), 61 deletions(-) diff --git a/src/actions/BlockchainActions.js b/src/actions/BlockchainActions.js index efddb58b..3a3279f8 100644 --- a/src/actions/BlockchainActions.js +++ b/src/actions/BlockchainActions.js @@ -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 => async (dispatch: Dispatch): Promise => { @@ -89,11 +92,24 @@ export const estimateGasLimit = (network: string, data: string, value: string, g return result; }; -export const onBlockMined = (coinInfo: any): PromiseAction => async (dispatch: Dispatch, getState: GetState): Promise => { - // incoming "coinInfo" from TrezorConnect is CoinInfo | EthereumNetwork type - const network: string = coinInfo.shortcut.toLowerCase(); +export const onBlockMined = (payload: $ElementType): PromiseAction => async (dispatch: Dispatch, getState: GetState): Promise => { + 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 => async (dispa // not used for now, waiting for fix in blockbook -/* -export const onNotification = (payload: any): PromiseAction => async (dispatch: Dispatch, getState: GetState): Promise => { +export const onNotification = (payload: $ElementType): PromiseAction => async (/*dispatch: Dispatch, getState: GetState*/): Promise => { + 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 => async (dispatch: Dispatch, getState: GetState): Promise => { diff --git a/src/actions/TrezorConnectActions.js b/src/actions/TrezorConnectActions.js index d90530fb..dfbde361 100644 --- a/src/actions/TrezorConnectActions.js +++ b/src/actions/TrezorConnectActions.js @@ -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()) { diff --git a/src/actions/constants/blockchain.js b/src/actions/constants/blockchain.js index ff0d9b36..9e19d3e3 100644 --- a/src/actions/constants/blockchain.js +++ b/src/actions/constants/blockchain.js @@ -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'; \ No newline at end of file +export const UPDATE_FEE: 'blockchain__update_fee' = 'blockchain__update_fee'; \ No newline at end of file diff --git a/src/flowtype/index.js b/src/flowtype/index.js index bbcfdd86..f486b32c 100644 --- a/src/flowtype/index.js +++ b/src/flowtype/index.js @@ -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