1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-15 12:59:09 +00:00
trezor-wallet/src/reducers/Web3Reducer.js

70 lines
2.1 KiB
JavaScript
Raw Normal View History

2017-12-13 11:01:37 +00:00
/* @flow */
2018-07-30 10:52:13 +00:00
2018-02-20 09:30:36 +00:00
import Web3 from 'web3';
2017-12-13 11:01:37 +00:00
2018-09-12 11:24:07 +00:00
import type { Contract } from 'web3';
2018-08-14 13:11:52 +00:00
import * as WEB3 from 'actions/constants/web3';
2018-02-20 09:30:36 +00:00
2018-08-14 12:56:47 +00:00
import type { Action } from 'flowtype';
2019-03-04 12:33:02 +00:00
import type { Web3UpdateBlockAction, Web3UpdateGasPriceAction } from 'actions/Web3Actions';
2018-04-16 21:19:50 +00:00
export type Web3Instance = {
2019-03-04 12:33:02 +00:00
network: string,
web3: Web3,
chainId: number,
latestBlock: any,
gasPrice: string,
erc20: Contract,
};
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-09-12 11:24:07 +00:00
const index: number = state.findIndex(w3 => w3.network === instance.network);
2018-07-30 10:52:13 +00:00
const newState: Array<Web3Instance> = [...state];
2018-09-12 11:24:07 +00:00
if (index >= 0) {
newState[index] = instance;
} else {
newState.push(instance);
}
2018-02-20 09:30:36 +00:00
return newState;
2018-07-30 10:52:13 +00:00
};
2018-02-20 09:30:36 +00:00
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-07-30 10:52:13 +00:00
const newState: Array<Web3Instance> = [...state];
2018-02-20 09:30:36 +00:00
newState[index].latestBlock = action.blockHash;
return newState;
2018-07-30 10:52:13 +00:00
};
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-07-30 10:52:13 +00:00
const newState: State = [...state];
2018-02-20 09:30:36 +00:00
newState[index].gasPrice = action.gasPrice;
return newState;
2018-07-30 10:52:13 +00:00
};
2017-12-13 11:01:37 +00:00
2018-09-13 12:40:41 +00:00
const disconnect = (state: State, instance: Web3Instance): State => {
const index: number = state.indexOf(instance);
const newState: Array<Web3Instance> = [...state];
newState.splice(index, 1);
return newState;
};
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-07-30 10:52:13 +00:00
case WEB3.CREATE:
2018-05-22 08:26:34 +00:00
return createWeb3(state, action.instance);
2018-07-30 10:52:13 +00:00
case WEB3.BLOCK_UPDATED:
2018-02-20 09:30:36 +00:00
return updateLatestBlock(state, action);
2018-07-30 10:52:13 +00:00
case WEB3.GAS_PRICE_UPDATED:
2018-02-20 09:30:36 +00:00
return updateGasPrice(state, action);
2018-09-13 12:40:41 +00:00
case WEB3.DISCONNECT:
return disconnect(state, action.instance);
2017-12-13 11:01:37 +00:00
default:
return state;
}
2019-03-04 12:33:02 +00:00
}