1 import { combineReducers } from 'redux'
2 import { testnetUrl, mode } 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)
35 export const configuredAt = (state, action) => {
36 let value = coreConfigReducer('configuredAt', state, '', action)
37 if (action.type == 'UPDATE_CORE_INFO' && value != '') {
38 value = moment(value).format(LONG_TIME_FORMAT)
43 // export const mockhsm = (state, action) =>
44 // buildConfigReducer('isMockhsm', state, false, action)
45 // export const localhostAuth = (state, action) =>
46 // buildConfigReducer('isLocalhostAuth', state, false, action)
47 // export const reset = (state, action) =>
48 // buildConfigReducer('isReset', state, false, action)
49 // export const httpOk = (state, action) =>
50 // buildConfigReducer('isHttpOk', state, false, action)
51 export const blockHeight = (state, action) =>
52 coreConfigReducer('highestBlock', state, 0, action)
53 export const currentBlockHeight = (state, action) =>
54 coreConfigReducer('currentBlock', state, 0, action)
55 export const peerCount = (state, action) =>
56 coreConfigReducer('peerCount', state, 0, action)
57 export const generatorBlockHeight = (state, action) => {
58 if (action.type == 'UPDATE_CORE_INFO') {
59 if (action.param.generatorBlockHeight == 0) return '???'
62 return coreConfigReducer('generatorBlockHeight', state, 0, action)
64 export const signer = (state, action) =>
65 coreConfigReducer('isSigner', state, false, action)
66 export const generator = (state, action) =>
67 coreConfigReducer('isGenerator', state, false, action)
68 export const generatorUrl = (state, action) =>
69 coreConfigReducer('generatorUrl', state, false, action)
70 export const generatorAccessToken = (state, action) =>
71 coreConfigReducer('generatorAccessToken', state, false, action)
72 export const blockchainId = (state, action) =>
73 coreConfigReducer('blockchainId', state, 0, action)
74 // export const crosscoreRpcVersion = (state, action) =>
75 // coreConfigReducer('crosscoreRpcVersion', state, 0, action)
77 // export const coreType = (state = '', action) => {
78 // if (action.type == 'UPDATE_CORE_INFO') {
79 // if (action.param.isGenerator) return 'Generator'
80 // if (action.param.isSigner) return 'Signer'
81 // return 'Participant'
86 // export const replicationLag = (state = null, action) => {
87 // if (action.type == 'UPDATE_CORE_INFO') {
88 // if (action.param.generatorBlockHeight == 0) {
91 // return action.param.generatorBlockHeight - action.param.blockHeight
97 // let syncSamplers = null
98 // const resetSyncSamplers = () => {
100 // snapshot: new DeltaSampler({sampleTtl: 10 * 1000}),
101 // replicationLag: new DeltaSampler({sampleTtl: 10 * 1000}),
105 // export const syncEstimates = (state = {}, action) => {
106 // switch (action.type) {
107 // case 'UPDATE_CORE_INFO': {
108 // if (!syncSamplers) {
109 // resetSyncSamplers()
114 // generatorBlockHeight,
118 // const estimates = {}
120 // if (snapshot && snapshot.inProgress) {
121 // const speed = syncSamplers.snapshot.sample(snapshot.downloaded)
124 // estimates.snapshot = (snapshot.size - snapshot.downloaded) / speed
126 // } else if (generatorBlockHeight > 0) {
127 // const replicationLag = generatorBlockHeight - blockHeight
128 // const speed = syncSamplers.replicationLag.sample(replicationLag)
130 // const duration = -1 * replicationLag / speed
131 // if (duration > 0) {
132 // estimates.replicationLag = duration
140 // case 'CORE_DISCONNECT':
141 // resetSyncSamplers()
149 // export const replicationLagClass = (state = null, action) => {
150 // if (action.type == 'UPDATE_CORE_INFO') {
151 // if (action.param.generatorBlockHeight == 0) {
154 // let lag = action.param.generatorBlockHeight - action.param.blockHeight
157 // } else if (lag < 10) {
168 // export const onTestnet = (state = false, action) => {
169 // if (action.type == 'UPDATE_CORE_INFO') {
170 // return (action.param.generatorUrl || '').indexOf(testnetUrl) >= 0
176 export const requireClientToken = (state = false, action) => {
177 if (action.type == 'ERROR' && action.payload.status == 401) return true
182 export const clientToken = (state = '', action) => {
183 if (action.type == 'SET_CLIENT_TOKEN') return action.token
184 else if (action.type == 'ERROR' &&
185 action.payload.status == 401) return ''
190 export const validToken = (state = false, action) => {
191 if (action.type == 'SET_CLIENT_TOKEN') return false
192 else if (action.type == 'USER_LOG_IN') return true
193 else if (action.type == 'ERROR' &&
194 action.payload.status == 401) return false
199 export const connected = (state = true, action) => {
200 if (action.type == 'UPDATE_CORE_INFO') return true
201 else if (action.type == 'CORE_DISCONNECT') return false
206 export const btmAmountUnit = (state = 'BTM' , action) => {
207 if (action.type == 'UPDATE_BTM_AMOUNT_UNIT') {
213 let configuredState = false
215 configuredState = window.remote.getGlobal('fileExist')
217 export const configured = (state = configuredState, action) => {
218 if( mode === 'electron'){
219 if (action.type == 'SET_CONFIGURED') {
228 const mingStatus = (state = false, action) => {
229 if (action.type == 'UPDATE_CORE_INFO') {
230 if(window.remote && window.remote.getGlobal('mining').isMining!= action.param.data.mining){
231 window.remote.getGlobal('mining').isMining = action.param.data.mining
232 window.ipcRenderer.send('refresh-menu')
235 return action.param.data.mining
240 const coreData = (state = null, action) => {
241 if (action.type == 'UPDATE_CORE_INFO') {
242 return action.param.data || null
247 const accountInit = (state = false, action) => {
248 if (action.type == 'CREATE_REGISTER_ACCOUNT') {
254 const snapshot = (state = null, action) => {
255 if (action.type == 'UPDATE_CORE_INFO') {
256 return action.param.snapshot || null // snapshot may be undefined, which Redux doesn't like.
261 const version = (state = 'N/A', action) => {
262 if (action.type == 'UPDATE_CORE_INFO') {
263 return action.param.data.versionInfo.version
268 const newVersionCode = (state = 'N/A', action) => {
269 if (action.type == 'UPDATE_CORE_INFO') {
270 return action.param.data.versionInfo.newVersion
275 const update = (state = false, action) => {
276 if (action.type == 'UPDATE_CORE_INFO') {
277 return action.param.data.versionInfo.update > 0
283 export default combineReducers({
296 generatorAccessToken,
297 generatorBlockHeight,
303 // crosscoreRpcVersion,
308 // replicationLagClass,