2018-08-01 14:09:58 -04:00
|
|
|
import {combineReducers} from 'redux';
|
2018-08-30 13:13:37 +01:00
|
|
|
import {REQUEST, SUCCESS, FAILURE, CONTRACT_COMPILE, FILES} from "../actions";
|
2018-08-07 15:09:55 +01:00
|
|
|
|
|
|
|
const BN_FACTOR = 10000;
|
2018-08-14 10:23:48 +01:00
|
|
|
const voidAddress = '0x0000000000000000000000000000000000000000';
|
2018-08-07 15:09:55 +01:00
|
|
|
|
|
|
|
const entitiesDefaultState = {
|
|
|
|
accounts: [],
|
|
|
|
blocks: [],
|
|
|
|
transactions: [],
|
|
|
|
processes: [],
|
|
|
|
processLogs: [],
|
|
|
|
contracts: [],
|
|
|
|
contractProfiles: [],
|
2018-08-16 12:17:13 +01:00
|
|
|
contractFunctions: [],
|
2018-08-16 16:18:07 +01:00
|
|
|
contractDeploys: [],
|
2018-08-30 13:13:37 +01:00
|
|
|
contractCompiles: [],
|
2018-08-09 15:33:07 -04:00
|
|
|
contractLogs: [],
|
2018-08-08 15:03:46 -04:00
|
|
|
commands: [],
|
|
|
|
messages: [],
|
2018-08-08 15:09:40 -04:00
|
|
|
messageChannels: [],
|
2018-08-10 11:31:10 +01:00
|
|
|
versions: [],
|
2018-08-13 12:08:59 +01:00
|
|
|
plugins: [],
|
2018-08-30 13:13:37 +01:00
|
|
|
ensRecords: [],
|
2018-08-31 16:44:43 -04:00
|
|
|
files: [],
|
2018-08-30 13:13:37 +01:00
|
|
|
gasOracleStats: [],
|
|
|
|
currentFiles: []
|
2018-08-07 15:09:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const sorter = {
|
|
|
|
blocks: function(a, b) {
|
|
|
|
return b.number - a.number;
|
|
|
|
},
|
|
|
|
transactions: function(a, b) {
|
|
|
|
return ((BN_FACTOR * b.blockNumber) + b.transactionIndex) - ((BN_FACTOR * a.blockNumber) + a.transactionIndex);
|
|
|
|
},
|
|
|
|
processLogs: function(a, b) {
|
|
|
|
return a.timestamp - b.timestamp;
|
2018-08-08 15:03:46 -04:00
|
|
|
},
|
2018-08-09 15:33:07 -04:00
|
|
|
contractLogs: function(a, b) {
|
|
|
|
return a.timestamp - b.timestamp;
|
|
|
|
},
|
2018-08-08 15:03:46 -04:00
|
|
|
messages: function(a, b) {
|
|
|
|
return a.time - b.time;
|
2018-08-31 10:57:52 +01:00
|
|
|
},
|
|
|
|
commands: function(a, b) {
|
|
|
|
return b.timestamp - a.timestamp;
|
2018-08-30 13:13:37 +01:00
|
|
|
},
|
|
|
|
files: function(a, b) {
|
|
|
|
if (a.name < b.name) return -1;
|
|
|
|
if (a.name > b.name) return 1;
|
|
|
|
return 0;
|
2018-08-07 15:09:55 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const filtrer = {
|
|
|
|
processes: function(process, index, self) {
|
|
|
|
return index === self.findIndex((t) => t.name === process.name);
|
|
|
|
},
|
|
|
|
contracts: function(contract, index, self) {
|
2018-08-10 12:57:37 -04:00
|
|
|
return index === self.findIndex((t) => t.className === contract.className);
|
2018-08-07 15:09:55 +01:00
|
|
|
},
|
|
|
|
accounts: function(account, index, self) {
|
|
|
|
return index === self.findIndex((t) => t.address === account.address);
|
|
|
|
},
|
|
|
|
blocks: function(block, index, self) {
|
|
|
|
return index === self.findIndex((t) => t.number === block.number);
|
|
|
|
},
|
|
|
|
transactions: function(tx, index, self) {
|
|
|
|
return index === self.findIndex((t) => (
|
|
|
|
t.blockNumber === tx.blockNumber && t.transactionIndex === tx.transactionIndex
|
|
|
|
));
|
2018-08-14 10:23:48 +01:00
|
|
|
},
|
|
|
|
ensRecords: function(record, index, self) {
|
2018-08-14 16:22:15 +01:00
|
|
|
return record.name && record.address && record.address !== voidAddress && index === self.findIndex((r) => (
|
2018-09-07 10:40:28 -04:00
|
|
|
r.address === record.address && r.name === record.name
|
2018-08-14 10:23:48 +01:00
|
|
|
));
|
2018-08-30 13:13:37 +01:00
|
|
|
},
|
|
|
|
files: function(file, index, self) {
|
|
|
|
return index === self.findIndex((f) => (
|
|
|
|
file.name === f.name
|
|
|
|
));
|
2018-08-31 16:47:58 -04:00
|
|
|
},
|
|
|
|
gasOracleStats: function(stat, index, _self) {
|
|
|
|
return index === 0; // Only keep last one
|
2018-08-07 15:09:55 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function entities(state = entitiesDefaultState, action) {
|
2018-08-30 13:13:37 +01:00
|
|
|
if (action.type === FILES[SUCCESS]) {
|
|
|
|
return {...state, files: action.files};
|
|
|
|
}
|
2018-08-07 15:09:55 +01:00
|
|
|
for (let name of Object.keys(state)) {
|
|
|
|
let filter = filtrer[name] || (() => true);
|
|
|
|
let sort = sorter[name] || (() => true);
|
2018-08-10 11:31:10 +01:00
|
|
|
if (action[name] && action[name].length > 1) {
|
2018-08-07 15:09:55 +01:00
|
|
|
return {...state, [name]: [...action[name], ...state[name]].filter(filter).sort(sort)};
|
|
|
|
}
|
|
|
|
if (action[name] && action[name].length === 1) {
|
|
|
|
let entity = action[name][0];
|
|
|
|
let nested = Object.keys(state).reduce((acc, entityName) => {
|
|
|
|
if (entity[entityName] && entity[entityName].length > 0) {
|
|
|
|
let entityFilter = filtrer[entityName] || (() => true);
|
|
|
|
let entitySort = sorter[entityName] || (() => true);
|
|
|
|
acc[entityName] = [...entity[entityName], ...state[entityName]].filter(entityFilter).sort(entitySort);
|
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
return {
|
|
|
|
...state, ...nested, [name]: [...action[name], ...state[name]].filter(filter).sort(sort)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
function errorMessage(state = null, action) {
|
2018-08-10 12:38:55 +10:00
|
|
|
return action.error || state;
|
2018-08-07 15:09:55 +01:00
|
|
|
}
|
|
|
|
|
2018-08-14 17:17:47 +01:00
|
|
|
function errorEntities(state = {}, action) {
|
|
|
|
if (!action.type.endsWith(SUCCESS)) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
let newState = {};
|
|
|
|
for (let name of Object.keys(entitiesDefaultState)) {
|
|
|
|
if (action[name] && action[name].length > 0) {
|
|
|
|
newState[name] = action[name][0].error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {...state, ...newState};
|
|
|
|
}
|
|
|
|
|
2018-08-07 15:09:55 +01:00
|
|
|
function loading(_state = false, action) {
|
2018-09-04 13:51:33 -04:00
|
|
|
return action.type.endsWith(REQUEST) && !action.noLoading;
|
2018-08-07 15:09:55 +01:00
|
|
|
}
|
2018-08-01 14:13:51 +01:00
|
|
|
|
2018-08-30 13:13:37 +01:00
|
|
|
function compilingContract(state = false, action) {
|
2018-09-07 10:40:28 -04:00
|
|
|
if (action.type === CONTRACT_COMPILE[REQUEST]) {
|
2018-08-30 13:13:37 +01:00
|
|
|
return true;
|
|
|
|
} else if (action.type === CONTRACT_COMPILE[FAILURE] || action.type === CONTRACT_COMPILE[SUCCESS]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2018-09-07 10:40:28 -04:00
|
|
|
function token(state = null, action) {
|
|
|
|
return (action.token) ? action.token : state;
|
|
|
|
}
|
|
|
|
|
2018-08-02 11:27:33 +01:00
|
|
|
const rootReducer = combineReducers({
|
2018-08-07 15:09:55 +01:00
|
|
|
entities,
|
|
|
|
loading,
|
2018-08-30 13:13:37 +01:00
|
|
|
compilingContract,
|
2018-08-14 17:17:47 +01:00
|
|
|
errorMessage,
|
2018-09-07 10:40:28 -04:00
|
|
|
errorEntities,
|
|
|
|
token
|
2018-08-02 11:27:33 +01:00
|
|
|
});
|
2018-08-01 14:13:51 +01:00
|
|
|
|
|
|
|
export default rootReducer;
|