This repository has been archived by the owner on Sep 20, 2023. It is now read-only.
/
eventCache.js
251 lines (224 loc) · 6.94 KB
/
eventCache.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import { get } from '@origin/ipfs'
// import { post } from '@origin/ipfs'
import uniqBy from 'lodash/uniqBy'
import chunk from 'lodash/chunk'
import flattenDeep from 'lodash/flattenDeep'
import createDebug from 'debug'
import LZString from 'lz-string'
const debug = createDebug('event-cache:')
// Given a contract and block range, break the range up into requests of no
// more than 20,000 blocks, then send those requests in batches of 7.
const pastEventBatcher = async (contract, fromBlock, uptoBlock) => {
// const start = +new Date() // Uncomment for benchmarking
if (fromBlock > uptoBlock) throw new Error('fromBlock > toBlock')
const partitions = []
while (fromBlock <= uptoBlock) {
const toBlock = Math.min(fromBlock + 3000, uptoBlock)
partitions.push(contract.getPastEvents('allEvents', { fromBlock, toBlock }))
fromBlock += 3000
}
const results = []
const chunks = chunk(partitions, 7)
for (const chunklet of chunks) {
results.push(await Promise.all(chunklet))
}
// debug('Got events in ', +new Date() - start) // Uncomment for benchmarking
return flattenDeep(results)
}
export default function eventCache(contract, fromBlock = 0, web3, config) {
function padNum(num) {
return web3.utils.padLeft(web3.utils.numberToHex(num), 64)
}
const queue = []
let events = [],
toBlock = 0,
lastLookup = 0,
processing = false,
triedIpfs = false
function updateBlock(block) {
debug(`Update block ${block}`)
toBlock = block
}
function getBlockNumber() {
return toBlock
}
if (!contract.options.address) {
return { updateBlock }
}
const cacheStr = `eventCache${contract.options.address.slice(2, 8)}`
try {
if (window.localStorage[cacheStr]) {
let str = window.localStorage[cacheStr]
if (str[0] !== '{') {
str = LZString.decompress(str)
}
const parsed = JSON.parse(str)
events = parsed.events
lastLookup = fromBlock = parsed.lastLookup
triedIpfs = true
}
} catch (e) {
/* Ignore */
}
const isDone = () => new Promise(resolve => queue.push(resolve))
async function getPastEvents() {
if (processing) {
await isDone()
}
processing = true
if (!triedIpfs && config.ipfsEventCache) {
debug('Try IPFS cache...')
let ipfsData
try {
ipfsData = await get(config.ipfsGateway, config.ipfsEventCache)
if (ipfsData.compressed) {
const decompressed = LZString.decompress(ipfsData.compressed)
ipfsData = JSON.parse(decompressed)
}
} catch (e) {
/* Ignore */
}
if (ipfsData && ipfsData.events) {
debug('Got IPFS cache')
// console.log(ipfsData)
events = ipfsData.events
lastLookup = ipfsData.lastLookup
fromBlock = ipfsData.lastLookup
// ({ events, lastLookup } = ipfsData)
} else {
debug('Error getting IPFS cache')
}
triedIpfs = true
}
if (!toBlock) {
toBlock = await web3.eth.getBlockNumber()
}
if (lastLookup && lastLookup === toBlock) {
processing = false
return
}
if (lastLookup === fromBlock) {
fromBlock += 1
}
debug(
`Fetching events from ${fromBlock} to ${toBlock}, last lookup ${lastLookup}`
)
lastLookup = toBlock
const newEvents = await pastEventBatcher(contract, fromBlock, toBlock)
events = uniqBy(
[
...events,
...newEvents.map(e => ({ ...e, block: { id: e.blockNumber } }))
],
e => e.id
)
debug(`Found ${events.length} events, ${newEvents.length} new`)
fromBlock = toBlock + 1
processing = false
while (queue.length) {
queue.pop()()
}
if (typeof window !== 'undefined') {
const compressed = LZString.compress(
JSON.stringify({
lastLookup,
events
})
)
window.localStorage[cacheStr] = compressed
// const hash = await post(config.ipfsRPC, { compressed }, true)
// console.log('IPFS Hash', hash)
}
}
// offerIds an optional array in the format ['0-1', '1-2'] (listingId-offerId)
async function allEvents(eventNames, party, offerIds) {
await getPastEvents()
if (!Array.isArray(eventNames)) {
eventNames = eventNames ? [eventNames] : []
}
return events.filter(e => {
const topics = e.raw.topics
let matches = true
if (eventNames && eventNames.length && eventNames.indexOf(e.event) < 0)
matches = false
if (offerIds && offerIds.length) {
if (
!offerIds.find(id => {
const [listingId, offerId] = id.split('-')
const listingTopic = padNum(Number(listingId)),
offerTopic = padNum(Number(offerId))
return topics[2] === listingTopic && topics[3] === offerTopic
})
) {
matches = false
}
}
if (party) {
if (
topics[1].toLowerCase() !==
web3.utils.padLeft(party, 64).toLowerCase()
)
matches = false
}
return matches
})
}
async function listings(listingId, eventName, blockNumber) {
await getPastEvents()
const listingTopic = web3.utils.padLeft(
web3.utils.numberToHex(listingId),
64
)
return events.filter(e => {
const topics = e.raw.topics
let matches = topics[2] === listingTopic
if (eventName && e.event !== eventName) matches = false
if (blockNumber && e.blockNumber > blockNumber) matches = false
return matches
})
}
async function offers(listingIds, offerId, eventNames, notParty, isParty) {
await getPastEvents()
const matchListings = typeof listingIds !== 'undefined'
if (!Array.isArray(listingIds)) {
listingIds = matchListings ? [listingIds] : []
}
if (!Array.isArray(eventNames)) {
eventNames = eventNames ? [eventNames] : []
}
const listingTopics = listingIds.map(listingId =>
web3.utils.padLeft(web3.utils.numberToHex(listingId), 64)
)
const offerTopic =
typeof offerId === 'number'
? web3.utils.padLeft(web3.utils.numberToHex(offerId), 64)
: null
return events.filter(e => {
const topics = e.raw.topics
let matchesParty = true
if (notParty) {
if (
topics[1].toLowerCase() ===
web3.utils.padLeft(notParty, 64).toLowerCase()
)
matchesParty = false
}
if (isParty) {
if (
topics[1].toLowerCase() !==
web3.utils.padLeft(isParty, 64).toLowerCase()
)
matchesParty = false
}
const matchesListing = matchListings
? listingTopics.indexOf(topics[2]) >= 0
: true,
matchesOffer = offerTopic ? topics[3] === offerTopic : true,
matchesEvent = eventNames.length
? eventNames.indexOf(e.event) >= 0
: true
return matchesListing && matchesOffer && matchesEvent && matchesParty
})
}
return { listings, offers, allEvents, updateBlock, getBlockNumber }
}