You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
trezor-wallet/src/reducers/Web3Reducer.js

60 lines
1.7 KiB

6 years ago
/* @flow */
6 years ago
6 years ago
import Web3 from 'web3';
6 years ago
import type { ContractFactory } from 'web3';
import * as STORAGE from 'actions/constants/localStorage';
import * as WEB3 from 'actions/constants/web3';
6 years ago
import type { Action } from 'flowtype';
import type {
Web3UpdateBlockAction,
Web3UpdateGasPriceAction,
} from 'actions/Web3Actions';
export type Web3Instance = {
network: string;
6 years ago
web3: Web3;
chainId: number;
latestBlock: any;
gasPrice: string;
erc20: ContractFactory;
6 years ago
}
export type State = Array<Web3Instance>;
const initialState: State = [];
6 years ago
const createWeb3 = (state: State, instance: Web3Instance): State => {
const newState: Array<Web3Instance> = [...state];
6 years ago
newState.push(instance);
return newState;
};
6 years ago
const updateLatestBlock = (state: State, action: Web3UpdateBlockAction): State => {
const index: number = state.findIndex(w3 => w3.network === action.network);
const newState: Array<Web3Instance> = [...state];
6 years ago
newState[index].latestBlock = action.blockHash;
return newState;
};
6 years ago
const updateGasPrice = (state: State, action: Web3UpdateGasPriceAction): State => {
const index: number = state.findIndex(w3 => w3.network === action.network);
const newState: State = [...state];
6 years ago
newState[index].gasPrice = action.gasPrice;
return newState;
};
6 years ago
export default function web3(state: State = initialState, action: Action): State {
6 years ago
switch (action.type) {
case WEB3.CREATE:
return createWeb3(state, action.instance);
case WEB3.BLOCK_UPDATED:
6 years ago
return updateLatestBlock(state, action);
case WEB3.GAS_PRICE_UPDATED:
6 years ago
return updateGasPrice(state, action);
6 years ago
default:
return state;
}
}