1 import { combineReducers } from 'redux'
2 import { testnetUrl } from 'utility/environment'
3 import moment from 'moment'
4 import { DeltaSampler } from 'utility/time'
6 const LONG_TIME_FORMAT = 'YYYY-MM-DD, h:mm:ss a'
8 const coreConfigReducer = (key, state, defaultState, action) => {
9 if (action.type == 'UPDATE_CORE_INFO') {
10 return action.param.data[key] || defaultState
13 return state || defaultState
16 const buildConfigReducer = (key, state, defaultState, action) => {
17 // if (action.type == 'UPDATE_CORE_INFO') {
18 // return action.param.buildConfig[key] || defaultState
21 return state || defaultState
24 const configKnown = (state = false, action) => {
25 if (action.type == 'UPDATE_CORE_INFO') {
31 export const configured = (state, action) =>
32 coreConfigReducer('isConfigured', state, false, action)
33 export const configuredAt = (state, action) => {
34 let value = coreConfigReducer('configuredAt', state, '', action)
35 if (action.type == 'UPDATE_CORE_INFO' && value != '') {
36 value = moment(value).format(LONG_TIME_FORMAT)
41 // export const mockhsm = (state, action) =>
42 // buildConfigReducer('isMockhsm', state, false, action)
43 // export const localhostAuth = (state, action) =>
44 // buildConfigReducer('isLocalhostAuth', state, false, action)
45 // export const reset = (state, action) =>
46 // buildConfigReducer('isReset', state, false, action)
47 // export const httpOk = (state, action) =>
48 // buildConfigReducer('isHttpOk', state, false, action)
49 export const blockHeight = (state, action) =>
50 coreConfigReducer('highestBlock', state, 0, action)
51 export const currentBlockHeight = (state, action) =>
52 coreConfigReducer('currentBlock', state, 0, action)
53 export const peerCount = (state, action) =>
54 coreConfigReducer('peerCount', state, 0, action)
55 export const generatorBlockHeight = (state, action) => {
56 if (action.type == 'UPDATE_CORE_INFO') {
57 if (action.param.generatorBlockHeight == 0) return '???'
60 return coreConfigReducer('generatorBlockHeight', state, 0, action)
62 export const signer = (state, action) =>
63 coreConfigReducer('isSigner', state, false, action)
64 export const generator = (state, action) =>
65 coreConfigReducer('isGenerator', state, false, action)
66 export const generatorUrl = (state, action) =>
67 coreConfigReducer('generatorUrl', state, false, action)
68 export const generatorAccessToken = (state, action) =>
69 coreConfigReducer('generatorAccessToken', state, false, action)
70 export const blockchainId = (state, action) =>
71 coreConfigReducer('blockchainId', state, 0, action)
72 // export const crosscoreRpcVersion = (state, action) =>
73 // coreConfigReducer('crosscoreRpcVersion', state, 0, action)
75 // export const coreType = (state = '', action) => {
76 // if (action.type == 'UPDATE_CORE_INFO') {
77 // if (action.param.isGenerator) return 'Generator'
78 // if (action.param.isSigner) return 'Signer'
79 // return 'Participant'
84 // export const replicationLag = (state = null, action) => {
85 // if (action.type == 'UPDATE_CORE_INFO') {
86 // if (action.param.generatorBlockHeight == 0) {
89 // return action.param.generatorBlockHeight - action.param.blockHeight
95 // let syncSamplers = null
96 // const resetSyncSamplers = () => {
98 // snapshot: new DeltaSampler({sampleTtl: 10 * 1000}),
99 // replicationLag: new DeltaSampler({sampleTtl: 10 * 1000}),
103 // export const syncEstimates = (state = {}, action) => {
104 // switch (action.type) {
105 // case 'UPDATE_CORE_INFO': {
106 // if (!syncSamplers) {
107 // resetSyncSamplers()
112 // generatorBlockHeight,
116 // const estimates = {}
118 // if (snapshot && snapshot.inProgress) {
119 // const speed = syncSamplers.snapshot.sample(snapshot.downloaded)
122 // estimates.snapshot = (snapshot.size - snapshot.downloaded) / speed
124 // } else if (generatorBlockHeight > 0) {
125 // const replicationLag = generatorBlockHeight - blockHeight
126 // const speed = syncSamplers.replicationLag.sample(replicationLag)
128 // const duration = -1 * replicationLag / speed
129 // if (duration > 0) {
130 // estimates.replicationLag = duration
138 // case 'CORE_DISCONNECT':
139 // resetSyncSamplers()
147 // export const replicationLagClass = (state = null, action) => {
148 // if (action.type == 'UPDATE_CORE_INFO') {
149 // if (action.param.generatorBlockHeight == 0) {
152 // let lag = action.param.generatorBlockHeight - action.param.blockHeight
155 // } else if (lag < 10) {
166 // export const onTestnet = (state = false, action) => {
167 // if (action.type == 'UPDATE_CORE_INFO') {
168 // return (action.param.generatorUrl || '').indexOf(testnetUrl) >= 0
174 export const requireClientToken = (state = false, action) => {
175 if (action.type == 'ERROR' && action.payload.status == 401) return true
180 export const clientToken = (state = '', action) => {
181 if (action.type == 'SET_CLIENT_TOKEN') return action.token
182 else if (action.type == 'ERROR' &&
183 action.payload.status == 401) return ''
188 export const validToken = (state = false, action) => {
189 if (action.type == 'SET_CLIENT_TOKEN') return false
190 else if (action.type == 'USER_LOG_IN') return true
191 else if (action.type == 'ERROR' &&
192 action.payload.status == 401) return false
197 export const connected = (state = true, action) => {
198 if (action.type == 'UPDATE_CORE_INFO') return true
199 else if (action.type == 'CORE_DISCONNECT') return false
204 export const btmAmountUnit = (state = 'BTM' , action) => {
205 if (action.type == 'UPDATE_BTM_AMOUNT_UNIT') {
211 const mingStatus = (state = false, action) => {
212 if (action.type == 'UPDATE_CORE_INFO') {
213 return action.param.data.mining
218 const coreData = (state = null, action) => {
219 if (action.type == 'UPDATE_CORE_INFO') {
220 return action.param.data || null
225 const accountInit = (state = false, action) => {
226 if (action.type == 'CREATE_REGISTER_ACCOUNT') {
232 const snapshot = (state = null, action) => {
233 if (action.type == 'UPDATE_CORE_INFO') {
234 return action.param.snapshot || null // snapshot may be undefined, which Redux doesn't like.
239 const version = (state = 'N/A', action) => {
240 if (action.type == 'UPDATE_CORE_INFO') {
241 return action.param.data.versionInfo.version
246 const newVersionCode = (state = 'N/A', action) => {
247 if (action.type == 'UPDATE_CORE_INFO') {
248 return action.param.data.versionInfo.newVersion
253 const update = (state = false, action) => {
254 if (action.type == 'UPDATE_CORE_INFO') {
255 return action.param.data.versionInfo.update > 0
261 export default combineReducers({
274 generatorAccessToken,
275 generatorBlockHeight,
281 // crosscoreRpcVersion,
286 // replicationLagClass,