@mikeatlast/ponder 0.16.1-fork.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (619) hide show
  1. package/CHANGELOG.md +3415 -0
  2. package/README.md +186 -0
  3. package/dist/esm/bin/commands/codegen.js +46 -0
  4. package/dist/esm/bin/commands/codegen.js.map +1 -0
  5. package/dist/esm/bin/commands/createViews.js +196 -0
  6. package/dist/esm/bin/commands/createViews.js.map +1 -0
  7. package/dist/esm/bin/commands/dev.js +430 -0
  8. package/dist/esm/bin/commands/dev.js.map +1 -0
  9. package/dist/esm/bin/commands/list.js +148 -0
  10. package/dist/esm/bin/commands/list.js.map +1 -0
  11. package/dist/esm/bin/commands/prune.js +223 -0
  12. package/dist/esm/bin/commands/prune.js.map +1 -0
  13. package/dist/esm/bin/commands/serve.js +198 -0
  14. package/dist/esm/bin/commands/serve.js.map +1 -0
  15. package/dist/esm/bin/commands/start.js +253 -0
  16. package/dist/esm/bin/commands/start.js.map +1 -0
  17. package/dist/esm/bin/isolatedController.js +200 -0
  18. package/dist/esm/bin/isolatedController.js.map +1 -0
  19. package/dist/esm/bin/isolatedWorker.js +146 -0
  20. package/dist/esm/bin/isolatedWorker.js.map +1 -0
  21. package/dist/esm/bin/ponder.js +137 -0
  22. package/dist/esm/bin/ponder.js.map +1 -0
  23. package/dist/esm/bin/utils/codegen.js +25 -0
  24. package/dist/esm/bin/utils/codegen.js.map +1 -0
  25. package/dist/esm/bin/utils/exit.js +100 -0
  26. package/dist/esm/bin/utils/exit.js.map +1 -0
  27. package/dist/esm/build/config.js +745 -0
  28. package/dist/esm/build/config.js.map +1 -0
  29. package/dist/esm/build/factory.js +82 -0
  30. package/dist/esm/build/factory.js.map +1 -0
  31. package/dist/esm/build/index.js +567 -0
  32. package/dist/esm/build/index.js.map +1 -0
  33. package/dist/esm/build/plugin.js +53 -0
  34. package/dist/esm/build/plugin.js.map +1 -0
  35. package/dist/esm/build/pre.js +83 -0
  36. package/dist/esm/build/pre.js.map +1 -0
  37. package/dist/esm/build/schema.js +202 -0
  38. package/dist/esm/build/schema.js.map +1 -0
  39. package/dist/esm/build/stacktrace.js +137 -0
  40. package/dist/esm/build/stacktrace.js.map +1 -0
  41. package/dist/esm/client/index.js +441 -0
  42. package/dist/esm/client/index.js.map +1 -0
  43. package/dist/esm/config/address.js +2 -0
  44. package/dist/esm/config/address.js.map +1 -0
  45. package/dist/esm/config/eventFilter.js +2 -0
  46. package/dist/esm/config/eventFilter.js.map +1 -0
  47. package/dist/esm/config/index.js +2 -0
  48. package/dist/esm/config/index.js.map +1 -0
  49. package/dist/esm/config/utilityTypes.js +2 -0
  50. package/dist/esm/config/utilityTypes.js.map +1 -0
  51. package/dist/esm/database/actions.js +445 -0
  52. package/dist/esm/database/actions.js.map +1 -0
  53. package/dist/esm/database/index.js +604 -0
  54. package/dist/esm/database/index.js.map +1 -0
  55. package/dist/esm/database/queryBuilder.js +314 -0
  56. package/dist/esm/database/queryBuilder.js.map +1 -0
  57. package/dist/esm/drizzle/bigint.js +38 -0
  58. package/dist/esm/drizzle/bigint.js.map +1 -0
  59. package/dist/esm/drizzle/bytes.js +47 -0
  60. package/dist/esm/drizzle/bytes.js.map +1 -0
  61. package/dist/esm/drizzle/hex.js +40 -0
  62. package/dist/esm/drizzle/hex.js.map +1 -0
  63. package/dist/esm/drizzle/index.js +28 -0
  64. package/dist/esm/drizzle/index.js.map +1 -0
  65. package/dist/esm/drizzle/json.js +123 -0
  66. package/dist/esm/drizzle/json.js.map +1 -0
  67. package/dist/esm/drizzle/kit/index.js +927 -0
  68. package/dist/esm/drizzle/kit/index.js.map +1 -0
  69. package/dist/esm/drizzle/onchain.js +184 -0
  70. package/dist/esm/drizzle/onchain.js.map +1 -0
  71. package/dist/esm/drizzle/text.js +61 -0
  72. package/dist/esm/drizzle/text.js.map +1 -0
  73. package/dist/esm/graphql/graphiql.html.js +59 -0
  74. package/dist/esm/graphql/graphiql.html.js.map +1 -0
  75. package/dist/esm/graphql/index.js +934 -0
  76. package/dist/esm/graphql/index.js.map +1 -0
  77. package/dist/esm/graphql/json.js +42 -0
  78. package/dist/esm/graphql/json.js.map +1 -0
  79. package/dist/esm/graphql/middleware.js +83 -0
  80. package/dist/esm/graphql/middleware.js.map +1 -0
  81. package/dist/esm/index.js +9 -0
  82. package/dist/esm/index.js.map +1 -0
  83. package/dist/esm/indexing/addStackTrace.js +54 -0
  84. package/dist/esm/indexing/addStackTrace.js.map +1 -0
  85. package/dist/esm/indexing/client.js +675 -0
  86. package/dist/esm/indexing/client.js.map +1 -0
  87. package/dist/esm/indexing/index.js +663 -0
  88. package/dist/esm/indexing/index.js.map +1 -0
  89. package/dist/esm/indexing/profile.js +584 -0
  90. package/dist/esm/indexing/profile.js.map +1 -0
  91. package/dist/esm/indexing-store/cache.js +666 -0
  92. package/dist/esm/indexing-store/cache.js.map +1 -0
  93. package/dist/esm/indexing-store/index.js +461 -0
  94. package/dist/esm/indexing-store/index.js.map +1 -0
  95. package/dist/esm/indexing-store/profile.js +428 -0
  96. package/dist/esm/indexing-store/profile.js.map +1 -0
  97. package/dist/esm/indexing-store/utils.js +111 -0
  98. package/dist/esm/indexing-store/utils.js.map +1 -0
  99. package/dist/esm/internal/common.js +2 -0
  100. package/dist/esm/internal/common.js.map +1 -0
  101. package/dist/esm/internal/errors.js +300 -0
  102. package/dist/esm/internal/errors.js.map +1 -0
  103. package/dist/esm/internal/logger.js +178 -0
  104. package/dist/esm/internal/logger.js.map +1 -0
  105. package/dist/esm/internal/metrics.js +1049 -0
  106. package/dist/esm/internal/metrics.js.map +1 -0
  107. package/dist/esm/internal/options.js +73 -0
  108. package/dist/esm/internal/options.js.map +1 -0
  109. package/dist/esm/internal/shutdown.js +24 -0
  110. package/dist/esm/internal/shutdown.js.map +1 -0
  111. package/dist/esm/internal/telemetry.js +200 -0
  112. package/dist/esm/internal/telemetry.js.map +1 -0
  113. package/dist/esm/internal/types.js +2 -0
  114. package/dist/esm/internal/types.js.map +1 -0
  115. package/dist/esm/rpc/actions.js +988 -0
  116. package/dist/esm/rpc/actions.js.map +1 -0
  117. package/dist/esm/rpc/http.js +130 -0
  118. package/dist/esm/rpc/http.js.map +1 -0
  119. package/dist/esm/rpc/index.js +749 -0
  120. package/dist/esm/rpc/index.js.map +1 -0
  121. package/dist/esm/runtime/events.js +664 -0
  122. package/dist/esm/runtime/events.js.map +1 -0
  123. package/dist/esm/runtime/filter.js +476 -0
  124. package/dist/esm/runtime/filter.js.map +1 -0
  125. package/dist/esm/runtime/fragments.js +478 -0
  126. package/dist/esm/runtime/fragments.js.map +1 -0
  127. package/dist/esm/runtime/historical.js +954 -0
  128. package/dist/esm/runtime/historical.js.map +1 -0
  129. package/dist/esm/runtime/index.js +316 -0
  130. package/dist/esm/runtime/index.js.map +1 -0
  131. package/dist/esm/runtime/init.js +12 -0
  132. package/dist/esm/runtime/init.js.map +1 -0
  133. package/dist/esm/runtime/isolated.js +464 -0
  134. package/dist/esm/runtime/isolated.js.map +1 -0
  135. package/dist/esm/runtime/multichain.js +511 -0
  136. package/dist/esm/runtime/multichain.js.map +1 -0
  137. package/dist/esm/runtime/omnichain.js +546 -0
  138. package/dist/esm/runtime/omnichain.js.map +1 -0
  139. package/dist/esm/runtime/realtime.js +722 -0
  140. package/dist/esm/runtime/realtime.js.map +1 -0
  141. package/dist/esm/server/error.js +56 -0
  142. package/dist/esm/server/error.js.map +1 -0
  143. package/dist/esm/server/index.js +121 -0
  144. package/dist/esm/server/index.js.map +1 -0
  145. package/dist/esm/sync-historical/index.js +703 -0
  146. package/dist/esm/sync-historical/index.js.map +1 -0
  147. package/dist/esm/sync-realtime/bloom.js +76 -0
  148. package/dist/esm/sync-realtime/bloom.js.map +1 -0
  149. package/dist/esm/sync-realtime/index.js +918 -0
  150. package/dist/esm/sync-realtime/index.js.map +1 -0
  151. package/dist/esm/sync-store/encode.js +105 -0
  152. package/dist/esm/sync-store/encode.js.map +1 -0
  153. package/dist/esm/sync-store/index.js +885 -0
  154. package/dist/esm/sync-store/index.js.map +1 -0
  155. package/dist/esm/sync-store/migrations.js +1595 -0
  156. package/dist/esm/sync-store/migrations.js.map +1 -0
  157. package/dist/esm/sync-store/schema.js +181 -0
  158. package/dist/esm/sync-store/schema.js.map +1 -0
  159. package/dist/esm/types/db.js +2 -0
  160. package/dist/esm/types/db.js.map +1 -0
  161. package/dist/esm/types/eth.js +2 -0
  162. package/dist/esm/types/eth.js.map +1 -0
  163. package/dist/esm/types/utils.js +2 -0
  164. package/dist/esm/types/utils.js.map +1 -0
  165. package/dist/esm/types/virtual.js +2 -0
  166. package/dist/esm/types/virtual.js.map +1 -0
  167. package/dist/esm/ui/app.js +157 -0
  168. package/dist/esm/ui/app.js.map +1 -0
  169. package/dist/esm/ui/index.js +29 -0
  170. package/dist/esm/ui/index.js.map +1 -0
  171. package/dist/esm/ui/patch.js +140 -0
  172. package/dist/esm/ui/patch.js.map +1 -0
  173. package/dist/esm/utils/abi.js +55 -0
  174. package/dist/esm/utils/abi.js.map +1 -0
  175. package/dist/esm/utils/bigint.js +37 -0
  176. package/dist/esm/utils/bigint.js.map +1 -0
  177. package/dist/esm/utils/chains.js +21 -0
  178. package/dist/esm/utils/chains.js.map +1 -0
  179. package/dist/esm/utils/checkpoint.js +139 -0
  180. package/dist/esm/utils/checkpoint.js.map +1 -0
  181. package/dist/esm/utils/chunk.js +8 -0
  182. package/dist/esm/utils/chunk.js.map +1 -0
  183. package/dist/esm/utils/copy.js +129 -0
  184. package/dist/esm/utils/copy.js.map +1 -0
  185. package/dist/esm/utils/date.js +27 -0
  186. package/dist/esm/utils/date.js.map +1 -0
  187. package/dist/esm/utils/debug.js +2 -0
  188. package/dist/esm/utils/debug.js.map +1 -0
  189. package/dist/esm/utils/decodeAbiParameters.js +290 -0
  190. package/dist/esm/utils/decodeAbiParameters.js.map +1 -0
  191. package/dist/esm/utils/decodeEventLog.js +75 -0
  192. package/dist/esm/utils/decodeEventLog.js.map +1 -0
  193. package/dist/esm/utils/dedupe.js +29 -0
  194. package/dist/esm/utils/dedupe.js.map +1 -0
  195. package/dist/esm/utils/duplicates.js +19 -0
  196. package/dist/esm/utils/duplicates.js.map +1 -0
  197. package/dist/esm/utils/estimate.js +6 -0
  198. package/dist/esm/utils/estimate.js.map +1 -0
  199. package/dist/esm/utils/finality.js +38 -0
  200. package/dist/esm/utils/finality.js.map +1 -0
  201. package/dist/esm/utils/format.js +20 -0
  202. package/dist/esm/utils/format.js.map +1 -0
  203. package/dist/esm/utils/generators.js +121 -0
  204. package/dist/esm/utils/generators.js.map +1 -0
  205. package/dist/esm/utils/hash.js +11 -0
  206. package/dist/esm/utils/hash.js.map +1 -0
  207. package/dist/esm/utils/interval.js +171 -0
  208. package/dist/esm/utils/interval.js.map +1 -0
  209. package/dist/esm/utils/lowercase.js +7 -0
  210. package/dist/esm/utils/lowercase.js.map +1 -0
  211. package/dist/esm/utils/mutex.js +26 -0
  212. package/dist/esm/utils/mutex.js.map +1 -0
  213. package/dist/esm/utils/never.js +4 -0
  214. package/dist/esm/utils/never.js.map +1 -0
  215. package/dist/esm/utils/offset.js +101 -0
  216. package/dist/esm/utils/offset.js.map +1 -0
  217. package/dist/esm/utils/order.js +18 -0
  218. package/dist/esm/utils/order.js.map +1 -0
  219. package/dist/esm/utils/partition.js +46 -0
  220. package/dist/esm/utils/partition.js.map +1 -0
  221. package/dist/esm/utils/pg.js +175 -0
  222. package/dist/esm/utils/pg.js.map +1 -0
  223. package/dist/esm/utils/pglite.js +80 -0
  224. package/dist/esm/utils/pglite.js.map +1 -0
  225. package/dist/esm/utils/port.js +30 -0
  226. package/dist/esm/utils/port.js.map +1 -0
  227. package/dist/esm/utils/print.js +23 -0
  228. package/dist/esm/utils/print.js.map +1 -0
  229. package/dist/esm/utils/promiseAllSettledWithThrow.js +19 -0
  230. package/dist/esm/utils/promiseAllSettledWithThrow.js.map +1 -0
  231. package/dist/esm/utils/promiseWithResolvers.js +13 -0
  232. package/dist/esm/utils/promiseWithResolvers.js.map +1 -0
  233. package/dist/esm/utils/queue.js +150 -0
  234. package/dist/esm/utils/queue.js.map +1 -0
  235. package/dist/esm/utils/range.js +8 -0
  236. package/dist/esm/utils/range.js.map +1 -0
  237. package/dist/esm/utils/result.js +10 -0
  238. package/dist/esm/utils/result.js.map +1 -0
  239. package/dist/esm/utils/sql-parse.js +1326 -0
  240. package/dist/esm/utils/sql-parse.js.map +1 -0
  241. package/dist/esm/utils/timer.js +9 -0
  242. package/dist/esm/utils/timer.js.map +1 -0
  243. package/dist/esm/utils/truncate.js +15 -0
  244. package/dist/esm/utils/truncate.js.map +1 -0
  245. package/dist/esm/utils/wait.js +10 -0
  246. package/dist/esm/utils/wait.js.map +1 -0
  247. package/dist/esm/utils/zipper.js +67 -0
  248. package/dist/esm/utils/zipper.js.map +1 -0
  249. package/dist/types/bin/commands/codegen.d.ts +5 -0
  250. package/dist/types/bin/commands/codegen.d.ts.map +1 -0
  251. package/dist/types/bin/commands/createViews.d.ts +8 -0
  252. package/dist/types/bin/commands/createViews.d.ts.map +1 -0
  253. package/dist/types/bin/commands/dev.d.ts +5 -0
  254. package/dist/types/bin/commands/dev.d.ts.map +1 -0
  255. package/dist/types/bin/commands/list.d.ts +5 -0
  256. package/dist/types/bin/commands/list.d.ts.map +1 -0
  257. package/dist/types/bin/commands/prune.d.ts +5 -0
  258. package/dist/types/bin/commands/prune.d.ts.map +1 -0
  259. package/dist/types/bin/commands/serve.d.ts +5 -0
  260. package/dist/types/bin/commands/serve.d.ts.map +1 -0
  261. package/dist/types/bin/commands/start.d.ts +19 -0
  262. package/dist/types/bin/commands/start.d.ts.map +1 -0
  263. package/dist/types/bin/isolatedController.d.ts +13 -0
  264. package/dist/types/bin/isolatedController.d.ts.map +1 -0
  265. package/dist/types/bin/isolatedWorker.d.ts +9 -0
  266. package/dist/types/bin/isolatedWorker.d.ts.map +1 -0
  267. package/dist/types/bin/ponder.d.ts +37 -0
  268. package/dist/types/bin/ponder.d.ts.map +1 -0
  269. package/dist/types/bin/utils/codegen.d.ts +6 -0
  270. package/dist/types/bin/utils/codegen.d.ts.map +1 -0
  271. package/dist/types/bin/utils/exit.d.ts +10 -0
  272. package/dist/types/bin/utils/exit.d.ts.map +1 -0
  273. package/dist/types/build/config.d.ts +97 -0
  274. package/dist/types/build/config.d.ts.map +1 -0
  275. package/dist/types/build/factory.d.ts +15 -0
  276. package/dist/types/build/factory.d.ts.map +1 -0
  277. package/dist/types/build/index.d.ts +84 -0
  278. package/dist/types/build/index.d.ts.map +1 -0
  279. package/dist/types/build/plugin.d.ts +4 -0
  280. package/dist/types/build/plugin.d.ts.map +1 -0
  281. package/dist/types/build/pre.d.ts +29 -0
  282. package/dist/types/build/pre.d.ts.map +1 -0
  283. package/dist/types/build/schema.d.ts +20 -0
  284. package/dist/types/build/schema.d.ts.map +1 -0
  285. package/dist/types/build/stacktrace.d.ts +13 -0
  286. package/dist/types/build/stacktrace.d.ts.map +1 -0
  287. package/dist/types/client/index.d.ts +27 -0
  288. package/dist/types/client/index.d.ts.map +1 -0
  289. package/dist/types/config/address.d.ts +34 -0
  290. package/dist/types/config/address.d.ts.map +1 -0
  291. package/dist/types/config/eventFilter.d.ts +18 -0
  292. package/dist/types/config/eventFilter.d.ts.map +1 -0
  293. package/dist/types/config/index.d.ts +144 -0
  294. package/dist/types/config/index.d.ts.map +1 -0
  295. package/dist/types/config/utilityTypes.d.ts +43 -0
  296. package/dist/types/config/utilityTypes.d.ts.map +1 -0
  297. package/dist/types/database/actions.d.ts +99 -0
  298. package/dist/types/database/actions.d.ts.map +1 -0
  299. package/dist/types/database/index.d.ts +493 -0
  300. package/dist/types/database/index.d.ts.map +1 -0
  301. package/dist/types/database/queryBuilder.d.ts +65 -0
  302. package/dist/types/database/queryBuilder.d.ts.map +1 -0
  303. package/dist/types/drizzle/bigint.d.ts +25 -0
  304. package/dist/types/drizzle/bigint.d.ts.map +1 -0
  305. package/dist/types/drizzle/bytes.d.ts +31 -0
  306. package/dist/types/drizzle/bytes.d.ts.map +1 -0
  307. package/dist/types/drizzle/hex.d.ts +25 -0
  308. package/dist/types/drizzle/hex.d.ts.map +1 -0
  309. package/dist/types/drizzle/index.d.ts +6 -0
  310. package/dist/types/drizzle/index.d.ts.map +1 -0
  311. package/dist/types/drizzle/json.d.ts +51 -0
  312. package/dist/types/drizzle/json.d.ts.map +1 -0
  313. package/dist/types/drizzle/kit/index.d.ts +187 -0
  314. package/dist/types/drizzle/kit/index.d.ts.map +1 -0
  315. package/dist/types/drizzle/onchain.d.ts +298 -0
  316. package/dist/types/drizzle/onchain.d.ts.map +1 -0
  317. package/dist/types/drizzle/text.d.ts +29 -0
  318. package/dist/types/drizzle/text.d.ts.map +1 -0
  319. package/dist/types/graphql/graphiql.html.d.ts +2 -0
  320. package/dist/types/graphql/graphiql.html.d.ts.map +1 -0
  321. package/dist/types/graphql/index.d.ts +12 -0
  322. package/dist/types/graphql/index.d.ts.map +1 -0
  323. package/dist/types/graphql/json.d.ts +3 -0
  324. package/dist/types/graphql/json.d.ts.map +1 -0
  325. package/dist/types/graphql/middleware.d.ts +29 -0
  326. package/dist/types/graphql/middleware.d.ts.map +1 -0
  327. package/dist/types/index.d.ts +23 -0
  328. package/dist/types/index.d.ts.map +1 -0
  329. package/dist/types/indexing/addStackTrace.d.ts +3 -0
  330. package/dist/types/indexing/addStackTrace.d.ts.map +1 -0
  331. package/dist/types/indexing/client.d.ts +154 -0
  332. package/dist/types/indexing/client.d.ts.map +1 -0
  333. package/dist/types/indexing/index.d.ts +72 -0
  334. package/dist/types/indexing/index.d.ts.map +1 -0
  335. package/dist/types/indexing/profile.d.ts +16 -0
  336. package/dist/types/indexing/profile.d.ts.map +1 -0
  337. package/dist/types/indexing-store/cache.d.ts +115 -0
  338. package/dist/types/indexing-store/cache.d.ts.map +1 -0
  339. package/dist/types/indexing-store/index.d.ts +24 -0
  340. package/dist/types/indexing-store/index.d.ts.map +1 -0
  341. package/dist/types/indexing-store/profile.d.ts +7 -0
  342. package/dist/types/indexing-store/profile.d.ts.map +1 -0
  343. package/dist/types/indexing-store/utils.d.ts +19 -0
  344. package/dist/types/indexing-store/utils.d.ts.map +1 -0
  345. package/dist/types/internal/common.d.ts +15 -0
  346. package/dist/types/internal/common.d.ts.map +1 -0
  347. package/dist/types/internal/errors.d.ts +101 -0
  348. package/dist/types/internal/errors.d.ts.map +1 -0
  349. package/dist/types/internal/logger.d.ts +37 -0
  350. package/dist/types/internal/logger.d.ts.map +1 -0
  351. package/dist/types/internal/metrics.d.ts +120 -0
  352. package/dist/types/internal/metrics.d.ts.map +1 -0
  353. package/dist/types/internal/options.d.ts +62 -0
  354. package/dist/types/internal/options.d.ts.map +1 -0
  355. package/dist/types/internal/shutdown.d.ts +8 -0
  356. package/dist/types/internal/shutdown.d.ts.map +1 -0
  357. package/dist/types/internal/telemetry.d.ts +43 -0
  358. package/dist/types/internal/telemetry.d.ts.map +1 -0
  359. package/dist/types/internal/types.d.ts +443 -0
  360. package/dist/types/internal/types.d.ts.map +1 -0
  361. package/dist/types/rpc/actions.d.ts +360 -0
  362. package/dist/types/rpc/actions.d.ts.map +1 -0
  363. package/dist/types/rpc/http.d.ts +17 -0
  364. package/dist/types/rpc/http.d.ts.map +1 -0
  365. package/dist/types/rpc/index.d.ts +43 -0
  366. package/dist/types/rpc/index.d.ts.map +1 -0
  367. package/dist/types/runtime/events.d.ts +40 -0
  368. package/dist/types/runtime/events.d.ts.map +1 -0
  369. package/dist/types/runtime/filter.d.ts +96 -0
  370. package/dist/types/runtime/filter.d.ts.map +1 -0
  371. package/dist/types/runtime/fragments.d.ts +30 -0
  372. package/dist/types/runtime/fragments.d.ts.map +1 -0
  373. package/dist/types/runtime/historical.d.ts +123 -0
  374. package/dist/types/runtime/historical.d.ts.map +1 -0
  375. package/dist/types/runtime/index.d.ts +89 -0
  376. package/dist/types/runtime/index.d.ts.map +1 -0
  377. package/dist/types/runtime/init.d.ts +28 -0
  378. package/dist/types/runtime/init.d.ts.map +1 -0
  379. package/dist/types/runtime/isolated.d.ts +14 -0
  380. package/dist/types/runtime/isolated.d.ts.map +1 -0
  381. package/dist/types/runtime/multichain.d.ts +13 -0
  382. package/dist/types/runtime/multichain.d.ts.map +1 -0
  383. package/dist/types/runtime/omnichain.d.ts +23 -0
  384. package/dist/types/runtime/omnichain.d.ts.map +1 -0
  385. package/dist/types/runtime/realtime.d.ts +93 -0
  386. package/dist/types/runtime/realtime.d.ts.map +1 -0
  387. package/dist/types/server/error.d.ts +5 -0
  388. package/dist/types/server/error.d.ts.map +1 -0
  389. package/dist/types/server/index.d.ts +13 -0
  390. package/dist/types/server/index.d.ts.map +1 -0
  391. package/dist/types/sync-historical/index.d.ts +36 -0
  392. package/dist/types/sync-historical/index.d.ts.map +1 -0
  393. package/dist/types/sync-realtime/bloom.d.ts +18 -0
  394. package/dist/types/sync-realtime/bloom.d.ts.map +1 -0
  395. package/dist/types/sync-realtime/index.d.ts +48 -0
  396. package/dist/types/sync-realtime/index.d.ts.map +1 -0
  397. package/dist/types/sync-store/encode.d.ts +25 -0
  398. package/dist/types/sync-store/encode.d.ts.map +1 -0
  399. package/dist/types/sync-store/index.d.ts +135 -0
  400. package/dist/types/sync-store/index.d.ts.map +1 -0
  401. package/dist/types/sync-store/migrations.d.ts +8 -0
  402. package/dist/types/sync-store/migrations.d.ts.map +1 -0
  403. package/dist/types/sync-store/schema.d.ts +1828 -0
  404. package/dist/types/sync-store/schema.d.ts.map +1 -0
  405. package/dist/types/types/db.d.ts +213 -0
  406. package/dist/types/types/db.d.ts.map +1 -0
  407. package/dist/types/types/eth.d.ts +196 -0
  408. package/dist/types/types/eth.d.ts.map +1 -0
  409. package/dist/types/types/utils.d.ts +38 -0
  410. package/dist/types/types/utils.d.ts.map +1 -0
  411. package/dist/types/types/virtual.d.ts +99 -0
  412. package/dist/types/types/virtual.d.ts.map +1 -0
  413. package/dist/types/ui/app.d.ts +22 -0
  414. package/dist/types/ui/app.d.ts.map +1 -0
  415. package/dist/types/ui/index.d.ts +5 -0
  416. package/dist/types/ui/index.d.ts.map +1 -0
  417. package/dist/types/ui/patch.d.ts +7 -0
  418. package/dist/types/ui/patch.d.ts.map +1 -0
  419. package/dist/types/utils/abi.d.ts +23 -0
  420. package/dist/types/utils/abi.d.ts.map +1 -0
  421. package/dist/types/utils/bigint.d.ts +15 -0
  422. package/dist/types/utils/bigint.d.ts.map +1 -0
  423. package/dist/types/utils/chains.d.ts +42 -0
  424. package/dist/types/utils/chains.d.ts.map +1 -0
  425. package/dist/types/utils/checkpoint.d.ts +52 -0
  426. package/dist/types/utils/checkpoint.d.ts.map +1 -0
  427. package/dist/types/utils/chunk.d.ts +2 -0
  428. package/dist/types/utils/chunk.d.ts.map +1 -0
  429. package/dist/types/utils/copy.d.ts +16 -0
  430. package/dist/types/utils/copy.d.ts.map +1 -0
  431. package/dist/types/utils/date.d.ts +7 -0
  432. package/dist/types/utils/date.d.ts.map +1 -0
  433. package/dist/types/utils/debug.d.ts +105 -0
  434. package/dist/types/utils/debug.d.ts.map +1 -0
  435. package/dist/types/utils/decodeAbiParameters.d.ts +28 -0
  436. package/dist/types/utils/decodeAbiParameters.d.ts.map +1 -0
  437. package/dist/types/utils/decodeEventLog.d.ts +12 -0
  438. package/dist/types/utils/decodeEventLog.d.ts.map +1 -0
  439. package/dist/types/utils/dedupe.d.ts +20 -0
  440. package/dist/types/utils/dedupe.d.ts.map +1 -0
  441. package/dist/types/utils/duplicates.d.ts +7 -0
  442. package/dist/types/utils/duplicates.d.ts.map +1 -0
  443. package/dist/types/utils/estimate.d.ts +11 -0
  444. package/dist/types/utils/estimate.d.ts.map +1 -0
  445. package/dist/types/utils/finality.d.ts +12 -0
  446. package/dist/types/utils/finality.d.ts.map +1 -0
  447. package/dist/types/utils/format.d.ts +3 -0
  448. package/dist/types/utils/format.d.ts.map +1 -0
  449. package/dist/types/utils/generators.d.ts +42 -0
  450. package/dist/types/utils/generators.d.ts.map +1 -0
  451. package/dist/types/utils/hash.d.ts +11 -0
  452. package/dist/types/utils/hash.d.ts.map +1 -0
  453. package/dist/types/utils/interval.d.ts +53 -0
  454. package/dist/types/utils/interval.d.ts.map +1 -0
  455. package/dist/types/utils/lowercase.d.ts +5 -0
  456. package/dist/types/utils/lowercase.d.ts.map +1 -0
  457. package/dist/types/utils/mutex.d.ts +5 -0
  458. package/dist/types/utils/mutex.d.ts.map +1 -0
  459. package/dist/types/utils/never.d.ts +2 -0
  460. package/dist/types/utils/never.d.ts.map +1 -0
  461. package/dist/types/utils/offset.d.ts +8 -0
  462. package/dist/types/utils/offset.d.ts.map +1 -0
  463. package/dist/types/utils/order.d.ts +2 -0
  464. package/dist/types/utils/order.d.ts.map +1 -0
  465. package/dist/types/utils/partition.d.ts +22 -0
  466. package/dist/types/utils/partition.d.ts.map +1 -0
  467. package/dist/types/utils/pg.d.ts +8 -0
  468. package/dist/types/utils/pg.d.ts.map +1 -0
  469. package/dist/types/utils/pglite.d.ts +25 -0
  470. package/dist/types/utils/pglite.d.ts.map +1 -0
  471. package/dist/types/utils/port.d.ts +5 -0
  472. package/dist/types/utils/port.d.ts.map +1 -0
  473. package/dist/types/utils/print.d.ts +2 -0
  474. package/dist/types/utils/print.d.ts.map +1 -0
  475. package/dist/types/utils/promiseAllSettledWithThrow.d.ts +8 -0
  476. package/dist/types/utils/promiseAllSettledWithThrow.d.ts.map +1 -0
  477. package/dist/types/utils/promiseWithResolvers.d.ts +10 -0
  478. package/dist/types/utils/promiseWithResolvers.d.ts.map +1 -0
  479. package/dist/types/utils/queue.d.ts +33 -0
  480. package/dist/types/utils/queue.d.ts.map +1 -0
  481. package/dist/types/utils/range.d.ts +8 -0
  482. package/dist/types/utils/range.d.ts.map +1 -0
  483. package/dist/types/utils/result.d.ts +17 -0
  484. package/dist/types/utils/result.d.ts.map +1 -0
  485. package/dist/types/utils/sql-parse.d.ts +21 -0
  486. package/dist/types/utils/sql-parse.d.ts.map +1 -0
  487. package/dist/types/utils/timer.d.ts +6 -0
  488. package/dist/types/utils/timer.d.ts.map +1 -0
  489. package/dist/types/utils/truncate.d.ts +9 -0
  490. package/dist/types/utils/truncate.d.ts.map +1 -0
  491. package/dist/types/utils/wait.d.ts +6 -0
  492. package/dist/types/utils/wait.d.ts.map +1 -0
  493. package/dist/types/utils/zipper.d.ts +36 -0
  494. package/dist/types/utils/zipper.d.ts.map +1 -0
  495. package/package.json +114 -0
  496. package/src/bin/commands/codegen.ts +56 -0
  497. package/src/bin/commands/createViews.ts +318 -0
  498. package/src/bin/commands/dev.ts +490 -0
  499. package/src/bin/commands/list.ts +208 -0
  500. package/src/bin/commands/prune.ts +322 -0
  501. package/src/bin/commands/serve.ts +236 -0
  502. package/src/bin/commands/start.ts +319 -0
  503. package/src/bin/isolatedController.ts +300 -0
  504. package/src/bin/isolatedWorker.ts +192 -0
  505. package/src/bin/ponder.ts +208 -0
  506. package/src/bin/utils/codegen.ts +32 -0
  507. package/src/bin/utils/exit.ts +112 -0
  508. package/src/build/config.ts +1141 -0
  509. package/src/build/factory.ts +147 -0
  510. package/src/build/index.ts +790 -0
  511. package/src/build/plugin.ts +58 -0
  512. package/src/build/pre.ts +114 -0
  513. package/src/build/schema.ts +358 -0
  514. package/src/build/stacktrace.ts +137 -0
  515. package/src/client/index.ts +551 -0
  516. package/src/config/address.ts +45 -0
  517. package/src/config/eventFilter.ts +33 -0
  518. package/src/config/index.ts +240 -0
  519. package/src/config/utilityTypes.ts +152 -0
  520. package/src/database/actions.ts +873 -0
  521. package/src/database/index.ts +1029 -0
  522. package/src/database/queryBuilder.ts +537 -0
  523. package/src/drizzle/bigint.ts +57 -0
  524. package/src/drizzle/bytes.ts +68 -0
  525. package/src/drizzle/hex.ts +58 -0
  526. package/src/drizzle/index.ts +40 -0
  527. package/src/drizzle/json.ts +159 -0
  528. package/src/drizzle/kit/index.ts +1348 -0
  529. package/src/drizzle/onchain.ts +476 -0
  530. package/src/drizzle/text.ts +77 -0
  531. package/src/graphql/graphiql.html.ts +59 -0
  532. package/src/graphql/index.ts +1351 -0
  533. package/src/graphql/json.ts +62 -0
  534. package/src/graphql/middleware.ts +115 -0
  535. package/src/index.ts +139 -0
  536. package/src/indexing/addStackTrace.ts +69 -0
  537. package/src/indexing/client.ts +1184 -0
  538. package/src/indexing/index.ts +976 -0
  539. package/src/indexing/profile.ts +771 -0
  540. package/src/indexing-store/cache.ts +1057 -0
  541. package/src/indexing-store/index.ts +628 -0
  542. package/src/indexing-store/profile.ts +557 -0
  543. package/src/indexing-store/utils.ts +162 -0
  544. package/src/internal/common.ts +15 -0
  545. package/src/internal/errors.ts +228 -0
  546. package/src/internal/logger.ts +252 -0
  547. package/src/internal/metrics.ts +1030 -0
  548. package/src/internal/options.ts +130 -0
  549. package/src/internal/shutdown.ts +32 -0
  550. package/src/internal/telemetry.ts +303 -0
  551. package/src/internal/types.ts +611 -0
  552. package/src/rpc/actions.ts +1344 -0
  553. package/src/rpc/http.ts +164 -0
  554. package/src/rpc/index.ts +959 -0
  555. package/src/runtime/events.ts +875 -0
  556. package/src/runtime/filter.ts +705 -0
  557. package/src/runtime/fragments.ts +674 -0
  558. package/src/runtime/historical.ts +1522 -0
  559. package/src/runtime/index.ts +569 -0
  560. package/src/runtime/init.ts +49 -0
  561. package/src/runtime/isolated.ts +775 -0
  562. package/src/runtime/multichain.ts +860 -0
  563. package/src/runtime/omnichain.ts +920 -0
  564. package/src/runtime/realtime.ts +1164 -0
  565. package/src/server/error.ts +68 -0
  566. package/src/server/index.ts +173 -0
  567. package/src/sync-historical/index.ts +1065 -0
  568. package/src/sync-realtime/bloom.ts +102 -0
  569. package/src/sync-realtime/index.ts +1304 -0
  570. package/src/sync-store/encode.ts +153 -0
  571. package/src/sync-store/index.ts +1633 -0
  572. package/src/sync-store/migrations.ts +1801 -0
  573. package/src/sync-store/schema.ts +248 -0
  574. package/src/types/db.ts +292 -0
  575. package/src/types/eth.ts +216 -0
  576. package/src/types/utils.ts +47 -0
  577. package/src/types/virtual.ts +244 -0
  578. package/src/types.d.ts +38 -0
  579. package/src/ui/app.ts +207 -0
  580. package/src/ui/index.ts +37 -0
  581. package/src/ui/patch.ts +204 -0
  582. package/src/utils/abi.ts +103 -0
  583. package/src/utils/bigint.ts +41 -0
  584. package/src/utils/chains.ts +22 -0
  585. package/src/utils/checkpoint.ts +203 -0
  586. package/src/utils/chunk.ts +7 -0
  587. package/src/utils/copy.ts +151 -0
  588. package/src/utils/date.ts +26 -0
  589. package/src/utils/debug.ts +110 -0
  590. package/src/utils/decodeAbiParameters.ts +428 -0
  591. package/src/utils/decodeEventLog.ts +100 -0
  592. package/src/utils/dedupe.ts +32 -0
  593. package/src/utils/duplicates.ts +19 -0
  594. package/src/utils/estimate.ts +27 -0
  595. package/src/utils/finality.ts +40 -0
  596. package/src/utils/format.ts +22 -0
  597. package/src/utils/generators.ts +157 -0
  598. package/src/utils/hash.ts +22 -0
  599. package/src/utils/interval.ts +212 -0
  600. package/src/utils/lowercase.ts +6 -0
  601. package/src/utils/mutex.ts +33 -0
  602. package/src/utils/never.ts +3 -0
  603. package/src/utils/offset.ts +133 -0
  604. package/src/utils/order.ts +16 -0
  605. package/src/utils/partition.ts +53 -0
  606. package/src/utils/pg.ts +230 -0
  607. package/src/utils/pglite.ts +97 -0
  608. package/src/utils/port.ts +34 -0
  609. package/src/utils/print.ts +31 -0
  610. package/src/utils/promiseAllSettledWithThrow.ts +27 -0
  611. package/src/utils/promiseWithResolvers.ts +20 -0
  612. package/src/utils/queue.ts +258 -0
  613. package/src/utils/range.ts +8 -0
  614. package/src/utils/result.ts +26 -0
  615. package/src/utils/sql-parse.ts +1477 -0
  616. package/src/utils/timer.ts +8 -0
  617. package/src/utils/truncate.ts +15 -0
  618. package/src/utils/wait.ts +8 -0
  619. package/src/utils/zipper.ts +80 -0
@@ -0,0 +1,1141 @@
1
+ import type { Factory } from "@/config/address.js";
2
+ import type { Config } from "@/config/index.js";
3
+ import type { Common } from "@/internal/common.js";
4
+ import { BuildError } from "@/internal/errors.js";
5
+ import type {
6
+ BlockFilter,
7
+ Chain,
8
+ Contract,
9
+ EventCallback,
10
+ FilterAddress,
11
+ IndexingBuild,
12
+ IndexingFunctions,
13
+ LightBlock,
14
+ LogFilter,
15
+ SetupCallback,
16
+ SyncBlock,
17
+ TraceFilter,
18
+ TransactionFilter,
19
+ TransferFilter,
20
+ } from "@/internal/types.js";
21
+ import { eth_getBlockByNumber } from "@/rpc/actions.js";
22
+ import { type Rpc, createRpc } from "@/rpc/index.js";
23
+ import {
24
+ defaultBlockFilterInclude,
25
+ defaultLogFilterInclude,
26
+ defaultTraceFilterInclude,
27
+ defaultTransactionFilterInclude,
28
+ defaultTransactionReceiptInclude,
29
+ defaultTransferFilterInclude,
30
+ } from "@/runtime/filter.js";
31
+ import { buildTopics, toSafeName } from "@/utils/abi.js";
32
+ import { hyperliquidEvm, chains as viemChains } from "@/utils/chains.js";
33
+ import { dedupe } from "@/utils/dedupe.js";
34
+ import { getFinalityBlockCount } from "@/utils/finality.js";
35
+ import { toLowerCase } from "@/utils/lowercase.js";
36
+ import {
37
+ type Abi,
38
+ type AbiEvent,
39
+ type AbiFunction,
40
+ type Address,
41
+ BlockNotFoundError,
42
+ type Hex,
43
+ type LogTopic,
44
+ hexToNumber,
45
+ numberToHex,
46
+ toEventSelector,
47
+ toFunctionSelector,
48
+ } from "viem";
49
+ import { buildLogFactory } from "./factory.js";
50
+
51
+ const flattenSources = <
52
+ T extends Config["contracts"] | Config["accounts"] | Config["blocks"],
53
+ >(
54
+ config: T,
55
+ ): (Omit<T[string], "chain"> & { name: string; chain: string })[] => {
56
+ return Object.entries(config).flatMap(
57
+ ([name, source]: [string, T[string]]) => {
58
+ if (typeof source.chain === "object") {
59
+ return Object.entries(source.chain).map(([chain, sourceOverride]) => {
60
+ const { chain: _chain, ...base } = source;
61
+
62
+ return {
63
+ name,
64
+ chain,
65
+ ...base,
66
+ ...sourceOverride,
67
+ };
68
+ });
69
+ } else {
70
+ // Handles string, null, or undefined
71
+ return {
72
+ name,
73
+ ...source,
74
+ };
75
+ }
76
+ },
77
+ );
78
+ };
79
+
80
+ export async function buildIndexingFunctions({
81
+ common,
82
+ config,
83
+ indexingFunctions,
84
+ configBuild: { chains, rpcs },
85
+ }: {
86
+ common: Common;
87
+ config: Config;
88
+ indexingFunctions: IndexingFunctions;
89
+ configBuild: Pick<IndexingBuild, "chains" | "rpcs">;
90
+ }): Promise<{
91
+ chains: Chain[];
92
+ rpcs: Rpc[];
93
+ finalizedBlocks: LightBlock[];
94
+ eventCallbacks: EventCallback[][];
95
+ setupCallbacks: SetupCallback[][];
96
+ contracts: {
97
+ [name: string]: {
98
+ abi: Abi;
99
+ address?: Address | readonly Address[];
100
+ startBlock?: number;
101
+ endBlock?: number;
102
+ };
103
+ }[];
104
+ logs: ({ level: "warn" | "info" | "debug"; msg: string } & Record<
105
+ string,
106
+ unknown
107
+ >)[];
108
+ }> {
109
+ const context = { logger: common.logger.child({ action: "build" }) };
110
+
111
+ const logs: ({ level: "warn" | "info" | "debug"; msg: string } & Record<
112
+ string,
113
+ unknown
114
+ >)[] = [];
115
+
116
+ const perChainLatestBlockNumber = new Map<string, Promise<number>>();
117
+
118
+ const resolveBlockNumber = async (
119
+ blockNumberOrTag: number | "latest" | undefined,
120
+ chain: Chain,
121
+ ) => {
122
+ if (blockNumberOrTag === undefined) {
123
+ return undefined;
124
+ }
125
+
126
+ if (Number.isNaN(blockNumberOrTag)) {
127
+ return undefined;
128
+ }
129
+ if (blockNumberOrTag === "latest") {
130
+ if (perChainLatestBlockNumber.has(chain.name)) {
131
+ return perChainLatestBlockNumber.get(chain.name)!;
132
+ } else {
133
+ const rpc = rpcs[chains.findIndex((c) => c.name === chain.name)]!;
134
+ const blockPromise = rpc
135
+ .request(
136
+ {
137
+ method: "eth_getBlockByNumber",
138
+ params: ["latest", false],
139
+ },
140
+ context,
141
+ )
142
+ .then((block) => {
143
+ if (!block)
144
+ throw new BlockNotFoundError({ blockNumber: "latest" as any });
145
+ return hexToNumber((block as SyncBlock).number);
146
+ })
147
+ .catch((e) => {
148
+ throw new Error(
149
+ `Unable to fetch "latest" block for chain '${chain.name}':\n${e.message}`,
150
+ );
151
+ });
152
+ perChainLatestBlockNumber.set(chain.name, blockPromise);
153
+ return blockPromise;
154
+ }
155
+ }
156
+
157
+ return blockNumberOrTag;
158
+ };
159
+
160
+ const finalizedBlocks = await Promise.all(
161
+ chains.map((chain) => {
162
+ const rpc = rpcs[chains.findIndex((c) => c.name === chain.name)]!;
163
+ const blockPromise = eth_getBlockByNumber(rpc, ["latest", false], {
164
+ ...context,
165
+ retryNullBlockRequest: true,
166
+ })
167
+ .then((block) => hexToNumber((block as SyncBlock).number))
168
+ .catch((e) => {
169
+ throw new Error(
170
+ `Unable to fetch "latest" block for chain '${chain.name}':\n${e.message}`,
171
+ );
172
+ });
173
+
174
+ perChainLatestBlockNumber.set(chain.name, blockPromise);
175
+
176
+ return blockPromise.then((latest) =>
177
+ eth_getBlockByNumber(
178
+ rpc,
179
+ [numberToHex(Math.max(latest - chain.finalityBlockCount, 0)), false],
180
+ { ...context, retryNullBlockRequest: true },
181
+ ).then((block) => ({
182
+ hash: block.hash,
183
+ parentHash: block.parentHash,
184
+ number: block.number,
185
+ timestamp: block.timestamp,
186
+ })),
187
+ );
188
+ }),
189
+ );
190
+
191
+ const sourceNames = new Set<string>();
192
+ for (const source of [
193
+ ...Object.keys(config.contracts ?? {}),
194
+ ...Object.keys(config.accounts ?? {}),
195
+ ...Object.keys(config.blocks ?? {}),
196
+ ]) {
197
+ if (sourceNames.has(source)) {
198
+ throw new Error(
199
+ `Validation failed: Duplicate name '${source}' not allowed. The name must be unique across blocks, contracts, and accounts.`,
200
+ );
201
+ }
202
+ sourceNames.add(source);
203
+ }
204
+
205
+ // Validate and build indexing functions
206
+ if (indexingFunctions.length === 0) {
207
+ throw new Error(
208
+ "Validation failed: Found 0 registered indexing functions.",
209
+ );
210
+ }
211
+
212
+ const eventNames = new Set<string>();
213
+
214
+ for (const { name: eventName } of indexingFunctions) {
215
+ const eventNameComponents = eventName.includes(".")
216
+ ? eventName.split(".")
217
+ : eventName.split(":");
218
+
219
+ const [sourceName] = eventNameComponents;
220
+
221
+ if (!sourceName) {
222
+ throw new Error(
223
+ `Validation failed: Invalid event '${eventName}', expected format '{sourceName}:{eventName}' or '{sourceName}.{functionName}'.`,
224
+ );
225
+ }
226
+
227
+ if (eventNameComponents.length === 3) {
228
+ const [, sourceType, fromOrTo] = eventNameComponents;
229
+
230
+ if (
231
+ (sourceType !== "transaction" && sourceType !== "transfer") ||
232
+ (fromOrTo !== "from" && fromOrTo !== "to")
233
+ ) {
234
+ throw new Error(
235
+ `Validation failed: Invalid event '${eventName}', expected format '{sourceName}:transaction:from', '{sourceName}:transaction:to', '{sourceName}:transfer:from', or '{sourceName}:transfer:to'.`,
236
+ );
237
+ }
238
+ } else if (eventNameComponents.length === 2) {
239
+ const [, sourceEventName] = eventNameComponents;
240
+
241
+ if (!sourceEventName) {
242
+ throw new Error(
243
+ `Validation failed: Invalid event '${eventName}', expected format '{sourceName}:{eventName}' or '{sourceName}.{functionName}'.`,
244
+ );
245
+ }
246
+ } else {
247
+ throw new Error(
248
+ `Validation failed: Invalid event '${eventName}', expected format '{sourceName}:{eventName}' or '{sourceName}.{functionName}'.`,
249
+ );
250
+ }
251
+
252
+ if (eventNames.has(eventName)) {
253
+ throw new Error(
254
+ `Validation failed: Multiple indexing functions registered for event '${eventName}'.`,
255
+ );
256
+ }
257
+
258
+ eventNames.add(eventName);
259
+
260
+ // Validate that the indexing function uses a sourceName that is present in the config.
261
+ const matchedSourceName = Object.keys({
262
+ ...(config.contracts ?? {}),
263
+ ...(config.accounts ?? {}),
264
+ ...(config.blocks ?? {}),
265
+ }).find((_sourceName) => _sourceName === sourceName);
266
+
267
+ if (!matchedSourceName) {
268
+ throw new Error(
269
+ `Validation failed: Invalid event '${eventName}' uses an unrecognized contract, account, or block interval name. Expected one of [${Array.from(
270
+ sourceNames,
271
+ )
272
+ .map((n) => `'${n}'`)
273
+ .join(", ")}].`,
274
+ );
275
+ }
276
+ }
277
+
278
+ // common validation for all sources
279
+ for (const source of [
280
+ ...flattenSources(config.contracts ?? {}),
281
+ ...flattenSources(config.accounts ?? {}),
282
+ ...flattenSources(config.blocks ?? {}),
283
+ ]) {
284
+ if (source.chain === null || source.chain === undefined) {
285
+ throw new Error(
286
+ `Validation failed: Chain for '${source.name}' is null or undefined. Expected one of [${chains
287
+ .map((n) => `'${n.name}'`)
288
+ .join(
289
+ ", ",
290
+ )}]. Did you forget to change 'network' to 'chain' when migrating to 0.11?`,
291
+ );
292
+ }
293
+
294
+ const chain = chains.find((n) => n.name === source.chain);
295
+ if (!chain) {
296
+ throw new Error(
297
+ `Validation failed: Invalid chain for '${
298
+ source.name
299
+ }'. Got '${source.chain}', expected one of [${chains
300
+ .map((n) => `'${n.name}'`)
301
+ .join(", ")}].`,
302
+ );
303
+ }
304
+
305
+ const startBlock = await resolveBlockNumber(source.startBlock, chain);
306
+ const endBlock = await resolveBlockNumber(source.endBlock, chain);
307
+
308
+ if (
309
+ startBlock !== undefined &&
310
+ endBlock !== undefined &&
311
+ endBlock < startBlock
312
+ ) {
313
+ throw new Error(
314
+ `Validation failed: Start block for '${source.name}' is after end block (${startBlock} > ${endBlock}).`,
315
+ );
316
+ }
317
+
318
+ if (startBlock !== undefined && Number.isInteger(startBlock) === false) {
319
+ throw new Error(
320
+ `Validation failed: Invalid start block for '${source.name}'. Got ${startBlock} typeof ${typeof startBlock}, expected an integer.`,
321
+ );
322
+ }
323
+
324
+ if (endBlock !== undefined && Number.isInteger(endBlock) === false) {
325
+ throw new Error(
326
+ `Validation failed: Invalid end block for '${source.name}'. Got ${endBlock} typeof ${typeof endBlock}, expected an integer.`,
327
+ );
328
+ }
329
+
330
+ if (
331
+ "address" in source &&
332
+ typeof source.address === "object" &&
333
+ !Array.isArray(source.address)
334
+ ) {
335
+ const factoryStartBlock =
336
+ (await resolveBlockNumber(source.address.startBlock, chain)) ??
337
+ startBlock;
338
+
339
+ const factoryEndBlock =
340
+ (await resolveBlockNumber(source.address.endBlock, chain)) ?? endBlock;
341
+
342
+ if (
343
+ factoryStartBlock !== undefined &&
344
+ (startBlock === undefined || factoryStartBlock > startBlock)
345
+ ) {
346
+ throw new Error(
347
+ `Validation failed: Start block for '${source.name}' is before start block of factory address (${factoryStartBlock} > ${startBlock}).`,
348
+ );
349
+ }
350
+
351
+ if (
352
+ endBlock !== undefined &&
353
+ (factoryEndBlock === undefined || factoryEndBlock > endBlock)
354
+ ) {
355
+ throw new Error(
356
+ `Validation failed: End block for ${source.name} is before end block of factory address (${factoryEndBlock} > ${endBlock}).`,
357
+ );
358
+ }
359
+
360
+ if (
361
+ factoryStartBlock !== undefined &&
362
+ factoryEndBlock !== undefined &&
363
+ factoryEndBlock < factoryStartBlock
364
+ ) {
365
+ throw new Error(
366
+ `Validation failed: Start block for '${source.name}' factory address is after end block (${factoryStartBlock} > ${factoryEndBlock}).`,
367
+ );
368
+ }
369
+ }
370
+ }
371
+
372
+ const perChainEventCallbacks: Map<number, EventCallback[]> = new Map();
373
+ const perChainSetupCallbacks: Map<number, SetupCallback[]> = new Map();
374
+ const perChainContracts: Map<
375
+ number,
376
+ {
377
+ [name: string]: Contract;
378
+ }
379
+ > = new Map();
380
+ for (const chain of chains) {
381
+ perChainEventCallbacks.set(chain.id, []);
382
+ perChainSetupCallbacks.set(chain.id, []);
383
+ perChainContracts.set(chain.id, {});
384
+ }
385
+
386
+ for (const source of flattenSources(config.contracts ?? {})) {
387
+ const chain = chains.find((n) => n.name === source.chain)!;
388
+
389
+ const fromBlock = await resolveBlockNumber(source.startBlock, chain);
390
+ const toBlock = await resolveBlockNumber(source.endBlock, chain);
391
+
392
+ if (indexingFunctions.some((f) => f.name === `${source.name}:setup`)) {
393
+ perChainSetupCallbacks.get(chain.id)!.push({
394
+ name: `${source.name}:setup`,
395
+ fn: indexingFunctions.find((f) => f.name === `${source.name}:setup`)!
396
+ .fn,
397
+ chain,
398
+ block: fromBlock,
399
+ });
400
+ }
401
+
402
+ let address: FilterAddress;
403
+
404
+ const resolvedAddress = source?.address;
405
+ if (
406
+ typeof resolvedAddress === "object" &&
407
+ Array.isArray(resolvedAddress) === false
408
+ ) {
409
+ const factoryAddress = resolvedAddress as Factory;
410
+ const factoryFromBlock =
411
+ (await resolveBlockNumber(factoryAddress.startBlock, chain)) ??
412
+ fromBlock;
413
+
414
+ const factoryToBlock =
415
+ (await resolveBlockNumber(factoryAddress.endBlock, chain)) ?? toBlock;
416
+
417
+ // Note that this can throw.
418
+ const logFactory = buildLogFactory({
419
+ chainId: chain.id,
420
+ sourceId: source.name,
421
+ ...factoryAddress,
422
+ fromBlock: factoryFromBlock,
423
+ toBlock: factoryToBlock,
424
+ });
425
+
426
+ perChainContracts.get(chain.id)![source.name] = {
427
+ abi: source.abi,
428
+ address: undefined,
429
+ startBlock: fromBlock,
430
+ endBlock: toBlock,
431
+ };
432
+
433
+ address = logFactory;
434
+ } else {
435
+ if (resolvedAddress !== undefined) {
436
+ for (const address of Array.isArray(resolvedAddress)
437
+ ? resolvedAddress
438
+ : [resolvedAddress as Address]) {
439
+ if (!address!.startsWith("0x"))
440
+ throw new Error(
441
+ `Validation failed: Invalid prefix for address '${address}'. Got '${address!.slice(
442
+ 0,
443
+ 2,
444
+ )}', expected '0x'.`,
445
+ );
446
+ if (address!.length !== 42)
447
+ throw new Error(
448
+ `Validation failed: Invalid length for address '${address}'. Got ${address!.length}, expected 42 characters.`,
449
+ );
450
+ }
451
+ }
452
+
453
+ const validatedAddress = Array.isArray(resolvedAddress)
454
+ ? dedupe(resolvedAddress).map((r) => toLowerCase(r))
455
+ : resolvedAddress !== undefined
456
+ ? toLowerCase(resolvedAddress as Address)
457
+ : undefined;
458
+
459
+ perChainContracts.get(chain.id)![source.name] = {
460
+ abi: source.abi,
461
+ address: validatedAddress,
462
+ startBlock: fromBlock,
463
+ endBlock: toBlock,
464
+ };
465
+
466
+ address = validatedAddress;
467
+ }
468
+
469
+ const filteredEventSelectors = new Map<
470
+ Hex,
471
+ ReturnType<typeof buildTopics>[number]
472
+ >();
473
+
474
+ if (source.filter) {
475
+ const eventFilters = Array.isArray(source.filter)
476
+ ? source.filter
477
+ : [source.filter];
478
+
479
+ for (const filter of eventFilters) {
480
+ const abiEvent = source.abi.find(
481
+ (item): item is AbiEvent =>
482
+ item.type === "event" &&
483
+ toSafeName({ abi: source.abi, item }) === filter.event,
484
+ );
485
+ if (!abiEvent) {
486
+ throw new Error(
487
+ `Validation failed: Invalid filter for contract '${
488
+ source.name
489
+ }'. Got event name '${filter.event}', expected one of [${source.abi
490
+ .filter((item): item is AbiEvent => item.type === "event")
491
+ .map((item) => `'${toSafeName({ abi: source.abi, item })}'`)
492
+ .join(", ")}].`,
493
+ );
494
+ }
495
+ }
496
+ const topics = buildTopics(source.abi, eventFilters);
497
+
498
+ for (const { topic0, topic1, topic2, topic3 } of topics) {
499
+ const abiItem = source.abi.find(
500
+ (item): item is AbiEvent =>
501
+ item.type === "event" && toEventSelector(item) === topic0,
502
+ )!;
503
+ const indexingFunction = indexingFunctions.find(
504
+ (f) => f.name === `${source.name}:${abiItem.name}`,
505
+ );
506
+
507
+ if (indexingFunction === undefined) {
508
+ throw new Error(
509
+ `Validation failed: Event selector '${toSafeName({ abi: source.abi, item: abiItem })}' is used in a filter but does not have a corresponding indexing function.`,
510
+ );
511
+ }
512
+
513
+ filteredEventSelectors.set(topic0, { topic0, topic1, topic2, topic3 });
514
+ }
515
+ }
516
+
517
+ const registeredLogEvents: string[] = [];
518
+ const registeredCallTraceEvents: string[] = [];
519
+ for (const { name: eventName } of indexingFunctions) {
520
+ // log event
521
+ if (eventName.includes(":")) {
522
+ const [logContractName, logEventName] = eventName.split(":") as [
523
+ string,
524
+ string,
525
+ ];
526
+ if (logContractName === source.name && logEventName !== "setup") {
527
+ registeredLogEvents.push(logEventName);
528
+ }
529
+ }
530
+
531
+ // trace event
532
+ if (eventName.includes(".")) {
533
+ const [functionContractName, functionName] = eventName.split(".") as [
534
+ string,
535
+ string,
536
+ ];
537
+
538
+ if (source.includeCallTraces !== true) {
539
+ continue;
540
+ }
541
+ if (functionContractName === source.name) {
542
+ registeredCallTraceEvents.push(functionName);
543
+ }
544
+ }
545
+ }
546
+
547
+ for (const logEventName of registeredLogEvents) {
548
+ const abiEvent = source.abi.find(
549
+ (item): item is AbiEvent =>
550
+ item.type === "event" &&
551
+ toSafeName({ abi: source.abi, item }) === logEventName,
552
+ );
553
+ if (abiEvent === undefined) {
554
+ throw new Error(
555
+ `Validation failed: Event name for event '${logEventName}' not found in the contract ABI. Got '${logEventName}', expected one of [${source.abi
556
+ .filter((item): item is AbiEvent => item.type === "event")
557
+ .map((item) => `'${toSafeName({ abi: source.abi, item })}'`)
558
+ .join(", ")}].`,
559
+ );
560
+ }
561
+
562
+ const eventName = `${source.name}:${logEventName}`;
563
+
564
+ const indexingFunction = indexingFunctions.find(
565
+ (f) => f.name === eventName,
566
+ )!;
567
+
568
+ let topic1: LogTopic;
569
+ let topic2: LogTopic;
570
+ let topic3: LogTopic;
571
+
572
+ const eventSelector = toEventSelector(abiEvent);
573
+
574
+ if (filteredEventSelectors.has(eventSelector)) {
575
+ topic1 = filteredEventSelectors.get(eventSelector)!.topic1;
576
+ topic2 = filteredEventSelectors.get(eventSelector)!.topic2;
577
+ topic3 = filteredEventSelectors.get(eventSelector)!.topic3;
578
+ } else {
579
+ topic1 = null;
580
+ topic2 = null;
581
+ topic3 = null;
582
+ }
583
+
584
+ const filter = {
585
+ type: "log",
586
+ chainId: chain.id,
587
+ sourceId: source.name,
588
+ address,
589
+ topic0: eventSelector,
590
+ topic1,
591
+ topic2,
592
+ topic3,
593
+ fromBlock,
594
+ toBlock,
595
+ hasTransactionReceipt: source.includeTransactionReceipts ?? false,
596
+ include: defaultLogFilterInclude.concat(
597
+ source.includeTransactionReceipts
598
+ ? defaultTransactionReceiptInclude.map(
599
+ (value) => `transactionReceipt.${value}` as const,
600
+ )
601
+ : [],
602
+ ),
603
+ } satisfies LogFilter;
604
+
605
+ const eventCallback = {
606
+ filter,
607
+ name: eventName,
608
+ fn: indexingFunction.fn,
609
+ chain,
610
+ type: "contract",
611
+ abiItem: abiEvent,
612
+ metadata: {
613
+ safeName: logEventName,
614
+ abi: source.abi,
615
+ },
616
+ } satisfies EventCallback;
617
+
618
+ perChainEventCallbacks.get(chain.id)!.push(eventCallback);
619
+ }
620
+
621
+ for (const functionEventName of registeredCallTraceEvents) {
622
+ const abiFunction = source.abi.find(
623
+ (item): item is AbiFunction =>
624
+ item.type === "function" &&
625
+ toSafeName({ abi: source.abi, item }) === functionEventName,
626
+ );
627
+ if (abiFunction === undefined) {
628
+ throw new Error(
629
+ `Validation failed: Function name for function '${functionEventName}' not found in the contract ABI. Got '${functionEventName}', expected one of [${source.abi
630
+ .filter((item): item is AbiFunction => item.type === "function")
631
+ .map((item) => `'${toSafeName({ abi: source.abi, item })}'`)
632
+ .join(", ")}].`,
633
+ );
634
+ }
635
+
636
+ const eventName = `${source.name}.${functionEventName}`;
637
+
638
+ const indexingFunction = indexingFunctions.find(
639
+ (f) => f.name === eventName,
640
+ )!;
641
+
642
+ const filter = {
643
+ type: "trace",
644
+ chainId: chain.id,
645
+ sourceId: source.name,
646
+ fromAddress: undefined,
647
+ toAddress: address,
648
+ callType: "CALL",
649
+ functionSelector: toFunctionSelector(abiFunction),
650
+ includeReverted: false,
651
+ fromBlock,
652
+ toBlock,
653
+ hasTransactionReceipt: source.includeTransactionReceipts ?? false,
654
+ include: defaultTraceFilterInclude.concat(
655
+ source.includeTransactionReceipts
656
+ ? defaultTransactionReceiptInclude.map(
657
+ (value) => `transactionReceipt.${value}` as const,
658
+ )
659
+ : [],
660
+ ),
661
+ } satisfies TraceFilter;
662
+
663
+ const eventCallback = {
664
+ filter,
665
+ name: eventName,
666
+ fn: indexingFunction.fn,
667
+ chain,
668
+ type: "contract",
669
+ abiItem: abiFunction,
670
+ metadata: {
671
+ safeName: functionEventName,
672
+ abi: source.abi,
673
+ },
674
+ } satisfies EventCallback;
675
+
676
+ perChainEventCallbacks.get(chain.id)!.push(eventCallback);
677
+ }
678
+
679
+ if (
680
+ registeredLogEvents.length === 0 &&
681
+ registeredCallTraceEvents.length === 0
682
+ ) {
683
+ logs.push({
684
+ level: "warn",
685
+ msg: "No registered indexing functions",
686
+ name: source.name,
687
+ type: "contract",
688
+ });
689
+ }
690
+ }
691
+
692
+ for (const source of flattenSources(config.accounts ?? {})) {
693
+ const chain = chains.find((n) => n.name === source.chain)!;
694
+
695
+ const fromBlock = await resolveBlockNumber(source.startBlock, chain);
696
+ const toBlock = await resolveBlockNumber(source.endBlock, chain);
697
+
698
+ const resolvedAddress = source?.address;
699
+ if (resolvedAddress === undefined) {
700
+ throw new Error(
701
+ `Validation failed: Account '${source.name}' must specify an 'address'.`,
702
+ );
703
+ }
704
+
705
+ let address: FilterAddress;
706
+
707
+ if (
708
+ typeof resolvedAddress === "object" &&
709
+ !Array.isArray(resolvedAddress)
710
+ ) {
711
+ const factoryFromBlock =
712
+ (await resolveBlockNumber(resolvedAddress.startBlock, chain)) ??
713
+ fromBlock;
714
+
715
+ const factoryToBlock =
716
+ (await resolveBlockNumber(resolvedAddress.endBlock, chain)) ?? toBlock;
717
+
718
+ // Note that this can throw.
719
+ const logFactory = buildLogFactory({
720
+ chainId: chain.id,
721
+ sourceId: source.name,
722
+ ...resolvedAddress,
723
+ fromBlock: factoryFromBlock,
724
+ toBlock: factoryToBlock,
725
+ });
726
+
727
+ address = logFactory;
728
+ } else {
729
+ for (const address of Array.isArray(resolvedAddress)
730
+ ? resolvedAddress
731
+ : [resolvedAddress]) {
732
+ if (!address!.startsWith("0x"))
733
+ throw new Error(
734
+ `Validation failed: Invalid prefix for address '${address}'. Got '${address!.slice(
735
+ 0,
736
+ 2,
737
+ )}', expected '0x'.`,
738
+ );
739
+ if (address!.length !== 42)
740
+ throw new Error(
741
+ `Validation failed: Invalid length for address '${address}'. Got ${address!.length}, expected 42 characters.`,
742
+ );
743
+ }
744
+
745
+ const validatedAddress = Array.isArray(resolvedAddress)
746
+ ? dedupe(resolvedAddress).map((r) => toLowerCase(r))
747
+ : resolvedAddress !== undefined
748
+ ? toLowerCase(resolvedAddress)
749
+ : undefined;
750
+
751
+ address = validatedAddress;
752
+ }
753
+
754
+ const filters = [
755
+ {
756
+ type: "transaction",
757
+ chainId: chain.id,
758
+ sourceId: source.name,
759
+ fromAddress: undefined,
760
+ toAddress: address,
761
+ includeReverted: false,
762
+ fromBlock,
763
+ toBlock,
764
+ hasTransactionReceipt: true,
765
+ include: defaultTransactionFilterInclude,
766
+ },
767
+ {
768
+ type: "transaction",
769
+ chainId: chain.id,
770
+ sourceId: source.name,
771
+ fromAddress: address,
772
+ toAddress: undefined,
773
+ includeReverted: false,
774
+ fromBlock,
775
+ toBlock,
776
+ hasTransactionReceipt: true,
777
+ include: defaultTransactionFilterInclude,
778
+ },
779
+ {
780
+ type: "transfer",
781
+ chainId: chain.id,
782
+ sourceId: source.name,
783
+ fromAddress: undefined,
784
+ toAddress: address,
785
+ includeReverted: false,
786
+ fromBlock,
787
+ toBlock,
788
+ hasTransactionReceipt: source.includeTransactionReceipts ?? false,
789
+ include: defaultTransferFilterInclude.concat(
790
+ source.includeTransactionReceipts
791
+ ? defaultTransactionReceiptInclude.map(
792
+ (value) => `transactionReceipt.${value}` as const,
793
+ )
794
+ : [],
795
+ ),
796
+ },
797
+ {
798
+ type: "transfer",
799
+ chainId: chain.id,
800
+ sourceId: source.name,
801
+ fromAddress: address,
802
+ toAddress: undefined,
803
+ includeReverted: false,
804
+ fromBlock,
805
+ toBlock,
806
+ hasTransactionReceipt: source.includeTransactionReceipts ?? false,
807
+ include: defaultTransferFilterInclude.concat(
808
+ source.includeTransactionReceipts
809
+ ? defaultTransactionReceiptInclude.map(
810
+ (value) => `transactionReceipt.${value}` as const,
811
+ )
812
+ : [],
813
+ ),
814
+ },
815
+ ] satisfies [
816
+ TransactionFilter,
817
+ TransactionFilter,
818
+ TransferFilter,
819
+ TransferFilter,
820
+ ];
821
+
822
+ let hasRegisteredIndexingFunction = false;
823
+
824
+ for (const filter of filters) {
825
+ const eventName =
826
+ filter.type === "transaction"
827
+ ? filter.fromAddress === undefined
828
+ ? `${source.name}:transaction:to`
829
+ : `${source.name}:transaction:from`
830
+ : filter.fromAddress === undefined
831
+ ? `${source.name}:transfer:to`
832
+ : `${source.name}:transfer:from`;
833
+
834
+ const indexingFunction = indexingFunctions.find(
835
+ (f) => f.name === eventName,
836
+ );
837
+
838
+ if (indexingFunction) {
839
+ hasRegisteredIndexingFunction = true;
840
+
841
+ const eventCallback = {
842
+ filter,
843
+ name: eventName,
844
+ fn: indexingFunction.fn,
845
+ chain,
846
+ type: "account",
847
+ direction: filter.fromAddress === undefined ? "to" : "from",
848
+ } satisfies EventCallback;
849
+
850
+ perChainEventCallbacks.get(chain.id)!.push(eventCallback);
851
+ }
852
+ }
853
+
854
+ if (hasRegisteredIndexingFunction === false) {
855
+ logs.push({
856
+ level: "warn",
857
+ msg: "No registered indexing functions",
858
+ name: source.name,
859
+ type: "account",
860
+ });
861
+ }
862
+ }
863
+
864
+ for (const source of flattenSources(config.blocks ?? {})) {
865
+ const chain = chains.find((n) => n.name === source.chain)!;
866
+
867
+ const intervalMaybeNan = source.interval ?? 1;
868
+ const interval = Number.isNaN(intervalMaybeNan) ? 0 : intervalMaybeNan;
869
+
870
+ if (!Number.isInteger(interval) || interval === 0) {
871
+ throw new Error(
872
+ `Validation failed: Invalid interval for block interval '${source.name}'. Got ${interval}, expected a non-zero integer.`,
873
+ );
874
+ }
875
+
876
+ const fromBlock = await resolveBlockNumber(source.startBlock, chain);
877
+ const toBlock = await resolveBlockNumber(source.endBlock, chain);
878
+
879
+ const eventName = `${source.name}:block`;
880
+
881
+ const indexingFunction = indexingFunctions.find(
882
+ (f) => f.name === eventName,
883
+ );
884
+
885
+ if (indexingFunction) {
886
+ const filter = {
887
+ type: "block",
888
+ chainId: chain.id,
889
+ sourceId: source.name,
890
+ interval: interval,
891
+ offset: (fromBlock ?? 0) % interval,
892
+ fromBlock,
893
+ toBlock,
894
+ hasTransactionReceipt: false,
895
+ include: defaultBlockFilterInclude,
896
+ } satisfies BlockFilter;
897
+
898
+ const eventCallback = {
899
+ filter,
900
+ name: eventName,
901
+ fn: indexingFunction.fn,
902
+ chain,
903
+ type: "block",
904
+ } satisfies EventCallback;
905
+
906
+ perChainEventCallbacks.get(chain.id)!.push(eventCallback);
907
+ } else {
908
+ logs.push({
909
+ level: "warn",
910
+ msg: "No registered indexing functions",
911
+ name: source.name,
912
+ type: "block",
913
+ });
914
+ }
915
+ }
916
+
917
+ // Filter out any chains that don't have any sources registered.
918
+ const chainsWithSources: Chain[] = [];
919
+ const rpcsWithSources: Rpc[] = [];
920
+ const finalizedBlocksWithSources: LightBlock[] = [];
921
+ const eventCallbacksWithSources: EventCallback[][] = [];
922
+ const setupCallbacksWithSources: SetupCallback[][] = [];
923
+ const contractsWithSources: { [name: string]: Contract }[] = [];
924
+
925
+ for (let i = 0; i < chains.length; i++) {
926
+ const chain = chains[i]!;
927
+ const rpc = rpcs[i]!;
928
+ const hasIndexingFunctions =
929
+ perChainEventCallbacks.get(chain.id)!.length > 0 ||
930
+ perChainSetupCallbacks.get(chain.id)!.length > 0;
931
+
932
+ if (hasIndexingFunctions) {
933
+ chainsWithSources.push(chain);
934
+ rpcsWithSources.push(rpc);
935
+ finalizedBlocksWithSources.push(finalizedBlocks[i]!);
936
+ eventCallbacksWithSources.push(perChainEventCallbacks.get(chain.id)!);
937
+ setupCallbacksWithSources.push(perChainSetupCallbacks.get(chain.id)!);
938
+ contractsWithSources.push(perChainContracts.get(chain.id)!);
939
+ } else {
940
+ logs.push({
941
+ level: "warn",
942
+ msg: "No registered indexing functions",
943
+ chain: chain.name,
944
+ chain_id: chain.id,
945
+ });
946
+ }
947
+ }
948
+
949
+ if (chainsWithSources.length === 0) {
950
+ throw new Error(
951
+ "Validation failed: Found 0 chains with registered indexing functions.",
952
+ );
953
+ }
954
+
955
+ return {
956
+ chains: chainsWithSources,
957
+ rpcs: rpcsWithSources,
958
+ finalizedBlocks: finalizedBlocksWithSources,
959
+ eventCallbacks: eventCallbacksWithSources,
960
+ setupCallbacks: setupCallbacksWithSources,
961
+ contracts: contractsWithSources,
962
+ logs,
963
+ };
964
+ }
965
+
966
+ export function buildConfig({
967
+ common,
968
+ config,
969
+ }: { common: Common; config: Config }): {
970
+ chains: Chain[];
971
+ rpcs: Rpc[];
972
+ logs: ({ level: "warn" | "info" | "debug"; msg: string } & Record<
973
+ string,
974
+ unknown
975
+ >)[];
976
+ } {
977
+ const logs: ({ level: "warn" | "info" | "debug"; msg: string } & Record<
978
+ string,
979
+ unknown
980
+ >)[] = [];
981
+
982
+ const chains: Chain[] = Object.entries(config.chains).map(
983
+ ([chainName, chain]) => {
984
+ if (chain.id > Number.MAX_SAFE_INTEGER) {
985
+ throw new Error(
986
+ `Chain "${chainName}" with id ${chain.id} has a chain_id that is too large.`,
987
+ );
988
+ }
989
+
990
+ let matchedChain = Object.values(viemChains).find((c) =>
991
+ "id" in c ? c.id === chain.id : false,
992
+ );
993
+ if (chain.id === 999) {
994
+ matchedChain = hyperliquidEvm;
995
+ }
996
+
997
+ if (chain.rpc === undefined || chain.rpc === "") {
998
+ if (matchedChain === undefined) {
999
+ throw new Error(
1000
+ `Chain "${chainName}" with id ${chain.id} has no RPC defined and no default RPC URL was found in 'viem/chains'.`,
1001
+ );
1002
+ }
1003
+
1004
+ chain.rpc = matchedChain.rpcUrls.default.http as string[];
1005
+ }
1006
+
1007
+ if (typeof chain.rpc === "string" || Array.isArray(chain.rpc)) {
1008
+ const rpcs = Array.isArray(chain.rpc) ? chain.rpc : [chain.rpc];
1009
+
1010
+ if (rpcs.length === 0) {
1011
+ throw new Error(
1012
+ `Chain "${chainName}" with id ${chain.id} has no RPC URLs.`,
1013
+ );
1014
+ }
1015
+
1016
+ if (matchedChain) {
1017
+ for (const rpc of rpcs) {
1018
+ for (const http of matchedChain.rpcUrls.default.http) {
1019
+ if (http === rpc) {
1020
+ logs.push({
1021
+ level: "warn",
1022
+ msg: "Detected public RPC URL. Most apps require an RPC URL with a higher rate limit.",
1023
+ chain: chainName,
1024
+ chain_id: chain.id,
1025
+ url: http,
1026
+ });
1027
+ break;
1028
+ }
1029
+ }
1030
+ for (const ws of matchedChain.rpcUrls.default.webSocket ?? []) {
1031
+ if (ws === rpc) {
1032
+ logs.push({
1033
+ level: "warn",
1034
+ msg: "Detected public RPC URL. Most apps require an RPC URL with a higher rate limit.",
1035
+ chain: chainName,
1036
+ chain_id: chain.id,
1037
+ url: ws,
1038
+ });
1039
+ break;
1040
+ }
1041
+ }
1042
+ }
1043
+ }
1044
+ }
1045
+
1046
+ if (chain.pollingInterval !== undefined && chain.pollingInterval! < 100) {
1047
+ throw new Error(
1048
+ `Invalid 'pollingInterval' for chain '${chainName}. Expected 100 milliseconds or greater, got ${chain.pollingInterval} milliseconds.`,
1049
+ );
1050
+ }
1051
+
1052
+ return {
1053
+ id: chain.id,
1054
+ name: chainName,
1055
+ rpc: chain.rpc,
1056
+ ws: chain.ws,
1057
+ pollingInterval: chain.pollingInterval ?? 1_000,
1058
+ finalityBlockCount: getFinalityBlockCount({ chain: matchedChain }),
1059
+ disableCache: chain.disableCache ?? false,
1060
+ ethGetLogsBlockRange: chain.ethGetLogsBlockRange,
1061
+ viemChain: matchedChain,
1062
+ } satisfies Chain;
1063
+ },
1064
+ );
1065
+
1066
+ const chainIds = new Set<number>();
1067
+ for (const chain of chains) {
1068
+ if (chainIds.has(chain.id)) {
1069
+ throw new Error(
1070
+ `Invalid id for chain "${chain.name}". ${chain.id} is already in use.`,
1071
+ );
1072
+ }
1073
+ chainIds.add(chain.id);
1074
+ }
1075
+
1076
+ const rpcs = chains.map((chain) =>
1077
+ createRpc({
1078
+ common,
1079
+ chain,
1080
+ concurrency: Math.floor(common.options.rpcMaxConcurrency / chains.length),
1081
+ }),
1082
+ );
1083
+
1084
+ return { chains, rpcs, logs };
1085
+ }
1086
+
1087
+ export async function safeBuildIndexingFunctions({
1088
+ common,
1089
+ config,
1090
+ indexingFunctions,
1091
+ configBuild,
1092
+ }: {
1093
+ common: Common;
1094
+ config: Config;
1095
+ indexingFunctions: IndexingFunctions;
1096
+ configBuild: Pick<IndexingBuild, "chains" | "rpcs">;
1097
+ }) {
1098
+ try {
1099
+ const result = await buildIndexingFunctions({
1100
+ common,
1101
+ config,
1102
+ indexingFunctions,
1103
+ configBuild,
1104
+ });
1105
+
1106
+ return {
1107
+ status: "success",
1108
+ chains: result.chains,
1109
+ rpcs: result.rpcs,
1110
+ finalizedBlocks: result.finalizedBlocks,
1111
+ eventCallbacks: result.eventCallbacks,
1112
+ setupCallbacks: result.setupCallbacks,
1113
+ contracts: result.contracts,
1114
+ logs: result.logs,
1115
+ } as const;
1116
+ } catch (_error) {
1117
+ const buildError = new BuildError((_error as Error).message);
1118
+ buildError.stack = undefined;
1119
+ return { status: "error", error: buildError } as const;
1120
+ }
1121
+ }
1122
+
1123
+ export function safeBuildConfig({
1124
+ common,
1125
+ config,
1126
+ }: { common: Common; config: Config }) {
1127
+ try {
1128
+ const result = buildConfig({ common, config });
1129
+
1130
+ return {
1131
+ status: "success",
1132
+ chains: result.chains,
1133
+ rpcs: result.rpcs,
1134
+ logs: result.logs,
1135
+ } as const;
1136
+ } catch (_error) {
1137
+ const buildError = new BuildError((_error as Error).message);
1138
+ buildError.stack = undefined;
1139
+ return { status: "error", error: buildError } as const;
1140
+ }
1141
+ }