event-log-harvester
Advanced tools
Comparing version 0.1.2 to 0.2.0
import { BaseLog, Config, EventFiltersRecord, LogDecodedRecord } from './types'; | ||
export declare function fetchAndDecodeLogs<T extends EventFiltersRecord, D extends BaseLog>(getLogs: Config<T, D>["getLogs"], eventFiltersRecord: T, fromBlock: number, toBlock: number, failOnDecodeError?: boolean): Promise<LogDecodedRecord<T>>; | ||
export declare function fetchAndDecodeLogs<T extends EventFiltersRecord, D extends BaseLog>(config: Pick<Config<T, D>, "eventFilters" | "getLogs" | "failOnDecodeError">, fromBlock: number, toBlock: number): Promise<LogDecodedRecord<T>>; |
@@ -13,2 +13,2 @@ import { BaseLog, Config, EventFiltersRecord } from './types'; | ||
*/ | ||
export declare function fetchAndDecodeLogsWithRetries<T extends EventFiltersRecord, D extends BaseLog>(getLogs: Config<T, D>["getLogs"], eventFiltersRecord: T, fromBlock: number, toBlock: number, maxRetries: number, failOnDecodeError?: boolean): Promise<import('./types').LogDecodedRecord<T>>; | ||
export declare function fetchAndDecodeLogsWithRetries<T extends EventFiltersRecord, D extends BaseLog>(config: Pick<Config<T, D>, "eventFilters" | "getLogs" | "failOnDecodeError" | "maxRetries">, startBlock: number, endBlock: number): Promise<import('./types').LogDecodedRecord<T>>; |
@@ -1,120 +0,115 @@ | ||
async function F(i, r, t, s, c) { | ||
const d = {}; | ||
let e = /* @__PURE__ */ new Set(); | ||
const o = /* @__PURE__ */ new Set(); | ||
let k = !1; | ||
for (const f in r) | ||
d[f] = [], r[f].forEach((h) => { | ||
!h.addresses || h.addresses.length === 0 ? k = !0 : h.addresses.forEach((n) => e.add(n)), h.topics.forEach((n) => o.add(n)); | ||
async function F(l, c, o) { | ||
const { | ||
eventFilters: e, | ||
getLogs: a, | ||
failOnDecodeError: f = !1 | ||
} = l, t = {}; | ||
let r = /* @__PURE__ */ new Set(); | ||
const h = /* @__PURE__ */ new Set(); | ||
let s = !1; | ||
for (const n in e) | ||
t[n] = [], e[n].forEach((k) => { | ||
!k.addresses || k.addresses.length === 0 ? s = !0 : k.addresses.forEach((p) => r.add(p)), k.topics.forEach((p) => h.add(p)); | ||
}); | ||
k && (e = void 0); | ||
const a = (await i({ | ||
fromBlock: t, | ||
toBlock: s, | ||
addresses: e ? Array.from(e) : void 0, | ||
topics: o.size > 0 ? Array.from(o) : void 0 | ||
})).map(async (f) => { | ||
for (const m in r) | ||
await E( | ||
r, | ||
m, | ||
f, | ||
d, | ||
c | ||
s && (r = void 0); | ||
const w = (await a({ | ||
fromBlock: c, | ||
toBlock: o, | ||
addresses: r ? Array.from(r) : void 0, | ||
topics: h.size > 0 ? Array.from(h) : void 0 | ||
})).map(async (n) => { | ||
for (const i in e) | ||
await g( | ||
e, | ||
i, | ||
n, | ||
t, | ||
f | ||
); | ||
}); | ||
return await Promise.all(a), d; | ||
return await Promise.all(w), t; | ||
} | ||
async function E(i, r, t, s, c) { | ||
const e = i[r].find( | ||
(o) => (!o.addresses || o.addresses.length === 0 || o.addresses.includes(t.address)) && o.topics[0] === t.topics[0] && t.blockNumber >= o.fromBlock && t.blockNumber <= o.toBlock | ||
async function g(l, c, o, e, a) { | ||
const t = l[c].find( | ||
(r) => (!r.addresses || r.addresses.length === 0 || r.addresses.includes(o.address)) && r.topics[0] === o.topics[0] && o.blockNumber >= r.fromBlock && o.blockNumber <= r.toBlock | ||
); | ||
if (e) | ||
if (t) | ||
try { | ||
t.data = t.data === "0x" ? "0x0000000000000000000000000000000000000000000000000000000000000000" : t.data; | ||
const o = await e.decodeLog(t); | ||
s[r] || (s[r] = []), s[r].push(o); | ||
} catch (o) { | ||
o.data = o.data === "0x" ? "0x0000000000000000000000000000000000000000000000000000000000000000" : o.data; | ||
const r = await t.decodeLog(o); | ||
e[c] || (e[c] = []), e[c].push(r); | ||
} catch (r) { | ||
if (console.error( | ||
`Error decoding log for key ${r.toString()}:`, | ||
`Error decoding log for key ${c.toString()}:`, | ||
r, | ||
o, | ||
t, | ||
e | ||
), c) | ||
t | ||
), a) | ||
throw new Error("Decoding error occurred"); | ||
} | ||
} | ||
async function A(i, r, t, s, c, d) { | ||
let e = 0; | ||
for (; c === -1 || e < c; ) | ||
async function E(l, c, o) { | ||
const { maxRetries: e = 0 } = l; | ||
let a = 0; | ||
for (; e === -1 || a < e; ) | ||
try { | ||
return await F( | ||
i, | ||
r, | ||
t, | ||
s, | ||
d | ||
); | ||
} catch (o) { | ||
if (e += 1, console.error(`Attempt ${e} failed: ${o.message}`), c !== -1 && e >= c) | ||
throw new Error(`Failed to fetch logs after ${c} retries`); | ||
await new Promise((k) => setTimeout(k, 1e3 * e)); | ||
return await F(l, c, o); | ||
} catch (f) { | ||
if (a += 1, console.error(`Attempt ${a} failed: ${f.message}`), e !== -1 && a >= e) | ||
throw new Error(`Failed to fetch logs after ${e} retries`); | ||
await new Promise((t) => setTimeout(t, 1e3 * a)); | ||
} | ||
throw new Error("Failed to fetch logs"); | ||
} | ||
function L(i, r, t) { | ||
const s = {}; | ||
for (const c in i) { | ||
const e = i[c].filter( | ||
(o) => o.fromBlock <= t && o.toBlock >= r | ||
function A(l, c, o) { | ||
const e = {}; | ||
for (const a in l) { | ||
const t = l[a].filter( | ||
(r) => r.fromBlock <= o && r.toBlock >= c | ||
); | ||
e.length > 0 && (s[c] = e); | ||
t.length > 0 && (e[a] = t); | ||
} | ||
return s; | ||
return e; | ||
} | ||
async function M(i) { | ||
async function $(l) { | ||
const { | ||
batchSize: r = 1e3, | ||
maxRetries: t = 0, | ||
eventFilters: s, | ||
getLogs: c, | ||
failOnDecodeError: d = !1, | ||
failOnCallbackError: e = !1 | ||
} = i, o = {}, k = Math.min( | ||
...Object.values(s).flat().map((a) => a.fromBlock) | ||
), b = Math.max( | ||
...Object.values(s).flat().map((a) => a.toBlock) | ||
batchSize: c = 1e3, | ||
eventFilters: o, | ||
failOnCallbackError: e = !1, | ||
onBatchComplete: a, | ||
progressInfo: f | ||
} = l, t = {}, r = Math.min( | ||
...Object.values(o).flat().map((s) => s.fromBlock) | ||
), h = Math.max( | ||
...Object.values(o).flat().map((s) => s.toBlock) | ||
); | ||
for (let a = k; a <= b; a += r) { | ||
const f = Math.min(a + r - 1, b), m = L( | ||
for (let s = r; s <= h; s += c) { | ||
const m = Math.min(s + c - 1, h), w = A( | ||
o, | ||
s, | ||
a, | ||
f | ||
m | ||
); | ||
if (Object.keys(m).length > 0) { | ||
const h = await A( | ||
c, | ||
m, | ||
a, | ||
f, | ||
t, | ||
d | ||
if (Object.keys(w).length > 0) { | ||
const n = await E( | ||
l, | ||
s, | ||
m | ||
); | ||
for (const n in h) { | ||
o[n] || (o[n] = []); | ||
const w = h[n]; | ||
o[n].push(...w); | ||
const p = m[n].map((l) => { | ||
if (l.callback) { | ||
const y = w.filter((u) => { | ||
const g = !l.addresses || l.addresses.includes(u.address), v = l.topics[0] === u.topics[0], B = u.blockNumber >= l.fromBlock && u.blockNumber <= l.toBlock; | ||
return g && v && B; | ||
for (const i in n) { | ||
t[i] || (t[i] = []); | ||
const k = n[i]; | ||
t[i].push(...k); | ||
const p = w[i].map((d) => { | ||
if (d.onBatchComplete) { | ||
const b = k.filter((u) => { | ||
const B = !d.addresses || d.addresses.includes(u.address), v = d.topics[0] === u.topics[0], y = u.blockNumber >= d.fromBlock && u.blockNumber <= d.toBlock; | ||
return B && v && y; | ||
}); | ||
try { | ||
return l.callback(y, { | ||
startBlock: a, | ||
endBlock: f | ||
return d.onBatchComplete(b, { | ||
startBlock: s, | ||
endBlock: m | ||
}); | ||
} catch (u) { | ||
if (console.error(`Error in callback for filter ${n}:`, u), e) | ||
if (console.error(`Error in callback for filter ${i}:`, u), e) | ||
throw u; | ||
@@ -126,13 +121,38 @@ } | ||
await Promise.all(p); | ||
} catch (l) { | ||
if (console.error(`Error in callback for filter ${n}:`, l), e) | ||
throw l; | ||
} catch (d) { | ||
if (console.error(`Error in callback for filter ${i}:`, d), e) | ||
throw d; | ||
} | ||
} | ||
if (a) | ||
try { | ||
await a(n, { startBlock: s, endBlock: m }); | ||
} catch (i) { | ||
if (console.error( | ||
`Error in callback onBatchComplete from ${s} to ${m} }:`, | ||
i | ||
), e) | ||
throw i; | ||
} | ||
} | ||
if (f) | ||
try { | ||
const n = { | ||
fromBlock: r, | ||
toBlock: h, | ||
batchSize: c, | ||
batchStartBlock: s, | ||
batchEndBlock: m, | ||
progressPercent: m / h * 100 | ||
}; | ||
f(n); | ||
} catch (n) { | ||
if (console.error("Error in callback progressInfo", n), e) | ||
throw n; | ||
} | ||
} | ||
return o; | ||
return t; | ||
} | ||
export { | ||
M as harvestLogs | ||
$ as harvestLogs | ||
}; |
@@ -20,2 +20,14 @@ export type BaseLog = { | ||
}) => Promise<Array<TLog>>; | ||
onBatchComplete?: (results: LogDecodedRecord<T>, metadata: { | ||
startBlock: number; | ||
endBlock: number; | ||
}) => Promise<void> | void; | ||
progressInfo?: (metadata: { | ||
fromBlock: number; | ||
toBlock: number; | ||
batchSize: number; | ||
batchStartBlock: number; | ||
batchEndBlock: number; | ||
progressPercent: number; | ||
}) => Promise<void> | void; | ||
} | ||
@@ -28,3 +40,3 @@ export interface EventFilter<TValue extends Record<string, unknown>> { | ||
decodeLog: (log: BaseLog) => Promise<TValue & BaseLog> | (TValue & BaseLog); | ||
callback?: (logs: TValue[], metadata: { | ||
onBatchComplete?: (logs: TValue[], metadata: { | ||
startBlock: number; | ||
@@ -31,0 +43,0 @@ endBlock: number; |
{ | ||
"name": "event-log-harvester", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
15286
252
1