From 12ab43ef41d8d5ab18e816e6cfffe1622b2666c4 Mon Sep 17 00:00:00 2001 From: Szymon Lesisz Date: Mon, 7 May 2018 13:25:46 +0200 Subject: [PATCH] backend not working --- src/js/actions/DiscoveryActions.js | 24 ++++++++-- src/js/actions/Web3Actions.js | 20 ++++++--- src/js/actions/constants/discovery.js | 3 +- .../wallet/account/AbstractAccount.js | 17 +++++-- src/js/flowtype/web3.js | 12 +++-- src/js/reducers/DiscoveryReducer.js | 45 ++++++++++++++++--- src/js/reducers/Web3Reducer.js | 5 ++- src/js/services/LocalStorageService.js | 1 - 8 files changed, 101 insertions(+), 26 deletions(-) diff --git a/src/js/actions/DiscoveryActions.js b/src/js/actions/DiscoveryActions.js index 7c426e6b..b445b886 100644 --- a/src/js/actions/DiscoveryActions.js +++ b/src/js/actions/DiscoveryActions.js @@ -35,7 +35,7 @@ export type DiscoveryStartAction = { } export type DiscoveryWaitingAction = { - type: typeof DISCOVERY.WAITING, + type: typeof DISCOVERY.WAITING_FOR_DEVICE | typeof DISCOVERY.WAITING_FOR_BACKEND, device: TrezorDevice, network: string } @@ -69,13 +69,31 @@ export const start = (device: TrezorDevice, network: string, ignoreCompleted?: b console.warn("Start discovery: Selected device is unavailable...") return; } + + const web3 = getState().web3.find(w3 => w3.network === network); + if (!web3) { + console.error("Start discovery: Web3 does not exist", network) + return; + } + if (!web3.web3.currentProvider.isConnected()) { + console.error("Start discovery: Web3 is not connected", network) + dispatch({ + type: DISCOVERY.WAITING_FOR_BACKEND, + device, + network + }); + return; + } + const discovery: State = getState().discovery; let discoveryProcess: ?Discovery = discovery.find(d => d.deviceState === device.state && d.network === network); + + if (!selected.connected && (!discoveryProcess || !discoveryProcess.completed)) { dispatch({ - type: DISCOVERY.WAITING, + type: DISCOVERY.WAITING_FOR_DEVICE, device, network }); @@ -111,7 +129,7 @@ const begin = (device: TrezorDevice, network: string): AsyncAction => { if (!coinToDiscover) return; dispatch({ - type: DISCOVERY.WAITING, + type: DISCOVERY.WAITING_FOR_DEVICE, device, network }); diff --git a/src/js/actions/Web3Actions.js b/src/js/actions/Web3Actions.js index f1b67f25..1a889937 100644 --- a/src/js/actions/Web3Actions.js +++ b/src/js/actions/Web3Actions.js @@ -18,6 +18,7 @@ import type { Action, AsyncAction, } from '../flowtype'; +import type { ContractFactory } from 'web3'; import type { Account } from '../reducers/AccountsReducer'; import type { PendingTx } from '../reducers/PendingTxReducer'; @@ -32,15 +33,15 @@ export type Web3Action = { export type Web3CreateAction = { type: typeof WEB3.CREATE, network: string, - web3: Web3, //(web3instance) - erc20: any, + web3: Web3, + erc20: ContractFactory, chainId: string; }; export type Web3UpdateBlockAction = { type: typeof WEB3.BLOCK_UPDATED, network: string, - blockHash: any + blockHash: string }; export type Web3UpdateGasPriceAction = { @@ -76,9 +77,18 @@ export function init(web3: ?Web3, coinIndex: number = 0): AsyncAction { if (currentHostIndex + 1 < urls.length) { web3host = urls[currentHostIndex + 1]; } else { - console.error("TODO: Backend " + network + " not working"); + console.error("TODO: Backend " + network + " not working", web3.currentProvider ); + + dispatch({ + type: WEB3.CREATE, + network, + web3, + erc20: web3.eth.contract(ERC20Abi), + chainId: '0' + }); + // try next coin - dispatch( init(web3, coinIndex + 1) ); + dispatch( init(null, coinIndex + 1) ); return; } } diff --git a/src/js/actions/constants/discovery.js b/src/js/actions/constants/discovery.js index e43bc4c7..977f6e00 100644 --- a/src/js/actions/constants/discovery.js +++ b/src/js/actions/constants/discovery.js @@ -4,5 +4,6 @@ export const START: 'discovery__start' = 'discovery__start'; export const STOP: 'discovery__stop' = 'discovery__stop'; export const COMPLETE: 'discovery__complete' = 'discovery__complete'; -export const WAITING: 'discovery__waiting' = 'discovery__waiting'; +export const WAITING_FOR_DEVICE: 'discovery__waiting_for_device' = 'discovery__waiting_for_device'; +export const WAITING_FOR_BACKEND: 'discovery__waiting_for_backend' = 'discovery__waiting_for_backend'; export const FROM_STORAGE: 'discovery__from_storage' = 'discovery__from_storage'; \ No newline at end of file diff --git a/src/js/components/wallet/account/AbstractAccount.js b/src/js/components/wallet/account/AbstractAccount.js index e18b3d4a..2a095813 100644 --- a/src/js/components/wallet/account/AbstractAccount.js +++ b/src/js/components/wallet/account/AbstractAccount.js @@ -58,7 +58,7 @@ export default class AbstractAccount

extends Component d.deviceState === device.state && d.network === currentState.network); - if (!discovery) return; + // if (!discovery) return; const account = props.accounts.find(a => a.deviceState === currentState.deviceState && a.index === currentState.index && a.network === currentState.network); @@ -68,10 +68,13 @@ export default class AbstractAccount

extends Component; } else if (!device.available) { deviceStatusNotification = ; - } else if (!discovery.completed) { - deviceStatusNotification = ; } } + + if (discovery && !discovery.completed && !deviceStatusNotification) { + deviceStatusNotification = ; + } + this.setState({ device, discovery, @@ -112,7 +115,7 @@ export default class AbstractAccount

extends Component - + ); } else { @@ -137,6 +140,12 @@ export default class AbstractAccount

extends Component ); } + } else if (discovery.waitingForBackend) { + return ( +

+ +
+ ); } else if (discovery.completed) { return (
diff --git a/src/js/flowtype/web3.js b/src/js/flowtype/web3.js index 9c31ad1c..1ce4bc79 100644 --- a/src/js/flowtype/web3.js +++ b/src/js/flowtype/web3.js @@ -2,11 +2,17 @@ import type BigNumber from 'bignumber.js'; import type { EthereumUnitT, EthereumAddressT } from 'ethereum-types'; declare module 'web3' { - declare type ProviderT = Object; + declare type ProviderT = { + host: string; + timeout: number; + isConnected: () => boolean; + send: (payload: any) => any; + sendAsync: (payload: any, callback: (error: Error, result: any) => void) => any; + }; declare class Web3T { static providers: { - HttpProvider: (url: string) => ProviderT; + HttpProvider: (host: string, timeout?: number) => ProviderT; }; constructor(ProviderT): Web3T; @@ -54,7 +60,7 @@ declare module 'web3' { // constructor(abi: Array); eth: Eth; abi: Array; - at: (address: string, callback: (error: Error, contract: Contract) => void) => Contract; + at: (address: string, callback: ?(error: Error, contract: Contract) => void) => Contract; // TODO } declare export class Contract { diff --git a/src/js/reducers/DiscoveryReducer.js b/src/js/reducers/DiscoveryReducer.js index 7b9ded10..6977efc0 100644 --- a/src/js/reducers/DiscoveryReducer.js +++ b/src/js/reducers/DiscoveryReducer.js @@ -29,7 +29,7 @@ export type Discovery = { interrupted: boolean; completed: boolean; waitingForDevice: boolean; - waitingForAuth?: boolean; + waitingForBackend: boolean; } export type State = Array; @@ -50,7 +50,8 @@ const start = (state: State, action: DiscoveryStartAction): State => { accountIndex: 0, interrupted: false, completed: false, - waitingForDevice: false + waitingForDevice: false, + waitingForBackend: false, } const newState: State = [ ...state ]; @@ -92,7 +93,7 @@ const stop = (state: State, action: DiscoveryStopAction): State => { }); } -const waiting = (state: State, action: DiscoveryWaitingAction): State => { +const waitingForDevice = (state: State, action: DiscoveryWaitingAction): State => { const deviceState: string = action.device.state || '0'; const instance: Discovery = { @@ -104,7 +105,34 @@ const waiting = (state: State, action: DiscoveryWaitingAction): State => { accountIndex: 0, interrupted: false, completed: false, - waitingForDevice: true + waitingForDevice: true, + waitingForBackend: false, + } + + const index: number = findIndex(state, action.network, deviceState); + const newState: State = [ ...state ]; + if (index >= 0) { + newState[index] = instance; + } else { + newState.push(instance); + } + + return newState; +} + +const waitingForBackend = (state: State, action: DiscoveryWaitingAction): State => { + const deviceState: string = action.device.state || '0'; + const instance: Discovery = { + network: action.network, + deviceState, + xpub: '', + hdKey: null, + basePath: [], + accountIndex: 0, + interrupted: false, + completed: false, + waitingForDevice: false, + waitingForBackend: true } const index: number = findIndex(state, action.network, deviceState); @@ -129,14 +157,17 @@ export default function discovery(state: State = initialState, action: Action): return stop(state, action); case DISCOVERY.COMPLETE : return complete(state, action); - case DISCOVERY.WAITING : - return waiting(state, action) + case DISCOVERY.WAITING_FOR_DEVICE : + return waitingForDevice(state, action); + case DISCOVERY.WAITING_FOR_BACKEND : + return waitingForBackend(state, action); case DISCOVERY.FROM_STORAGE : return action.payload.map(d => { return { ...d, interrupted: false, - waitingForDevice: false + waitingForDevice: false, + waitingForBackend: false, } }) case CONNECT.FORGET : diff --git a/src/js/reducers/Web3Reducer.js b/src/js/reducers/Web3Reducer.js index be13292a..25ab7b8c 100644 --- a/src/js/reducers/Web3Reducer.js +++ b/src/js/reducers/Web3Reducer.js @@ -12,14 +12,15 @@ import type { Web3UpdateBlockAction, Web3UpdateGasPriceAction } from '../actions/Web3Actions'; +import type { ContractFactory } from 'web3'; export type Web3Instance = { network: string; web3: Web3; chainId: number; latestBlock: any; - gasPrice: string; //BigNumber - erc20: any; //ContractFactory + gasPrice: string; + erc20: ContractFactory; } export type State = Array; diff --git a/src/js/services/LocalStorageService.js b/src/js/services/LocalStorageService.js index 88753163..37905f38 100644 --- a/src/js/services/LocalStorageService.js +++ b/src/js/services/LocalStorageService.js @@ -118,7 +118,6 @@ const LocalStorageService: Middleware = (api: MiddlewareAPI) => (next: Middlewar case DISCOVERY.START : case DISCOVERY.STOP : case DISCOVERY.COMPLETE : - // case DISCOVERY.WAITING : save(api.dispatch, api.getState); break;