-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstore.js
105 lines (90 loc) · 3.01 KB
/
store.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { createStore, applyMiddleware } from 'redux'
import thunkMiddleware from 'redux-thunk'
import promiseMiddleware from 'redux-promise'
import Web3 from 'web3'
const web3 = new Web3('http://localhost:8545')
const web3websockets = new Web3('wss://mainnet.infura.io/ws')
const initialState = {
latestBlock: '',
peers: [],
activity: [],
suspiciousActivity: [],
currentGasPrice: 0
}
export const actionTypes = {
LATEST_BLOCK: 'LATEST_BLOCK',
PEERS: 'PEERS',
CURRENT_GAS_PRICE: 'CURRENT_GAS_PRICE',
SUSPICIOUS_ACTIVITY: 'SUSPICIOUS_ACTIVITY'
}
export const hackerWatcher = (watchedAddress) => (dispatch) => {
web3websockets.eth.subscribe('pendingTransactions')
.on('data', (pending) => {
web3.eth.getTransaction(pending)
.then((suspicious) => {
if (suspicious) {
if (suspicious.to === watchedAddress || suspicious.from === watchedAddress) {
// Disable Twilio for now
// fetch('/message')
dispatch(setSuspiciousActivity(suspicious))
}
}
})
})
}
export const reducer = (state = initialState, action) => {
switch (action.type) {
case actionTypes.LATEST_BLOCK:
const numberOfTransactions = action.latestBlock.transactions.length
let activity_ = [...state.activity]
if (activity_.length > 99) {
activity_.shift()
}
if (numberOfTransactions !== activity_.length) {
activity_.push({ name: 'activity', amount: numberOfTransactions })
}
return { ...state, latestBlock: action.latestBlock, activity: activity_ }
case actionTypes.PEERS:
return { ...state, peers: action.peers }
case actionTypes.SUSPICIOUS_ACTIVITY:
return { ...state, suspiciousActivity: [...state.suspiciousActivity, action.suspiciousActivity] }
case actionTypes.CURRENT_GAS_PRICE:
return { ...state, currentGasPrice: action.currentGasPrice }
default:
return state
}
}
export const startUpdates = () => (dispatch) => {
// Leave this here for now so it doesn't hammer the iplocation API
dispatch(getPeers())
setInterval(() => {
dispatch(getLatestBlock())
dispatch(getGasPrice())
}, 1500)
}
const getLatestBlock = async () => {
const latestBlock = await web3.eth.getBlock('latest')
return { type: actionTypes.LATEST_BLOCK, latestBlock }
}
const resolvePeers = async () => {
const peers = await fetch('/peers.json')
return peers.json()
}
const getPeers = async () => {
const peers = await resolvePeers()
return { type: actionTypes.PEERS, peers }
}
const setSuspiciousActivity = (suspiciousActivity) => {
return { type: actionTypes.SUSPICIOUS_ACTIVITY, suspiciousActivity }
}
const getGasPrice = async () => {
const wei = await web3.eth.getGasPrice()
return { type: actionTypes.CURRENT_GAS_PRICE, currentGasPrice: web3.utils.fromWei(wei, 'ether') }
}
export const initStore = (initialState = initialState) => {
return createStore(
reducer,
initialState,
applyMiddleware(promiseMiddleware, thunkMiddleware)
)
}