|
1 | 1 | import { processPayloadKey } from "./payload"; |
2 | | -import { getDecoded, getRequiredEnvString } from "../../utils/helpers"; |
| 2 | +import { delay, getDecoded, getRequiredEnvString } from "../../utils/helpers"; |
3 | 3 | import EventSource from "eventsource"; |
4 | | -import { DispatchInfo } from "../../jobs/publisher-job"; |
| 4 | +import { dispatch, DispatchInfo } from "../../jobs/publisher-job"; |
5 | 5 | import { uint64ToInt64 } from "../../utils/int-uint-64"; |
6 | 6 | import Block, { BlockAttributes } from "../../models/block"; |
7 | 7 | import { sequelize } from "../../config/database"; |
@@ -46,13 +46,23 @@ export async function startStreaming() { |
46 | 46 |
|
47 | 47 | const promises = blocksToProcess.map(async (block: any) => { |
48 | 48 | try { |
49 | | - await saveBlock(block); |
| 49 | + return saveBlock(block); |
50 | 50 | } catch (error) { |
51 | 51 | console.error("Error saving block:", error); |
52 | 52 | } |
53 | 53 | }); |
54 | 54 |
|
55 | | - await Promise.all(promises); |
| 55 | + const res = (await Promise.all(promises)).filter( |
| 56 | + (r) => r !== null, |
| 57 | + ) as DispatchInfo[]; |
| 58 | + |
| 59 | + const dispatches = res.map(async (r, index) => { |
| 60 | + await dispatch(r); |
| 61 | + await delay(500); |
| 62 | + console.log("Dispatched block:", index); |
| 63 | + }); |
| 64 | + |
| 65 | + await Promise.all(dispatches); |
56 | 66 | console.log("Done processing blocks: ", blocksToProcess.length); |
57 | 67 | }, 1000 * 10); |
58 | 68 |
|
@@ -122,42 +132,27 @@ async function saveBlock(parsedData: any): Promise<DispatchInfo | null> { |
122 | 132 | transaction: tx, |
123 | 133 | }); |
124 | 134 |
|
125 | | - await processPayloadKey(createdBlock, payloadData, tx); |
126 | | - |
127 | | - const txs: Array<{ |
128 | | - requestKey: string; |
129 | | - qualifiedEventNames: string[]; |
130 | | - }> = transactions.map((t: any) => { |
131 | | - const qualifiedEventNames = t[1].events.map((e: any) => { |
132 | | - const module = e.module.namespace |
133 | | - ? `${e.module.namespace}.${e.module.name}` |
134 | | - : e.module.name; |
135 | | - const name = e.name; |
136 | | - return `${module}.${name}`; |
137 | | - }); |
138 | | - return { |
139 | | - requestKey: t[1].reqKey, |
140 | | - qualifiedEventNames, |
141 | | - }; |
142 | | - }); |
| 135 | + const eventsCreated = await processPayloadKey( |
| 136 | + createdBlock, |
| 137 | + payloadData, |
| 138 | + tx, |
| 139 | + ); |
| 140 | + |
| 141 | + const uniqueRequestKeys = new Set( |
| 142 | + eventsCreated.map((t) => t.requestkey).filter(Boolean), |
| 143 | + ); |
143 | 144 |
|
144 | | - const events = transactions.flatMap((t: any) => t.qualifiedEventNames); |
145 | | - const qualifiedEventNamesSet = new Set(); |
146 | | - const uniqueQualifiedEventNames = events.filter( |
147 | | - (qualifiedEventName: any) => { |
148 | | - const isDuplicate = qualifiedEventNamesSet.has(qualifiedEventName); |
149 | | - qualifiedEventNamesSet.add(qualifiedEventName); |
150 | | - return !isDuplicate; |
151 | | - }, |
| 145 | + const uniqueQualifiedEventNames = new Set( |
| 146 | + eventsCreated.map((t) => `${t.module}.${t.name}`).filter(Boolean), |
152 | 147 | ); |
153 | 148 |
|
154 | 149 | await tx.commit(); |
155 | 150 | return { |
156 | 151 | hash: createdBlock.hash, |
157 | 152 | chainId: createdBlock.chainId.toString(), |
158 | 153 | height: createdBlock.height, |
159 | | - requestKeys: txs.map((t) => t.requestKey), |
160 | | - qualifiedEventNames: uniqueQualifiedEventNames, |
| 154 | + requestKeys: Array.from(uniqueRequestKeys), |
| 155 | + qualifiedEventNames: Array.from(uniqueQualifiedEventNames), |
161 | 156 | }; |
162 | 157 | } catch (error) { |
163 | 158 | await tx.rollback(); |
|
0 commit comments