This repository was archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 816
Expand file tree
/
Copy pathactions.js
More file actions
226 lines (191 loc) · 6.59 KB
/
Copy pathactions.js
File metadata and controls
226 lines (191 loc) · 6.59 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import { ipcRenderer } from "electron";
import { web3ActionCreator } from "../web3/helpers/Web3ActionCreator";
const prefix = "EVENTS";
const PAGE_SIZE = 10;
export const ADD_EVENTS_TO_VIEW = `${prefix}/ADD_EVENTS_TO_VIEW`;
export const CLEAR_EVENTS_IN_VIEW = `${prefix}/CLEAR_EVENTS_IN_VIEW`;
export const SET_BLOCKS_REQUESTED = `${prefix}/SET_BLOCKS_REQUESTED`;
export const clearEventsInView = function () {
return { type: CLEAR_EVENTS_IN_VIEW, events: [] };
};
export const SET_SUBSCRIBED_TOPICS = `${prefix}/SET_SUBSCRIBED_TOPICS`;
export const setSubscribedTopics = function (topics) {
return { type: SET_SUBSCRIBED_TOPICS, topics };
};
export const SET_LOADING = `${prefix}/SET_LOADING`;
/**
* @param {number} startBlockNumber
* @param {number} endBlockNumber
* @returns {function(any, any): Promise<void>}
*/
export const requestPage = function (startBlockNumber, endBlockNumber) {
return async function (dispatch, getState) {
if (startBlockNumber == null) {
startBlockNumber = getState().core.latestBlock;
}
if (endBlockNumber == null) {
const state = getState();
endBlockNumber = state.config.settings.workspace.server.fork
? state.config.settings.workspace.server.fork_block_number + 1
: 0;
}
let earliestBlockToRequest = Math.max(
startBlockNumber - PAGE_SIZE,
endBlockNumber,
);
dispatch({
type: SET_BLOCKS_REQUESTED,
start: earliestBlockToRequest,
end: startBlockNumber,
});
dispatch({
type: SET_LOADING,
loading: true,
});
// I was going to use the blocks inView here by just dispatching `requestPage`
// from the blocks redux, but that gets cleared and doesnt work well, so I'm
// doing it the dirty and brute force way
let blockTimestamps = {};
for (let i = earliestBlockToRequest; i <= startBlockNumber; i++) {
const block = await web3ActionCreator(dispatch, getState, "getBlock", [
i,
false,
]);
blockTimestamps[i] = block ? block.timestamp : null;
}
let logs = await web3ActionCreator(dispatch, getState, "getPastLogs", [
{
fromBlock: earliestBlockToRequest,
toBlock: startBlockNumber,
},
]);
const subscribedTopics = getState().events.subscribedTopics;
logs = logs.filter(log => {
for (let i = 0; i < log.topics.length; i++) {
if (subscribedTopics.indexOf(log.topics[i]) >= 0) {
return true;
}
}
return false;
});
const contractCache = getState().workspaces.current.contractCache;
const projects = getState().workspaces.current.projects;
for (let i = 0; i < logs.length; i++) {
const log = logs[i];
const cache = contractCache[log.address];
log.timestamp = blockTimestamps[log.blockNumber];
if (cache && cache.contract) {
const contract = cache.contract;
const projectContracts = projects[contract.projectIndex].contracts;
const decodedLog = await new Promise(resolve => {
// TODO: there's a better way to do this to not have to send `contract` and `contracts` every time
ipcRenderer.once(GET_DECODED_EVENT, (event, decodedLog) => {
resolve(decodedLog);
});
ipcRenderer.send(GET_DECODED_EVENT, contract, projectContracts, log);
});
if (decodedLog) {
log.name = decodedLog.name;
log.contract = contract.contractName;
}
}
}
dispatch({ type: ADD_EVENTS_TO_VIEW, events: logs });
dispatch({
type: SET_LOADING,
loading: false,
});
};
};
// The "next" page is the next set of blocks, from the last requested down to 0
export const requestNextPage = function () {
return function (dispatch, getState) {
var blocksRequested = Object.keys(getState().events.blocksRequested);
var earliestBlockRequested = Math.min.apply(Math, blocksRequested);
dispatch(requestPage(earliestBlockRequested - 1));
};
};
export const requestPreviousPage = function () {
return function (dispatch, getState) {
var blocksRequested = Object.keys(getState().events.blocksRequested);
if (blocksRequested.length == 0) {
return dispatch(requestPage(getState().core.latestBlock));
}
var latestBlockRequested = Math.max.apply(Math, blocksRequested);
var latestBlock = getState().core.latestBlock;
var startBlock = Math.min(latestBlock, latestBlockRequested + PAGE_SIZE);
var endBlock = latestBlockRequested + 1;
dispatch(requestPage(startBlock, endBlock));
};
};
export const GET_DECODED_EVENT = `${prefix}/GET_DECODED_EVENT`;
export const getDecodedEvent = function (transactionHash, logIndex) {
return async function (dispatch, getState) {
let event;
const transaction = await web3ActionCreator(
dispatch,
getState,
"getTransaction",
[transactionHash],
);
const receipt = await web3ActionCreator(
dispatch,
getState,
"getTransactionReceipt",
[transactionHash],
);
const block = await web3ActionCreator(dispatch, getState, "getBlock", [
transaction.blockNumber,
false,
]);
let contract;
const state = getState();
// TODO: This is shared code in redux/workspaces/actions.js
for (let j = 0; j < receipt.logs.length; j++) {
if (receipt.logs[j].logIndex === logIndex) {
event = {
transactionHash,
logIndex,
log: {
...receipt.logs[j],
timestamp: block.timestamp,
},
decodedLog: null,
};
break;
}
}
let isSubscribedTopic = false;
for (let k = 0; k < event.log.topics.length; k++) {
if (state.events.subscribedTopics.indexOf(event.log.topics[k]) >= 0) {
isSubscribedTopic = true;
break;
}
}
const contractCache =
state.workspaces.current.contractCache[event.log.address];
if (contractCache) {
contract = contractCache.contract;
}
if (contract && isSubscribedTopic) {
event.decodedLog = await new Promise(resolve => {
// TODO: there's a better way to do this to not have to send `contract` and `contracts` every time
ipcRenderer.once(GET_DECODED_EVENT, (event, decodedLog) => {
resolve(decodedLog);
});
ipcRenderer.send(
GET_DECODED_EVENT,
contract,
state.workspaces.current.projects[contract.projectIndex].contracts,
event.log,
);
});
}
dispatch({
type: GET_DECODED_EVENT,
event,
contractName: contract ? contract.contractName : null,
contractAddress: event.log.address,
});
};
};