Skip to content

Commit 9820be7

Browse files
committed
simplify logic a bit
1 parent 9b00006 commit 9820be7

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

src/worker/tasks/processEventLogsWorker.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,16 @@ const handler: Processor<any, void, string> = async (job: Job<string>) => {
4343
return;
4444
}
4545

46-
// Get webhooks.
47-
const webhooksByContractAddress = await getWebhooksByContractAddresses(
48-
chainId,
49-
);
50-
5146
// Store logs to DB.
5247
const insertedLogs = await bulkInsertContractEventLogs({ logs });
5348
if (insertedLogs.length === 0) {
5449
return;
5550
}
5651

5752
// Enqueue webhooks.
58-
// This step should happen immediately after inserting to DB.
53+
const webhooksByContractAddress = await getWebhooksByContractAddresses(
54+
chainId,
55+
);
5956
for (const eventLog of insertedLogs) {
6057
const webhooks = webhooksByContractAddress[eventLog.contractAddress] ?? [];
6158
for (const webhook of webhooks) {
@@ -119,6 +116,7 @@ const getLogs = async ({
119116
}
120117

121118
const chain = defineChain(chainId);
119+
// Store a reference to `contract` so ABI fetches are cached.
122120
const addressConfig: Record<
123121
Address,
124122
{
@@ -140,6 +138,7 @@ const getLogs = async ({
140138
const { contract } = addressConfig[f.address];
141139

142140
// Get events to filter by, if any.
141+
// Resolve the event name, "Transfer", to event signature, "Transfer(address to, uint256 quantity)".
143142
const events: PreparedEvent<AbiEvent>[] = [];
144143
if (f.events.length > 0) {
145144
const abi = await resolveContractAbi<AbiEvent[]>(contract);
@@ -154,7 +153,7 @@ const getLogs = async ({
154153
contract,
155154
fromBlock: BigInt(fromBlock),
156155
toBlock: BigInt(toBlock),
157-
events,
156+
events, // [] means return all events
158157
});
159158
});
160159

@@ -211,7 +210,7 @@ const getLogs = async ({
211210
* "quantity": {
212211
* "type:" "uint256",
213212
* "value": "2"
214-
* },
213+
* }
215214
* }
216215
*/
217216
const formatDecodedLog = async (args: {

src/worker/tasks/processTransactionReceiptsWorker.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,18 @@ const handler: Processor<any, void, string> = async (job: Job<string>) => {
4141
return;
4242
}
4343

44-
// Get webhooks.
45-
const webhooksByContractAddress = await getWebhooksByContractAddresses(
46-
chainId,
47-
);
48-
4944
// Store logs to DB.
5045
const insertedReceipts = await bulkInsertContractTransactionReceipts({
5146
receipts,
5247
});
53-
5448
if (insertedReceipts.length === 0) {
5549
return;
5650
}
5751

5852
// Enqueue webhooks.
59-
// This step should happen immediately after inserting to DB.
53+
const webhooksByContractAddress = await getWebhooksByContractAddresses(
54+
chainId,
55+
);
6056
for (const transactionReceipt of insertedReceipts) {
6157
const webhooks =
6258
webhooksByContractAddress[transactionReceipt.contractAddress] ?? [];
@@ -115,13 +111,18 @@ const getFormattedTransactionReceipts = async ({
115111
const addressConfig: Record<
116112
Address,
117113
{
118-
functions: Set<string>;
114+
functions: string[];
119115
contract: ThirdwebContract;
120116
}
121117
> = {};
122118
for (const filter of filters) {
119+
// Merge multiple filters for the same address.
120+
if (filter.address in addressConfig) {
121+
addressConfig[filter.address].functions.push(...filter.functions);
122+
}
123+
123124
addressConfig[filter.address] = {
124-
functions: new Set(filter.functions),
125+
functions: filter.functions,
125126
contract: getContract({
126127
client: thirdwebClient,
127128
chain,
@@ -152,13 +153,13 @@ const getFormattedTransactionReceipts = async ({
152153
}
153154

154155
let functionName: string | undefined = undefined;
155-
if (config.functions.size > 0) {
156+
if (config.functions.length > 0) {
156157
functionName = await getFunctionName({
157158
contract: config.contract,
158159
data: transaction.input,
159160
});
160161

161-
if (!config.functions.has(functionName)) {
162+
if (!config.functions.includes(functionName)) {
162163
// This transaction is not for a subscribed function name.
163164
continue;
164165
}

0 commit comments

Comments
 (0)