1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-15 21:08:57 +00:00
trezor-wallet/src/js/reducers/Web3Reducer.js

63 lines
1.7 KiB
JavaScript
Raw Normal View History

2017-12-13 11:01:37 +00:00
/* @flow */
'use strict';
2018-02-20 09:30:36 +00:00
import Web3 from 'web3';
2017-12-13 11:01:37 +00:00
2018-04-11 10:13:38 +00:00
import * as STORAGE from '../actions/constants/localStorage';
import * as WEB3 from '../actions/constants/web3';
2018-02-20 09:30:36 +00:00
import type { Action } from '~/flowtype';
2018-04-16 21:19:50 +00:00
import type {
Web3UpdateBlockAction,
Web3UpdateGasPriceAction
} from '../actions/Web3Actions';
2018-05-07 11:25:46 +00:00
import type { ContractFactory } from 'web3';
2018-04-16 21:19:50 +00:00
export type Web3Instance = {
network: string;
2018-02-20 09:30:36 +00:00
web3: Web3;
chainId: number;
latestBlock: any;
2018-05-07 11:25:46 +00:00
gasPrice: string;
erc20: ContractFactory;
2018-02-20 09:30:36 +00:00
}
2018-04-16 21:19:50 +00:00
export type State = Array<Web3Instance>;
const initialState: State = [];
2018-02-20 09:30:36 +00:00
2018-05-22 08:26:34 +00:00
const createWeb3 = (state: State, instance: Web3Instance): State => {
2018-02-20 09:30:36 +00:00
const newState: Array<Web3Instance> = [ ...state ];
newState.push(instance);
return newState;
}
2018-04-16 21:19:50 +00:00
const updateLatestBlock = (state: State, action: Web3UpdateBlockAction): State => {
const index: number = state.findIndex(w3 => w3.network === action.network);
2018-02-20 09:30:36 +00:00
const newState: Array<Web3Instance> = [ ...state ];
newState[index].latestBlock = action.blockHash;
return newState;
2017-12-13 11:01:37 +00:00
}
2018-04-16 21:19:50 +00:00
const updateGasPrice = (state: State, action: Web3UpdateGasPriceAction): State => {
const index: number = state.findIndex(w3 => w3.network === action.network);
2018-04-16 21:19:50 +00:00
const newState: State = [ ...state ];
2018-02-20 09:30:36 +00:00
newState[index].gasPrice = action.gasPrice;
return newState;
}
2017-12-13 11:01:37 +00:00
2018-04-16 21:19:50 +00:00
export default function web3(state: State = initialState, action: Action): State {
2017-12-13 11:01:37 +00:00
switch (action.type) {
2018-02-20 09:30:36 +00:00
case WEB3.CREATE :
2018-05-22 08:26:34 +00:00
return createWeb3(state, action.instance);
2018-02-20 09:30:36 +00:00
case WEB3.BLOCK_UPDATED :
return updateLatestBlock(state, action);
case WEB3.GAS_PRICE_UPDATED :
return updateGasPrice(state, action);
2017-12-13 11:01:37 +00:00
default:
return state;
}
}