Skip to content

Commit f025b10

Browse files
authored
batched-parallel processing (#50)
1 parent 53a15e8 commit f025b10

File tree

6 files changed

+622
-714
lines changed

6 files changed

+622
-714
lines changed

services/decoder/decoder.ts

+50-48
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
type QueryOptions,
1818
} from "./decoder.types";
1919
import { encodeEventTopics, type Abi } from "viem";
20+
import { chunkify } from "../../utils/functions";
2021

2122
export class GoldRushDecoder {
2223
private static configs: DecoderConfig = {};
@@ -28,7 +29,7 @@ export class GoldRushDecoder {
2829
private static fileExtension: "js" | "ts" =
2930
process.env.NODE_ENV !== "test" ? "js" : "ts";
3031

31-
public static initDecoder = () => {
32+
public static initDecoder = (): void => {
3233
console.info("Initializing GoldrushDecoder Service...");
3334

3435
const protocolsDirectoryPath: string = join(__dirname, "/protocols");
@@ -104,7 +105,7 @@ export class GoldRushDecoder {
104105
chain_names: Chain[],
105106
abi: Abi,
106107
decoding_function: DecodingFunction
107-
) => {
108+
): void => {
108109
const [protocol, event_name] = event_id.split(":");
109110
const [topic0_hash] = encodeEventTopics({
110111
abi: abi,
@@ -143,7 +144,7 @@ export class GoldRushDecoder {
143144
event_name: string,
144145
abi: Abi,
145146
decoding_function: DecodingFunction
146-
) => {
147+
): void => {
147148
const [topic0_hash] = encodeEventTopics({
148149
abi: abi,
149150
eventName: event_name,
@@ -155,7 +156,7 @@ export class GoldRushDecoder {
155156
this.fallbacks[lowercaseTopic0Hash] = fallback_function_index;
156157
};
157158

158-
public static native = (native_decoder: NativeDecodingFunction) => {
159+
public static native = (native_decoder: NativeDecodingFunction): void => {
159160
this.native_decoder = native_decoder;
160161
};
161162

@@ -164,55 +165,56 @@ export class GoldRushDecoder {
164165
tx: Transaction,
165166
covalent_api_key: string,
166167
options: QueryOptions
167-
) => {
168+
): Promise<EventType[]> => {
168169
const covalent_client = new CovalentClient(covalent_api_key);
169-
const events: (EventType | null)[] = [];
170+
let events: (EventType | null)[] = [];
170171
if (tx.value) {
171172
const nativeEvent = this.native_decoder(tx, options);
172173
events.push(nativeEvent);
173174
}
174-
175-
const decodedEvents = await Promise.all(
176-
(tx.log_events ?? []).map((log_event) => {
177-
const {
178-
raw_log_topics: [topic0_hash],
179-
sender_address,
180-
sender_factory_address,
181-
} = log_event;
182-
const lowercaseChainName = chain_name.toLowerCase() as Chain;
183-
const lowercaseSenderAddress = sender_address?.toLowerCase();
184-
const lowercaseSenderFactoryAddress =
185-
sender_factory_address?.toLowerCase();
186-
const lowercaseTopic0Hash = topic0_hash?.toLowerCase();
187-
188-
const decoding_index =
189-
this.decoders[lowercaseChainName]?.[
190-
lowercaseSenderAddress
191-
]?.[lowercaseTopic0Hash] ??
192-
this.decoders[lowercaseChainName]?.[
193-
lowercaseSenderFactoryAddress
194-
]?.[lowercaseTopic0Hash];
195-
const fallback_index = this.fallbacks[lowercaseTopic0Hash];
196-
197-
const logFunction =
198-
decoding_index !== undefined
199-
? this.decoding_functions[decoding_index]
200-
: fallback_index !== undefined
201-
? this.fallback_functions[fallback_index]
175+
const logChunks = chunkify(tx.log_events ?? [], 100);
176+
for (const logChunk of logChunks) {
177+
const decodedChunk = await Promise.all(
178+
logChunk.map((log_event) => {
179+
const {
180+
raw_log_topics: [topic0_hash],
181+
sender_address,
182+
sender_factory_address,
183+
} = log_event;
184+
const lowercaseChainName =
185+
chain_name.toLowerCase() as Chain;
186+
const lowercaseSenderAddress =
187+
sender_address?.toLowerCase();
188+
const lowercaseSenderFactoryAddress =
189+
sender_factory_address?.toLowerCase();
190+
const lowercaseTopic0Hash = topic0_hash?.toLowerCase();
191+
const decoding_index =
192+
this.decoders[lowercaseChainName]?.[
193+
lowercaseSenderAddress
194+
]?.[lowercaseTopic0Hash] ??
195+
this.decoders[lowercaseChainName]?.[
196+
lowercaseSenderFactoryAddress
197+
]?.[lowercaseTopic0Hash];
198+
const fallback_index = this.fallbacks[lowercaseTopic0Hash];
199+
const logFunction =
200+
(decoding_index !== undefined &&
201+
this.decoding_functions[decoding_index]) ||
202+
(fallback_index !== undefined &&
203+
this.fallback_functions[fallback_index]) ||
204+
null;
205+
return logFunction
206+
? logFunction(
207+
log_event,
208+
tx,
209+
chain_name,
210+
covalent_client,
211+
options
212+
)
202213
: null;
203-
204-
return logFunction
205-
? logFunction(
206-
log_event,
207-
tx,
208-
chain_name,
209-
covalent_client,
210-
options
211-
)
212-
: null;
213-
})
214-
);
215-
216-
return events.concat(decodedEvents).filter(Boolean) as EventType[];
214+
})
215+
);
216+
events = [...events, ...decodedChunk];
217+
}
218+
return events.filter(Boolean) as EventType[];
217219
};
218220
}

utils/functions/chunkify.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const chunkify = <T>(arr: Array<T>, size: number) => {
2+
const chunks: Array<Array<T>> = [];
3+
for (let i = 0; i < arr.length; i += size) {
4+
chunks.push(arr.slice(i, i + size));
5+
}
6+
return chunks;
7+
};

utils/functions/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export { chunkify } from "./chunkify";
12
export { currencyToNumber } from "./currency-to-number";
23
export { slugify } from "./slugify";
34
export { timestampParser } from "./timestamp-parser";

utils/functions/slugify.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
export const slugify = (input: string): string =>
2-
input
1+
export const slugify = (input: string): string => {
2+
return input
33
.replace(/\s+/g, "-")
44
.replace(/([a-z])([A-Z])/g, "$1-$2")
55
.toLowerCase();
6+
};

utils/functions/timestamp-parser.ts

+9-12
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ export const timestampParser = (
1717
timestamp: Date,
1818
type: "descriptive" | "YYYY-MM-DD" | "relative"
1919
): string => {
20+
const _unix: Date = new Date(timestamp);
21+
2022
switch (type) {
2123
case "descriptive": {
22-
const _unix: Date = new Date(timestamp);
23-
2424
const _minutes = _unix.getMinutes();
2525
const _hours = _unix.getHours();
2626
const _seconds = _unix.getSeconds();
@@ -49,17 +49,14 @@ export const timestampParser = (
4949

5050
case "relative": {
5151
const currentTime = new Date();
52-
const pastTime = new Date(timestamp);
52+
const timeDifference = currentTime.getTime() - _unix.getTime();
5353

54-
const yearsDifference =
55-
currentTime.getFullYear() - pastTime.getFullYear();
56-
const monthsDifference =
57-
currentTime.getMonth() - pastTime.getMonth();
58-
const daysDifference = currentTime.getDate() - pastTime.getDate();
59-
const hoursDifference =
60-
currentTime.getHours() - pastTime.getHours();
61-
const minutesDifference =
62-
currentTime.getMinutes() - pastTime.getMinutes();
54+
const secondsDifference = Math.floor(timeDifference / 1000);
55+
const minutesDifference = Math.floor(secondsDifference / 60);
56+
const hoursDifference = Math.floor(minutesDifference / 60);
57+
const daysDifference = Math.floor(hoursDifference / 24);
58+
const monthsDifference = Math.floor(daysDifference / 30);
59+
const yearsDifference = Math.floor(monthsDifference / 12);
6360

6461
if (yearsDifference > 0) {
6562
return `${yearsDifference} year${

0 commit comments

Comments
 (0)