From cfe5f739eec11f26750107d40786335f8c28e0e5 Mon Sep 17 00:00:00 2001 From: Nimrod Teich Date: Thu, 16 Apr 2026 23:02:48 +0300 Subject: [PATCH 01/25] chore(api): register @fastify/compress for gzip+brotli Large JSON responses (e.g., /providers list ~150KB, /all_providers_apr potentially 500KB+) were sent uncompressed. Register @fastify/compress globally with a 1KB threshold so brotli/gzip is applied where it helps. --- apps/api/package.json | 1 + apps/api/src/main.ts | 2 + pnpm-lock.yaml | 202 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 205 insertions(+) diff --git a/apps/api/package.json b/apps/api/package.json index 003b5d5..6ad7679 100644 --- a/apps/api/package.json +++ b/apps/api/package.json @@ -11,6 +11,7 @@ "test": "vitest run" }, "dependencies": { + "@fastify/compress": "^8.3.1", "@fastify/cors": "^11.0.0", "@fastify/helmet": "^13.0.2", "@fastify/rate-limit": "^10.3.0", diff --git a/apps/api/src/main.ts b/apps/api/src/main.ts index e404092..c79426c 100644 --- a/apps/api/src/main.ts +++ b/apps/api/src/main.ts @@ -2,6 +2,7 @@ import Fastify from "fastify"; import cors from "@fastify/cors"; import helmet from "@fastify/helmet"; import rateLimit from "@fastify/rate-limit"; +import compress from "@fastify/compress"; import { redisPlugin } from "./plugins/redis.js"; import { cachePlugin } from "./plugins/cache.js"; import { paginationPlugin } from "./plugins/pagination.js"; @@ -44,6 +45,7 @@ async function main() { await app.register(cors, { origin: CORS_ORIGINS, methods: ["GET", "HEAD", "OPTIONS", "DELETE"] }); await app.register(helmet, { contentSecurityPolicy: false }); // CSP handled by Next.js frontend + await app.register(compress, { global: true, threshold: 1024, encodings: ["br", "gzip"] }); await app.register(rateLimit, { max: parseInt(process.env.RATE_LIMIT_MAX ?? "100", 10), timeWindow: "1 minute", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index def6631..1b865fa 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -20,6 +20,9 @@ importers: apps/api: dependencies: + '@fastify/compress': + specifier: ^8.3.1 + version: 8.3.1 '@fastify/cors': specifier: ^11.0.0 version: 11.2.0 @@ -471,6 +474,9 @@ packages: '@fastify/ajv-compiler@4.0.5': resolution: {integrity: sha512-KoWKW+MhvfTRWL4qrhUwAAZoaChluo0m0vbiJlGMt2GXvL4LVPQEjt8kSpHI3IBq5Rez8fg+XeH3cneztq+C7A==} + '@fastify/compress@8.3.1': + resolution: {integrity: sha512-BUpItLr6MUX9e9ukg5Y6xekyA/7pBFG8QWtFCrUDm9ctoBc3R2/nA16yOaOWtVoccpXGjdDEYA/MxAb5+8cxag==} + '@fastify/cors@11.2.0': resolution: {integrity: sha512-LbLHBuSAdGdSFZYTLVA3+Ch2t+sA6nq3Ejc6XLAKiQ6ViS2qFnvicpj0htsx03FyYeLs04HfRNBsz/a8SvbcUw==} @@ -1281,6 +1287,10 @@ packages: '@vitest/utils@4.1.3': resolution: {integrity: sha512-Pc/Oexse/khOWsGB+w3q4yzA4te7W4gpZZAvk+fr8qXfTURZUMj5i7kuxsNK5mP/dEB6ao3jfr0rs17fHhbHdw==} + abort-controller@3.0.0: + resolution: {integrity: sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==} + engines: {node: '>=6.5'} + abstract-logging@2.0.1: resolution: {integrity: sha512-2BjRTZxTPvheOvGbBslFSYOUkr+SjPtOnrLP33f+VIWLzezQpZcqVg7ja3L4dBXmzzgwT+a029jRx5PCi3JuiA==} @@ -1324,6 +1334,9 @@ packages: resolution: {integrity: sha512-BLrgEcRTwX2o6gGxGOCNyMvGSp35YofuYzw9h1IMTRmKqttAZZVU67bdb9Pr2vUHA8+j3i2tJfjO6C6+4myGTA==} engines: {node: 18 || 20 || >=22} + base64-js@1.5.1: + resolution: {integrity: sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==} + baseline-browser-mapping@2.10.16: resolution: {integrity: sha512-Lyf3aK28zpsD1yQMiiHD4RvVb6UdMoo8xzG2XzFIfR9luPzOpcBlAsT/qfB1XWS1bxWT+UtE4WmQgsp297FYOA==} engines: {node: '>=6.0.0'} @@ -1333,6 +1346,12 @@ packages: resolution: {integrity: sha512-VZznLgtwhn+Mact9tfiwx64fA9erHH/MCXEUfB/0bX/6Fz6ny5EGTXYltMocqg4xFAQZtnO3DHWWXi8RiuN7cQ==} engines: {node: 18 || 20 || >=22} + buffer-from@1.1.2: + resolution: {integrity: sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==} + + buffer@6.0.3: + resolution: {integrity: sha512-FTiCpNxtwiZZHEZbcbTIcZjERVICn9yq/pDFkTl95/AxzD1naBctN7YO68riM/gLSDY7sdrMby8hofADYuuqOA==} + call-bind-apply-helpers@1.0.2: resolution: {integrity: sha512-Sp1ablJ0ivDkSzjcaJdxEunN5/XvksFJ2sMBFfq6x0ryhQV/2b/KwFe21cMpmHtPOSij8K99/wSfoEuTObmuMQ==} engines: {node: '>= 0.4'} @@ -1387,6 +1406,9 @@ packages: resolution: {integrity: sha512-ei8Aos7ja0weRpFzJnEA9UHJ/7XQmqglbRwnf2ATjcB9Wq874VKH9kfjjirM6UhU2/E5fFYadylyhFldcqSidQ==} engines: {node: '>=18'} + core-util-is@1.0.3: + resolution: {integrity: sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==} + csstype@3.2.3: resolution: {integrity: sha512-z1HGKcYy2xA8AGQfwrn0PAy+PB7X/GSj3UVJW9qKyn43xWa+gl5nXmU4qqLMRzWVLFC8KusUX8T/0kCiOYpAIQ==} @@ -1476,6 +1498,12 @@ packages: resolution: {integrity: sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A==} engines: {node: '>= 0.4'} + duplexify@3.7.1: + resolution: {integrity: sha512-07z8uv2wMyS51kKhD1KsdXJg5WQ6t93RneqRxUHnskXVtlYYkLqM0gqStQZ3pj073g687jPCHrqNfCzawLYh5g==} + + duplexify@4.1.3: + resolution: {integrity: sha512-M3BmBhwJRZsSx38lZyhE53Csddgzl5R7xGJNk7CVddZD6CcmwMCH8J+7AprIrQKH7TonKxaCjcv27Qmf+sQ+oA==} + emoji-regex@8.0.0: resolution: {integrity: sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==} @@ -1528,9 +1556,17 @@ packages: estree-walker@3.0.3: resolution: {integrity: sha512-7RUKfXgSMMkzt6ZuXmqapOurLGPPfgj6l9uRZ7lRGolvk0y2yocc35LdcxKC5PQZdn2DMqioAQ2NoWcrTKmm6g==} + event-target-shim@5.0.1: + resolution: {integrity: sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==} + engines: {node: '>=6'} + eventemitter3@5.0.4: resolution: {integrity: sha512-mlsTRyGaPBjPedk6Bvw+aqbsXDtoAyAzm5MO7JgU+yVRyMQ5O8bD4Kcci7BS85f93veegeCPkL8R4GLClnjLFw==} + events@3.3.0: + resolution: {integrity: sha512-mQw+2fkQbALzQ7V0MY0IqdnXNOeTtP4r0lN9z7AAawCXgqea7bDii20AYrIBrFd/Hx0M2Ocz6S111CaFkUcb0Q==} + engines: {node: '>=0.8.x'} + expect-type@1.3.0: resolution: {integrity: sha512-knvyeauYhqjOYvQ66MznSMs83wmHrCycNEN6Ao+2AeYEfxUIkuiVxdEa1qlGEPK+We3n0THiDciYSsCcgW/DoA==} engines: {node: '>=12.0.0'} @@ -1648,6 +1684,9 @@ packages: resolution: {integrity: sha512-4FbRdAX+bSdmo4AUFuS0WNiPz8NgFt+r8ThgNWmlrjQjt1Q7ZR9+zTlce2859x4KSXrwIsaeTqDoKQmtP8pLmQ==} engines: {node: '>= 0.8'} + ieee754@1.2.1: + resolution: {integrity: sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==} + immer@10.2.0: resolution: {integrity: sha512-d/+XTN3zfODyjr89gM3mPq1WNX2B8pYsu7eORitdwyA2sBubnTl3laYlBk4sXY5FUa5qTZGBDPJICVbvqzjlbw==} @@ -1673,6 +1712,9 @@ packages: resolution: {integrity: sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==} engines: {node: '>=8'} + isarray@1.0.0: + resolution: {integrity: sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==} + jiti@2.6.1: resolution: {integrity: sha512-ekilCSN1jwRvIbgeg/57YFh8qQDNbwDb9xT/qu2DAHbFFZUicIl4ygVaAvzveMhMVr3LnpSKTNnwt8PoOfmKhQ==} hasBin: true @@ -1869,6 +1911,9 @@ packages: pathe@2.0.3: resolution: {integrity: sha512-WUjGcAqP1gQacoQe+OBJsFA7Ld4DyXuUIjZ5cc75cLHvJ7dtNsTugphxIADwspS+AraAUePCKrSVtPLFj/F88w==} + peek-stream@1.1.3: + resolution: {integrity: sha512-FhJ+YbOSBb9/rIl2ZeE/QHEsWn7PqNYt8ARAY3kIgNGOk13g9FGyIY6JIl/xB/3TFRVoTv5as0l11weORrTekA==} + picocolors@1.1.1: resolution: {integrity: sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA==} @@ -1902,12 +1947,19 @@ packages: resolution: {integrity: sha512-GD3qdB0x1z9xgFI6cdRD6xu2Sp2WCOEoe3mtnyB5Ee0XrrL5Pe+e4CCnJrRMnL1zYtRDZmQQVbvOttLnKDLnaw==} engines: {node: '>=12'} + process-nextick-args@2.0.1: + resolution: {integrity: sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==} + process-warning@4.0.1: resolution: {integrity: sha512-3c2LzQ3rY9d0hc1emcsHhfT9Jwz0cChib/QN89oME2R451w5fy3f0afAhERFZAwrbDU43wk12d0ORBpDVME50Q==} process-warning@5.0.0: resolution: {integrity: sha512-a39t9ApHNx2L4+HBnQKqxxHNs1r7KF+Intd8Q/g1bUh6q0WIp9voPXJ/x0j+ZL45KF1pJd9+q2jLIRMfvEshkA==} + process@0.11.10: + resolution: {integrity: sha512-cdGef/drWFoydD1JsMzuFf8100nZl+GT+yacc2bEced5f9Rjk4z+WtFUTBu9PhOi9j/jfmBPu0mMEY4wIdAF8A==} + engines: {node: '>= 0.6.0'} + protobufjs@7.5.4: resolution: {integrity: sha512-CvexbZtbov6jW2eXAvLukXjXUW1TzFaivC46BpWc/3BpcCysb5Vffu+B3XHMm8lVEuy2Mm4XGex8hBSg1yapPg==} engines: {node: '>=12.0.0'} @@ -1919,6 +1971,9 @@ packages: pump@3.0.4: resolution: {integrity: sha512-VS7sjc6KR7e1ukRFhQSY5LM2uBWAUPiOPa/A3mkKmiMwSmRFUITt0xuj+/lesgnCv+dPIEYlkzrcyXgquIHMcA==} + pumpify@2.0.1: + resolution: {integrity: sha512-m7KOje7jZxrmutanlkS1daj1dS6z6BgslzOXmcSEpIlCxM3VJH7lG5QLeck/6hgF6F4crFf01UtQmNsJfweTAw==} + quick-format-unescaped@4.0.4: resolution: {integrity: sha512-tYC1Q1hgyRuHgloV/YXs2w15unPVh8qfu/qCTfhTYamaw7fyhumKa2yGpdSo87vY32rIclj+4fWYQXUMs9EHvg==} @@ -1946,6 +2001,17 @@ packages: resolution: {integrity: sha512-9nfp2hYpCwOjAN+8TZFGhtWEwgvWHXqESH8qT89AT/lWklpLON22Lc8pEtnpsZz7VmawabSU0gCjnj8aC0euHQ==} engines: {node: '>=0.10.0'} + readable-stream@2.3.8: + resolution: {integrity: sha512-8p0AUk4XODgIewSi0l8Epjs+EVnWiK7NoDIEGU0HhE7+ZyY8D1IMY7odu5lRrFXGg71L15KG8QrPmum45RTtdA==} + + readable-stream@3.6.2: + resolution: {integrity: sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA==} + engines: {node: '>= 6'} + + readable-stream@4.7.0: + resolution: {integrity: sha512-oIGGmcpTLwPga8Bn6/Z75SVaH1z5dUut2ibSyAMVhmUggWpmDn2dapB0n7f8nwaSiRtepAsfJyfXIO5DCVAODg==} + engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} + real-require@0.2.0: resolution: {integrity: sha512-57frrGM/OCTLqLOAh0mhVA9VBMHd+9U7Zb2THMGdBUoZVOtGbJzjxsYGDJ3A9AYYCP4hn6y1TVbaOfzWtm5GFg==} engines: {node: '>= 12.13.0'} @@ -2004,6 +2070,12 @@ packages: engines: {node: '>=18.0.0', npm: '>=8.0.0'} hasBin: true + safe-buffer@5.1.2: + resolution: {integrity: sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==} + + safe-buffer@5.2.1: + resolution: {integrity: sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==} + safe-regex2@5.1.0: resolution: {integrity: sha512-pNHAuBW7TrcleFHsxBr5QMi/Iyp0ENjUKz7GCcX1UO7cMh+NmVK6HxQckNL1tJp1XAJVjG6B8OKIPqodqj9rtw==} hasBin: true @@ -2060,10 +2132,19 @@ packages: std-env@4.0.0: resolution: {integrity: sha512-zUMPtQ/HBY3/50VbpkupYHbRroTRZJPRLvreamgErJVys0ceuzMkD44J/QjqhHjOzK42GQ3QZIeFG1OYfOtKqQ==} + stream-shift@1.0.3: + resolution: {integrity: sha512-76ORR0DO1o1hlKwTbi/DM3EXWGf3ZJYO8cXX5RJwnul2DEg2oyoZyjLNoQM8WsvZiFKCRfC1O0J7iCvie3RZmQ==} + string-width@4.2.3: resolution: {integrity: sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==} engines: {node: '>=8'} + string_decoder@1.1.1: + resolution: {integrity: sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==} + + string_decoder@1.3.0: + resolution: {integrity: sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==} + strip-ansi@6.0.1: resolution: {integrity: sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==} engines: {node: '>=8'} @@ -2104,6 +2185,9 @@ packages: resolution: {integrity: sha512-4iMVL6HAINXWf1ZKZjIPcz5wYaOdPhtO8ATvZ+Xqp3BTdaqtAwQkNmKORqcIo5YkQqGXq5cwfswDwMqqQNrpJA==} engines: {node: '>=20'} + through2@2.0.5: + resolution: {integrity: sha512-/mrRod8xqpA+IHSLyGCQ2s8SPHiCDEeQJSep1jqLYeEUClOFG2Qsh+4FU6G9VeqpZnGW/Su8LQGc4YKni5rYSQ==} + tiny-invariant@1.3.3: resolution: {integrity: sha512-+FbBPE1o9QAYvviau/qC5SE3caw21q3xkvWKBtja5vgqOWIHHJ3ioaq1VPfn/Szqctz2bU/oYeKd9/z5BL+PVg==} @@ -2151,6 +2235,9 @@ packages: peerDependencies: react: ^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0 + util-deprecate@1.0.2: + resolution: {integrity: sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==} + victory-vendor@37.3.6: resolution: {integrity: sha512-SbPDPdDBYp+5MJHhBCAyI7wKM3d5ivekigc2Dk2s7pgbZ9wIgIBYGVw4zGHBml/qTFbexrofXW6Gu4noGxrOwQ==} @@ -2247,6 +2334,10 @@ packages: wrappy@1.0.2: resolution: {integrity: sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==} + xtend@4.0.2: + resolution: {integrity: sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==} + engines: {node: '>=0.4'} + y18n@5.0.8: resolution: {integrity: sha512-0pfFzegeDWJHJIAmTLRP2DwHjdF5s7jo9tuztdQxAhINCdvS+3nGINqPd00AphqJR/0LhANUS6/+7SCb98YOfA==} engines: {node: '>=10'} @@ -2437,6 +2528,17 @@ snapshots: ajv-formats: 3.0.1(ajv@8.18.0) fast-uri: 3.1.0 + '@fastify/compress@8.3.1': + dependencies: + '@fastify/accept-negotiator': 2.0.1 + fastify-plugin: 5.1.0 + mime-db: 1.52.0 + minipass: 7.1.3 + peek-stream: 1.1.3 + pump: 3.0.4 + pumpify: 2.0.1 + readable-stream: 4.7.0 + '@fastify/cors@11.2.0': dependencies: fastify-plugin: 5.1.0 @@ -3082,6 +3184,10 @@ snapshots: convert-source-map: 2.0.0 tinyrainbow: 3.1.0 + abort-controller@3.0.0: + dependencies: + event-target-shim: 5.0.1 + abstract-logging@2.0.1: {} ajv-formats@3.0.1(ajv@8.18.0): @@ -3122,12 +3228,21 @@ snapshots: balanced-match@4.0.4: {} + base64-js@1.5.1: {} + baseline-browser-mapping@2.10.16: {} brace-expansion@5.0.5: dependencies: balanced-match: 4.0.4 + buffer-from@1.1.2: {} + + buffer@6.0.3: + dependencies: + base64-js: 1.5.1 + ieee754: 1.2.1 + call-bind-apply-helpers@1.0.2: dependencies: es-errors: 1.3.0 @@ -3171,6 +3286,8 @@ snapshots: cookie@1.1.1: {} + core-util-is@1.0.3: {} + csstype@3.2.3: {} d3-array@3.2.4: @@ -3237,6 +3354,20 @@ snapshots: es-errors: 1.3.0 gopd: 1.2.0 + duplexify@3.7.1: + dependencies: + end-of-stream: 1.4.5 + inherits: 2.0.4 + readable-stream: 2.3.8 + stream-shift: 1.0.3 + + duplexify@4.1.3: + dependencies: + end-of-stream: 1.4.5 + inherits: 2.0.4 + readable-stream: 3.6.2 + stream-shift: 1.0.3 + emoji-regex@8.0.0: {} end-of-stream@1.4.5: @@ -3333,8 +3464,12 @@ snapshots: dependencies: '@types/estree': 1.0.8 + event-target-shim@5.0.1: {} + eventemitter3@5.0.4: {} + events@3.3.0: {} + expect-type@1.3.0: {} fast-copy@4.0.2: {} @@ -3465,6 +3600,8 @@ snapshots: statuses: 2.0.2 toidentifier: 1.0.1 + ieee754@1.2.1: {} + immer@10.2.0: {} immer@11.1.4: {} @@ -3491,6 +3628,8 @@ snapshots: is-fullwidth-code-point@3.0.0: {} + isarray@1.0.0: {} + jiti@2.6.1: {} joycon@3.1.1: {} @@ -3645,6 +3784,12 @@ snapshots: pathe@2.0.3: {} + peek-stream@1.1.3: + dependencies: + buffer-from: 1.1.2 + duplexify: 3.7.1 + through2: 2.0.5 + picocolors@1.1.1: {} picomatch@4.0.4: {} @@ -3699,10 +3844,14 @@ snapshots: postgres@3.4.9: {} + process-nextick-args@2.0.1: {} + process-warning@4.0.1: {} process-warning@5.0.0: {} + process@0.11.10: {} + protobufjs@7.5.4: dependencies: '@protobufjs/aspromise': 1.1.2 @@ -3725,6 +3874,12 @@ snapshots: end-of-stream: 1.4.5 once: 1.4.0 + pumpify@2.0.1: + dependencies: + duplexify: 4.1.3 + inherits: 2.0.4 + pump: 3.0.4 + quick-format-unescaped@4.0.4: {} react-dom@19.2.4(react@19.2.4): @@ -3745,6 +3900,30 @@ snapshots: react@19.2.4: {} + readable-stream@2.3.8: + dependencies: + core-util-is: 1.0.3 + inherits: 2.0.4 + isarray: 1.0.0 + process-nextick-args: 2.0.1 + safe-buffer: 5.1.2 + string_decoder: 1.1.1 + util-deprecate: 1.0.2 + + readable-stream@3.6.2: + dependencies: + inherits: 2.0.4 + string_decoder: 1.3.0 + util-deprecate: 1.0.2 + + readable-stream@4.7.0: + dependencies: + abort-controller: 3.0.0 + buffer: 6.0.3 + events: 3.3.0 + process: 0.11.10 + string_decoder: 1.3.0 + real-require@0.2.0: {} recharts@3.8.1(@types/react@19.2.14)(react-dom@19.2.4(react@19.2.4))(react-is@18.3.1)(react@19.2.4)(redux@5.0.1): @@ -3824,6 +4003,10 @@ snapshots: '@rollup/rollup-win32-x64-msvc': 4.60.0 fsevents: 2.3.3 + safe-buffer@5.1.2: {} + + safe-buffer@5.2.1: {} + safe-regex2@5.1.0: dependencies: ret: 0.5.0 @@ -3890,12 +4073,22 @@ snapshots: std-env@4.0.0: {} + stream-shift@1.0.3: {} + string-width@4.2.3: dependencies: emoji-regex: 8.0.0 is-fullwidth-code-point: 3.0.0 strip-ansi: 6.0.1 + string_decoder@1.1.1: + dependencies: + safe-buffer: 5.1.2 + + string_decoder@1.3.0: + dependencies: + safe-buffer: 5.2.1 + strip-ansi@6.0.1: dependencies: ansi-regex: 5.0.1 @@ -3923,6 +4116,11 @@ snapshots: dependencies: real-require: 0.2.0 + through2@2.0.5: + dependencies: + readable-stream: 2.3.8 + xtend: 4.0.2 + tiny-invariant@1.3.3: {} tinybench@2.9.0: {} @@ -3957,6 +4155,8 @@ snapshots: dependencies: react: 19.2.4 + util-deprecate@1.0.2: {} + victory-vendor@37.3.6: dependencies: '@types/d3-array': 3.2.2 @@ -4030,6 +4230,8 @@ snapshots: wrappy@1.0.2: {} + xtend@4.0.2: {} + y18n@5.0.8: {} yaml@2.8.3: {} From 5a018317d2707fccee653be2163596968df87150 Mon Sep 17 00:00:00 2001 From: Nimrod Teich Date: Thu, 16 Apr 2026 23:04:18 +0300 Subject: [PATCH 02/25] refactor(api): drop dead exports in rpc/lava.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Delete fetchProviderRewardPoolsAmount — zero callers anywhere. - Delete fetchLavaUsdPriceAt — zero callers anywhere. - Unexport fetchStakingPool — only used inside the module. - Unexport fetchBondedValidators — only used inside the module. --- apps/api/src/rpc/lava.ts | 27 ++------------------------- 1 file changed, 2 insertions(+), 25 deletions(-) diff --git a/apps/api/src/rpc/lava.ts b/apps/api/src/rpc/lava.ts index 3f7ac65..1c0f571 100644 --- a/apps/api/src/rpc/lava.ts +++ b/apps/api/src/rpc/lava.ts @@ -153,10 +153,6 @@ const PROVIDER_REWARD_POOLS = [ ]; /** Fetch total ulava in provider-side reward pools (excludes validator pools). */ -export async function fetchProviderRewardPoolsAmount(): Promise { - return sumPoolsUlava(await fetchRewardPools(), PROVIDER_REWARD_POOLS); -} - interface VestingStats { continuousVesting: bigint; periodicVesting: bigint; @@ -363,7 +359,7 @@ export async function fetchAllSpecs(): Promise ({ index: c.chainID, name: chainDisplayName(c.chainID, c.chainName) })); } -export async function fetchStakingPool(): Promise<{ bonded_tokens: string; not_bonded_tokens: string }> { +async function fetchStakingPool(): Promise<{ bonded_tokens: string; not_bonded_tokens: string }> { const data = await fetchRest<{ pool: { bonded_tokens: string; not_bonded_tokens: string } }>( "/cosmos/staking/v1beta1/pool", ); @@ -393,25 +389,6 @@ export async function fetchLavaUsdPrice(): Promise { return price; } -// Fetch LAVA USD price at a specific date via CoinGecko /coins/{id}/history. -// date is a Date object; CoinGecko expects dd-mm-yyyy format. -export async function fetchLavaUsdPriceAt(date: Date): Promise { - const dd = String(date.getUTCDate()).padStart(2, "0"); - const mm = String(date.getUTCMonth() + 1).padStart(2, "0"); - const yyyy = date.getUTCFullYear(); - const res = await fetch( - `${COINGECKO_API_URL}/coins/lava-network/history?date=${dd}-${mm}-${yyyy}&localization=false`, - { signal: AbortSignal.timeout(15_000) }, - ); - if (!res.ok) throw new Error(`CoinGecko ${res.status}`); - const data = (await res.json()) as { - market_data?: { current_price?: { usd?: number } }; - }; - const price = data.market_data?.current_price?.usd; - if (!price || price <= 0) throw new Error(`No LAVA price for ${yyyy}-${mm}-${dd}`); - return price; -} - async function fetchTvlRewardPoolsUlava(): Promise { const data = await fetchRest<{ pools: Array<{ name: string; balance: Array<{ denom: string; amount: string }> }>; @@ -1129,7 +1106,7 @@ export async function fetchValidatorsWithRewards(): Promise<{ } /** Fetch all bonded validator operator addresses */ -export async function fetchBondedValidators(): Promise { +async function fetchBondedValidators(): Promise { const validators: string[] = []; let nextKey: string | null = null; do { From b3d6f49154646f223cfae50c9211dfc1344fe722 Mon Sep 17 00:00:00 2001 From: Nimrod Teich Date: Thu, 16 Apr 2026 23:07:08 +0300 Subject: [PATCH 03/25] refactor: share strict parseYMD date parser MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit optimizer-metrics used new Date(query.from) which accepts ISO, RFC-2822, and timezone-spec strings — much looser than the YYYY-MM-DD the route advertises. provider-rewards already had a strict parser; promote it to @info/shared and use it in both routes. Invalid dates now return 400. Includes tests covering calendar-invalid dates (Feb 30, month 13, day 0) and leap-year handling. --- apps/api/src/routes/optimizer-metrics.ts | 15 +++++++--- apps/api/src/routes/provider-rewards.ts | 23 ++------------- packages/shared/src/__tests__/dates.test.ts | 31 ++++++++++++++++++++- packages/shared/src/utils/dates.ts | 23 +++++++++++++++ 4 files changed, 67 insertions(+), 25 deletions(-) diff --git a/apps/api/src/routes/optimizer-metrics.ts b/apps/api/src/routes/optimizer-metrics.ts index 805078c..28daee7 100644 --- a/apps/api/src/routes/optimizer-metrics.ts +++ b/apps/api/src/routes/optimizer-metrics.ts @@ -1,4 +1,5 @@ import type { FastifyInstance } from "fastify"; +import { parseYMD } from "@info/shared/utils"; interface MetricRow { hourly_timestamp: Date; @@ -161,8 +162,11 @@ export async function optimizerMetricsRoutes(app: FastifyInstance) { try { const { addr } = request.params as { addr: string }; const query = request.query as { from?: string; to?: string; consumer?: string; chain_id?: string }; - const from = clampFrom(query.from ? new Date(query.from) : defaultFrom()); - const to = query.to ? new Date(query.to) : new Date(); + const fromRaw = query.from ? parseYMD(query.from) : defaultFrom(); + if (!fromRaw) return reply.status(400).send({ error: "invalid from (expected YYYY-MM-DD)" }); + const to = query.to ? parseYMD(query.to) : new Date(); + if (!to) return reply.status(400).send({ error: "invalid to (expected YYYY-MM-DD)" }); + const from = clampFrom(fromRaw); const conditions = [ app.relaysDb`provider = ${addr}`, @@ -227,8 +231,11 @@ export async function optimizerMetricsRoutes(app: FastifyInstance) { try { const { specId } = request.params as { specId: string }; const query = request.query as { from?: string; to?: string; consumer?: string }; - const from = clampFrom(query.from ? new Date(query.from) : defaultFrom()); - const to = query.to ? new Date(query.to) : new Date(); + const fromRaw = query.from ? parseYMD(query.from) : defaultFrom(); + if (!fromRaw) return reply.status(400).send({ error: "invalid from (expected YYYY-MM-DD)" }); + const to = query.to ? parseYMD(query.to) : new Date(); + if (!to) return reply.status(400).send({ error: "invalid to (expected YYYY-MM-DD)" }); + const from = clampFrom(fromRaw); const conditions = [ app.relaysDb`chain = ${specId}`, diff --git a/apps/api/src/routes/provider-rewards.ts b/apps/api/src/routes/provider-rewards.ts index 3ec54be..3c87c75 100644 --- a/apps/api/src/routes/provider-rewards.ts +++ b/apps/api/src/routes/provider-rewards.ts @@ -1,11 +1,11 @@ import type { FastifyInstance } from "fastify"; +import { parseYMD } from "@info/shared/utils"; import { gqlSafe } from "../graphql/client.js"; import { fetchProvidersForSpec, fetchAllProviders } from "../rpc/lava.js"; // ── Helpers ────────────────────────────────────────────────────────────────── const SPEC_ID_RE = /^[a-zA-Z][a-zA-Z0-9_-]*$/; -const DATE_RE = /^(\d{4})-(\d{2})-(\d{2})$/; const MAX_RANGE_MS = 6 * 30 * 24 * 60 * 60 * 1000; // ~6 months const ONE_DAY_MS = 24 * 60 * 60 * 1000; const PAST_WINDOW_TTL = 86_400; // 1 day — immutable historical window @@ -15,23 +15,6 @@ function validateSpecId(s: string): boolean { return s.length > 2 && s.length <= 50 && SPEC_ID_RE.test(s); } -// Parses YYYY-MM-DD strictly. Rejects calendar-invalid dates like 2025-02-30 -// (which `new Date()` would silently roll over to 2025-03-02). -function parseStrictDate(s: string): Date | null { - const m = DATE_RE.exec(s); - if (!m) return null; - const y = Number(m[1]); - const mo = Number(m[2]); - const d = Number(m[3]); - if (mo < 1 || mo > 12 || d < 1 || d > 31) return null; - const ts = Date.UTC(y, mo - 1, d); - const rt = new Date(ts); - if (rt.getUTCFullYear() !== y || rt.getUTCMonth() !== mo - 1 || rt.getUTCDate() !== d) { - return null; - } - return rt; -} - function computeAdjustedRewards( avgLat: number, avgAvail: number, avgSync: number, qosCus: number, ): number { @@ -155,9 +138,9 @@ export async function providerRewardsRoutes(app: FastifyInstance) { } // ── Validate & normalize dates ───────────────────────────────── - let dateFrom = parseStrictDate(q.from); + let dateFrom = parseYMD(q.from); if (!dateFrom) return reply.status(400).send({ error: "Error - bad from date format" }); - let dateTo = parseStrictDate(q.to); + let dateTo = parseYMD(q.to); if (!dateTo) return reply.status(400).send({ error: "Error - bad to date format" }); if (dateTo < dateFrom) { diff --git a/packages/shared/src/__tests__/dates.test.ts b/packages/shared/src/__tests__/dates.test.ts index 6eae3f8..6256ddd 100644 --- a/packages/shared/src/__tests__/dates.test.ts +++ b/packages/shared/src/__tests__/dates.test.ts @@ -1,5 +1,5 @@ import { describe, it, expect } from "vitest"; -import { formatDateTime, formatShortDate } from "../utils/dates.js"; +import { formatDateTime, formatShortDate, parseYMD } from "../utils/dates.js"; describe("formatDateTime", () => { it("formats Date objects", () => { @@ -30,3 +30,32 @@ describe("formatShortDate", () => { expect(result).toContain("25"); }); }); + +describe("parseYMD", () => { + it("parses valid YYYY-MM-DD as UTC midnight", () => { + const d = parseYMD("2025-06-17"); + expect(d).not.toBeNull(); + expect(d!.toISOString()).toBe("2025-06-17T00:00:00.000Z"); + }); + + it("rejects non-matching formats", () => { + expect(parseYMD("2025/06/17")).toBeNull(); + expect(parseYMD("2025-06-17T00:00:00Z")).toBeNull(); + expect(parseYMD("06-17-2025")).toBeNull(); + expect(parseYMD("")).toBeNull(); + expect(parseYMD("junk")).toBeNull(); + }); + + it("rejects calendar-invalid dates", () => { + expect(parseYMD("2025-02-30")).toBeNull(); // no Feb 30 + expect(parseYMD("2025-13-01")).toBeNull(); // no month 13 + expect(parseYMD("2025-00-01")).toBeNull(); // no month 0 + expect(parseYMD("2025-06-32")).toBeNull(); // no day 32 + expect(parseYMD("2025-06-00")).toBeNull(); // no day 0 + }); + + it("accepts Feb 29 on leap years only", () => { + expect(parseYMD("2024-02-29")).not.toBeNull(); + expect(parseYMD("2025-02-29")).toBeNull(); + }); +}); diff --git a/packages/shared/src/utils/dates.ts b/packages/shared/src/utils/dates.ts index 880536f..090177e 100644 --- a/packages/shared/src/utils/dates.ts +++ b/packages/shared/src/utils/dates.ts @@ -11,3 +11,26 @@ export function formatShortDate(date: Date | string): string { const d = typeof date === "string" ? new Date(date) : date; return format(d, "MMM d, yyyy"); } + +const YMD_RE = /^(\d{4})-(\d{2})-(\d{2})$/; + +/** + * Parses a YYYY-MM-DD string strictly as a UTC Date. + * Rejects non-matching formats and calendar-invalid dates like 2025-02-30 + * (which `new Date()` would silently roll over to 2025-03-02). + * Returns null on any parse failure. + */ +export function parseYMD(s: string): Date | null { + const m = YMD_RE.exec(s); + if (!m) return null; + const y = Number(m[1]); + const mo = Number(m[2]); + const d = Number(m[3]); + if (mo < 1 || mo > 12 || d < 1 || d > 31) return null; + const ts = Date.UTC(y, mo - 1, d); + const rt = new Date(ts); + if (rt.getUTCFullYear() !== y || rt.getUTCMonth() !== mo - 1 || rt.getUTCDate() !== d) { + return null; + } + return rt; +} From 3e4e4b88ec370f5064ba8f671738e6589a3e6c6a Mon Sep 17 00:00:00 2001 From: Nimrod Teich Date: Thu, 16 Apr 2026 23:09:26 +0300 Subject: [PATCH 04/25] refactor: share weightedQos helper across index + providers routes Both /index/charts and /providers/:addr/charts computed QoS from weighted-sum / weight. Extract weightedQos to @info/shared/utils so there's one source of truth for the formula, including the excellence-QoS variant (same shape, different weight). Includes unit tests for zero/negative weight and null component sums. --- apps/api/src/routes/index.ts | 5 ++-- apps/api/src/routes/providers.ts | 17 ++++++----- packages/shared/src/__tests__/qos.test.ts | 36 +++++++++++++++++++++++ packages/shared/src/utils/index.ts | 1 + packages/shared/src/utils/qos.ts | 22 ++++++++++++++ 5 files changed, 70 insertions(+), 11 deletions(-) create mode 100644 packages/shared/src/__tests__/qos.test.ts create mode 100644 packages/shared/src/utils/qos.ts diff --git a/apps/api/src/routes/index.ts b/apps/api/src/routes/index.ts index 1d80e05..64c2683 100644 --- a/apps/api/src/routes/index.ts +++ b/apps/api/src/routes/index.ts @@ -1,4 +1,5 @@ import type { FastifyInstance } from "fastify"; +import { weightedQos } from "@info/shared/utils"; import { gqlSafe } from "../graphql/client.js"; import { fetchLatestBlockHeight, fetchAllProviders } from "../rpc/lava.js"; @@ -160,9 +161,7 @@ export async function indexRoutes(app: FastifyInstance) { date, chainId, cu: v.cu.toString(), relays: v.relays.toString(), - qosSync: v.qosWeight > 0 ? v.qosSyncW / v.qosWeight : null, - qosAvailability: v.qosWeight > 0 ? v.qosAvailW / v.qosWeight : null, - qosLatency: v.qosWeight > 0 ? v.qosLatW / v.qosWeight : null, + ...weightedQos(v.qosSyncW, v.qosAvailW, v.qosLatW, v.qosWeight), }); } diff --git a/apps/api/src/routes/providers.ts b/apps/api/src/routes/providers.ts index 75243aa..39fca3d 100644 --- a/apps/api/src/routes/providers.ts +++ b/apps/api/src/routes/providers.ts @@ -1,4 +1,5 @@ import type { FastifyInstance } from "fastify"; +import { weightedQos } from "@info/shared/utils"; import { gqlSafe } from "../graphql/client.js"; import { fetchAllProviders, @@ -295,19 +296,19 @@ export async function providerRoutes(app: FastifyInstance) { return { data: data.mvRelayDailies.nodes.map((n) => { - const w = Number(n.qosWeight); - const ew = Number(n.exQosWeight); + const qos = weightedQos(n.qosSyncW, n.qosAvailW, n.qosLatencyW, Number(n.qosWeight)); + const ex = weightedQos(n.exQosSyncW, n.exQosAvailW, n.exQosLatencyW, Number(n.exQosWeight)); return { date: n.date, chainId: n.chainId, cu: n.cu, relays: n.relays, - qosSync: w > 0 ? (n.qosSyncW ?? 0) / w : null, - qosAvailability: w > 0 ? (n.qosAvailW ?? 0) / w : null, - qosLatency: w > 0 ? (n.qosLatencyW ?? 0) / w : null, - excellenceQosSync: ew > 0 ? (n.exQosSyncW ?? 0) / ew : null, - excellenceQosAvailability: ew > 0 ? (n.exQosAvailW ?? 0) / ew : null, - excellenceQosLatency: ew > 0 ? (n.exQosLatencyW ?? 0) / ew : null, + qosSync: qos.qosSync, + qosAvailability: qos.qosAvailability, + qosLatency: qos.qosLatency, + excellenceQosSync: ex.qosSync, + excellenceQosAvailability: ex.qosAvailability, + excellenceQosLatency: ex.qosLatency, }; }), }; diff --git a/packages/shared/src/__tests__/qos.test.ts b/packages/shared/src/__tests__/qos.test.ts new file mode 100644 index 0000000..2151c83 --- /dev/null +++ b/packages/shared/src/__tests__/qos.test.ts @@ -0,0 +1,36 @@ +import { describe, it, expect } from "vitest"; +import { weightedQos } from "../utils/qos.js"; + +describe("weightedQos", () => { + it("returns all nulls when weight is 0", () => { + expect(weightedQos(100, 50, 10, 0)).toEqual({ + qosSync: null, + qosAvailability: null, + qosLatency: null, + }); + }); + + it("returns all nulls when weight is negative", () => { + expect(weightedQos(100, 50, 10, -1)).toEqual({ + qosSync: null, + qosAvailability: null, + qosLatency: null, + }); + }); + + it("divides weighted sums by the weight", () => { + expect(weightedQos(500, 200, 50, 10)).toEqual({ + qosSync: 50, + qosAvailability: 20, + qosLatency: 5, + }); + }); + + it("treats null component sums as 0", () => { + expect(weightedQos(null, null, null, 10)).toEqual({ + qosSync: 0, + qosAvailability: 0, + qosLatency: 0, + }); + }); +}); diff --git a/packages/shared/src/utils/index.ts b/packages/shared/src/utils/index.ts index 6fbb5f4..3a474c1 100644 --- a/packages/shared/src/utils/index.ts +++ b/packages/shared/src/utils/index.ts @@ -1,3 +1,4 @@ export * from "./formatting.js"; export * from "./dates.js"; export * from "./geolocation.js"; +export * from "./qos.js"; diff --git a/packages/shared/src/utils/qos.ts b/packages/shared/src/utils/qos.ts new file mode 100644 index 0000000..45b6b9b --- /dev/null +++ b/packages/shared/src/utils/qos.ts @@ -0,0 +1,22 @@ +/** + * Compute relay-weighted QoS triple from MV (materialized-view) weighted sums. + * Returns nulls when weight is 0 (no relays = QoS is undefined). + * + * Matches the formula used across the explorer: `qosField = fieldW / weight`. + * Excellence QoS (`exQos*`) is computed the same way with its own weight. + */ +export function weightedQos( + syncW: number | null, + availW: number | null, + latencyW: number | null, + weight: number, +): { qosSync: number | null; qosAvailability: number | null; qosLatency: number | null } { + if (weight <= 0) { + return { qosSync: null, qosAvailability: null, qosLatency: null }; + } + return { + qosSync: (syncW ?? 0) / weight, + qosAvailability: (availW ?? 0) / weight, + qosLatency: (latencyW ?? 0) / weight, + }; +} From 8b29686be514619bd4ba71fcda62cb861aec17e3 Mon Sep 17 00:00:00 2001 From: Nimrod Teich Date: Thu, 16 Apr 2026 23:12:57 +0300 Subject: [PATCH 05/25] perf(api): share undici keep-alive dispatcher + add GQL timeout Every gql() call was opening a fresh TCP+TLS connection to the indexer; with 2-4 GraphQL calls per route this wasted 100-400ms on handshakes. Register a pooled undici Agent as the global dispatcher so all outbound fetch() (GraphQL + REST) benefit, and add AbortSignal.timeout() so slow indexer responses don't stack up requests on the event loop. INDEXER_TIMEOUT_MS env var (default 15000) controls the timeout. Pinned undici to ^6 because v8 requires Node >=20 for File() and the project still wants to support local dev on Node 18; prod runs Node 22+. --- apps/api/package.json | 3 ++- apps/api/src/graphql/client.ts | 14 ++++++++++++++ pnpm-lock.yaml | 9 +++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/apps/api/package.json b/apps/api/package.json index 6ad7679..4b1d424 100644 --- a/apps/api/package.json +++ b/apps/api/package.json @@ -24,7 +24,8 @@ "fastify-plugin": "^5.1.0", "ioredis": "^5.4.0", "pino": "^10.0.0", - "postgres": "^3.4.9" + "postgres": "^3.4.9", + "undici": "^6.25.0" }, "devDependencies": { "@types/node": "^25.5.0", diff --git a/apps/api/src/graphql/client.ts b/apps/api/src/graphql/client.ts index a396dce..c145def 100644 --- a/apps/api/src/graphql/client.ts +++ b/apps/api/src/graphql/client.ts @@ -1,13 +1,27 @@ import pino from "pino"; +import { Agent, setGlobalDispatcher } from "undici"; const logger = pino({ name: "graphql" }); const INDEXER_GRAPHQL_URL = process.env.INDEXER_GRAPHQL_URL ?? "http://localhost:3000"; +const GQL_TIMEOUT_MS = parseInt(process.env.INDEXER_TIMEOUT_MS ?? "15000", 10); + +// Shared connection pool — reuses TCP/TLS across outbound requests so each +// call saves the ~50-100ms handshake cost. Applies to all fetch() in the +// process via undici's global dispatcher. +setGlobalDispatcher( + new Agent({ + connections: 32, + keepAliveTimeout: 30_000, + keepAliveMaxTimeout: 60_000, + }), +); export async function gql(query: string, variables?: Record): Promise { const res = await fetch(INDEXER_GRAPHQL_URL, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ query, variables }), + signal: AbortSignal.timeout(GQL_TIMEOUT_MS), }); if (!res.ok) throw new Error(`GraphQL request failed: ${res.status}`); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1b865fa..5ad4a11 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -62,6 +62,9 @@ importers: postgres: specifier: ^3.4.9 version: 3.4.9 + undici: + specifier: ^6.25.0 + version: 6.25.0 devDependencies: '@types/node': specifier: ^25.5.0 @@ -2230,6 +2233,10 @@ packages: undici-types@7.18.2: resolution: {integrity: sha512-AsuCzffGHJybSaRrmr5eHr81mwJU3kjw6M+uprWvCXiNeN9SOGwQ3Jn8jb8m3Z6izVgknn1R0FTCEAP2QrLY/w==} + undici@6.25.0: + resolution: {integrity: sha512-ZgpWDC5gmNiuY9CnLVXEH8rl50xhRCuLNA97fAUnKi8RRuV4E6KG31pDTsLVUKnohJE0I3XDrTeEydAXRw47xg==} + engines: {node: '>=18.17'} + use-sync-external-store@1.6.0: resolution: {integrity: sha512-Pp6GSwGP/NrPIrxVFAIkOQeyw8lFenOHijQWkUTrDvrF4ALqylP2C/KCkeS9dpUM3KvYRQhna5vt7IL95+ZQ9w==} peerDependencies: @@ -4151,6 +4158,8 @@ snapshots: undici-types@7.18.2: {} + undici@6.25.0: {} + use-sync-external-store@1.6.0(react@19.2.4): dependencies: react: 19.2.4 From 214f23f5116ae64922cbd7c371e45ded30f43dce Mon Sep 17 00:00:00 2001 From: Nimrod Teich Date: Thu, 16 Apr 2026 23:14:25 +0300 Subject: [PATCH 06/25] perf(api): health-probe batches honor RPC_BATCH_SIZE MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Both buildProbeTargets (fetching providers per spec) and the probe loop itself were hardcoded to batches of 5 — stale since RPC_BATCH_SIZE was introduced (default 25). On dedicated RPC endpoints this becomes the bottleneck. Use the env-configured size instead. --- apps/api/src/plugins/health-probe.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/apps/api/src/plugins/health-probe.ts b/apps/api/src/plugins/health-probe.ts index 3c83b60..182cc27 100644 --- a/apps/api/src/plugins/health-probe.ts +++ b/apps/api/src/plugins/health-probe.ts @@ -1,7 +1,7 @@ import fp from "fastify-plugin"; import type { FastifyInstance } from "fastify"; import { normalizeBlock } from "@info/shared/constants"; -import { fetchAllSpecs, fetchProvidersForSpec } from "../rpc/lava.js"; +import { fetchAllSpecs, fetchProvidersForSpec, RPC_BATCH_SIZE } from "../rpc/lava.js"; import type { ProviderEndpoint } from "../rpc/lava.js"; import { probeProvider } from "../services/grpc-probe.js"; import { writeHealthStatus } from "../services/health-store.js"; @@ -17,9 +17,9 @@ async function buildProbeTargets(): Promise { const specs = await fetchAllSpecs(); const targets: ProbeTarget[] = []; - // Fetch in batches of 5 to avoid rate limiting - for (let i = 0; i < specs.length; i += 5) { - const batch = specs.slice(i, i + 5); + // Fetch in batches — raise RPC_BATCH_SIZE via env for dedicated RPC endpoints. + for (let i = 0; i < specs.length; i += RPC_BATCH_SIZE) { + const batch = specs.slice(i, i + RPC_BATCH_SIZE); const results = await Promise.all( batch.map((s) => fetchProvidersForSpec(s.index) @@ -113,10 +113,10 @@ export const healthProbePlugin = fp(async (app: FastifyInstance) => { const shuffled = shuffle(targets); app.log.info({ count: shuffled.length }, "Starting health probe cycle"); - // Probe in batches of 5 to balance throughput vs resource usage - for (let i = 0; i < shuffled.length; i += 5) { + // Probe in batches — raise RPC_BATCH_SIZE via env for dedicated endpoints. + for (let i = 0; i < shuffled.length; i += RPC_BATCH_SIZE) { if (!running) break; - const batch = shuffled.slice(i, i + 5); + const batch = shuffled.slice(i, i + RPC_BATCH_SIZE); await Promise.allSettled(batch.map(async (target) => { const geolocation = target.endpoint.geolocation.toString(); From de4650a026d1f038f7c5bd3dc09d664dc23f6077 Mon Sep 17 00:00:00 2001 From: Nimrod Teich Date: Thu, 16 Apr 2026 23:17:47 +0300 Subject: [PATCH 07/25] =?UTF-8?q?perf(api):=20cache=20address=E2=86=92moni?= =?UTF-8?q?ker=20map=20across=20reward=20routes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit /provider-rewards called fetchAllProviders() (which iterates every spec on chain) just to build a Map. Introduce a cached fetchAllProviderMonikers() in the rpc module — 60-second in-process TTL shared across routes. Routes that only need monikers skip re-enumeration when another route populated the cache in the last minute. /provider-rewards now uses the helper. Callers that need the full provider list (e.g. provider-claimable-rewards) keep fetchAllProviders. --- .../__tests__/routes-provider-rewards.test.ts | 14 +++++------ apps/api/src/routes/provider-rewards.ts | 20 ++++++---------- apps/api/src/rpc/lava.ts | 24 +++++++++++++++++++ 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/apps/api/src/__tests__/routes-provider-rewards.test.ts b/apps/api/src/__tests__/routes-provider-rewards.test.ts index 0ea7560..891eec6 100644 --- a/apps/api/src/__tests__/routes-provider-rewards.test.ts +++ b/apps/api/src/__tests__/routes-provider-rewards.test.ts @@ -9,11 +9,11 @@ vi.mock("../graphql/client.js", () => ({ vi.mock("../rpc/lava.js", () => ({ fetchProvidersForSpec: vi.fn(), - fetchAllProviders: vi.fn(), + fetchAllProviderMonikers: vi.fn(), })); const { gqlSafe } = await import("../graphql/client.js"); -const { fetchAllProviders } = await import("../rpc/lava.js"); +const { fetchAllProviderMonikers } = await import("../rpc/lava.js"); const { providerRewardsRoutes } = await import("../routes/provider-rewards.js"); async function buildApp() { @@ -82,15 +82,15 @@ const MOCK_MV_SPEC = { }, }; -const MOCK_PROVIDERS = [ - { address: "lava@1abc", moniker: "AlphaProvider" }, - { address: "lava@2def", moniker: "BetaProvider" }, -]; +const MOCK_MONIKER_MAP = new Map([ + ["lava@1abc", "AlphaProvider"], + ["lava@2def", "BetaProvider"], +]); beforeEach(() => { vi.resetAllMocks(); (gqlSafe as ReturnType).mockResolvedValue(MOCK_MV_PROVIDER); - (fetchAllProviders as ReturnType).mockResolvedValue(MOCK_PROVIDERS); + (fetchAllProviderMonikers as ReturnType).mockResolvedValue(MOCK_MONIKER_MAP); }); describe("GET /provider-rewards (default groupBy=provider)", () => { diff --git a/apps/api/src/routes/provider-rewards.ts b/apps/api/src/routes/provider-rewards.ts index 3c87c75..6c85f57 100644 --- a/apps/api/src/routes/provider-rewards.ts +++ b/apps/api/src/routes/provider-rewards.ts @@ -1,7 +1,7 @@ import type { FastifyInstance } from "fastify"; import { parseYMD } from "@info/shared/utils"; import { gqlSafe } from "../graphql/client.js"; -import { fetchProvidersForSpec, fetchAllProviders } from "../rpc/lava.js"; +import { fetchProvidersForSpec, fetchAllProviderMonikers } from "../rpc/lava.js"; // ── Helpers ────────────────────────────────────────────────────────────────── @@ -207,19 +207,13 @@ export async function providerRewardsRoutes(app: FastifyInstance) { }`, vars, { mvRelayDailies: { groupedAggregates: [] } }), (async () => { + if (!specs) return fetchAllProviderMonikers(); + // Spec-scoped: only need monikers for providers on the filtered specs. const map = new Map(); - if (specs) { - const fetches = await Promise.all( - specs.map((s) => fetchProvidersForSpec(s)), - ); - for (const providers of fetches) { - for (const p of providers) { - if (!map.has(p.address)) map.set(p.address, p.moniker); - } - } - } else { - for (const p of await fetchAllProviders()) { - map.set(p.address, p.moniker); + const fetches = await Promise.all(specs.map((s) => fetchProvidersForSpec(s))); + for (const providers of fetches) { + for (const p of providers) { + if (!map.has(p.address)) map.set(p.address, p.moniker); } } return map; diff --git a/apps/api/src/rpc/lava.ts b/apps/api/src/rpc/lava.ts index 1c0f571..21cc02e 100644 --- a/apps/api/src/rpc/lava.ts +++ b/apps/api/src/rpc/lava.ts @@ -1507,6 +1507,30 @@ export function fetchAllProviders(): Promise { return pendingProviders; } +// Moniker-only cache. Routes that need just address→moniker shouldn't pay +// the full fetchAllProviders cost (stake/delegation/commission/specs) on +// cache misses. 60s TTL is short enough to stay fresh, long enough to +// deduplicate the 3+ reward routes that all call it in the same minute. +const MONIKER_CACHE_TTL_MS = 60_000; +let monikerCache: { at: number; map: Map } | null = null; +let pendingMonikers: Promise> | null = null; + +/** address → moniker map. 60-second in-process cache shared across routes. */ +export function fetchAllProviderMonikers(): Promise> { + if (monikerCache && Date.now() - monikerCache.at < MONIKER_CACHE_TTL_MS) { + return Promise.resolve(monikerCache.map); + } + if (pendingMonikers) return pendingMonikers; + pendingMonikers = (async () => { + const providers = await fetchAllProviders(); + const map = new Map(); + for (const p of providers) map.set(p.address, p.moniker); + monikerCache = { at: Date.now(), map }; + return map; + })().finally(() => { pendingMonikers = null; }); + return pendingMonikers; +} + async function fetchAllProvidersImpl(): Promise { const specs = await fetchAllSpecs(); From 7241c8ffcb3f9255ad201327e5b1c4ec8e4f618a Mon Sep 17 00:00:00 2001 From: Nimrod Teich Date: Thu, 16 Apr 2026 23:20:14 +0300 Subject: [PATCH 08/25] refactor: wire shared PaginatedResponse/ProviderListItem types MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The types in packages/shared/src/types/api.ts were defined but imported only by their own test file — so api and web each re-declared response shapes locally and could drift silently. Add a ProviderListItem shape that matches the /providers response, have the api route return PaginatedResponse, and have the /providers page useApi>. ApiError.message/statusCode made optional — callers (e.g. 404 replies) already return shorter variants. --- apps/api/src/routes/providers.ts | 5 +++-- apps/web/src/app/providers/page.tsx | 23 ++++------------------- packages/shared/src/types/api.ts | 17 +++++++++++++++-- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/apps/api/src/routes/providers.ts b/apps/api/src/routes/providers.ts index 39fca3d..038e121 100644 --- a/apps/api/src/routes/providers.ts +++ b/apps/api/src/routes/providers.ts @@ -1,5 +1,6 @@ import type { FastifyInstance } from "fastify"; import { weightedQos } from "@info/shared/utils"; +import type { PaginatedResponse, ProviderListItem } from "@info/shared/types"; import { gqlSafe } from "../graphql/client.js"; import { fetchAllProviders, @@ -33,7 +34,7 @@ export async function providerRoutes(app: FastifyInstance) { }, }, config: { cacheTTL: 300 }, - }, async (request) => { + }, async (request): Promise> => { const { page, limit, offset } = request.pagination; const since = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000) @@ -74,7 +75,7 @@ export async function providerRoutes(app: FastifyInstance) { const paged = providers.slice(offset, offset + limit); return { - data: paged.map((p) => { + data: paged.map((p): ProviderListItem => { const relay = relayMap.get(p.address); return { provider: p.address, diff --git a/apps/web/src/app/providers/page.tsx b/apps/web/src/app/providers/page.tsx index be50517..ca8d582 100644 --- a/apps/web/src/app/providers/page.tsx +++ b/apps/web/src/app/providers/page.tsx @@ -11,32 +11,17 @@ import { type SortingState, } from "@tanstack/react-table"; import { ChevronUp, ChevronDown, ChevronsUpDown } from "lucide-react"; +import type { PaginatedResponse, ProviderListItem } from "@info/shared/types"; import { useApi } from "@/hooks/use-api"; import { Loading } from "@/components/data/Loading"; import { ProviderLink } from "@/components/data/ProviderLink"; import { LavaAmount } from "@/components/data/LavaAmount"; -interface Provider { - provider: string; - moniker: string; - identity?: string; - activeServices: number; - totalStake: string; - totalDelegation: string; - cuSum30d: string; - relaySum30d: string; -} - -interface ProvidersResponse { - data: Provider[]; - pagination: { total: number }; -} - -function toBigInt(v: string | undefined): bigint { +function toBigInt(v: string | null | undefined): bigint { try { return BigInt(v ?? "0"); } catch { return 0n; } } -const columns: ColumnDef[] = [ +const columns: ColumnDef[] = [ { id: "moniker", header: "Moniker", @@ -78,7 +63,7 @@ const columns: ColumnDef[] = [ ]; function ProvidersContent() { -const { data: resp, isLoading } = useApi("/providers?limit=10000"); +const { data: resp, isLoading } = useApi>("/providers?limit=10000"); const providers = useMemo(() => resp?.data ?? [], [resp]); const [sorting, setSorting] = useState([ diff --git a/packages/shared/src/types/api.ts b/packages/shared/src/types/api.ts index 9b1087c..82fcd92 100644 --- a/packages/shared/src/types/api.ts +++ b/packages/shared/src/types/api.ts @@ -17,6 +17,19 @@ export interface PaginationParams { export interface ApiError { error: string; - message: string; - statusCode: number; + message?: string; + statusCode?: number; +} + +/** Row shape returned by GET /providers (paginated provider list). */ +export interface ProviderListItem { + provider: string; + moniker: string; + identity: string; + activeServices: number; + totalStake: string; + totalDelegation: string; + commission: string; + cuSum30d: string | null; + relaySum30d: string | null; } From dd1365f5e07e5d4202fdbac476db37575d6890a8 Mon Sep 17 00:00:00 2001 From: Nimrod Teich Date: Thu, 16 Apr 2026 23:30:37 +0300 Subject: [PATCH 09/25] refactor(api): split rpc/lava.ts into focused modules MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The 1,673-line rpc/lava.ts mixed ten distinct concerns (HTTP plumbing, block lookup, specs, providers, supply, pricing, rewards, validators, APR, Keybase). Extract each concern into its own file so changes stay scoped and tests can target narrower surfaces: - rpc/rest.ts — fetchRest, RPC_BATCH_SIZE, inflight coalescing - rpc/block.ts — fetchLatestBlockHeight, fetchBlockAtTimestamp - rpc/specs.ts — fetchAllSpecs + chain display-name overrides - rpc/supply.ts — total/circulating supply, vesting, reward pools - rpc/providers.ts — spec/provider queries + moniker cache - rpc/pricing.ts — CoinGecko, denom registry, TVL + DEX sources - rpc/rewards.ts — multi-denom reward conversion helpers - rpc/validators.ts — validator RPC + rewards breakdown - rpc/apr.ts — APR math + weighted Redis history - rpc/keybase.ts — Keybase avatar lookup rpc/lava.ts is now a thin barrel that re-exports the original public surface so external callers don't need updating. New code should import directly from the focused modules. No behavior change — all 97 tests continue to pass. --- apps/api/src/rpc/apr.ts | 293 ++++++ apps/api/src/rpc/block.ts | 82 ++ apps/api/src/rpc/keybase.ts | 51 + apps/api/src/rpc/lava.ts | 1725 +------------------------------- apps/api/src/rpc/pricing.ts | 291 ++++++ apps/api/src/rpc/providers.ts | 286 ++++++ apps/api/src/rpc/rest.ts | 36 + apps/api/src/rpc/rewards.ts | 245 +++++ apps/api/src/rpc/specs.ts | 46 + apps/api/src/rpc/supply.ts | 133 +++ apps/api/src/rpc/validators.ts | 244 +++++ 11 files changed, 1758 insertions(+), 1674 deletions(-) create mode 100644 apps/api/src/rpc/apr.ts create mode 100644 apps/api/src/rpc/block.ts create mode 100644 apps/api/src/rpc/keybase.ts create mode 100644 apps/api/src/rpc/pricing.ts create mode 100644 apps/api/src/rpc/providers.ts create mode 100644 apps/api/src/rpc/rest.ts create mode 100644 apps/api/src/rpc/rewards.ts create mode 100644 apps/api/src/rpc/specs.ts create mode 100644 apps/api/src/rpc/supply.ts create mode 100644 apps/api/src/rpc/validators.ts diff --git a/apps/api/src/rpc/apr.ts b/apps/api/src/rpc/apr.ts new file mode 100644 index 0000000..78c9b5d --- /dev/null +++ b/apps/api/src/rpc/apr.ts @@ -0,0 +1,293 @@ +import type { Redis } from "ioredis"; +import { RPC_BATCH_SIZE } from "./rest.js"; +import { prewarmPriceCache, fetchTokenUsdPrice } from "./pricing.js"; +import { + APR_BENCHMARK_LAVA, + type RewardToken, + type RewardsBySpecEntry, + fetchEstimatedRewards, + fetchRewardsBySpec, +} from "./rewards.js"; +import { + type ProviderSpecEntry, + fetchAllProviders, + fetchProvidersWithSpecs, +} from "./providers.js"; +import { fetchBondedValidators } from "./validators.js"; +import { fetchProviderAvatar } from "./keybase.js"; + +/** 80th percentile, capped at 30% — same thresholds as jsinfo */ +const APR_PERCENTILE = 0.8; +const APR_MAX_PERCENTILE_CAP = 0.3; +const APR_MAX_INDIVIDUAL = 0.8; +const APR_MIN = 1e-11; + +/** Percentile calculation matching jsinfo `CalculatePercentile` */ +function calculatePercentile(values: number[], rank: number): number { + if (values.length === 0 || rank < 0 || rank > 1) return 0; + if (values.length === 1) return values[0]; + + const sorted = [...values].sort((a, b) => a - b); + const pos = Math.floor((sorted.length - 1) * rank); + + if (sorted.length % 2 === 0) { + return sorted[pos] + (sorted[pos + 1] - sorted[pos]) * rank; + } + return sorted[pos]; +} + +/** APR from monthly reward: (1 + rewardUsd/investedUsd)^12 - 1 */ +function calculateApr(rewardUsd: number, investedUsd: number): number { + if (investedUsd <= 0 || rewardUsd <= 0) return 0; + const rate = rewardUsd / investedUsd; + const apr = Math.pow(1 + rate, 12) - 1; + if (!isFinite(apr) || apr < APR_MIN || apr > 100) return 0; + return apr; +} + +// --- Weighted APR history (Redis, matches jsinfo AprWeighted) --- + +const APR_WEIGHTS = [0.4, 0.25, 0.15, 0.1, 0.05, 0.03, 0.02]; +const APR_DAYS_TO_KEEP = 7; +const APR_HISTORY_TTL = 30 * 24 * 60 * 60; // 30 days + +interface AprRecord { date: string; aprSum: number; count: number } +interface AprHistory { records: AprRecord[]; lastUpdated: string } + +function aprHistoryKey(type: string, address: string): string { + return `apr_history:${type}:${address}`; +} + +function todayStr(): string { + const d = new Date(); + return `${d.getFullYear()}/${String(d.getMonth() + 1).padStart(2, "0")}/${String(d.getDate()).padStart(2, "0")}`; +} + +async function storeApr(redis: Redis, type: string, address: string, apr: number): Promise { + if (apr === 0) return; + const key = aprHistoryKey(type, address); + const today = todayStr(); + try { + const raw = await redis.get(key); + const history: AprHistory = raw ? JSON.parse(raw) : { records: [], lastUpdated: today }; + + const rec = history.records.find((r) => r.date === today); + if (rec) { rec.aprSum += apr; rec.count += 1; } + else { history.records.push({ date: today, aprSum: apr, count: 1 }); } + + history.records = history.records + .sort((a, b) => b.date.localeCompare(a.date)) + .slice(0, APR_DAYS_TO_KEEP); + history.lastUpdated = today; + + await redis.set(key, JSON.stringify(history), "EX", APR_HISTORY_TTL); + } catch { /* Redis failures are non-fatal */ } +} + +async function getWeightedApr(redis: Redis, type: string, address: string): Promise { + try { + const raw = await redis.get(aprHistoryKey(type, address)); + if (!raw) return null; + const history: AprHistory = JSON.parse(raw); + if (history.records.length === 0) return null; + + let weightedSum = 0; + let weightSum = 0; + for (let i = 0; i < history.records.length && i < APR_WEIGHTS.length; i++) { + const avg = history.records[i].aprSum / history.records[i].count; + weightedSum += avg * APR_WEIGHTS[i]; + weightSum += APR_WEIGHTS[i]; + } + return weightSum > 0 ? weightedSum / weightSum : null; + } catch { + return null; + } +} + +/** + * Compute APR percentiles matching jsinfo `/apr` response. + * + * For each active provider and bonded validator: + * 1. Query `estimated_{type}_rewards` with a 10 000 LAVA benchmark + * 2. Convert multi-denom rewards to USD via CoinGecko + * 3. Compute APR via monthly compounding: (1 + monthlyRate)^12 - 1 + * 4. Store in 7-day weighted history (Redis, if available) + * 5. Take 80th percentile, cap at 30% + */ +export async function computeAPR(redis?: Redis | null): Promise<{ + restaking_apr_percentile: number; + staking_apr_percentile: number; +}> { + // Pre-warm all token prices in a single CoinGecko call + await prewarmPriceCache(); + + const lavaPrice = await fetchTokenUsdPrice("lava"); + const investedUsd = APR_BENCHMARK_LAVA * lavaPrice; + + const [providers, validators] = await Promise.all([ + fetchAllProviders(), + fetchBondedValidators(), + ]); + + const providerAddresses = providers.map((p) => p.address); + + // Collect per-entity APRs (batched by RPC_BATCH_SIZE) + const [providerAprs, validatorAprs] = await Promise.all([ + collectEntityAprs("provider", providerAddresses, investedUsd, redis), + collectEntityAprs("validator", validators, investedUsd, redis), + ]); + + return { + restaking_apr_percentile: Math.min( + calculatePercentile(providerAprs, APR_PERCENTILE), + APR_MAX_PERCENTILE_CAP, + ), + staking_apr_percentile: Math.min( + calculatePercentile(validatorAprs, APR_PERCENTILE), + APR_MAX_PERCENTILE_CAP, + ), + }; +} + +/** Batch-fetch estimated rewards, compute APR per entity, apply weighted history */ +async function collectEntityAprs( + type: "provider" | "validator", + addresses: string[], + investedUsd: number, + redis?: Redis | null, +): Promise { + const aprs: number[] = []; + + for (let i = 0; i < addresses.length; i += RPC_BATCH_SIZE) { + const batch = addresses.slice(i, i + RPC_BATCH_SIZE); + const rewards = await Promise.all( + batch.map((addr) => fetchEstimatedRewards(type, addr)), + ); + + for (let j = 0; j < batch.length; j++) { + const currentApr = calculateApr(rewards[j].totalUsd, investedUsd); + if (currentApr <= 0) continue; + + let finalApr = currentApr; + if (redis) { + await storeApr(redis, type, batch[j], currentApr); + const weighted = await getWeightedApr(redis, type, batch[j]); + if (weighted !== null) finalApr = weighted; + } + + if (finalApr > 0 && finalApr < APR_MAX_INDIVIDUAL) { + aprs.push(finalApr); + } + } + } + + return aprs; +} + +/** Max display APR for individual providers (matching jsinfo) */ +const MAX_DISPLAY_APR = 0.9; + +/** Format APR as percentage string matching jsinfo format */ +function formatAprPercent(apr: number): string { + if (apr <= 0) return "-"; + if (apr > MAX_DISPLAY_APR) return "90.0%"; + return `${(apr * 100).toFixed(4)}%`; +} + +/** Format commission as percentage string */ +function formatCommission(commission: string): string { + if (!commission) return "-"; + const n = parseFloat(commission); + if (!isFinite(n)) return "-"; + return `${n.toFixed(1)}%`; +} + +export interface AllProviderAprEntry { + address: string; + moniker: string; + apr: string; + commission: string; + "30_days_cu_served": string; + "30_days_relays_served": string; + rewards_10k_lava_delegation: RewardToken[]; + rewards_last_month: RewardsBySpecEntry[]; + specs: ProviderSpecEntry[]; + stake: string; + stakestatus: string; + addons: string; + extensions: string; + delegateTotal: string; + avatar: string | null; +} + +/** + * Compute per-provider APR data matching jsinfo `/all_providers_apr`. + * + * Returns array of provider objects with APR, commission, 30d relay data, + * per-token reward breakdown, specs, and avatar. + */ +export async function computeAllProvidersApr( + relay30d: Map, + redis?: Redis | null, +): Promise { + // Pre-warm all token prices in a single CoinGecko call + await prewarmPriceCache(); + + const lavaPrice = await fetchTokenUsdPrice("lava"); + const investedUsd = APR_BENCHMARK_LAVA * lavaPrice; + + const { providers: providerMap, specNames } = await fetchProvidersWithSpecs(); + const addresses = Array.from(providerMap.keys()); + const results: AllProviderAprEntry[] = []; + + for (let i = 0; i < addresses.length; i += RPC_BATCH_SIZE) { + const batch = addresses.slice(i, i + RPC_BATCH_SIZE); + const [rewardResults, rewardsLastMonthResults, avatarResults] = await Promise.all([ + Promise.all(batch.map((addr) => fetchEstimatedRewards("provider", addr))), + Promise.all(batch.map((addr) => fetchRewardsBySpec(addr, specNames))), + Promise.all(batch.map((addr) => { + const p = providerMap.get(addr)!; + return fetchProviderAvatar(addr, p.identity || undefined).catch(() => null); + })), + ]); + + for (let j = 0; j < batch.length; j++) { + const addr = batch[j]; + const provider = providerMap.get(addr)!; + const rewards = rewardResults[j]; + const rewardsLastMonth = rewardsLastMonthResults[j]; + const avatar = avatarResults[j]; + const currentApr = calculateApr(rewards.totalUsd, investedUsd); + + let finalApr = currentApr; + if (redis && currentApr > 0) { + await storeApr(redis, "provider", addr, currentApr); + const weighted = await getWeightedApr(redis, "provider", addr); + if (weighted !== null) finalApr = weighted; + } + + const relay = relay30d.get(addr); + const firstSpec = provider.specs[0]; + + results.push({ + address: addr, + moniker: provider.moniker || "-", + apr: formatAprPercent(finalApr), + commission: formatCommission(provider.commission), + "30_days_cu_served": relay?.cu ?? "-", + "30_days_relays_served": relay?.relays ?? "-", + rewards_10k_lava_delegation: rewards.tokens, + rewards_last_month: rewardsLastMonth, + specs: provider.specs, + stake: firstSpec?.stake ?? "", + stakestatus: firstSpec ? "Active" : "", + addons: firstSpec?.addons ?? "", + extensions: firstSpec?.extensions ?? "", + delegateTotal: firstSpec?.delegateTotal ?? "", + avatar, + }); + } + } + + return results; +} diff --git a/apps/api/src/rpc/block.ts b/apps/api/src/rpc/block.ts new file mode 100644 index 0000000..0d4f67c --- /dev/null +++ b/apps/api/src/rpc/block.ts @@ -0,0 +1,82 @@ +// Lava mainnet genesis timestamp (block 1) — used to narrow binary search range. +const LAVA_GENESIS_UNIX = 1_713_350_000; // ~2024-04-17 + +// LRU-ish cache for timestamp → block height (avoids repeated binary searches). +const blockAtTsCache = new Map(); +const BLOCK_AT_TS_CACHE_MAX = 200; + +export async function fetchLatestBlockHeight(): Promise<{ + height: number; + time: string; +}> { + const LAVA_RPC_URL = process.env.LAVA_RPC_URL ?? "https://lava.tendermintrpc.lava.build:443"; + const res = await fetch(`${LAVA_RPC_URL}/status`, { + signal: AbortSignal.timeout(10_000), + }); + if (!res.ok) throw new Error(`RPC ${res.status}`); + const data = (await res.json()) as { + result: { sync_info: { latest_block_height: string; latest_block_time: string } }; + }; + return { + height: parseInt(data.result.sync_info.latest_block_height, 10), + time: data.result.sync_info.latest_block_time, + }; +} + +// Binary-search Tendermint blocks to find the block closest to a target unix timestamp. +// Uses linear interpolation to estimate the starting range, reducing iterations from ~23 to ~10. +export async function fetchBlockAtTimestamp(targetUnix: number): Promise { + const cached = blockAtTsCache.get(targetUnix); + if (cached !== undefined) return cached; + + const LAVA_RPC_URL = process.env.LAVA_RPC_URL ?? "https://lava.tendermintrpc.lava.build:443"; + + async function blockTime(height: number): Promise { + const res = await fetch(`${LAVA_RPC_URL}/block?height=${height}`, { + signal: AbortSignal.timeout(10_000), + }); + if (!res.ok) throw new Error(`RPC ${res.status}`); + const data = (await res.json()) as { + result: { block: { header: { time: string } } }; + }; + return Math.floor(new Date(data.result.block.header.time).getTime() / 1000); + } + + const { height: latestHeight, time: latestTime } = await fetchLatestBlockHeight(); + const latestUnix = Math.floor(new Date(latestTime).getTime() / 1000); + + if (targetUnix >= latestUnix) return latestHeight; + + // Linear interpolation to narrow the search range + const genesisUnix = LAVA_GENESIS_UNIX; + const estimatedHeight = Math.max(1, Math.min( + latestHeight, + Math.floor(latestHeight * (targetUnix - genesisUnix) / (latestUnix - genesisUnix)), + )); + const margin = 2000; + let lo = Math.max(1, estimatedHeight - margin); + let hi = Math.min(latestHeight, estimatedHeight + margin); + + // Verify bounds contain the target; widen if not + if (await blockTime(lo) > targetUnix) lo = 1; + if (await blockTime(hi) < targetUnix) hi = latestHeight; + + while (lo < hi) { + const mid = Math.floor((lo + hi) / 2); + const t = await blockTime(mid); + if (t < targetUnix) { + lo = mid + 1; + } else { + hi = mid; + } + } + + // Evict oldest entry if cache is full + if (blockAtTsCache.size >= BLOCK_AT_TS_CACHE_MAX) { + const firstKey = blockAtTsCache.keys().next().value; + if (firstKey !== undefined) blockAtTsCache.delete(firstKey); + } + blockAtTsCache.set(targetUnix, lo); + + return lo; +} diff --git a/apps/api/src/rpc/keybase.ts b/apps/api/src/rpc/keybase.ts new file mode 100644 index 0000000..9fa94a5 --- /dev/null +++ b/apps/api/src/rpc/keybase.ts @@ -0,0 +1,51 @@ +import { fetchRest } from "./rest.js"; +import { fetchAllSpecs } from "./specs.js"; + +async function fetchProviderMetadata(provider: string): Promise<{ + description?: { identity?: string; moniker?: string }; +} | null> { + try { + const specs = await fetchAllSpecs(); + for (const spec of specs) { + try { + const data = await fetchRest<{ + stakeEntry: Array<{ + address: string; + description?: { identity?: string; moniker?: string }; + }>; + }>(`/lavanet/lava/pairing/providers/${spec.index}`); + const match = data.stakeEntry?.find((p) => p.address === provider); + if (match?.description?.identity) return match; + } catch { + continue; + } + } + return null; + } catch { + return null; + } +} + +export async function fetchProviderAvatar(provider: string, identityHint?: string): Promise { + try { + let identity = identityHint; + if (!identity) { + const meta = await fetchProviderMetadata(provider); + identity = meta?.description?.identity ?? undefined; + } + if (!identity) return null; + + const KEYBASE_API_URL = process.env.KEYBASE_API_URL ?? "https://keybase.io/_/api/1.0"; + const res = await fetch( + `${KEYBASE_API_URL}/user/lookup.json?key_suffix=${encodeURIComponent(identity)}&fields=pictures`, + { signal: AbortSignal.timeout(10_000) }, + ); + if (!res.ok) return null; + const data = (await res.json()) as { + them?: Array<{ pictures?: { primary?: { url?: string } } }>; + }; + return data.them?.[0]?.pictures?.primary?.url ?? null; + } catch { + return null; + } +} diff --git a/apps/api/src/rpc/lava.ts b/apps/api/src/rpc/lava.ts index 21cc02e..3fa686a 100644 --- a/apps/api/src/rpc/lava.ts +++ b/apps/api/src/rpc/lava.ts @@ -1,1674 +1,51 @@ -import pino from "pino"; -import type { Redis } from "ioredis"; - -const logger = pino({ name: "rpc" }); - -const LAVA_REST_URL = process.env.LAVA_REST_URL ?? "https://lava.rest.lava.build"; - -// Concurrent in-flight RPC calls per batch loop. Operators running dedicated -// RPC endpoints can raise this (env: RPC_BATCH_SIZE). Public endpoints should -// keep a conservative value (~5) to avoid rate limiting. -export const RPC_BATCH_SIZE = Math.max(1, parseInt(process.env.RPC_BATCH_SIZE ?? "25", 10)); - -// Request coalescing: concurrent fetches for the same path share one in-flight request -const inflightRpc = new Map>(); - -async function fetchRest(path: string, blockHeight?: number): Promise { - const cacheKey = blockHeight ? `${path}@${blockHeight}` : path; - const existing = inflightRpc.get(cacheKey); - if (existing) return existing as Promise; - - const promise = (async () => { - const headers: Record = {}; - if (blockHeight) headers["x-cosmos-block-height"] = String(blockHeight); - - const res = await fetch(`${LAVA_REST_URL}${path}`, { - headers, - signal: AbortSignal.timeout(15_000), - }); - if (!res.ok) throw new Error(`RPC ${res.status}: ${res.statusText}`); - return (await res.json()) as T; - })(); - - inflightRpc.set(cacheKey, promise); - const cleanup = () => { inflightRpc.delete(cacheKey); }; - promise.then(cleanup, cleanup); - return promise; -} - -export async function fetchTotalSupply(blockHeight?: number): Promise { - const data = await fetchRest<{ supply: Array<{ denom: string; amount: string }> }>( - "/cosmos/bank/v1beta1/supply", - blockHeight, - ); - const lava = data.supply?.find((c) => c.denom === "ulava"); - return BigInt(lava?.amount ?? "0"); -} - -// Lava mainnet genesis timestamp (block 1) — used to narrow binary search range. -const LAVA_GENESIS_UNIX = 1_713_350_000; // ~2024-04-17 - -// LRU-ish cache for timestamp → block height (avoids repeated binary searches). -const blockAtTsCache = new Map(); -const BLOCK_AT_TS_CACHE_MAX = 200; - -// Binary-search Tendermint blocks to find the block closest to a target unix timestamp. -// Uses linear interpolation to estimate the starting range, reducing iterations from ~23 to ~10. -export async function fetchBlockAtTimestamp(targetUnix: number): Promise { - const cached = blockAtTsCache.get(targetUnix); - if (cached !== undefined) return cached; - - const LAVA_RPC_URL = process.env.LAVA_RPC_URL ?? "https://lava.tendermintrpc.lava.build:443"; - - async function blockTime(height: number): Promise { - const res = await fetch(`${LAVA_RPC_URL}/block?height=${height}`, { - signal: AbortSignal.timeout(10_000), - }); - if (!res.ok) throw new Error(`RPC ${res.status}`); - const data = (await res.json()) as { - result: { block: { header: { time: string } } }; - }; - return Math.floor(new Date(data.result.block.header.time).getTime() / 1000); - } - - const { height: latestHeight, time: latestTime } = await fetchLatestBlockHeight(); - const latestUnix = Math.floor(new Date(latestTime).getTime() / 1000); - - if (targetUnix >= latestUnix) return latestHeight; - - // Linear interpolation to narrow the search range - const genesisUnix = LAVA_GENESIS_UNIX; - const estimatedHeight = Math.max(1, Math.min( - latestHeight, - Math.floor(latestHeight * (targetUnix - genesisUnix) / (latestUnix - genesisUnix)), - )); - const margin = 2000; - let lo = Math.max(1, estimatedHeight - margin); - let hi = Math.min(latestHeight, estimatedHeight + margin); - - // Verify bounds contain the target; widen if not - if (await blockTime(lo) > targetUnix) lo = 1; - if (await blockTime(hi) < targetUnix) hi = latestHeight; - - while (lo < hi) { - const mid = Math.floor((lo + hi) / 2); - const t = await blockTime(mid); - if (t < targetUnix) { - lo = mid + 1; - } else { - hi = mid; - } - } - - // Evict oldest entry if cache is full - if (blockAtTsCache.size >= BLOCK_AT_TS_CACHE_MAX) { - const firstKey = blockAtTsCache.keys().next().value; - if (firstKey !== undefined) blockAtTsCache.delete(firstKey); - } - blockAtTsCache.set(targetUnix, lo); - - return lo; -} - -interface RewardPool { - name: string; - balance: Array<{ denom: string; amount: string }>; -} - -/** Shared fetch for reward pools — both consumers filter by pool name. */ -async function fetchRewardPools(): Promise { - const data = await fetchRest<{ pools: RewardPool[] }>("/lavanet/lava/rewards/pools"); - return data.pools ?? []; -} - -function sumPoolsUlava(pools: RewardPool[], names: string[]): bigint { - const nameSet = new Set(names); - let total = 0n; - for (const pool of pools) { - if (nameSet.has(pool.name)) { - for (const coin of pool.balance ?? []) { - if (coin.denom === "ulava") total += BigInt(coin.amount); - } - } - } - return total; -} - -const ALL_REWARD_POOLS = [ - "validators_rewards_distribution_pool", - "validators_rewards_allocation_pool", - "providers_rewards_distribution_pool", - "providers_rewards_allocation_pool", - "iprpc_pool", -]; - -async function fetchRewardPoolsAmount(): Promise { - return sumPoolsUlava(await fetchRewardPools(), ALL_REWARD_POOLS); -} - -const PROVIDER_REWARD_POOLS = [ - "providers_rewards_distribution_pool", - "providers_rewards_allocation_pool", - "iprpc_pool", -]; - -/** Fetch total ulava in provider-side reward pools (excludes validator pools). */ -interface VestingStats { - continuousVesting: bigint; - periodicVesting: bigint; -} - -async function fetchLockedVestingTokens(): Promise { - const nowSeconds = Math.floor(Date.now() / 1000); - const stats: VestingStats = { continuousVesting: 0n, periodicVesting: 0n }; - let nextKey: string | null = null; - - do { - const params = new URLSearchParams({ "pagination.limit": "1000" }); - if (nextKey) params.set("pagination.key", nextKey); - - const data = await fetchRest<{ - accounts: Array<{ - "@type": string; - start_time?: string; - base_vesting_account?: { - original_vesting?: Array<{ denom: string; amount: string }>; - end_time?: string; - }; - vesting_periods?: Array<{ - length: string; - amount: Array<{ denom: string; amount: string }>; - }>; - }>; - pagination?: { next_key: string | null }; - }>(`/cosmos/auth/v1beta1/accounts?${params}`); - - for (const account of data.accounts ?? []) { - const type = account["@type"]; - - if (type === "/cosmos.vesting.v1beta1.ContinuousVestingAccount") { - const totalAmount = BigInt( - account.base_vesting_account?.original_vesting?.[0]?.amount ?? "0", - ); - const startTime = BigInt(account.start_time ?? "0"); - const endTime = BigInt(account.base_vesting_account?.end_time ?? "0"); - const now = BigInt(nowSeconds); - - if (now < startTime) { - stats.continuousVesting += totalAmount; - } else if (now < endTime && endTime > startTime) { - stats.continuousVesting += ((endTime - now) * totalAmount) / (endTime - startTime); - } - } else if (type === "/cosmos.vesting.v1beta1.PeriodicVestingAccount") { - let currentTime = parseInt(account.start_time ?? "0"); - for (const period of account.vesting_periods ?? []) { - currentTime += parseInt(period.length); - if (currentTime >= nowSeconds) { - stats.periodicVesting += BigInt(period.amount?.[0]?.amount ?? "0"); - } - } - } - } - - nextKey = data.pagination?.next_key ?? null; - } while (nextKey); - - return stats; -} - -export async function fetchCirculatingSupply(): Promise { - const [totalSupply, pools, vesting] = await Promise.all([ - fetchTotalSupply(), - fetchRewardPoolsAmount(), - fetchLockedVestingTokens(), - ]); - - const circulating = totalSupply - vesting.continuousVesting - vesting.periodicVesting - pools; - if (circulating < 0n) { - logger.warn( - `Negative circulating supply: total=${totalSupply} continuous=${vesting.continuousVesting} periodic=${vesting.periodicVesting} pools=${pools}`, - ); - return 0n; - } - return circulating; -} - -export interface ProviderEndpoint { - iPPORT: string; - geolocation: number; - apiInterfaces: string[]; - addons: string[]; - extensions: string[]; -} - -export interface ProviderForSpec { - address: string; - moniker: string; - identity: string; - stake: { amount: string }; - delegate_total: { amount: string }; - delegate_commission: string; - geolocation: number; - addons: string; - extensions: string; - apiInterfaces: string; - endpoints: ProviderEndpoint[]; -} - -export async function fetchProvidersForSpec(specId: string): Promise { - const data = await fetchRest<{ - stakeEntry: Array<{ - address: string; - moniker: string; - description?: { identity?: string }; - stake: { amount: string }; - delegate_total: { amount: string }; - delegate_commission: string; - geolocation: number; - endpoints?: Array<{ - iPPORT?: string; - geolocation?: number; - addons?: string[]; - extensions?: string[]; - api_interfaces?: string[]; - }>; - }>; - }>(`/lavanet/lava/pairing/providers/${specId}`); - - return (data.stakeEntry ?? []).map((entry) => { - const allAddons = new Set(); - const allExtensions = new Set(); - const allApiInterfaces = new Set(); - const endpoints: ProviderEndpoint[] = []; - - for (const ep of entry.endpoints ?? []) { - for (const a of ep.addons ?? []) if (a) allAddons.add(a); - for (const e of ep.extensions ?? []) if (e) allExtensions.add(e); - for (const i of ep.api_interfaces ?? []) if (i) allApiInterfaces.add(i); - - if (ep.iPPORT) { - endpoints.push({ - iPPORT: ep.iPPORT, - geolocation: ep.geolocation ?? entry.geolocation, - apiInterfaces: (ep.api_interfaces ?? []).filter(Boolean), - addons: (ep.addons ?? []).filter(Boolean), - extensions: (ep.extensions ?? []).filter(Boolean), - }); - } - } - - return { - address: entry.address, - moniker: entry.moniker, - identity: entry.description?.identity ?? "", - stake: entry.stake, - delegate_total: entry.delegate_total, - delegate_commission: entry.delegate_commission, - geolocation: entry.geolocation, - addons: Array.from(allAddons).join(","), - extensions: Array.from(allExtensions).join(","), - apiInterfaces: Array.from(allApiInterfaces).join(","), - endpoints, - }; - }); -} - -function titleCase(s: string): string { - return s.replace(/\b\w/g, (c) => c.toUpperCase()); -} - -/** Display name overrides — keys are specIDs (uppercase) */ -const CHAIN_DISPLAY_NAMES: Record = { - BSC: "BNB Chain Mainnet", - BSCT: "BNB Chain Testnet", - COSMOSHUB: "Cosmos Hub Mainnet", - COSMOSHUBT: "Cosmos Hub Testnet", - ETH1: "Ethereum Mainnet", - FTM4002: "Fantom Testnet", - FVMT: "Filecoin Testnet", - HEDERA: "Hedera Hashgraph Mainnet", - BTC: "Bitcoin Mainnet", - HOL1: "Ethereum Holesky Testnet", - LAVA: "Lava Mainnet", - LAV1: "Lava Testnet", - MOVEMENTT: "Movement Testnet", - OPTMS: "Optimism Sepolia Testnet", - POLYGONA: "Polygon Amoy Testnet", - SEP1: "Ethereum Sepolia Testnet", - SOLANAT: "Solana Testnet", - SONICT: "Sonic Blaze Testnet", - SPARK: "Fuse Testnet", - STRKS: "Starknet Sepolia Testnet", - TRX: "Tron Mainnet", - TRXT: "Tron Shasta Testnet", -}; - -function chainDisplayName(chainID: string, chainName: string): string { - if (CHAIN_DISPLAY_NAMES[chainID]) return CHAIN_DISPLAY_NAMES[chainID]; - return titleCase(chainName); -} - -import { BASE_SPECS } from "@info/shared/constants"; - -export async function fetchAllSpecs(): Promise> { - const data = await fetchRest<{ - chainInfoList: Array<{ chainName: string; chainID: string }>; - }>("/lavanet/lava/spec/show_all_chains"); - return (data.chainInfoList ?? []) - .filter((c) => !BASE_SPECS.has(c.chainID)) - .map((c) => ({ index: c.chainID, name: chainDisplayName(c.chainID, c.chainName) })); -} - -async function fetchStakingPool(): Promise<{ bonded_tokens: string; not_bonded_tokens: string }> { - const data = await fetchRest<{ pool: { bonded_tokens: string; not_bonded_tokens: string } }>( - "/cosmos/staking/v1beta1/pool", - ); - return data.pool; -} - - -// Reward pools that count toward TVL (iprpc_pool excluded) -const TVL_REWARD_POOLS = [ - "validators_rewards_distribution_pool", - "validators_rewards_allocation_pool", - "providers_rewards_distribution_pool", - "providers_rewards_allocation_pool", -]; - -const COINGECKO_API_URL = process.env.COINGECKO_API_URL ?? "https://api.coingecko.com/api/v3"; - -export async function fetchLavaUsdPrice(): Promise { - const res = await fetch( - `${COINGECKO_API_URL}/simple/price?ids=lava-network&vs_currencies=usd`, - { signal: AbortSignal.timeout(15_000) }, - ); - if (!res.ok) throw new Error(`CoinGecko ${res.status}`); - const data = (await res.json()) as { "lava-network"?: { usd?: number } }; - const price = data["lava-network"]?.usd; - if (!price || price <= 0) throw new Error("Invalid LAVA price from CoinGecko"); - return price; -} - -async function fetchTvlRewardPoolsUlava(): Promise { - const data = await fetchRest<{ - pools: Array<{ name: string; balance: Array<{ denom: string; amount: string }> }>; - }>("/lavanet/lava/rewards/pools"); - let total = 0n; - for (const pool of data.pools ?? []) { - if (TVL_REWARD_POOLS.includes(pool.name)) { - for (const coin of pool.balance ?? []) { - if (coin.denom === "ulava") total += BigInt(coin.amount); - } - } - } - return total; -} - -async function fetchSubscriptionCreditsUlava(): Promise { - try { - const data = await fetchRest<{ - subs_info: Array<{ credit: { amount: string; denom: string } }>; - }>("/lavanet/lava/subscription/list"); - let total = 0n; - for (const sub of data.subs_info ?? []) { - if (sub.credit?.denom === "ulava") { - const amount = BigInt(sub.credit.amount || "0"); - if (amount > 0n) total += amount; - } - } - return total; - } catch { - return 0n; - } -} - -async function fetchOsmosisLavaUlava(): Promise { - try { - const input = JSON.stringify({ - json: { - limit: 10, search: null, denoms: ["LAVA"], - types: ["weighted", "stable", "concentrated", "cosmwasm-transmuter", - "cosmwasm", "cosmwasm-astroport-pcl", "cosmwasm-whitewhale"], - incentiveTypes: ["superfluid", "osmosis", "boost", "none"], - sort: { keyPath: "market.volume24hUsd", direction: "desc" }, - minLiquidityUsd: 1000, cursor: 0, - }, - meta: { values: { search: ["undefined"] } }, - }); - const res = await fetch( - `https://app.osmosis.zone/api/edge-trpc-pools/pools.getPools?input=${encodeURIComponent(input)}`, - { signal: AbortSignal.timeout(15_000) }, - ); - if (!res.ok) return 0n; - const data = (await res.json()) as { - result: { data: { json: { items: Array<{ - reserveCoins: Array<{ currency: { coinDenom: string }; amount: string }>; - }> } } }; - }; - let total = 0n; - for (const pool of data.result?.data?.json?.items ?? []) { - for (const coin of pool.reserveCoins ?? []) { - if (coin.currency?.coinDenom?.toLowerCase() === "lava") { - total += BigInt(Math.round(parseFloat(coin.amount || "0"))); - } - } - } - return total; - } catch { - return 0n; - } -} - -async function fetchBaseDexUsd(): Promise { - try { - const res = await fetch( - "https://api.geckoterminal.com/api/v2/search/pools?query=lava", - { signal: AbortSignal.timeout(15_000) }, - ); - if (!res.ok) return 0; - const data = (await res.json()) as { - data: Array<{ id: string; attributes: { reserve_in_usd: string } }>; - }; - const basePool = data.data?.find((p: { id: string }) => p.id.startsWith("base_")); - return basePool ? parseFloat(basePool.attributes.reserve_in_usd || "0") : 0; - } catch { - return 0; - } -} - -async function fetchArbitrumDexUsd(): Promise { - try { - const res = await fetch("https://interface.gateway.uniswap.org/v1/graphql", { - method: "POST", - signal: AbortSignal.timeout(15_000), - headers: { - "Content-Type": "application/json", - Origin: "https://app.uniswap.org", - }, - body: JSON.stringify({ - query: `query($chain: Chain!, $address: String) { - token(chain: $chain, address: $address) { - market(currency: USD) { totalValueLocked { value } } - } - }`, - variables: { - address: "0x11e969e9b3f89cb16d686a03cd8508c9fc0361af", - chain: "ARBITRUM", - }, - }), - }); - if (!res.ok) return 0; - const data = (await res.json()) as { - data?: { token?: { market?: { totalValueLocked?: { value?: number } } } }; - }; - return data.data?.token?.market?.totalValueLocked?.value ?? 0; - } catch { - return 0; - } -} - -/** - * TVL (USD) = bonded tokens + 4 reward pools + subscriptions + DEX liquidity - * Matches jsinfo formula: all components converted to USD via CoinGecko LAVA price. - */ -export async function computeTVL(): Promise<{ tvl: string }> { - const [lavaPrice, pool, rewardPools, subscriptions, osmosis, baseUsd, arbitrumUsd] = - await Promise.all([ - fetchLavaUsdPrice(), - fetchStakingPool(), - fetchTvlRewardPoolsUlava(), - fetchSubscriptionCreditsUlava(), - fetchOsmosisLavaUlava(), - fetchBaseDexUsd(), - fetchArbitrumDexUsd(), - ]); - - const bondedTokens = BigInt(pool.bonded_tokens); - - // Convert DEX USD values to ulava to match jsinfo round-trip - const baseUlava = lavaPrice > 0 - ? BigInt(Math.round((baseUsd / lavaPrice) * 1_000_000)) - : 0n; - const arbitrumUlava = lavaPrice > 0 - ? BigInt(Math.round((arbitrumUsd / lavaPrice) * 1_000_000)) - : 0n; - - // Sum all components in ulava, then convert to USD - // Use BigInt division to avoid Number precision loss for large ulava sums - const totalUlava = bondedTokens + rewardPools + subscriptions - + osmosis + baseUlava + arbitrumUlava; - const totalLava = Number(totalUlava / 1_000_000n) + Number(totalUlava % 1_000_000n) / 1_000_000; - const tvlUsd = totalLava * lavaPrice; - - return { tvl: tvlUsd.toFixed(4) }; -} - -// --- APR calculation (matches jsinfo) --- - -/** Benchmark: 10,000 LAVA in ulava / base units */ -const APR_BENCHMARK_ULAVA = 10_000_000_000; -const APR_BENCHMARK_LAVA = 10_000; -const APR_BENCHMARK_DENOM = "ulava"; -/** 80th percentile, capped at 30% — same thresholds as jsinfo */ -const APR_PERCENTILE = 0.8; -const APR_MAX_PERCENTILE_CAP = 0.3; -const APR_MAX_INDIVIDUAL = 0.8; -const APR_MIN = 1e-11; - -/** Denom → base unit conversion (matching jsinfo DENOM_CONVERSIONS) */ -const DENOM_CONVERSIONS: Record = { - ulava: { baseDenom: "lava", factor: 1_000_000 }, - uatom: { baseDenom: "atom", factor: 1_000_000 }, - uosmo: { baseDenom: "osmo", factor: 1_000_000 }, - ujuno: { baseDenom: "juno", factor: 1_000_000 }, - ustars: { baseDenom: "stars", factor: 1_000_000 }, - uakt: { baseDenom: "akt", factor: 1_000_000 }, - uhuahua: { baseDenom: "huahua", factor: 1_000_000 }, - uevmos: { baseDenom: "evmos", factor: 1e18 }, - inj: { baseDenom: "inj", factor: 1e18 }, - aevmos: { baseDenom: "evmos", factor: 1e18 }, - basecro: { baseDenom: "cro", factor: 1e8 }, - uscrt: { baseDenom: "scrt", factor: 1_000_000 }, - uiris: { baseDenom: "iris", factor: 1_000_000 }, - uregen: { baseDenom: "regen", factor: 1_000_000 }, - uion: { baseDenom: "ion", factor: 1_000_000 }, - nanolike: { baseDenom: "like", factor: 1e9 }, - uaxl: { baseDenom: "axl", factor: 1_000_000 }, - uband: { baseDenom: "band", factor: 1_000_000 }, - ubld: { baseDenom: "bld", factor: 1_000_000 }, - ucmdx: { baseDenom: "cmdx", factor: 1_000_000 }, - ucre: { baseDenom: "cre", factor: 1_000_000 }, - uxprt: { baseDenom: "xprt", factor: 1_000_000 }, - uusdc: { baseDenom: "usdc", factor: 1_000_000 }, - "unit-move": { baseDenom: "move", factor: 1e7 }, -}; - -/** Base denom → CoinGecko coin ID */ -const DENOM_COINGECKO_ID: Record = { - lava: "lava-network", atom: "cosmos", osmo: "osmosis", - juno: "juno-network", stars: "stargaze", akt: "akash-network", - huahua: "chihuahua-token", evmos: "evmos", inj: "injective-protocol", - cro: "crypto-com-chain", scrt: "secret", iris: "iris-network", - regen: "regen", ion: "ion", like: "likecoin", axl: "axelar", - band: "band-protocol", bld: "agoric", cmdx: "comdex", - cre: "crescent-network", xprt: "persistence", usdc: "usd-coin", - move: "movement", -}; - -/** In-memory price cache (5 min TTL) */ -const priceCache = new Map(); -const PRICE_CACHE_MS = 300_000; - -/** - * Pre-warm the price cache with a single batch CoinGecko call for all known denoms. - * CoinGecko supports comma-separated IDs in one request. - */ -export async function prewarmPriceCache(): Promise { - const now = Date.now(); - // Skip if all entries are still fresh - const allFresh = Object.keys(DENOM_COINGECKO_ID).every((d) => { - const c = priceCache.get(d); - return c && now - c.ts < PRICE_CACHE_MS; - }); - if (allFresh) return; - - const ids = [...new Set(Object.values(DENOM_COINGECKO_ID))].join(","); - try { - const res = await fetch( - `https://api.coingecko.com/api/v3/simple/price?ids=${ids}&vs_currencies=usd`, - { signal: AbortSignal.timeout(15_000) }, - ); - if (!res.ok) return; - const data = (await res.json()) as Record; - const ts = Date.now(); - for (const [denom, cgId] of Object.entries(DENOM_COINGECKO_ID)) { - const price = data[cgId]?.usd ?? 0; - if (price > 0) priceCache.set(denom, { price, ts }); - } - } catch { - // Keep stale cache on failure - } -} - -/** Fetch USD price for a base denom — reads from cache (pre-warmed by prewarmPriceCache) */ -async function fetchTokenUsdPrice(baseDenom: string): Promise { - const cached = priceCache.get(baseDenom); - if (cached && Date.now() - cached.ts < PRICE_CACHE_MS) return cached.price; - - // Fallback: single-denom fetch if cache miss (shouldn't happen after prewarm) - const id = DENOM_COINGECKO_ID[baseDenom]; - if (!id) return 0; - - try { - const res = await fetch( - `https://api.coingecko.com/api/v3/simple/price?ids=${id}&vs_currencies=usd`, - { signal: AbortSignal.timeout(15_000) }, - ); - if (!res.ok) return cached?.price ?? 0; - const data = (await res.json()) as Record; - const price = data[id]?.usd ?? 0; - if (price > 0) priceCache.set(baseDenom, { price, ts: Date.now() }); - return price; - } catch { - return cached?.price ?? 0; - } -} - -/** Resolve an IBC hash to its base denom via chain RPC */ -async function fetchDenomTrace(ibcHash: string): Promise { - try { - const data = await fetchRest<{ - denom_trace: { base_denom: string }; - }>(`/ibc/apps/transfer/v1/denom_traces/${ibcHash}`); - return data.denom_trace?.base_denom ?? null; - } catch { - return null; - } -} - -interface EstimatedRewardsResponse { - info: Array<{ - source: string; - amount: { denom: string; amount: string } | Array<{ denom: string; amount: string }>; - }>; - total: Array<{ denom: string; amount: string }>; - recommended_block?: string; -} - -/** IBC test denoms to skip (matching jsinfo) */ -const TEST_DENOMS = new Set([ - "ibc/E3FCBEDDBAC500B1BAB90395C7D1E4F33D9B9ECFE82A16ED7D7D141A0152323F", -]); - -/** Token breakdown matching jsinfo RewardAmount */ -export interface RewardToken { - source_denom: string; - resolved_amount: string; - resolved_denom: string; - display_denom: string; - display_amount: string; - value_usd: string; -} - -export interface ProcessedRewards { - totalUsd: number; - tokens: RewardToken[]; -} - -/** Convert multi-denom reward array to USD total + per-token breakdown */ -async function processRewardTokens( - rewards: Array<{ denom: string; amount: string }>, -): Promise { - const tokens: RewardToken[] = []; - let totalUsd = 0; - - for (const { denom, amount } of rewards) { - if (TEST_DENOMS.has(denom)) continue; - - // Resolve IBC denoms to their base denom - let rawDenom = denom; - let resolvedDenom = denom; - if (denom.startsWith("ibc/")) { - const resolved = await fetchDenomTrace(denom.slice(4)); - if (!resolved) continue; - rawDenom = resolved; - resolvedDenom = resolved; - } - - const conversion = DENOM_CONVERSIONS[rawDenom]; - if (!conversion) continue; - - const displayAmount = divideByFactor(amount, conversion.factor); - const baseAmount = parseFloat(displayAmount); - if (!isFinite(baseAmount) || baseAmount <= 0) continue; - - const price = await fetchTokenUsdPrice(conversion.baseDenom); - const usd = price > 0 ? baseAmount * price : 0; - totalUsd += usd; - - tokens.push({ - source_denom: denom, - resolved_amount: formatTokenStr(amount), - resolved_denom: resolvedDenom, - display_denom: conversion.baseDenom, - display_amount: displayAmount, - value_usd: `$${formatTokenStr(usd.toFixed(14))}`, - }); - } - - return { totalUsd, tokens }; -} - -/** Strip trailing zeros from a decimal string (matching jsinfo FormatTokenAmount) */ -function formatTokenStr(s: string): string { - const [whole, frac] = s.split("."); - if (!frac) return whole; - const trimmed = frac.replace(/0+$/, ""); - return trimmed ? `${whole}.${trimmed}` : whole; -} - -/** String-based division to avoid floating-point noise for token amounts. - * Shifts the decimal point left by the number of digits in `divisor`. - * e.g. divideStr("23584370", 1_000_000) → "23.584370" → "23.58437" */ -function divideByFactor(raw: string, factor: number): string { - // Strip any existing decimal (RPC may return "123.000000000000000000") - const cleaned = formatTokenStr(raw); - const digits = Math.round(Math.log10(factor)); - const [intPart, decPart = ""] = cleaned.split("."); - const combined = intPart + decPart; - const shiftedDecPos = combined.length - digits - decPart.length; - - // Pad with leading zeros if the number is smaller than the factor - const padded = shiftedDecPos <= 0 - ? "0." + "0".repeat(-shiftedDecPos) + combined - : combined.slice(0, shiftedDecPos) + "." + combined.slice(shiftedDecPos); - - return formatTokenStr(padded || "0"); -} - -/** Fetch estimated rewards for a provider/validator — returns USD total + token breakdown */ -async function fetchEstimatedRewards( - type: "provider" | "validator", - address: string, -): Promise { - try { - const data = await fetchRest( - `/lavanet/lava/subscription/estimated_${type}_rewards/${address}/${APR_BENCHMARK_ULAVA}${APR_BENCHMARK_DENOM}`, - ); - return await processRewardTokens(data.total ?? []); - } catch { - return { totalUsd: 0, tokens: [] }; - } -} - -/** - * Fetch a provider's actual earned rewards (no benchmark amount) and group by spec. - * Matches jsinfo's rewards_last_month: calls estimated_provider_rewards/{addr}/ - * then splits "Boost: ETH1", "Pools: ETH1", "Subscription: ETH1" into per-spec groups. - * - * Pass blockHeight to query historical chain state (archive node required). - * Note: USD values still use current CoinGecko prices even for historical blocks — - * matches the old Job 2 behavior where CoinGecko was called at pipeline-run time. - */ -export async function fetchRewardsBySpec( - address: string, - specNameMap: Map, - blockHeight?: number, -): Promise { - try { - const data = await fetchRest( - `/lavanet/lava/subscription/estimated_provider_rewards/${address}/`, - blockHeight, - ); - - // Group info entries by spec (collapse Boost/Pools/Subscription sources) - const bySpec = new Map; totalUsd: number }>(); - - for (const entry of data.info ?? []) { - const parts = (entry.source as string).split(": "); - const spec = parts.length > 1 ? parts[1] : parts[0]; - if (!spec) continue; - - const key = spec.toLowerCase(); - const group = bySpec.get(key) ?? { tokens: new Map(), totalUsd: 0 }; - - const amounts = Array.isArray(entry.amount) ? entry.amount : [entry.amount]; - for (const coin of amounts) { - if (TEST_DENOMS.has(coin.denom)) continue; - const existing = group.tokens.get(coin.denom); - const amt = parseFloat(coin.amount) || 0; - if (existing) { - existing.amount += amt; - } else { - group.tokens.set(coin.denom, { amount: amt, denom: coin.denom }); - } - } - - bySpec.set(key, group); - } - - // Convert to output format with USD values - const results: RewardsBySpecEntry[] = []; - for (const [specKey, group] of bySpec) { - const tokens: RewardToken[] = []; - let specUsd = 0; - - for (const [, coin] of group.tokens) { - const processed = await processRewardTokens([ - { denom: coin.denom, amount: coin.amount.toString() }, - ]); - tokens.push(...processed.tokens); - specUsd += processed.totalUsd; - } - - results.push({ - chain: specNameMap.get(specKey.toUpperCase()) ?? specKey.toUpperCase(), - spec: specKey.toUpperCase(), - tokens, - total_usd: specUsd, - }); - } - - return results; - } catch { - return []; - } -} - -// ── Claimable rewards (jsinfo-shape) ───────────────────────────────────────── - -export interface ClaimableRewardEntry { - amount: string; // display amount in base units (e.g. "1.23" lava) - denom: string; // base denom (e.g. "lava") - usdcValue: string; // numeric USD string (no $) - provider: string; -} - -/** Convert multi-denom claimable rewards to jsinfo-shape entries. */ -export async function processClaimableRewards( - rewards: Array<{ denom: string; amount: string }>, - provider: string, -): Promise { - const processed = await processRewardTokens(rewards); - return processed.tokens.map((t) => ({ - amount: t.display_amount, - denom: t.display_denom, - usdcValue: t.value_usd.startsWith("$") ? t.value_usd.slice(1) : t.value_usd, - provider, - })); -} - -// ── Validators & rewards (jsinfo-shape) ────────────────────────────────────── - -export interface ValidatorCommissionRates { - rate: string; - max_rate: string; - max_change_rate: string; -} - -export interface ValidatorCommission { - commission_rates: ValidatorCommissionRates; - update_time: string; -} - -export interface ValidatorBase { - operator_address: string; - description?: { moniker?: string }; - jailed: boolean; - tokens: string; - commission: ValidatorCommission; -} - -interface RewardCoin { denom: string; amount: string } - -export interface TokenBreakdown { - tokens: RewardToken[]; - total_usd: number; -} - -export interface ValidatorDistributionRewards { - self_bond_rewards: TokenBreakdown; - commission: TokenBreakdown; - operator_address: string; -} - -export interface ValidatorDelegationResponse { - delegation: { - delegator_address: string; - validator_address: string; - shares: string; - }; - balance: RewardCoin; -} - -export interface ValidatorUnbondingEntry { - creation_height: string; - completion_time: string; - initial_balance: string; - balance: string; - unbonding_id: string; - unbonding_on_hold_ref_count: string; -} - -export interface ValidatorUnbondingResponse { - delegator_address: string; - validator_address: string; - entries: ValidatorUnbondingEntry[]; -} - -export interface ValidatorWithRewards { - address: string; - moniker: string; - jailed: boolean; - tokens: string; - commission: ValidatorCommission; - apr?: number | null; - distribution: ValidatorDistributionRewards; - outstanding_rewards: TokenBreakdown; - estimated_rewards: TokenBreakdown; - delegations: { - delegation_responses: ValidatorDelegationResponse[]; - pagination: { next_key: string | null; total: string }; - }; - unbonding_delegations: { - unbonding_responses: ValidatorUnbondingResponse[]; - pagination: { next_key: string | null; total: string }; - }; -} - -const EMPTY_BREAKDOWN: TokenBreakdown = { tokens: [], total_usd: 0 }; - -async function rewardsToBreakdown(rewards: RewardCoin[] | undefined): Promise { - if (!rewards || rewards.length === 0) return EMPTY_BREAKDOWN; - const processed = await processRewardTokens(rewards); - return { tokens: processed.tokens, total_usd: processed.totalUsd }; -} - -async function fetchAllValidatorObjects(): Promise { - const validators: ValidatorBase[] = []; - let nextKey: string | null = null; - do { - const params = new URLSearchParams({ - status: "BOND_STATUS_BONDED", - "pagination.limit": "200", - }); - if (nextKey) params.set("pagination.key", nextKey); - const data = await fetchRest<{ - validators: ValidatorBase[]; - pagination: { next_key: string | null }; - }>(`/cosmos/staking/v1beta1/validators?${params}`); - for (const v of data.validators ?? []) validators.push(v); - nextKey = data.pagination?.next_key ?? null; - } while (nextKey); - return validators; -} - -interface ValidatorDistributionRewardsResponse { - operator_address: string; - self_bond_rewards: RewardCoin[]; - commission: RewardCoin[]; -} - -async function fetchValidatorDistribution( - addr: string, -): Promise { - try { - return await fetchRest( - `/cosmos/distribution/v1beta1/validators/${addr}`, - ); - } catch { return null; } -} - -async function fetchValidatorOutstanding(addr: string): Promise { - try { - const data = await fetchRest<{ rewards: { rewards: RewardCoin[] } }>( - `/cosmos/distribution/v1beta1/validators/${addr}/outstanding_rewards`, - ); - return data.rewards?.rewards ?? []; - } catch { return []; } -} - -async function fetchValidatorEstimatedTotal(addr: string): Promise { - try { - const data = await fetchRest( - `/lavanet/lava/subscription/estimated_validator_rewards/${addr}/`, - ); - return data.total ?? []; - } catch { return []; } -} - -async function fetchValidatorDelegationsPage( - addr: string, -): Promise<{ delegation_responses: ValidatorDelegationResponse[]; pagination: { next_key: string | null; total: string } }> { - try { - return await fetchRest(`/cosmos/staking/v1beta1/validators/${addr}/delegations?pagination.limit=1000&pagination.count_total=true`); - } catch { - return { delegation_responses: [], pagination: { next_key: null, total: "0" } }; - } -} - -async function fetchValidatorUnbondingPage( - addr: string, -): Promise<{ unbonding_responses: ValidatorUnbondingResponse[]; pagination: { next_key: string | null; total: string } }> { - try { - return await fetchRest(`/cosmos/staking/v1beta1/validators/${addr}/unbonding_delegations?pagination.limit=1000&pagination.count_total=true`); - } catch { - return { unbonding_responses: [], pagination: { next_key: null, total: "0" } }; - } -} - -/** - * Build the full validator-with-rewards list matching jsinfo's - * /lava_mainnet_validators_and_rewards response shape. - * - * Each validator requires 5 chain RPC calls; processes 3 validators at a time - * (15 concurrent requests) to respect rate limits. With ~50 validators this - * completes in ~20-30s cold, well within the 6h cache window. - */ -export async function fetchValidatorsWithRewards(): Promise<{ - height: number; - datetime: number; - validators: ValidatorWithRewards[]; -}> { - await prewarmPriceCache(); - - const [validators, latestBlock] = await Promise.all([ - fetchAllValidatorObjects(), - fetchLatestBlockHeight(), - ]); - - const enriched: ValidatorWithRewards[] = []; - - for (let i = 0; i < validators.length; i += RPC_BATCH_SIZE) { - const batch = validators.slice(i, i + RPC_BATCH_SIZE); - const results = await Promise.all(batch.map(async (v) => { - const addr = v.operator_address; - const [dist, outstanding, estimatedTotal, delegations, unbonding] = await Promise.all([ - fetchValidatorDistribution(addr), - fetchValidatorOutstanding(addr), - fetchValidatorEstimatedTotal(addr), - fetchValidatorDelegationsPage(addr), - fetchValidatorUnbondingPage(addr), - ]); - - const [selfBond, commissionBreakdown, outstandingBreakdown, estimatedBreakdown] = await Promise.all([ - rewardsToBreakdown(dist?.self_bond_rewards), - rewardsToBreakdown(dist?.commission), - rewardsToBreakdown(outstanding), - rewardsToBreakdown(estimatedTotal), - ]); - - return { - address: addr, - moniker: v.description?.moniker ?? "", - jailed: v.jailed, - tokens: v.tokens, - commission: v.commission, - distribution: { - self_bond_rewards: selfBond, - commission: commissionBreakdown, - operator_address: dist?.operator_address ?? addr, - }, - outstanding_rewards: outstandingBreakdown, - estimated_rewards: estimatedBreakdown, - delegations, - unbonding_delegations: unbonding, - } satisfies ValidatorWithRewards; - })); - enriched.push(...results); - } - - return { - height: latestBlock.height, - datetime: Math.floor(new Date(latestBlock.time).getTime() / 1000), - validators: enriched, - }; -} - -/** Fetch all bonded validator operator addresses */ -async function fetchBondedValidators(): Promise { - const validators: string[] = []; - let nextKey: string | null = null; - do { - const params = new URLSearchParams({ - status: "BOND_STATUS_BONDED", - "pagination.limit": "200", - }); - if (nextKey) params.set("pagination.key", nextKey); - const data = await fetchRest<{ - validators: Array<{ operator_address: string }>; - pagination: { next_key: string | null }; - }>(`/cosmos/staking/v1beta1/validators?${params}`); - for (const v of data.validators ?? []) validators.push(v.operator_address); - nextKey = data.pagination?.next_key ?? null; - } while (nextKey); - return validators; -} - -/** Percentile calculation matching jsinfo `CalculatePercentile` */ -function calculatePercentile(values: number[], rank: number): number { - if (values.length === 0 || rank < 0 || rank > 1) return 0; - if (values.length === 1) return values[0]; - - const sorted = [...values].sort((a, b) => a - b); - const pos = Math.floor((sorted.length - 1) * rank); - - if (sorted.length % 2 === 0) { - return sorted[pos] + (sorted[pos + 1] - sorted[pos]) * rank; - } - return sorted[pos]; -} - -/** APR from monthly reward: (1 + rewardUsd/investedUsd)^12 - 1 */ -function calculateApr(rewardUsd: number, investedUsd: number): number { - if (investedUsd <= 0 || rewardUsd <= 0) return 0; - const rate = rewardUsd / investedUsd; - const apr = Math.pow(1 + rate, 12) - 1; - if (!isFinite(apr) || apr < APR_MIN || apr > 100) return 0; - return apr; -} - -// --- Weighted APR history (Redis, matches jsinfo AprWeighted) --- - -const APR_WEIGHTS = [0.4, 0.25, 0.15, 0.1, 0.05, 0.03, 0.02]; -const APR_DAYS_TO_KEEP = 7; -const APR_HISTORY_TTL = 30 * 24 * 60 * 60; // 30 days - -interface AprRecord { date: string; aprSum: number; count: number } -interface AprHistory { records: AprRecord[]; lastUpdated: string } - -function aprHistoryKey(type: string, address: string): string { - return `apr_history:${type}:${address}`; -} - -function todayStr(): string { - const d = new Date(); - return `${d.getFullYear()}/${String(d.getMonth() + 1).padStart(2, "0")}/${String(d.getDate()).padStart(2, "0")}`; -} - -async function storeApr(redis: Redis, type: string, address: string, apr: number): Promise { - if (apr === 0) return; - const key = aprHistoryKey(type, address); - const today = todayStr(); - try { - const raw = await redis.get(key); - const history: AprHistory = raw ? JSON.parse(raw) : { records: [], lastUpdated: today }; - - const rec = history.records.find((r) => r.date === today); - if (rec) { rec.aprSum += apr; rec.count += 1; } - else { history.records.push({ date: today, aprSum: apr, count: 1 }); } - - history.records = history.records - .sort((a, b) => b.date.localeCompare(a.date)) - .slice(0, APR_DAYS_TO_KEEP); - history.lastUpdated = today; - - await redis.set(key, JSON.stringify(history), "EX", APR_HISTORY_TTL); - } catch { /* Redis failures are non-fatal */ } -} - -async function getWeightedApr(redis: Redis, type: string, address: string): Promise { - try { - const raw = await redis.get(aprHistoryKey(type, address)); - if (!raw) return null; - const history: AprHistory = JSON.parse(raw); - if (history.records.length === 0) return null; - - let weightedSum = 0; - let weightSum = 0; - for (let i = 0; i < history.records.length && i < APR_WEIGHTS.length; i++) { - const avg = history.records[i].aprSum / history.records[i].count; - weightedSum += avg * APR_WEIGHTS[i]; - weightSum += APR_WEIGHTS[i]; - } - return weightSum > 0 ? weightedSum / weightSum : null; - } catch { - return null; - } -} - -/** - * Compute APR percentiles matching jsinfo `/apr` response. - * - * For each active provider and bonded validator: - * 1. Query `estimated_{type}_rewards` with a 10 000 LAVA benchmark - * 2. Convert multi-denom rewards to USD via CoinGecko - * 3. Compute APR via monthly compounding: (1 + monthlyRate)^12 - 1 - * 4. Store in 7-day weighted history (Redis, if available) - * 5. Take 80th percentile, cap at 30% - */ -export async function computeAPR(redis?: Redis | null): Promise<{ - restaking_apr_percentile: number; - staking_apr_percentile: number; -}> { - // Pre-warm all token prices in a single CoinGecko call - await prewarmPriceCache(); - - const lavaPrice = await fetchTokenUsdPrice("lava"); - const investedUsd = APR_BENCHMARK_LAVA * lavaPrice; - - const [providers, validators] = await Promise.all([ - fetchAllProviders(), - fetchBondedValidators(), - ]); - - const providerAddresses = providers.map((p) => p.address); - - // Collect per-entity APRs (batched by RPC_BATCH_SIZE) - const [providerAprs, validatorAprs] = await Promise.all([ - collectEntityAprs("provider", providerAddresses, investedUsd, redis), - collectEntityAprs("validator", validators, investedUsd, redis), - ]); - - return { - restaking_apr_percentile: Math.min( - calculatePercentile(providerAprs, APR_PERCENTILE), - APR_MAX_PERCENTILE_CAP, - ), - staking_apr_percentile: Math.min( - calculatePercentile(validatorAprs, APR_PERCENTILE), - APR_MAX_PERCENTILE_CAP, - ), - }; -} - -/** Batch-fetch estimated rewards, compute APR per entity, apply weighted history */ -async function collectEntityAprs( - type: "provider" | "validator", - addresses: string[], - investedUsd: number, - redis?: Redis | null, -): Promise { - const aprs: number[] = []; - - for (let i = 0; i < addresses.length; i += RPC_BATCH_SIZE) { - const batch = addresses.slice(i, i + RPC_BATCH_SIZE); - const rewards = await Promise.all( - batch.map((addr) => fetchEstimatedRewards(type, addr)), - ); - - for (let j = 0; j < batch.length; j++) { - const currentApr = calculateApr(rewards[j].totalUsd, investedUsd); - if (currentApr <= 0) continue; - - let finalApr = currentApr; - if (redis) { - await storeApr(redis, type, batch[j], currentApr); - const weighted = await getWeightedApr(redis, type, batch[j]); - if (weighted !== null) finalApr = weighted; - } - - if (finalApr > 0 && finalApr < APR_MAX_INDIVIDUAL) { - aprs.push(finalApr); - } - } - } - - return aprs; -} - -/** Max display APR for individual providers (matching jsinfo) */ -const MAX_DISPLAY_APR = 0.9; - -/** Format APR as percentage string matching jsinfo format */ -function formatAprPercent(apr: number): string { - if (apr <= 0) return "-"; - if (apr > MAX_DISPLAY_APR) return "90.0%"; - return `${(apr * 100).toFixed(4)}%`; -} - -/** Format commission as percentage string */ -function formatCommission(commission: string): string { - if (!commission) return "-"; - const n = parseFloat(commission); - if (!isFinite(n)) return "-"; - return `${n.toFixed(1)}%`; -} - -interface ProviderSpecEntry { - chain: string; - spec: string; - stakestatus: string; - stake: string; - addons: string; - extensions: string; - delegateCommission: string; - delegateTotal: string; - moniker: string; -} - -export interface RewardsBySpecEntry { - chain: string; - spec: string; - tokens: RewardToken[]; - total_usd: number; -} - -export interface AllProviderAprEntry { - address: string; - moniker: string; - apr: string; - commission: string; - "30_days_cu_served": string; - "30_days_relays_served": string; - rewards_10k_lava_delegation: RewardToken[]; - rewards_last_month: RewardsBySpecEntry[]; - specs: ProviderSpecEntry[]; - stake: string; - stakestatus: string; - addons: string; - extensions: string; - delegateTotal: string; - avatar: string | null; -} - -/** Build per-provider spec data + address list + spec name map from chain RPC */ -export async function fetchProvidersWithSpecs(): Promise<{ - providers: Map; - specNames: Map; -}> { - const allSpecs = await fetchAllSpecs(); - const specNames = new Map(allSpecs.map((s) => [s.index, s.name])); - const byAddress = new Map< - string, - { moniker: string; identity: string; commission: string; specs: ProviderSpecEntry[] } - >(); - - for (let i = 0; i < allSpecs.length; i += RPC_BATCH_SIZE) { - const batch = allSpecs.slice(i, i + RPC_BATCH_SIZE); - const results = await Promise.all( - batch.map((s) => fetchProvidersForSpec(s.index) - .then((ps) => ps.map((p) => ({ ...p, specId: s.index, specName: s.name }))) - .catch(() => [] as Array)), - ); - - for (const providers of results) { - for (const p of providers) { - const existing = byAddress.get(p.address); - const specEntry: ProviderSpecEntry = { - chain: p.specName, - spec: p.specId, - stakestatus: "Active", - stake: p.stake?.amount ?? "0", - addons: p.addons, - extensions: p.extensions, - delegateCommission: p.delegate_commission, - delegateTotal: p.delegate_total?.amount ?? "0", - moniker: p.moniker, - }; - - if (existing) { - existing.specs.push(specEntry); - if (!existing.commission && p.delegate_commission) existing.commission = p.delegate_commission; - if (!existing.identity && p.identity) existing.identity = p.identity; - } else { - byAddress.set(p.address, { - moniker: p.moniker ?? "", - identity: p.identity ?? "", - commission: p.delegate_commission ?? "", - specs: [specEntry], - }); - } - } - } - } - - return { providers: byAddress, specNames }; -} - -/** - * Compute per-provider APR data matching jsinfo `/all_providers_apr`. - * - * Returns array of provider objects with APR, commission, 30d relay data, - * per-token reward breakdown, specs, and avatar. - */ -export async function computeAllProvidersApr( - relay30d: Map, - redis?: Redis | null, -): Promise { - // Pre-warm all token prices in a single CoinGecko call - await prewarmPriceCache(); - - const lavaPrice = await fetchTokenUsdPrice("lava"); - const investedUsd = APR_BENCHMARK_LAVA * lavaPrice; - - const { providers: providerMap, specNames } = await fetchProvidersWithSpecs(); - const addresses = Array.from(providerMap.keys()); - const results: AllProviderAprEntry[] = []; - - for (let i = 0; i < addresses.length; i += RPC_BATCH_SIZE) { - const batch = addresses.slice(i, i + RPC_BATCH_SIZE); - const [rewardResults, rewardsLastMonthResults, avatarResults] = await Promise.all([ - Promise.all(batch.map((addr) => fetchEstimatedRewards("provider", addr))), - Promise.all(batch.map((addr) => fetchRewardsBySpec(addr, specNames))), - Promise.all(batch.map((addr) => { - const p = providerMap.get(addr)!; - return fetchProviderAvatar(addr, p.identity || undefined).catch(() => null); - })), - ]); - - for (let j = 0; j < batch.length; j++) { - const addr = batch[j]; - const provider = providerMap.get(addr)!; - const rewards = rewardResults[j]; - const rewardsLastMonth = rewardsLastMonthResults[j]; - const avatar = avatarResults[j]; - const currentApr = calculateApr(rewards.totalUsd, investedUsd); - - let finalApr = currentApr; - if (redis && currentApr > 0) { - await storeApr(redis, "provider", addr, currentApr); - const weighted = await getWeightedApr(redis, "provider", addr); - if (weighted !== null) finalApr = weighted; - } - - const relay = relay30d.get(addr); - const firstSpec = provider.specs[0]; - - results.push({ - address: addr, - moniker: provider.moniker || "-", - apr: formatAprPercent(finalApr), - commission: formatCommission(provider.commission), - "30_days_cu_served": relay?.cu ?? "-", - "30_days_relays_served": relay?.relays ?? "-", - rewards_10k_lava_delegation: rewards.tokens, - rewards_last_month: rewardsLastMonth, - specs: provider.specs, - stake: firstSpec?.stake ?? "", - stakestatus: firstSpec ? "Active" : "", - addons: firstSpec?.addons ?? "", - extensions: firstSpec?.extensions ?? "", - delegateTotal: firstSpec?.delegateTotal ?? "", - avatar, - }); - } - } - - return results; -} - -export async function fetchLatestBlockHeight(): Promise<{ - height: number; - time: string; -}> { - const LAVA_RPC_URL = process.env.LAVA_RPC_URL ?? "https://lava.tendermintrpc.lava.build:443"; - const res = await fetch(`${LAVA_RPC_URL}/status`, { - signal: AbortSignal.timeout(10_000), - }); - if (!res.ok) throw new Error(`RPC ${res.status}`); - const data = (await res.json()) as { - result: { sync_info: { latest_block_height: string; latest_block_time: string } }; - }; - return { - height: parseInt(data.result.sync_info.latest_block_height, 10), - time: data.result.sync_info.latest_block_time, - }; -} - -// Request coalescing: concurrent callers share one in-flight fetchAllProviders -let pendingProviders: Promise | null = null; - -type AllProvidersResult = Array<{ - address: string; - moniker: string; - identity: string; - totalStake: string; - totalDelegation: string; - commission: string; - specs: string[]; -}>; - -/** Fetch all providers across all specs, with dedup by address. - * Concurrent callers share one in-flight request (coalesced). */ -export function fetchAllProviders(): Promise { - if (pendingProviders) return pendingProviders; - pendingProviders = fetchAllProvidersImpl().finally(() => { pendingProviders = null; }); - return pendingProviders; -} - -// Moniker-only cache. Routes that need just address→moniker shouldn't pay -// the full fetchAllProviders cost (stake/delegation/commission/specs) on -// cache misses. 60s TTL is short enough to stay fresh, long enough to -// deduplicate the 3+ reward routes that all call it in the same minute. -const MONIKER_CACHE_TTL_MS = 60_000; -let monikerCache: { at: number; map: Map } | null = null; -let pendingMonikers: Promise> | null = null; - -/** address → moniker map. 60-second in-process cache shared across routes. */ -export function fetchAllProviderMonikers(): Promise> { - if (monikerCache && Date.now() - monikerCache.at < MONIKER_CACHE_TTL_MS) { - return Promise.resolve(monikerCache.map); - } - if (pendingMonikers) return pendingMonikers; - pendingMonikers = (async () => { - const providers = await fetchAllProviders(); - const map = new Map(); - for (const p of providers) map.set(p.address, p.moniker); - monikerCache = { at: Date.now(), map }; - return map; - })().finally(() => { pendingMonikers = null; }); - return pendingMonikers; -} - -async function fetchAllProvidersImpl(): Promise { - const specs = await fetchAllSpecs(); - - interface SpecEntry { - address: string; - moniker: string; - identity: string; - stake?: { amount: string }; - delegate_total?: { amount: string }; - delegate_commission: string; - specId: string; - } - - // Fetch in batches of RPC_BATCH_SIZE — raise via env for dedicated RPC endpoints. - const specProviders: Array> = []; - for (let i = 0; i < specs.length; i += RPC_BATCH_SIZE) { - const batch = specs.slice(i, i + RPC_BATCH_SIZE); - const results = await Promise.all( - batch.map((s) => - fetchProvidersForSpec(s.index) - .then((providers) => providers.map((p): SpecEntry => ({ - address: p.address, - moniker: p.moniker, - identity: p.identity, - stake: p.stake, - delegate_total: p.delegate_total, - delegate_commission: p.delegate_commission, - specId: s.index, - }))) - .catch(() => [] as SpecEntry[]), - ), - ); - specProviders.push(...results); - } - - const byAddress = new Map< - string, - { moniker: string; identity: string; totalStake: bigint; totalDelegation: bigint; commission: string; specs: string[] } - >(); - - for (const providers of specProviders) { - for (const p of providers) { - const existing = byAddress.get(p.address); - const stake = BigInt(p.stake?.amount ?? "0"); - const delegation = BigInt(p.delegate_total?.amount ?? "0"); - if (existing) { - existing.totalStake += stake; - existing.totalDelegation += delegation; - existing.specs.push(p.specId); - if (!existing.commission && p.delegate_commission) { - existing.commission = p.delegate_commission; - } - if (!existing.identity && p.identity) { - existing.identity = p.identity; - } - } else { - byAddress.set(p.address, { - moniker: p.moniker ?? "", - identity: p.identity ?? "", - totalStake: stake, - totalDelegation: delegation, - commission: p.delegate_commission ?? "", - specs: [p.specId], - }); - } - } - } - - return Array.from(byAddress.entries()).map(([address, data]) => ({ - address, - moniker: data.moniker, - identity: data.identity, - totalStake: data.totalStake.toString(), - totalDelegation: data.totalDelegation.toString(), - commission: data.commission, - specs: data.specs, - })); -} - -export async function fetchDelegatorRewards(provider: string): Promise< - Array<{ denom: string; amount: string }> -> { - try { - const data = await fetchRest<{ - rewards: Array<{ provider: string; amount: Array<{ denom: string; amount: string }> }>; - }>(`/lavanet/lava/dualstaking/delegator_rewards/${provider}`); - const entry = data.rewards?.find((r) => r.provider === provider); - return entry?.amount ?? []; - } catch { - return []; - } -} - -async function fetchProviderMetadata(provider: string): Promise<{ - description?: { identity?: string; moniker?: string }; -} | null> { - try { - const specs = await fetchAllSpecs(); - for (const spec of specs) { - try { - const data = await fetchRest<{ - stakeEntry: Array<{ - address: string; - description?: { identity?: string; moniker?: string }; - }>; - }>(`/lavanet/lava/pairing/providers/${spec.index}`); - const match = data.stakeEntry?.find((p) => p.address === provider); - if (match?.description?.identity) return match; - } catch { - continue; - } - } - return null; - } catch { - return null; - } -} - -export async function fetchProviderAvatar(provider: string, identityHint?: string): Promise { - try { - let identity = identityHint; - if (!identity) { - const meta = await fetchProviderMetadata(provider); - identity = meta?.description?.identity ?? undefined; - } - if (!identity) return null; - - const KEYBASE_API_URL = process.env.KEYBASE_API_URL ?? "https://keybase.io/_/api/1.0"; - const res = await fetch( - `${KEYBASE_API_URL}/user/lookup.json?key_suffix=${encodeURIComponent(identity)}&fields=pictures`, - { signal: AbortSignal.timeout(10_000) }, - ); - if (!res.ok) return null; - const data = (await res.json()) as { - them?: Array<{ pictures?: { primary?: { url?: string } } }>; - }; - return data.them?.[0]?.pictures?.primary?.url ?? null; - } catch { - return null; - } -} +// Thin barrel: lava.ts was a 1,673-line god-module covering 10 distinct +// concerns. It's now split into focused modules below; this barrel preserves +// the original import surface so existing callers don't need to change. +// New code should import directly from the focused modules. + +export { RPC_BATCH_SIZE } from "./rest.js"; +export { fetchBlockAtTimestamp, fetchLatestBlockHeight } from "./block.js"; +export { fetchAllSpecs } from "./specs.js"; +export { fetchTotalSupply, fetchCirculatingSupply } from "./supply.js"; +export { + type ProviderEndpoint, + type ProviderForSpec, + type AllProvidersResult, + type ProviderSpecEntry, + fetchProvidersForSpec, + fetchProvidersWithSpecs, + fetchAllProviders, + fetchAllProviderMonikers, + fetchDelegatorRewards, +} from "./providers.js"; +export { + fetchLavaUsdPrice, + prewarmPriceCache, + computeTVL, +} from "./pricing.js"; +export { + type RewardToken, + type ProcessedRewards, + type RewardsBySpecEntry, + type ClaimableRewardEntry, + type TokenBreakdown, + fetchRewardsBySpec, + processClaimableRewards, +} from "./rewards.js"; +export { + type ValidatorCommissionRates, + type ValidatorCommission, + type ValidatorBase, + type ValidatorDistributionRewards, + type ValidatorDelegationResponse, + type ValidatorUnbondingEntry, + type ValidatorUnbondingResponse, + type ValidatorWithRewards, + fetchValidatorsWithRewards, +} from "./validators.js"; +export { + type AllProviderAprEntry, + computeAPR, + computeAllProvidersApr, +} from "./apr.js"; +export { fetchProviderAvatar } from "./keybase.js"; diff --git a/apps/api/src/rpc/pricing.ts b/apps/api/src/rpc/pricing.ts new file mode 100644 index 0000000..2752b85 --- /dev/null +++ b/apps/api/src/rpc/pricing.ts @@ -0,0 +1,291 @@ +import { fetchRest } from "./rest.js"; +import { fetchStakingPool } from "./supply.js"; + +const COINGECKO_API_URL = process.env.COINGECKO_API_URL ?? "https://api.coingecko.com/api/v3"; + +export async function fetchLavaUsdPrice(): Promise { + const res = await fetch( + `${COINGECKO_API_URL}/simple/price?ids=lava-network&vs_currencies=usd`, + { signal: AbortSignal.timeout(15_000) }, + ); + if (!res.ok) throw new Error(`CoinGecko ${res.status}`); + const data = (await res.json()) as { "lava-network"?: { usd?: number } }; + const price = data["lava-network"]?.usd; + if (!price || price <= 0) throw new Error("Invalid LAVA price from CoinGecko"); + return price; +} + +/** Denom → base unit conversion (matching jsinfo DENOM_CONVERSIONS) */ +export const DENOM_CONVERSIONS: Record = { + ulava: { baseDenom: "lava", factor: 1_000_000 }, + uatom: { baseDenom: "atom", factor: 1_000_000 }, + uosmo: { baseDenom: "osmo", factor: 1_000_000 }, + ujuno: { baseDenom: "juno", factor: 1_000_000 }, + ustars: { baseDenom: "stars", factor: 1_000_000 }, + uakt: { baseDenom: "akt", factor: 1_000_000 }, + uhuahua: { baseDenom: "huahua", factor: 1_000_000 }, + uevmos: { baseDenom: "evmos", factor: 1e18 }, + inj: { baseDenom: "inj", factor: 1e18 }, + aevmos: { baseDenom: "evmos", factor: 1e18 }, + basecro: { baseDenom: "cro", factor: 1e8 }, + uscrt: { baseDenom: "scrt", factor: 1_000_000 }, + uiris: { baseDenom: "iris", factor: 1_000_000 }, + uregen: { baseDenom: "regen", factor: 1_000_000 }, + uion: { baseDenom: "ion", factor: 1_000_000 }, + nanolike: { baseDenom: "like", factor: 1e9 }, + uaxl: { baseDenom: "axl", factor: 1_000_000 }, + uband: { baseDenom: "band", factor: 1_000_000 }, + ubld: { baseDenom: "bld", factor: 1_000_000 }, + ucmdx: { baseDenom: "cmdx", factor: 1_000_000 }, + ucre: { baseDenom: "cre", factor: 1_000_000 }, + uxprt: { baseDenom: "xprt", factor: 1_000_000 }, + uusdc: { baseDenom: "usdc", factor: 1_000_000 }, + "unit-move": { baseDenom: "move", factor: 1e7 }, +}; + +/** Base denom → CoinGecko coin ID */ +export const DENOM_COINGECKO_ID: Record = { + lava: "lava-network", atom: "cosmos", osmo: "osmosis", + juno: "juno-network", stars: "stargaze", akt: "akash-network", + huahua: "chihuahua-token", evmos: "evmos", inj: "injective-protocol", + cro: "crypto-com-chain", scrt: "secret", iris: "iris-network", + regen: "regen", ion: "ion", like: "likecoin", axl: "axelar", + band: "band-protocol", bld: "agoric", cmdx: "comdex", + cre: "crescent-network", xprt: "persistence", usdc: "usd-coin", + move: "movement", +}; + +/** In-memory price cache (5 min TTL) */ +const priceCache = new Map(); +const PRICE_CACHE_MS = 300_000; + +/** + * Pre-warm the price cache with a single batch CoinGecko call for all known denoms. + * CoinGecko supports comma-separated IDs in one request. + */ +export async function prewarmPriceCache(): Promise { + const now = Date.now(); + // Skip if all entries are still fresh + const allFresh = Object.keys(DENOM_COINGECKO_ID).every((d) => { + const c = priceCache.get(d); + return c && now - c.ts < PRICE_CACHE_MS; + }); + if (allFresh) return; + + const ids = [...new Set(Object.values(DENOM_COINGECKO_ID))].join(","); + try { + const res = await fetch( + `https://api.coingecko.com/api/v3/simple/price?ids=${ids}&vs_currencies=usd`, + { signal: AbortSignal.timeout(15_000) }, + ); + if (!res.ok) return; + const data = (await res.json()) as Record; + const ts = Date.now(); + for (const [denom, cgId] of Object.entries(DENOM_COINGECKO_ID)) { + const price = data[cgId]?.usd ?? 0; + if (price > 0) priceCache.set(denom, { price, ts }); + } + } catch { + // Keep stale cache on failure + } +} + +/** Fetch USD price for a base denom — reads from cache (pre-warmed by prewarmPriceCache) */ +export async function fetchTokenUsdPrice(baseDenom: string): Promise { + const cached = priceCache.get(baseDenom); + if (cached && Date.now() - cached.ts < PRICE_CACHE_MS) return cached.price; + + // Fallback: single-denom fetch if cache miss (shouldn't happen after prewarm) + const id = DENOM_COINGECKO_ID[baseDenom]; + if (!id) return 0; + + try { + const res = await fetch( + `https://api.coingecko.com/api/v3/simple/price?ids=${id}&vs_currencies=usd`, + { signal: AbortSignal.timeout(15_000) }, + ); + if (!res.ok) return cached?.price ?? 0; + const data = (await res.json()) as Record; + const price = data[id]?.usd ?? 0; + if (price > 0) priceCache.set(baseDenom, { price, ts: Date.now() }); + return price; + } catch { + return cached?.price ?? 0; + } +} + +/** Resolve an IBC hash to its base denom via chain RPC */ +export async function fetchDenomTrace(ibcHash: string): Promise { + try { + const data = await fetchRest<{ + denom_trace: { base_denom: string }; + }>(`/ibc/apps/transfer/v1/denom_traces/${ibcHash}`); + return data.denom_trace?.base_denom ?? null; + } catch { + return null; + } +} + +// ── TVL computation (jsinfo-shape) ─────────────────────────────────────────── + +// Reward pools that count toward TVL (iprpc_pool excluded) +const TVL_REWARD_POOLS = [ + "validators_rewards_distribution_pool", + "validators_rewards_allocation_pool", + "providers_rewards_distribution_pool", + "providers_rewards_allocation_pool", +]; + +async function fetchTvlRewardPoolsUlava(): Promise { + const data = await fetchRest<{ + pools: Array<{ name: string; balance: Array<{ denom: string; amount: string }> }>; + }>("/lavanet/lava/rewards/pools"); + let total = 0n; + for (const pool of data.pools ?? []) { + if (TVL_REWARD_POOLS.includes(pool.name)) { + for (const coin of pool.balance ?? []) { + if (coin.denom === "ulava") total += BigInt(coin.amount); + } + } + } + return total; +} + +async function fetchSubscriptionCreditsUlava(): Promise { + try { + const data = await fetchRest<{ + subs_info: Array<{ credit: { amount: string; denom: string } }>; + }>("/lavanet/lava/subscription/list"); + let total = 0n; + for (const sub of data.subs_info ?? []) { + if (sub.credit?.denom === "ulava") { + const amount = BigInt(sub.credit.amount || "0"); + if (amount > 0n) total += amount; + } + } + return total; + } catch { + return 0n; + } +} + +async function fetchOsmosisLavaUlava(): Promise { + try { + const input = JSON.stringify({ + json: { + limit: 10, search: null, denoms: ["LAVA"], + types: ["weighted", "stable", "concentrated", "cosmwasm-transmuter", + "cosmwasm", "cosmwasm-astroport-pcl", "cosmwasm-whitewhale"], + incentiveTypes: ["superfluid", "osmosis", "boost", "none"], + sort: { keyPath: "market.volume24hUsd", direction: "desc" }, + minLiquidityUsd: 1000, cursor: 0, + }, + meta: { values: { search: ["undefined"] } }, + }); + const res = await fetch( + `https://app.osmosis.zone/api/edge-trpc-pools/pools.getPools?input=${encodeURIComponent(input)}`, + { signal: AbortSignal.timeout(15_000) }, + ); + if (!res.ok) return 0n; + const data = (await res.json()) as { + result: { data: { json: { items: Array<{ + reserveCoins: Array<{ currency: { coinDenom: string }; amount: string }>; + }> } } }; + }; + let total = 0n; + for (const pool of data.result?.data?.json?.items ?? []) { + for (const coin of pool.reserveCoins ?? []) { + if (coin.currency?.coinDenom?.toLowerCase() === "lava") { + total += BigInt(Math.round(parseFloat(coin.amount || "0"))); + } + } + } + return total; + } catch { + return 0n; + } +} + +async function fetchBaseDexUsd(): Promise { + try { + const res = await fetch( + "https://api.geckoterminal.com/api/v2/search/pools?query=lava", + { signal: AbortSignal.timeout(15_000) }, + ); + if (!res.ok) return 0; + const data = (await res.json()) as { + data: Array<{ id: string; attributes: { reserve_in_usd: string } }>; + }; + const basePool = data.data?.find((p: { id: string }) => p.id.startsWith("base_")); + return basePool ? parseFloat(basePool.attributes.reserve_in_usd || "0") : 0; + } catch { + return 0; + } +} + +async function fetchArbitrumDexUsd(): Promise { + try { + const res = await fetch("https://interface.gateway.uniswap.org/v1/graphql", { + method: "POST", + signal: AbortSignal.timeout(15_000), + headers: { + "Content-Type": "application/json", + Origin: "https://app.uniswap.org", + }, + body: JSON.stringify({ + query: `query($chain: Chain!, $address: String) { + token(chain: $chain, address: $address) { + market(currency: USD) { totalValueLocked { value } } + } + }`, + variables: { + address: "0x11e969e9b3f89cb16d686a03cd8508c9fc0361af", + chain: "ARBITRUM", + }, + }), + }); + if (!res.ok) return 0; + const data = (await res.json()) as { + data?: { token?: { market?: { totalValueLocked?: { value?: number } } } }; + }; + return data.data?.token?.market?.totalValueLocked?.value ?? 0; + } catch { + return 0; + } +} + +/** + * TVL (USD) = bonded tokens + 4 reward pools + subscriptions + DEX liquidity + * Matches jsinfo formula: all components converted to USD via CoinGecko LAVA price. + */ +export async function computeTVL(): Promise<{ tvl: string }> { + const [lavaPrice, pool, rewardPools, subscriptions, osmosis, baseUsd, arbitrumUsd] = + await Promise.all([ + fetchLavaUsdPrice(), + fetchStakingPool(), + fetchTvlRewardPoolsUlava(), + fetchSubscriptionCreditsUlava(), + fetchOsmosisLavaUlava(), + fetchBaseDexUsd(), + fetchArbitrumDexUsd(), + ]); + + const bondedTokens = BigInt(pool.bonded_tokens); + + // Convert DEX USD values to ulava to match jsinfo round-trip + const baseUlava = lavaPrice > 0 + ? BigInt(Math.round((baseUsd / lavaPrice) * 1_000_000)) + : 0n; + const arbitrumUlava = lavaPrice > 0 + ? BigInt(Math.round((arbitrumUsd / lavaPrice) * 1_000_000)) + : 0n; + + // Sum all components in ulava, then convert to USD + // Use BigInt division to avoid Number precision loss for large ulava sums + const totalUlava = bondedTokens + rewardPools + subscriptions + + osmosis + baseUlava + arbitrumUlava; + const totalLava = Number(totalUlava / 1_000_000n) + Number(totalUlava % 1_000_000n) / 1_000_000; + const tvlUsd = totalLava * lavaPrice; + + return { tvl: tvlUsd.toFixed(4) }; +} diff --git a/apps/api/src/rpc/providers.ts b/apps/api/src/rpc/providers.ts new file mode 100644 index 0000000..462ba8a --- /dev/null +++ b/apps/api/src/rpc/providers.ts @@ -0,0 +1,286 @@ +import { RPC_BATCH_SIZE, fetchRest } from "./rest.js"; +import { fetchAllSpecs } from "./specs.js"; + +export interface ProviderEndpoint { + iPPORT: string; + geolocation: number; + apiInterfaces: string[]; + addons: string[]; + extensions: string[]; +} + +export interface ProviderForSpec { + address: string; + moniker: string; + identity: string; + stake: { amount: string }; + delegate_total: { amount: string }; + delegate_commission: string; + geolocation: number; + addons: string; + extensions: string; + apiInterfaces: string; + endpoints: ProviderEndpoint[]; +} + +export async function fetchProvidersForSpec(specId: string): Promise { + const data = await fetchRest<{ + stakeEntry: Array<{ + address: string; + moniker: string; + description?: { identity?: string }; + stake: { amount: string }; + delegate_total: { amount: string }; + delegate_commission: string; + geolocation: number; + endpoints?: Array<{ + iPPORT?: string; + geolocation?: number; + addons?: string[]; + extensions?: string[]; + api_interfaces?: string[]; + }>; + }>; + }>(`/lavanet/lava/pairing/providers/${specId}`); + + return (data.stakeEntry ?? []).map((entry) => { + const allAddons = new Set(); + const allExtensions = new Set(); + const allApiInterfaces = new Set(); + const endpoints: ProviderEndpoint[] = []; + + for (const ep of entry.endpoints ?? []) { + for (const a of ep.addons ?? []) if (a) allAddons.add(a); + for (const e of ep.extensions ?? []) if (e) allExtensions.add(e); + for (const i of ep.api_interfaces ?? []) if (i) allApiInterfaces.add(i); + + if (ep.iPPORT) { + endpoints.push({ + iPPORT: ep.iPPORT, + geolocation: ep.geolocation ?? entry.geolocation, + apiInterfaces: (ep.api_interfaces ?? []).filter(Boolean), + addons: (ep.addons ?? []).filter(Boolean), + extensions: (ep.extensions ?? []).filter(Boolean), + }); + } + } + + return { + address: entry.address, + moniker: entry.moniker, + identity: entry.description?.identity ?? "", + stake: entry.stake, + delegate_total: entry.delegate_total, + delegate_commission: entry.delegate_commission, + geolocation: entry.geolocation, + addons: Array.from(allAddons).join(","), + extensions: Array.from(allExtensions).join(","), + apiInterfaces: Array.from(allApiInterfaces).join(","), + endpoints, + }; + }); +} + +export interface ProviderSpecEntry { + chain: string; + spec: string; + stakestatus: string; + stake: string; + addons: string; + extensions: string; + delegateCommission: string; + delegateTotal: string; + moniker: string; +} + +/** Build per-provider spec data + address list + spec name map from chain RPC */ +export async function fetchProvidersWithSpecs(): Promise<{ + providers: Map; + specNames: Map; +}> { + const allSpecs = await fetchAllSpecs(); + const specNames = new Map(allSpecs.map((s) => [s.index, s.name])); + const byAddress = new Map< + string, + { moniker: string; identity: string; commission: string; specs: ProviderSpecEntry[] } + >(); + + for (let i = 0; i < allSpecs.length; i += RPC_BATCH_SIZE) { + const batch = allSpecs.slice(i, i + RPC_BATCH_SIZE); + const results = await Promise.all( + batch.map((s) => fetchProvidersForSpec(s.index) + .then((ps) => ps.map((p) => ({ ...p, specId: s.index, specName: s.name }))) + .catch(() => [] as Array)), + ); + + for (const providers of results) { + for (const p of providers) { + const existing = byAddress.get(p.address); + const specEntry: ProviderSpecEntry = { + chain: p.specName, + spec: p.specId, + stakestatus: "Active", + stake: p.stake?.amount ?? "0", + addons: p.addons, + extensions: p.extensions, + delegateCommission: p.delegate_commission, + delegateTotal: p.delegate_total?.amount ?? "0", + moniker: p.moniker, + }; + + if (existing) { + existing.specs.push(specEntry); + if (!existing.commission && p.delegate_commission) existing.commission = p.delegate_commission; + if (!existing.identity && p.identity) existing.identity = p.identity; + } else { + byAddress.set(p.address, { + moniker: p.moniker ?? "", + identity: p.identity ?? "", + commission: p.delegate_commission ?? "", + specs: [specEntry], + }); + } + } + } + } + + return { providers: byAddress, specNames }; +} + +export type AllProvidersResult = Array<{ + address: string; + moniker: string; + identity: string; + totalStake: string; + totalDelegation: string; + commission: string; + specs: string[]; +}>; + +// Request coalescing: concurrent callers share one in-flight fetchAllProviders +let pendingProviders: Promise | null = null; + +/** Fetch all providers across all specs, with dedup by address. + * Concurrent callers share one in-flight request (coalesced). */ +export function fetchAllProviders(): Promise { + if (pendingProviders) return pendingProviders; + pendingProviders = fetchAllProvidersImpl().finally(() => { pendingProviders = null; }); + return pendingProviders; +} + +// Moniker-only cache. Routes that need just address→moniker shouldn't pay +// the full fetchAllProviders cost (stake/delegation/commission/specs) on +// cache misses. 60s TTL is short enough to stay fresh, long enough to +// deduplicate the 3+ reward routes that all call it in the same minute. +const MONIKER_CACHE_TTL_MS = 60_000; +let monikerCache: { at: number; map: Map } | null = null; +let pendingMonikers: Promise> | null = null; + +/** address → moniker map. 60-second in-process cache shared across routes. */ +export function fetchAllProviderMonikers(): Promise> { + if (monikerCache && Date.now() - monikerCache.at < MONIKER_CACHE_TTL_MS) { + return Promise.resolve(monikerCache.map); + } + if (pendingMonikers) return pendingMonikers; + pendingMonikers = (async () => { + const providers = await fetchAllProviders(); + const map = new Map(); + for (const p of providers) map.set(p.address, p.moniker); + monikerCache = { at: Date.now(), map }; + return map; + })().finally(() => { pendingMonikers = null; }); + return pendingMonikers; +} + +async function fetchAllProvidersImpl(): Promise { + const specs = await fetchAllSpecs(); + + interface SpecEntry { + address: string; + moniker: string; + identity: string; + stake?: { amount: string }; + delegate_total?: { amount: string }; + delegate_commission: string; + specId: string; + } + + // Fetch in batches of RPC_BATCH_SIZE — raise via env for dedicated RPC endpoints. + const specProviders: Array> = []; + for (let i = 0; i < specs.length; i += RPC_BATCH_SIZE) { + const batch = specs.slice(i, i + RPC_BATCH_SIZE); + const results = await Promise.all( + batch.map((s) => + fetchProvidersForSpec(s.index) + .then((providers) => providers.map((p): SpecEntry => ({ + address: p.address, + moniker: p.moniker, + identity: p.identity, + stake: p.stake, + delegate_total: p.delegate_total, + delegate_commission: p.delegate_commission, + specId: s.index, + }))) + .catch(() => [] as SpecEntry[]), + ), + ); + specProviders.push(...results); + } + + const byAddress = new Map< + string, + { moniker: string; identity: string; totalStake: bigint; totalDelegation: bigint; commission: string; specs: string[] } + >(); + + for (const providers of specProviders) { + for (const p of providers) { + const existing = byAddress.get(p.address); + const stake = BigInt(p.stake?.amount ?? "0"); + const delegation = BigInt(p.delegate_total?.amount ?? "0"); + if (existing) { + existing.totalStake += stake; + existing.totalDelegation += delegation; + existing.specs.push(p.specId); + if (!existing.commission && p.delegate_commission) { + existing.commission = p.delegate_commission; + } + if (!existing.identity && p.identity) { + existing.identity = p.identity; + } + } else { + byAddress.set(p.address, { + moniker: p.moniker ?? "", + identity: p.identity ?? "", + totalStake: stake, + totalDelegation: delegation, + commission: p.delegate_commission ?? "", + specs: [p.specId], + }); + } + } + } + + return Array.from(byAddress.entries()).map(([address, data]) => ({ + address, + moniker: data.moniker, + identity: data.identity, + totalStake: data.totalStake.toString(), + totalDelegation: data.totalDelegation.toString(), + commission: data.commission, + specs: data.specs, + })); +} + +export async function fetchDelegatorRewards(provider: string): Promise< + Array<{ denom: string; amount: string }> +> { + try { + const data = await fetchRest<{ + rewards: Array<{ provider: string; amount: Array<{ denom: string; amount: string }> }>; + }>(`/lavanet/lava/dualstaking/delegator_rewards/${provider}`); + const entry = data.rewards?.find((r) => r.provider === provider); + return entry?.amount ?? []; + } catch { + return []; + } +} diff --git a/apps/api/src/rpc/rest.ts b/apps/api/src/rpc/rest.ts new file mode 100644 index 0000000..0f44e77 --- /dev/null +++ b/apps/api/src/rpc/rest.ts @@ -0,0 +1,36 @@ +import pino from "pino"; + +export const logger = pino({ name: "rpc" }); + +export const LAVA_REST_URL = process.env.LAVA_REST_URL ?? "https://lava.rest.lava.build"; + +// Concurrent in-flight RPC calls per batch loop. Operators running dedicated +// RPC endpoints can raise this (env: RPC_BATCH_SIZE). Public endpoints should +// keep a conservative value (~5) to avoid rate limiting. +export const RPC_BATCH_SIZE = Math.max(1, parseInt(process.env.RPC_BATCH_SIZE ?? "25", 10)); + +// Request coalescing: concurrent fetches for the same path share one in-flight request +const inflightRpc = new Map>(); + +export async function fetchRest(path: string, blockHeight?: number): Promise { + const cacheKey = blockHeight ? `${path}@${blockHeight}` : path; + const existing = inflightRpc.get(cacheKey); + if (existing) return existing as Promise; + + const promise = (async () => { + const headers: Record = {}; + if (blockHeight) headers["x-cosmos-block-height"] = String(blockHeight); + + const res = await fetch(`${LAVA_REST_URL}${path}`, { + headers, + signal: AbortSignal.timeout(15_000), + }); + if (!res.ok) throw new Error(`RPC ${res.status}: ${res.statusText}`); + return (await res.json()) as T; + })(); + + inflightRpc.set(cacheKey, promise); + const cleanup = () => { inflightRpc.delete(cacheKey); }; + promise.then(cleanup, cleanup); + return promise; +} diff --git a/apps/api/src/rpc/rewards.ts b/apps/api/src/rpc/rewards.ts new file mode 100644 index 0000000..b286b65 --- /dev/null +++ b/apps/api/src/rpc/rewards.ts @@ -0,0 +1,245 @@ +import { fetchRest } from "./rest.js"; +import { + DENOM_CONVERSIONS, + fetchDenomTrace, + fetchTokenUsdPrice, +} from "./pricing.js"; + +/** Benchmark: 10,000 LAVA in ulava / base units */ +export const APR_BENCHMARK_ULAVA = 10_000_000_000; +export const APR_BENCHMARK_LAVA = 10_000; +const APR_BENCHMARK_DENOM = "ulava"; + +export interface EstimatedRewardsResponse { + info: Array<{ + source: string; + amount: { denom: string; amount: string } | Array<{ denom: string; amount: string }>; + }>; + total: Array<{ denom: string; amount: string }>; + recommended_block?: string; +} + +/** IBC test denoms to skip (matching jsinfo) */ +const TEST_DENOMS = new Set([ + "ibc/E3FCBEDDBAC500B1BAB90395C7D1E4F33D9B9ECFE82A16ED7D7D141A0152323F", +]); + +/** Token breakdown matching jsinfo RewardAmount */ +export interface RewardToken { + source_denom: string; + resolved_amount: string; + resolved_denom: string; + display_denom: string; + display_amount: string; + value_usd: string; +} + +export interface ProcessedRewards { + totalUsd: number; + tokens: RewardToken[]; +} + +/** Convert multi-denom reward array to USD total + per-token breakdown */ +async function processRewardTokens( + rewards: Array<{ denom: string; amount: string }>, +): Promise { + const tokens: RewardToken[] = []; + let totalUsd = 0; + + for (const { denom, amount } of rewards) { + if (TEST_DENOMS.has(denom)) continue; + + // Resolve IBC denoms to their base denom + let rawDenom = denom; + let resolvedDenom = denom; + if (denom.startsWith("ibc/")) { + const resolved = await fetchDenomTrace(denom.slice(4)); + if (!resolved) continue; + rawDenom = resolved; + resolvedDenom = resolved; + } + + const conversion = DENOM_CONVERSIONS[rawDenom]; + if (!conversion) continue; + + const displayAmount = divideByFactor(amount, conversion.factor); + const baseAmount = parseFloat(displayAmount); + if (!isFinite(baseAmount) || baseAmount <= 0) continue; + + const price = await fetchTokenUsdPrice(conversion.baseDenom); + const usd = price > 0 ? baseAmount * price : 0; + totalUsd += usd; + + tokens.push({ + source_denom: denom, + resolved_amount: formatTokenStr(amount), + resolved_denom: resolvedDenom, + display_denom: conversion.baseDenom, + display_amount: displayAmount, + value_usd: `$${formatTokenStr(usd.toFixed(14))}`, + }); + } + + return { totalUsd, tokens }; +} + +/** Strip trailing zeros from a decimal string (matching jsinfo FormatTokenAmount) */ +function formatTokenStr(s: string): string { + const [whole, frac] = s.split("."); + if (!frac) return whole; + const trimmed = frac.replace(/0+$/, ""); + return trimmed ? `${whole}.${trimmed}` : whole; +} + +/** String-based division to avoid floating-point noise for token amounts. + * Shifts the decimal point left by the number of digits in `divisor`. + * e.g. divideStr("23584370", 1_000_000) → "23.584370" → "23.58437" */ +function divideByFactor(raw: string, factor: number): string { + // Strip any existing decimal (RPC may return "123.000000000000000000") + const cleaned = formatTokenStr(raw); + const digits = Math.round(Math.log10(factor)); + const [intPart, decPart = ""] = cleaned.split("."); + const combined = intPart + decPart; + const shiftedDecPos = combined.length - digits - decPart.length; + + // Pad with leading zeros if the number is smaller than the factor + const padded = shiftedDecPos <= 0 + ? "0." + "0".repeat(-shiftedDecPos) + combined + : combined.slice(0, shiftedDecPos) + "." + combined.slice(shiftedDecPos); + + return formatTokenStr(padded || "0"); +} + +/** Fetch estimated rewards for a provider/validator — returns USD total + token breakdown */ +export async function fetchEstimatedRewards( + type: "provider" | "validator", + address: string, +): Promise { + try { + const data = await fetchRest( + `/lavanet/lava/subscription/estimated_${type}_rewards/${address}/${APR_BENCHMARK_ULAVA}${APR_BENCHMARK_DENOM}`, + ); + return await processRewardTokens(data.total ?? []); + } catch { + return { totalUsd: 0, tokens: [] }; + } +} + +export interface RewardsBySpecEntry { + chain: string; + spec: string; + tokens: RewardToken[]; + total_usd: number; +} + +/** + * Fetch a provider's actual earned rewards (no benchmark amount) and group by spec. + * Matches jsinfo's rewards_last_month: calls estimated_provider_rewards/{addr}/ + * then splits "Boost: ETH1", "Pools: ETH1", "Subscription: ETH1" into per-spec groups. + * + * Pass blockHeight to query historical chain state (archive node required). + * Note: USD values still use current CoinGecko prices even for historical blocks — + * matches the old Job 2 behavior where CoinGecko was called at pipeline-run time. + */ +export async function fetchRewardsBySpec( + address: string, + specNameMap: Map, + blockHeight?: number, +): Promise { + try { + const data = await fetchRest( + `/lavanet/lava/subscription/estimated_provider_rewards/${address}/`, + blockHeight, + ); + + // Group info entries by spec (collapse Boost/Pools/Subscription sources) + const bySpec = new Map; totalUsd: number }>(); + + for (const entry of data.info ?? []) { + const parts = (entry.source as string).split(": "); + const spec = parts.length > 1 ? parts[1] : parts[0]; + if (!spec) continue; + + const key = spec.toLowerCase(); + const group = bySpec.get(key) ?? { tokens: new Map(), totalUsd: 0 }; + + const amounts = Array.isArray(entry.amount) ? entry.amount : [entry.amount]; + for (const coin of amounts) { + if (TEST_DENOMS.has(coin.denom)) continue; + const existing = group.tokens.get(coin.denom); + const amt = parseFloat(coin.amount) || 0; + if (existing) { + existing.amount += amt; + } else { + group.tokens.set(coin.denom, { amount: amt, denom: coin.denom }); + } + } + + bySpec.set(key, group); + } + + // Convert to output format with USD values + const results: RewardsBySpecEntry[] = []; + for (const [specKey, group] of bySpec) { + const tokens: RewardToken[] = []; + let specUsd = 0; + + for (const [, coin] of group.tokens) { + const processed = await processRewardTokens([ + { denom: coin.denom, amount: coin.amount.toString() }, + ]); + tokens.push(...processed.tokens); + specUsd += processed.totalUsd; + } + + results.push({ + chain: specNameMap.get(specKey.toUpperCase()) ?? specKey.toUpperCase(), + spec: specKey.toUpperCase(), + tokens, + total_usd: specUsd, + }); + } + + return results; + } catch { + return []; + } +} + +export interface ClaimableRewardEntry { + amount: string; // display amount in base units (e.g. "1.23" lava) + denom: string; // base denom (e.g. "lava") + usdcValue: string; // numeric USD string (no $) + provider: string; +} + +/** Convert multi-denom claimable rewards to jsinfo-shape entries. */ +export async function processClaimableRewards( + rewards: Array<{ denom: string; amount: string }>, + provider: string, +): Promise { + const processed = await processRewardTokens(rewards); + return processed.tokens.map((t) => ({ + amount: t.display_amount, + denom: t.display_denom, + usdcValue: t.value_usd.startsWith("$") ? t.value_usd.slice(1) : t.value_usd, + provider, + })); +} + +// ── Validator reward breakdown helper (shared between validators.ts and rewards) ── + +interface RewardCoin { denom: string; amount: string } + +export interface TokenBreakdown { + tokens: RewardToken[]; + total_usd: number; +} + +export const EMPTY_BREAKDOWN: TokenBreakdown = { tokens: [], total_usd: 0 }; + +export async function rewardsToBreakdown(rewards: RewardCoin[] | undefined): Promise { + if (!rewards || rewards.length === 0) return EMPTY_BREAKDOWN; + const processed = await processRewardTokens(rewards); + return { tokens: processed.tokens, total_usd: processed.totalUsd }; +} diff --git a/apps/api/src/rpc/specs.ts b/apps/api/src/rpc/specs.ts new file mode 100644 index 0000000..dc5fc9c --- /dev/null +++ b/apps/api/src/rpc/specs.ts @@ -0,0 +1,46 @@ +import { BASE_SPECS } from "@info/shared/constants"; +import { fetchRest } from "./rest.js"; + +function titleCase(s: string): string { + return s.replace(/\b\w/g, (c) => c.toUpperCase()); +} + +/** Display name overrides — keys are specIDs (uppercase) */ +const CHAIN_DISPLAY_NAMES: Record = { + BSC: "BNB Chain Mainnet", + BSCT: "BNB Chain Testnet", + COSMOSHUB: "Cosmos Hub Mainnet", + COSMOSHUBT: "Cosmos Hub Testnet", + ETH1: "Ethereum Mainnet", + FTM4002: "Fantom Testnet", + FVMT: "Filecoin Testnet", + HEDERA: "Hedera Hashgraph Mainnet", + BTC: "Bitcoin Mainnet", + HOL1: "Ethereum Holesky Testnet", + LAVA: "Lava Mainnet", + LAV1: "Lava Testnet", + MOVEMENTT: "Movement Testnet", + OPTMS: "Optimism Sepolia Testnet", + POLYGONA: "Polygon Amoy Testnet", + SEP1: "Ethereum Sepolia Testnet", + SOLANAT: "Solana Testnet", + SONICT: "Sonic Blaze Testnet", + SPARK: "Fuse Testnet", + STRKS: "Starknet Sepolia Testnet", + TRX: "Tron Mainnet", + TRXT: "Tron Shasta Testnet", +}; + +function chainDisplayName(chainID: string, chainName: string): string { + if (CHAIN_DISPLAY_NAMES[chainID]) return CHAIN_DISPLAY_NAMES[chainID]; + return titleCase(chainName); +} + +export async function fetchAllSpecs(): Promise> { + const data = await fetchRest<{ + chainInfoList: Array<{ chainName: string; chainID: string }>; + }>("/lavanet/lava/spec/show_all_chains"); + return (data.chainInfoList ?? []) + .filter((c) => !BASE_SPECS.has(c.chainID)) + .map((c) => ({ index: c.chainID, name: chainDisplayName(c.chainID, c.chainName) })); +} diff --git a/apps/api/src/rpc/supply.ts b/apps/api/src/rpc/supply.ts new file mode 100644 index 0000000..f01418b --- /dev/null +++ b/apps/api/src/rpc/supply.ts @@ -0,0 +1,133 @@ +import { fetchRest, logger } from "./rest.js"; + +export async function fetchTotalSupply(blockHeight?: number): Promise { + const data = await fetchRest<{ supply: Array<{ denom: string; amount: string }> }>( + "/cosmos/bank/v1beta1/supply", + blockHeight, + ); + const lava = data.supply?.find((c) => c.denom === "ulava"); + return BigInt(lava?.amount ?? "0"); +} + +export async function fetchStakingPool(): Promise<{ bonded_tokens: string; not_bonded_tokens: string }> { + const data = await fetchRest<{ pool: { bonded_tokens: string; not_bonded_tokens: string } }>( + "/cosmos/staking/v1beta1/pool", + ); + return data.pool; +} + +interface RewardPool { + name: string; + balance: Array<{ denom: string; amount: string }>; +} + +/** Shared fetch for reward pools — both consumers filter by pool name. */ +export async function fetchRewardPools(): Promise { + const data = await fetchRest<{ pools: RewardPool[] }>("/lavanet/lava/rewards/pools"); + return data.pools ?? []; +} + +function sumPoolsUlava(pools: RewardPool[], names: string[]): bigint { + const nameSet = new Set(names); + let total = 0n; + for (const pool of pools) { + if (nameSet.has(pool.name)) { + for (const coin of pool.balance ?? []) { + if (coin.denom === "ulava") total += BigInt(coin.amount); + } + } + } + return total; +} + +const ALL_REWARD_POOLS = [ + "validators_rewards_distribution_pool", + "validators_rewards_allocation_pool", + "providers_rewards_distribution_pool", + "providers_rewards_allocation_pool", + "iprpc_pool", +]; + +async function fetchRewardPoolsAmount(): Promise { + return sumPoolsUlava(await fetchRewardPools(), ALL_REWARD_POOLS); +} + +interface VestingStats { + continuousVesting: bigint; + periodicVesting: bigint; +} + +async function fetchLockedVestingTokens(): Promise { + const nowSeconds = Math.floor(Date.now() / 1000); + const stats: VestingStats = { continuousVesting: 0n, periodicVesting: 0n }; + let nextKey: string | null = null; + + do { + const params = new URLSearchParams({ "pagination.limit": "1000" }); + if (nextKey) params.set("pagination.key", nextKey); + + const data = await fetchRest<{ + accounts: Array<{ + "@type": string; + start_time?: string; + base_vesting_account?: { + original_vesting?: Array<{ denom: string; amount: string }>; + end_time?: string; + }; + vesting_periods?: Array<{ + length: string; + amount: Array<{ denom: string; amount: string }>; + }>; + }>; + pagination?: { next_key: string | null }; + }>(`/cosmos/auth/v1beta1/accounts?${params}`); + + for (const account of data.accounts ?? []) { + const type = account["@type"]; + + if (type === "/cosmos.vesting.v1beta1.ContinuousVestingAccount") { + const totalAmount = BigInt( + account.base_vesting_account?.original_vesting?.[0]?.amount ?? "0", + ); + const startTime = BigInt(account.start_time ?? "0"); + const endTime = BigInt(account.base_vesting_account?.end_time ?? "0"); + const now = BigInt(nowSeconds); + + if (now < startTime) { + stats.continuousVesting += totalAmount; + } else if (now < endTime && endTime > startTime) { + stats.continuousVesting += ((endTime - now) * totalAmount) / (endTime - startTime); + } + } else if (type === "/cosmos.vesting.v1beta1.PeriodicVestingAccount") { + let currentTime = parseInt(account.start_time ?? "0"); + for (const period of account.vesting_periods ?? []) { + currentTime += parseInt(period.length); + if (currentTime >= nowSeconds) { + stats.periodicVesting += BigInt(period.amount?.[0]?.amount ?? "0"); + } + } + } + } + + nextKey = data.pagination?.next_key ?? null; + } while (nextKey); + + return stats; +} + +export async function fetchCirculatingSupply(): Promise { + const [totalSupply, pools, vesting] = await Promise.all([ + fetchTotalSupply(), + fetchRewardPoolsAmount(), + fetchLockedVestingTokens(), + ]); + + const circulating = totalSupply - vesting.continuousVesting - vesting.periodicVesting - pools; + if (circulating < 0n) { + logger.warn( + `Negative circulating supply: total=${totalSupply} continuous=${vesting.continuousVesting} periodic=${vesting.periodicVesting} pools=${pools}`, + ); + return 0n; + } + return circulating; +} diff --git a/apps/api/src/rpc/validators.ts b/apps/api/src/rpc/validators.ts new file mode 100644 index 0000000..a3e5662 --- /dev/null +++ b/apps/api/src/rpc/validators.ts @@ -0,0 +1,244 @@ +import { RPC_BATCH_SIZE, fetchRest } from "./rest.js"; +import { fetchLatestBlockHeight } from "./block.js"; +import { prewarmPriceCache } from "./pricing.js"; +import { + type EstimatedRewardsResponse, + type RewardToken, + type TokenBreakdown, + rewardsToBreakdown, +} from "./rewards.js"; + +export interface ValidatorCommissionRates { + rate: string; + max_rate: string; + max_change_rate: string; +} + +export interface ValidatorCommission { + commission_rates: ValidatorCommissionRates; + update_time: string; +} + +export interface ValidatorBase { + operator_address: string; + description?: { moniker?: string }; + jailed: boolean; + tokens: string; + commission: ValidatorCommission; +} + +interface RewardCoin { denom: string; amount: string } + +export interface ValidatorDistributionRewards { + self_bond_rewards: TokenBreakdown; + commission: TokenBreakdown; + operator_address: string; +} + +export interface ValidatorDelegationResponse { + delegation: { + delegator_address: string; + validator_address: string; + shares: string; + }; + balance: RewardCoin; +} + +export interface ValidatorUnbondingEntry { + creation_height: string; + completion_time: string; + initial_balance: string; + balance: string; + unbonding_id: string; + unbonding_on_hold_ref_count: string; +} + +export interface ValidatorUnbondingResponse { + delegator_address: string; + validator_address: string; + entries: ValidatorUnbondingEntry[]; +} + +export interface ValidatorWithRewards { + address: string; + moniker: string; + jailed: boolean; + tokens: string; + commission: ValidatorCommission; + apr?: number | null; + distribution: ValidatorDistributionRewards; + outstanding_rewards: TokenBreakdown; + estimated_rewards: TokenBreakdown; + delegations: { + delegation_responses: ValidatorDelegationResponse[]; + pagination: { next_key: string | null; total: string }; + }; + unbonding_delegations: { + unbonding_responses: ValidatorUnbondingResponse[]; + pagination: { next_key: string | null; total: string }; + }; +} + +async function fetchAllValidatorObjects(): Promise { + const validators: ValidatorBase[] = []; + let nextKey: string | null = null; + do { + const params = new URLSearchParams({ + status: "BOND_STATUS_BONDED", + "pagination.limit": "200", + }); + if (nextKey) params.set("pagination.key", nextKey); + const data = await fetchRest<{ + validators: ValidatorBase[]; + pagination: { next_key: string | null }; + }>(`/cosmos/staking/v1beta1/validators?${params}`); + for (const v of data.validators ?? []) validators.push(v); + nextKey = data.pagination?.next_key ?? null; + } while (nextKey); + return validators; +} + +interface ValidatorDistributionRewardsResponse { + operator_address: string; + self_bond_rewards: RewardCoin[]; + commission: RewardCoin[]; +} + +async function fetchValidatorDistribution( + addr: string, +): Promise { + try { + return await fetchRest( + `/cosmos/distribution/v1beta1/validators/${addr}`, + ); + } catch { return null; } +} + +async function fetchValidatorOutstanding(addr: string): Promise { + try { + const data = await fetchRest<{ rewards: { rewards: RewardCoin[] } }>( + `/cosmos/distribution/v1beta1/validators/${addr}/outstanding_rewards`, + ); + return data.rewards?.rewards ?? []; + } catch { return []; } +} + +async function fetchValidatorEstimatedTotal(addr: string): Promise { + try { + const data = await fetchRest( + `/lavanet/lava/subscription/estimated_validator_rewards/${addr}/`, + ); + return data.total ?? []; + } catch { return []; } +} + +async function fetchValidatorDelegationsPage( + addr: string, +): Promise<{ delegation_responses: ValidatorDelegationResponse[]; pagination: { next_key: string | null; total: string } }> { + try { + return await fetchRest(`/cosmos/staking/v1beta1/validators/${addr}/delegations?pagination.limit=1000&pagination.count_total=true`); + } catch { + return { delegation_responses: [], pagination: { next_key: null, total: "0" } }; + } +} + +async function fetchValidatorUnbondingPage( + addr: string, +): Promise<{ unbonding_responses: ValidatorUnbondingResponse[]; pagination: { next_key: string | null; total: string } }> { + try { + return await fetchRest(`/cosmos/staking/v1beta1/validators/${addr}/unbonding_delegations?pagination.limit=1000&pagination.count_total=true`); + } catch { + return { unbonding_responses: [], pagination: { next_key: null, total: "0" } }; + } +} + +/** + * Build the full validator-with-rewards list matching jsinfo's + * /lava_mainnet_validators_and_rewards response shape. + * + * Each validator requires 5 chain RPC calls; processes RPC_BATCH_SIZE at a time + * to respect rate limits. With ~50 validators this completes in ~20-30s cold, + * well within the 6h cache window. + */ +export async function fetchValidatorsWithRewards(): Promise<{ + height: number; + datetime: number; + validators: ValidatorWithRewards[]; +}> { + await prewarmPriceCache(); + + const [validators, latestBlock] = await Promise.all([ + fetchAllValidatorObjects(), + fetchLatestBlockHeight(), + ]); + + const enriched: ValidatorWithRewards[] = []; + + for (let i = 0; i < validators.length; i += RPC_BATCH_SIZE) { + const batch = validators.slice(i, i + RPC_BATCH_SIZE); + const results = await Promise.all(batch.map(async (v) => { + const addr = v.operator_address; + const [dist, outstanding, estimatedTotal, delegations, unbonding] = await Promise.all([ + fetchValidatorDistribution(addr), + fetchValidatorOutstanding(addr), + fetchValidatorEstimatedTotal(addr), + fetchValidatorDelegationsPage(addr), + fetchValidatorUnbondingPage(addr), + ]); + + const [selfBond, commissionBreakdown, outstandingBreakdown, estimatedBreakdown] = await Promise.all([ + rewardsToBreakdown(dist?.self_bond_rewards), + rewardsToBreakdown(dist?.commission), + rewardsToBreakdown(outstanding), + rewardsToBreakdown(estimatedTotal), + ]); + + return { + address: addr, + moniker: v.description?.moniker ?? "", + jailed: v.jailed, + tokens: v.tokens, + commission: v.commission, + distribution: { + self_bond_rewards: selfBond, + commission: commissionBreakdown, + operator_address: dist?.operator_address ?? addr, + }, + outstanding_rewards: outstandingBreakdown, + estimated_rewards: estimatedBreakdown, + delegations, + unbonding_delegations: unbonding, + } satisfies ValidatorWithRewards; + })); + enriched.push(...results); + } + + return { + height: latestBlock.height, + datetime: Math.floor(new Date(latestBlock.time).getTime() / 1000), + validators: enriched, + }; +} + +/** Fetch all bonded validator operator addresses */ +export async function fetchBondedValidators(): Promise { + const validators: string[] = []; + let nextKey: string | null = null; + do { + const params = new URLSearchParams({ + status: "BOND_STATUS_BONDED", + "pagination.limit": "200", + }); + if (nextKey) params.set("pagination.key", nextKey); + const data = await fetchRest<{ + validators: Array<{ operator_address: string }>; + pagination: { next_key: string | null }; + }>(`/cosmos/staking/v1beta1/validators?${params}`); + for (const v of data.validators ?? []) validators.push(v.operator_address); + nextKey = data.pagination?.next_key ?? null; + } while (nextKey); + return validators; +} + +// Re-export RewardToken so callers consuming ValidatorWithRewards can type tokens. +export type { RewardToken }; From 29233589807e8d34287e72503684a1e8e53f456b Mon Sep 17 00:00:00 2001 From: Nimrod Teich Date: Thu, 16 Apr 2026 23:33:18 +0300 Subject: [PATCH 10/25] refactor(api): extract health-probe into standalone worker MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The health probe enumerates every spec × provider on each 30s tick and makes gRPC probe calls — all on the Fastify event loop, causing periodic p95 latency spikes on the API during probe windows. Extract the probe loop into services/health-probe-loop.ts and add a bin/health-probe.ts entrypoint that runs it as its own process. Production deploys should run this as a separate ECS task (or any container) so probe traffic is isolated from the API's event loop. The in-process plugins/health-probe.ts now delegates to the shared loop and stays opt-in (ENABLE_HEALTH_PROBE=true) for local dev convenience. Scripts: pnpm --filter @info/api probe:dev # hot-reload dev pnpm --filter @info/api probe:start # production --- apps/api/package.json | 2 + apps/api/src/bin/health-probe.ts | 49 +++++++ apps/api/src/plugins/health-probe.ts | 148 ++------------------ apps/api/src/services/health-probe-loop.ts | 154 +++++++++++++++++++++ 4 files changed, 216 insertions(+), 137 deletions(-) create mode 100644 apps/api/src/bin/health-probe.ts create mode 100644 apps/api/src/services/health-probe-loop.ts diff --git a/apps/api/package.json b/apps/api/package.json index 4b1d424..052fe39 100644 --- a/apps/api/package.json +++ b/apps/api/package.json @@ -7,6 +7,8 @@ "build": "tsc", "dev": "tsx watch src/main.ts", "start": "node dist/main.js", + "probe:dev": "tsx watch src/bin/health-probe.ts", + "probe:start": "node dist/bin/health-probe.js", "typecheck": "tsc --noEmit", "test": "vitest run" }, diff --git a/apps/api/src/bin/health-probe.ts b/apps/api/src/bin/health-probe.ts new file mode 100644 index 0000000..c56a911 --- /dev/null +++ b/apps/api/src/bin/health-probe.ts @@ -0,0 +1,49 @@ +/** + * Standalone health-probe worker. + * + * Runs the probe loop as its own process so the gRPC probe traffic doesn't + * share the Fastify API's event loop. Deploy as a separate ECS task (or any + * container/systemd service). + * + * Env: + * REDIS_URL — required; where to write probe results + * HEALTH_PROBE_INTERVAL_MS — default 30000 + * LAVA_REST_URL — passed through to the rpc layer + * + * The in-process `healthProbePlugin` is still available for local dev via + * ENABLE_HEALTH_PROBE=true on the API container. + */ +import { Redis } from "ioredis"; +import pino from "pino"; +import { runProbeLoop } from "../services/health-probe-loop.js"; + +const log = pino({ name: "health-probe-bin" }); + +async function main() { + const redisUrl = process.env.REDIS_URL; + if (!redisUrl) { + log.error("REDIS_URL is required"); + process.exit(1); + } + + const redis = new Redis(redisUrl); + + let running = true; + const shutdown = (signal: string) => { + log.info({ signal }, "Shutting down health probe"); + running = false; + }; + process.on("SIGTERM", () => shutdown("SIGTERM")); + process.on("SIGINT", () => shutdown("SIGINT")); + + try { + await runProbeLoop({ redis, isRunning: () => running }); + } finally { + await redis.quit(); + } +} + +main().catch((err) => { + log.error({ err }, "Health probe worker crashed"); + process.exit(1); +}); diff --git a/apps/api/src/plugins/health-probe.ts b/apps/api/src/plugins/health-probe.ts index 182cc27..cf1ff8e 100644 --- a/apps/api/src/plugins/health-probe.ts +++ b/apps/api/src/plugins/health-probe.ts @@ -1,84 +1,15 @@ import fp from "fastify-plugin"; import type { FastifyInstance } from "fastify"; -import { normalizeBlock } from "@info/shared/constants"; -import { fetchAllSpecs, fetchProvidersForSpec, RPC_BATCH_SIZE } from "../rpc/lava.js"; -import type { ProviderEndpoint } from "../rpc/lava.js"; -import { probeProvider } from "../services/grpc-probe.js"; -import { writeHealthStatus } from "../services/health-store.js"; - -interface ProbeTarget { - address: string; - specId: string; - endpoint: ProviderEndpoint; - apiInterface: string; -} - -async function buildProbeTargets(): Promise { - const specs = await fetchAllSpecs(); - const targets: ProbeTarget[] = []; - - // Fetch in batches — raise RPC_BATCH_SIZE via env for dedicated RPC endpoints. - for (let i = 0; i < specs.length; i += RPC_BATCH_SIZE) { - const batch = specs.slice(i, i + RPC_BATCH_SIZE); - const results = await Promise.all( - batch.map((s) => - fetchProvidersForSpec(s.index) - .then((providers) => ({ specId: s.index, providers })) - .catch(() => ({ specId: s.index, providers: [] as Awaited> })), - ), - ); - - for (const { specId, providers } of results) { - for (const provider of providers) { - for (const ep of provider.endpoints) { - // Skip endpoints targeting private/internal networks - if (!isPublicEndpoint(ep.iPPORT)) continue; - - for (const apiInterface of ep.apiInterfaces) { - targets.push({ - address: provider.address, - specId, - endpoint: ep, - apiInterface, - }); - } - } - } - } - } - - return targets; -} - -/** Reject endpoints targeting private/loopback/internal networks (SSRF mitigation) */ -function isPublicEndpoint(iPPORT: string): boolean { - const host = iPPORT.split(":")[0]; - if (!host) return false; - if (host === "localhost" || host === "0.0.0.0") return false; - if (host.startsWith("127.")) return false; - if (host.startsWith("10.")) return false; - if (host.startsWith("192.168.")) return false; - if (host.startsWith("172.")) { - const second = parseInt(host.split(".")[1], 10); - if (second >= 16 && second <= 31) return false; - } - if (host.startsWith("169.254.")) return false; - return true; -} - -function shuffle(arr: T[]): T[] { - const a = [...arr]; - for (let i = a.length - 1; i > 0; i--) { - const j = Math.floor(Math.random() * (i + 1)); - [a[i], a[j]] = [a[j], a[i]]; - } - return a; -} - -function sleep(ms: number): Promise { - return new Promise((r) => setTimeout(r, ms)); -} - +import { runProbeLoop } from "../services/health-probe-loop.js"; + +/** + * Optional in-process health-probe runner for local dev / single-node setups. + * + * Production deployments should run the probe as a separate process via + * `apps/api/src/bin/health-probe.ts` (own ECS task / container) so gRPC probe + * traffic doesn't share the API's event loop. This plugin stays opt-in for + * convenience during local dev: set `ENABLE_HEALTH_PROBE=true` to enable. + */ export const healthProbePlugin = fp(async (app: FastifyInstance) => { if (process.env.ENABLE_HEALTH_PROBE !== "true") return; if (!app.redis) { @@ -86,9 +17,6 @@ export const healthProbePlugin = fp(async (app: FastifyInstance) => { return; } - const intervalMs = parseInt(process.env.HEALTH_PROBE_INTERVAL_MS ?? "30000", 10); - const region = process.env.HEALTH_PROBE_REGION ?? "Local"; - let running = true; app.addHook("onClose", () => { @@ -97,60 +25,6 @@ export const healthProbePlugin = fp(async (app: FastifyInstance) => { app.addHook("onReady", () => { const redis = app.redis!; - - void (async () => { - app.log.info("Health probe started"); - - while (running) { - try { - const targets = await buildProbeTargets(); - if (targets.length === 0) { - app.log.warn("No probe targets found, waiting"); - await sleep(60_000); - continue; - } - - const shuffled = shuffle(targets); - app.log.info({ count: shuffled.length }, "Starting health probe cycle"); - - // Probe in batches — raise RPC_BATCH_SIZE via env for dedicated endpoints. - for (let i = 0; i < shuffled.length; i += RPC_BATCH_SIZE) { - if (!running) break; - const batch = shuffled.slice(i, i + RPC_BATCH_SIZE); - - await Promise.allSettled(batch.map(async (target) => { - const geolocation = target.endpoint.geolocation.toString(); - try { - const result = await probeProvider( - target.endpoint.iPPORT, - target.specId, - target.apiInterface, - ); - const block = normalizeBlock(target.specId, result.latestBlock); - await writeHealthStatus( - redis, target.address, target.specId, target.apiInterface, geolocation, - "healthy", - { block, latency: result.latencyMs, lavaEpoch: result.lavaEpoch }, - ); - } catch (err) { - const message = err instanceof Error ? err.message : String(err); - await writeHealthStatus( - redis, target.address, target.specId, target.apiInterface, geolocation, - "unhealthy", - { message }, - ); - } - })); - } - - if (running) await sleep(intervalMs); - } catch (err) { - app.log.error({ err }, "Health probe loop error"); - await sleep(60_000); - } - } - - app.log.info("Health probe stopped"); - })(); + void runProbeLoop({ redis, isRunning: () => running }); }); }); diff --git a/apps/api/src/services/health-probe-loop.ts b/apps/api/src/services/health-probe-loop.ts new file mode 100644 index 0000000..8b67562 --- /dev/null +++ b/apps/api/src/services/health-probe-loop.ts @@ -0,0 +1,154 @@ +import type { Redis } from "ioredis"; +import pino from "pino"; +import { normalizeBlock } from "@info/shared/constants"; +import { fetchAllSpecs, fetchProvidersForSpec, RPC_BATCH_SIZE } from "../rpc/lava.js"; +import type { ProviderEndpoint } from "../rpc/lava.js"; +import { probeProvider } from "./grpc-probe.js"; +import { writeHealthStatus } from "./health-store.js"; + +const logger = pino({ name: "health-probe" }); + +interface ProbeTarget { + address: string; + specId: string; + endpoint: ProviderEndpoint; + apiInterface: string; +} + +async function buildProbeTargets(): Promise { + const specs = await fetchAllSpecs(); + const targets: ProbeTarget[] = []; + + // Fetch in batches — raise RPC_BATCH_SIZE via env for dedicated RPC endpoints. + for (let i = 0; i < specs.length; i += RPC_BATCH_SIZE) { + const batch = specs.slice(i, i + RPC_BATCH_SIZE); + const results = await Promise.all( + batch.map((s) => + fetchProvidersForSpec(s.index) + .then((providers) => ({ specId: s.index, providers })) + .catch(() => ({ specId: s.index, providers: [] as Awaited> })), + ), + ); + + for (const { specId, providers } of results) { + for (const provider of providers) { + for (const ep of provider.endpoints) { + // Skip endpoints targeting private/internal networks + if (!isPublicEndpoint(ep.iPPORT)) continue; + + for (const apiInterface of ep.apiInterfaces) { + targets.push({ + address: provider.address, + specId, + endpoint: ep, + apiInterface, + }); + } + } + } + } + } + + return targets; +} + +/** Reject endpoints targeting private/loopback/internal networks (SSRF mitigation) */ +function isPublicEndpoint(iPPORT: string): boolean { + const host = iPPORT.split(":")[0]; + if (!host) return false; + if (host === "localhost" || host === "0.0.0.0") return false; + if (host.startsWith("127.")) return false; + if (host.startsWith("10.")) return false; + if (host.startsWith("192.168.")) return false; + if (host.startsWith("172.")) { + const second = parseInt(host.split(".")[1], 10); + if (second >= 16 && second <= 31) return false; + } + if (host.startsWith("169.254.")) return false; + return true; +} + +function shuffle(arr: T[]): T[] { + const a = [...arr]; + for (let i = a.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [a[i], a[j]] = [a[j], a[i]]; + } + return a; +} + +function sleep(ms: number): Promise { + return new Promise((r) => setTimeout(r, ms)); +} + +export interface ProbeLoopOptions { + redis: Redis; + intervalMs?: number; + /** Returns true while the loop should keep running — called between iterations. */ + isRunning: () => boolean; +} + +/** + * Run one provider-health probe cycle followed by sleep, repeatedly. + * + * This loop is the core of the health-probe subsystem. It's extracted so it + * can be invoked from either an in-process Fastify plugin (dev / single-node) + * or a standalone worker process (production) — see `bin/health-probe.ts`. + */ +export async function runProbeLoop(opts: ProbeLoopOptions): Promise { + const { redis, isRunning } = opts; + const intervalMs = opts.intervalMs ?? parseInt(process.env.HEALTH_PROBE_INTERVAL_MS ?? "30000", 10); + + logger.info("Health probe started"); + + while (isRunning()) { + try { + const targets = await buildProbeTargets(); + if (targets.length === 0) { + logger.warn("No probe targets found, waiting"); + await sleep(60_000); + continue; + } + + const shuffled = shuffle(targets); + logger.info({ count: shuffled.length }, "Starting health probe cycle"); + + // Probe in batches — raise RPC_BATCH_SIZE via env for dedicated endpoints. + for (let i = 0; i < shuffled.length; i += RPC_BATCH_SIZE) { + if (!isRunning()) break; + const batch = shuffled.slice(i, i + RPC_BATCH_SIZE); + + await Promise.allSettled(batch.map(async (target) => { + const geolocation = target.endpoint.geolocation.toString(); + try { + const result = await probeProvider( + target.endpoint.iPPORT, + target.specId, + target.apiInterface, + ); + const block = normalizeBlock(target.specId, result.latestBlock); + await writeHealthStatus( + redis, target.address, target.specId, target.apiInterface, geolocation, + "healthy", + { block, latency: result.latencyMs, lavaEpoch: result.lavaEpoch }, + ); + } catch (err) { + const message = err instanceof Error ? err.message : String(err); + await writeHealthStatus( + redis, target.address, target.specId, target.apiInterface, geolocation, + "unhealthy", + { message }, + ); + } + })); + } + + if (isRunning()) await sleep(intervalMs); + } catch (err) { + logger.error({ err }, "Health probe loop error"); + await sleep(60_000); + } + } + + logger.info("Health probe stopped"); +} From 877835ee25e0c896e1822a59e734d413b1a9aad5 Mon Sep 17 00:00:00 2001 From: Nimrod Teich Date: Thu, 16 Apr 2026 23:36:54 +0300 Subject: [PATCH 11/25] perf(api): precompute heavy endpoints out-of-process MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit /supply/circulating (paginates ~53K accounts), /apr and /all_providers_apr (per-entity estimated_* RPC calls), and /validators-and-rewards (5 RPC × N validators) are all fine on warm cache but pathological on cache miss / cold deploy. Add a standalone worker that recomputes them on a 15-min interval and writes results to Redis under precompute:. Routes now read from the precompute store first and fall back to live compute if the worker hasn't populated the key yet — so the site still works without the worker, it's just slower. Scripts: pnpm --filter @info/api precompute:dev # hot-reload dev pnpm --filter @info/api precompute:start # production Env: PRECOMPUTE_INTERVAL_MS — default 900000 (15 min) --- apps/api/package.json | 2 + apps/api/src/bin/precompute.ts | 127 ++++++++++++++++++ apps/api/src/routes/all-providers-apr.ts | 11 +- apps/api/src/routes/apr.ts | 12 +- apps/api/src/routes/supply.ts | 9 +- apps/api/src/routes/validators-and-rewards.ts | 16 ++- apps/api/src/services/precompute-store.ts | 37 +++++ 7 files changed, 206 insertions(+), 8 deletions(-) create mode 100644 apps/api/src/bin/precompute.ts create mode 100644 apps/api/src/services/precompute-store.ts diff --git a/apps/api/package.json b/apps/api/package.json index 052fe39..eb13aaa 100644 --- a/apps/api/package.json +++ b/apps/api/package.json @@ -9,6 +9,8 @@ "start": "node dist/main.js", "probe:dev": "tsx watch src/bin/health-probe.ts", "probe:start": "node dist/bin/health-probe.js", + "precompute:dev": "tsx watch src/bin/precompute.ts", + "precompute:start": "node dist/bin/precompute.js", "typecheck": "tsc --noEmit", "test": "vitest run" }, diff --git a/apps/api/src/bin/precompute.ts b/apps/api/src/bin/precompute.ts new file mode 100644 index 0000000..62bbd06 --- /dev/null +++ b/apps/api/src/bin/precompute.ts @@ -0,0 +1,127 @@ +/** + * Standalone precompute worker. + * + * Recomputes the heaviest read-only endpoints on a fixed interval and writes + * each result to Redis so the API can serve them without ever hitting a cold + * code path. Prevents cache stampedes when TTLs expire under load. + * + * Covered endpoints: + * - /supply/circulating (paginates ~53K accounts) + * - /apr (per-entity estimated_* RPC calls) + * - /all_providers_apr (per-provider × per-spec) + * - /validators-and-rewards (5 RPC × N validators) + * + * Deploy as a separate ECS task / container. The API still falls back to + * live compute if the precompute key is missing, so this worker can be down + * without breaking the site — users just see the slower path. + * + * Env: + * REDIS_URL — required + * PRECOMPUTE_INTERVAL_MS — default 900000 (15 min) + * LAVA_REST_URL, COINGECKO_API_URL — passed through to the rpc layer + */ +import { Redis } from "ioredis"; +import pino from "pino"; +import { gqlSafe } from "../graphql/client.js"; +import { fetchCirculatingSupply } from "../rpc/supply.js"; +import { computeAPR, computeAllProvidersApr } from "../rpc/apr.js"; +import { fetchValidatorsWithRewards } from "../rpc/validators.js"; +import { writePrecomputed } from "../services/precompute-store.js"; + +const log = pino({ name: "precompute" }); + +async function recomputeCirculatingSupply(redis: Redis): Promise { + const supply = await fetchCirculatingSupply(); + const value = (Number(supply / 1_000_000n) + Number(supply % 1_000_000n) / 1_000_000).toString(); + await writePrecomputed(redis, "supply.circulating", value); +} + +async function recomputeApr(redis: Redis): Promise { + const apr = await computeAPR(redis); + await writePrecomputed(redis, "apr", apr); +} + +async function recomputeAllProvidersApr(redis: Redis): Promise { + const since = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000).toISOString().slice(0, 10); + const relayData = await gqlSafe<{ + mvRelayDailies: { + groupedAggregates: Array<{ keys: string[]; sum: { cu: string; relays: string } }>; + }; + } | null>(`query($since: Date!) { + mvRelayDailies(filter: { date: { greaterThanOrEqualTo: $since } }) { + groupedAggregates(groupBy: PROVIDER) { keys sum { cu relays } } + } + }`, { since }, null); + + const relay30d = new Map(); + if (relayData) { + for (const agg of relayData.mvRelayDailies.groupedAggregates) { + relay30d.set(agg.keys[0], { cu: agg.sum.cu, relays: agg.sum.relays }); + } + } + + const data = await computeAllProvidersApr(relay30d, redis); + await writePrecomputed(redis, "all_providers_apr", data); +} + +async function recomputeValidatorsAndRewards(redis: Redis): Promise { + const data = await fetchValidatorsWithRewards(); + await writePrecomputed(redis, "validators_and_rewards", data); +} + +const TASKS: Array<{ name: string; run: (redis: Redis) => Promise }> = [ + { name: "supply.circulating", run: recomputeCirculatingSupply }, + { name: "apr", run: recomputeApr }, + { name: "all_providers_apr", run: recomputeAllProvidersApr }, + { name: "validators_and_rewards", run: recomputeValidatorsAndRewards }, +]; + +async function runOnce(redis: Redis): Promise { + for (const task of TASKS) { + const started = Date.now(); + try { + await task.run(redis); + log.info({ task: task.name, ms: Date.now() - started }, "precompute done"); + } catch (err) { + log.error({ task: task.name, err }, "precompute failed"); + } + } +} + +function sleep(ms: number): Promise { + return new Promise((r) => setTimeout(r, ms)); +} + +async function main() { + const redisUrl = process.env.REDIS_URL; + if (!redisUrl) { + log.error("REDIS_URL is required"); + process.exit(1); + } + + const intervalMs = parseInt(process.env.PRECOMPUTE_INTERVAL_MS ?? "900000", 10); + const redis = new Redis(redisUrl); + + let running = true; + const shutdown = (signal: string) => { + log.info({ signal }, "Shutting down precompute worker"); + running = false; + }; + process.on("SIGTERM", () => shutdown("SIGTERM")); + process.on("SIGINT", () => shutdown("SIGINT")); + + log.info({ intervalMs }, "Precompute worker started"); + + while (running) { + await runOnce(redis); + if (!running) break; + await sleep(intervalMs); + } + + await redis.quit(); +} + +main().catch((err) => { + log.error({ err }, "Precompute worker crashed"); + process.exit(1); +}); diff --git a/apps/api/src/routes/all-providers-apr.ts b/apps/api/src/routes/all-providers-apr.ts index ee2a6f7..06c669f 100644 --- a/apps/api/src/routes/all-providers-apr.ts +++ b/apps/api/src/routes/all-providers-apr.ts @@ -1,14 +1,19 @@ import type { FastifyInstance } from "fastify"; -import { computeAllProvidersApr } from "../rpc/lava.js"; +import { computeAllProvidersApr, type AllProviderAprEntry } from "../rpc/lava.js"; import { gqlSafe } from "../graphql/client.js"; +import { readPrecomputed } from "../services/precompute-store.js"; export async function allProvidersAprRoutes(app: FastifyInstance) { - // GET /all_providers_apr — per-provider APR, commission, 30d relays, reward breakdown - // Matches jsinfo /all_providers_apr response format. Cached 30 min (expensive). + // GET /all_providers_apr — per-provider APR, commission, 30d relays, reward breakdown. + // Prefers the precomputed value from bin/precompute.ts; falls back to live + // compute when the precompute worker hasn't populated the key yet. app.get("/", { schema: { tags: ["APR"], summary: "Per-provider APR with commission, 30d relays, and reward breakdown" }, config: { cacheTTL: 1800 }, }, async (request) => { + const cached = await readPrecomputed(request.server.redis, "all_providers_apr"); + if (cached) return cached.value; + const since = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000) .toISOString() .slice(0, 10); diff --git a/apps/api/src/routes/apr.ts b/apps/api/src/routes/apr.ts index f7a9c14..fa758df 100644 --- a/apps/api/src/routes/apr.ts +++ b/apps/api/src/routes/apr.ts @@ -1,12 +1,22 @@ import type { FastifyInstance } from "fastify"; import { computeAPR } from "../rpc/lava.js"; +import { readPrecomputed } from "../services/precompute-store.js"; + +interface AprResult { + restaking_apr_percentile: number; + staking_apr_percentile: number; +} export async function aprRoutes(app: FastifyInstance) { - // GET /apr — per-entity estimated rewards with USD conversion, cached 30 min + // GET /apr — per-entity estimated rewards with USD conversion, cached 30 min. + // Prefers the precomputed value from bin/precompute.ts; falls back to live + // compute when the precompute worker hasn't populated the key yet. app.get("/apr", { schema: { tags: ["APR"], summary: "Restaking and staking APR percentiles (80th, capped at 30%)" }, config: { cacheTTL: 1800 }, }, async (request) => { + const cached = await readPrecomputed(request.server.redis, "apr"); + if (cached) return cached.value; return await computeAPR(request.server.redis); }); } diff --git a/apps/api/src/routes/supply.ts b/apps/api/src/routes/supply.ts index 63683db..0d17051 100644 --- a/apps/api/src/routes/supply.ts +++ b/apps/api/src/routes/supply.ts @@ -1,5 +1,6 @@ import type { FastifyInstance } from "fastify"; import { fetchTotalSupply, fetchCirculatingSupply } from "../rpc/lava.js"; +import { readPrecomputed } from "../services/precompute-store.js"; const ULAVA_TO_LAVA = 1_000_000n; @@ -14,13 +15,17 @@ export async function supplyRoutes(app: FastifyInstance) { return (total / ULAVA_TO_LAVA).toString(); }); - // GET /supply/circulating — total - locked vesting - reward pools, in lava + // GET /supply/circulating — total - locked vesting - reward pools, in lava. + // Prefers the precomputed value written by bin/precompute.ts; falls back to + // live compute when the precompute worker is down / has no entry yet. app.get("/circulating", { schema: { tags: ["Supply"], summary: "Circulating LAVA supply (plain text)" }, config: { cacheTTL: 300, rateLimit: { max: 10, timeWindow: "1 minute" } }, }, async (_request, reply) => { - const circulating = await fetchCirculatingSupply(); reply.header("Content-Type", "text/plain"); + const cached = await readPrecomputed(app.redis, "supply.circulating"); + if (cached) return cached.value; + const circulating = await fetchCirculatingSupply(); return (circulating / ULAVA_TO_LAVA).toString(); }); } diff --git a/apps/api/src/routes/validators-and-rewards.ts b/apps/api/src/routes/validators-and-rewards.ts index c68de9e..a97a3a9 100644 --- a/apps/api/src/routes/validators-and-rewards.ts +++ b/apps/api/src/routes/validators-and-rewards.ts @@ -1,17 +1,29 @@ import type { FastifyInstance } from "fastify"; -import { fetchValidatorsWithRewards } from "../rpc/lava.js"; +import { fetchValidatorsWithRewards, type ValidatorWithRewards } from "../rpc/lava.js"; +import { readPrecomputed } from "../services/precompute-store.js"; + +interface ValidatorsAndRewardsData { + height: number; + datetime: number; + validators: ValidatorWithRewards[]; +} export async function validatorsAndRewardsRoutes(app: FastifyInstance) { // Replaces jsinfo's /lava_mainnet_validators_and_rewards. For each bonded // validator: fetches distribution rewards, outstanding rewards, estimated // rewards, delegations, and unbonding delegations — all converted to USD. + // + // Prefers the precomputed value from bin/precompute.ts; falls back to live + // compute when the precompute worker hasn't populated the key yet. app.get("/validators-and-rewards", { schema: { tags: ["Validators"], summary: "All bonded validators with rewards, delegations, and unbonding — matches jsinfo shape", }, config: { cacheTTL: 7200 }, - }, async () => { + }, async (request) => { + const cached = await readPrecomputed(request.server.redis, "validators_and_rewards"); + if (cached) return { data: cached.value }; const data = await fetchValidatorsWithRewards(); return { data }; }); diff --git a/apps/api/src/services/precompute-store.ts b/apps/api/src/services/precompute-store.ts new file mode 100644 index 0000000..271b57f --- /dev/null +++ b/apps/api/src/services/precompute-store.ts @@ -0,0 +1,37 @@ +import type { Redis } from "ioredis"; + +/** + * Stale-while-revalidate store for heavy endpoints. + * + * A separate worker process (see bin/precompute.ts) periodically runs the + * expensive computations and writes results here. API route handlers read + * from here first — on a hit they skip the cold-path compute entirely, + * which means no cache stampede when TTLs expire under load. + * + * Values never expire in Redis; the worker overwrites them on each cycle. + * If the worker is down, routes fall back to live compute. + */ + +const KEY_PREFIX = "precompute:"; + +export interface PrecomputedValue { + /** Unix millis when the worker last wrote this value. */ + at: number; + value: T; +} + +export async function readPrecomputed(redis: Redis | null, name: string): Promise | null> { + if (!redis) return null; + try { + const raw = await redis.get(`${KEY_PREFIX}${name}`); + if (!raw) return null; + return JSON.parse(raw) as PrecomputedValue; + } catch { + return null; + } +} + +export async function writePrecomputed(redis: Redis, name: string, value: T): Promise { + const payload: PrecomputedValue = { at: Date.now(), value }; + await redis.set(`${KEY_PREFIX}${name}`, JSON.stringify(payload)); +} From 1f9a321f914c05f5a3b48bd8a36d1e714bf67a47 Mon Sep 17 00:00:00 2001 From: Nimrod Teich Date: Thu, 16 Apr 2026 23:44:22 +0300 Subject: [PATCH 12/25] test: add missing tests for routes + web utilities MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit API route coverage was at 10/16. Add focused happy-path + schema-rejection tests for the six missing routes: - index.ts (stats, charts) - lava.ts (specs) - optimizer-metrics (both provider + spec variants — 503 when DB off, 400 on invalid YYYY-MM-DD) - providers.ts (pagination/sort, addr schema, stakes) - search.ts (case-insensitive match across providers+specs) - specs.ts (sort-by-provider-count, :specId/health no-Redis path) Add vitest to apps/web with unit tests for the two most-used utilities: format.ts (formatNumber, formatNumberKMB, formatLava, formatLavaKMB) and chain-icons.ts (alias resolution + case handling). Test counts: apps/api 97 → 116 apps/web 0 → 15 shared 46 (unchanged) --- apps/api/src/__tests__/routes-index.test.ts | 80 +++++++++++++ apps/api/src/__tests__/routes-lava.test.ts | 40 +++++++ .../routes-optimizer-metrics.test.ts | 62 ++++++++++ .../src/__tests__/routes-providers.test.ts | 110 ++++++++++++++++++ apps/api/src/__tests__/routes-search.test.ts | 58 +++++++++ apps/api/src/__tests__/routes-specs.test.ts | 85 ++++++++++++++ apps/web/package.json | 6 +- .../web/src/lib/__tests__/chain-icons.test.ts | 20 ++++ apps/web/src/lib/__tests__/format.test.ts | 61 ++++++++++ apps/web/vitest.config.ts | 9 ++ pnpm-lock.yaml | 3 + 11 files changed, 532 insertions(+), 2 deletions(-) create mode 100644 apps/api/src/__tests__/routes-index.test.ts create mode 100644 apps/api/src/__tests__/routes-lava.test.ts create mode 100644 apps/api/src/__tests__/routes-optimizer-metrics.test.ts create mode 100644 apps/api/src/__tests__/routes-providers.test.ts create mode 100644 apps/api/src/__tests__/routes-search.test.ts create mode 100644 apps/api/src/__tests__/routes-specs.test.ts create mode 100644 apps/web/src/lib/__tests__/chain-icons.test.ts create mode 100644 apps/web/src/lib/__tests__/format.test.ts create mode 100644 apps/web/vitest.config.ts diff --git a/apps/api/src/__tests__/routes-index.test.ts b/apps/api/src/__tests__/routes-index.test.ts new file mode 100644 index 0000000..173fe5a --- /dev/null +++ b/apps/api/src/__tests__/routes-index.test.ts @@ -0,0 +1,80 @@ +import { describe, it, expect, vi, beforeEach } from "vitest"; +import Fastify from "fastify"; +import { errorHandlerPlugin } from "../plugins/error-handler.js"; + +vi.mock("../graphql/client.js", () => ({ gqlSafe: vi.fn() })); +vi.mock("../rpc/lava.js", () => ({ + fetchLatestBlockHeight: vi.fn(), + fetchAllProviders: vi.fn(), +})); + +const { gqlSafe } = await import("../graphql/client.js"); +const { fetchLatestBlockHeight, fetchAllProviders } = await import("../rpc/lava.js"); +const { indexRoutes } = await import("../routes/index.js"); + +async function buildApp() { + const app = Fastify({ logger: false }); + await app.register(errorHandlerPlugin); + await app.register(indexRoutes, { prefix: "/index" }); + return app; +} + +beforeEach(() => vi.resetAllMocks()); + +describe("GET /index/stats", () => { + it("returns aggregate stats enriched with latest block height", async () => { + (gqlSafe as ReturnType).mockResolvedValue({ + mvRelayDailies: { aggregates: { sum: { cu: "1000", relays: "50" } } }, + }); + (fetchLatestBlockHeight as ReturnType).mockResolvedValue({ + height: 12345, + time: "2025-01-01T00:00:00Z", + }); + (fetchAllProviders as ReturnType).mockResolvedValue([ + { address: "lava@a", totalStake: "1000", totalDelegation: "500" }, + ]); + + const app = await buildApp(); + const res = await app.inject({ method: "GET", url: "/index/stats" }); + expect(res.statusCode).toBe(200); + const body = JSON.parse(res.body); + expect(body.latestBlock).toBe(12345); + expect(body.activeProviderCount).toBe(1); + expect(body.totalStake).toBe("1500"); + }); +}); + +describe("GET /index/charts", () => { + it("returns empty data when indexer is down", async () => { + (gqlSafe as ReturnType).mockResolvedValue(null); + const app = await buildApp(); + const res = await app.inject({ method: "GET", url: "/index/charts" }); + expect(res.statusCode).toBe(200); + expect(JSON.parse(res.body)).toEqual({ data: [] }); + }); + + it("groups daily MV rows by (date, chainId) and computes weighted QoS", async () => { + (gqlSafe as ReturnType).mockResolvedValue({ + mvRelayDailies: { + nodes: [ + { + date: "2025-01-01", chainId: "ETH1", cu: "100", relays: "10", + qosSyncW: 8, qosAvailW: 9, qosLatencyW: 10, qosWeight: "10", + }, + { + date: "2025-01-01", chainId: "ETH1", cu: "50", relays: "5", + qosSyncW: 4, qosAvailW: 5, qosLatencyW: 5, qosWeight: "5", + }, + ], + }, + }); + const app = await buildApp(); + const res = await app.inject({ method: "GET", url: "/index/charts?from=2025-01-01&to=2025-01-31" }); + const body = JSON.parse(res.body); + expect(body.data).toHaveLength(1); + const row = body.data[0]; + expect(row.cu).toBe("150"); + expect(row.relays).toBe("15"); + expect(row.qosSync).toBeCloseTo(12 / 15); + }); +}); diff --git a/apps/api/src/__tests__/routes-lava.test.ts b/apps/api/src/__tests__/routes-lava.test.ts new file mode 100644 index 0000000..c7355f3 --- /dev/null +++ b/apps/api/src/__tests__/routes-lava.test.ts @@ -0,0 +1,40 @@ +import { describe, it, expect, vi } from "vitest"; +import Fastify from "fastify"; +import { errorHandlerPlugin } from "../plugins/error-handler.js"; + +vi.mock("../rpc/lava.js", () => ({ + fetchAllSpecs: vi.fn(), +})); + +const { fetchAllSpecs } = await import("../rpc/lava.js"); +const { lavaRoutes } = await import("../routes/lava.js"); + +async function buildApp() { + const app = Fastify({ logger: false }); + await app.register(errorHandlerPlugin); + await app.register(lavaRoutes, { prefix: "/lava" }); + return app; +} + +describe("GET /lava/specs", () => { + it("returns all chain specs from rpc", async () => { + (fetchAllSpecs as ReturnType).mockResolvedValue([ + { index: "ETH1", name: "Ethereum Mainnet" }, + { index: "LAVA", name: "Lava Mainnet" }, + ]); + const app = await buildApp(); + const res = await app.inject({ method: "GET", url: "/lava/specs" }); + expect(res.statusCode).toBe(200); + const body = JSON.parse(res.body); + expect(body.data).toHaveLength(2); + expect(body.data[0].index).toBe("ETH1"); + }); + + it("returns empty data if rpc returns nothing", async () => { + (fetchAllSpecs as ReturnType).mockResolvedValue([]); + const app = await buildApp(); + const res = await app.inject({ method: "GET", url: "/lava/specs" }); + expect(res.statusCode).toBe(200); + expect(JSON.parse(res.body).data).toEqual([]); + }); +}); diff --git a/apps/api/src/__tests__/routes-optimizer-metrics.test.ts b/apps/api/src/__tests__/routes-optimizer-metrics.test.ts new file mode 100644 index 0000000..8bcaa94 --- /dev/null +++ b/apps/api/src/__tests__/routes-optimizer-metrics.test.ts @@ -0,0 +1,62 @@ +import { describe, it, expect } from "vitest"; +import Fastify from "fastify"; +import { errorHandlerPlugin } from "../plugins/error-handler.js"; +import { optimizerMetricsRoutes } from "../routes/optimizer-metrics.js"; + +async function buildApp(withDb: boolean) { + const app = Fastify({ logger: false }); + await app.register(errorHandlerPlugin); + if (withDb) { + // Minimal tagged-template stub for routes that early-return when relaysDb is set; + // we don't actually need to execute queries for these tests. + const stub = Object.assign(() => ({}), { unsafe: () => "" }); + app.decorate("relaysDb", stub as never); + } else { + app.decorate("relaysDb", null); + } + await app.register(optimizerMetricsRoutes); + return app; +} + +describe("GET /providers/:addr/optimizer-metrics", () => { + it("returns 503 when relays DB is not configured", async () => { + const app = await buildApp(false); + const res = await app.inject({ + method: "GET", + url: "/providers/lava@abc/optimizer-metrics", + }); + expect(res.statusCode).toBe(503); + expect(JSON.parse(res.body).error).toContain("not configured"); + }); + + it("rejects invalid `from` date with 400", async () => { + const app = await buildApp(true); + const res = await app.inject({ + method: "GET", + url: "/providers/lava@abc/optimizer-metrics?from=not-a-date", + }); + expect(res.statusCode).toBe(400); + expect(JSON.parse(res.body).error).toContain("invalid from"); + }); + + it("rejects invalid `to` date with 400", async () => { + const app = await buildApp(true); + const res = await app.inject({ + method: "GET", + url: "/providers/lava@abc/optimizer-metrics?to=2025-02-30", + }); + expect(res.statusCode).toBe(400); + expect(JSON.parse(res.body).error).toContain("invalid to"); + }); +}); + +describe("GET /specs/:specId/optimizer-metrics", () => { + it("returns 503 when relays DB is not configured", async () => { + const app = await buildApp(false); + const res = await app.inject({ + method: "GET", + url: "/specs/ETH1/optimizer-metrics", + }); + expect(res.statusCode).toBe(503); + }); +}); diff --git a/apps/api/src/__tests__/routes-providers.test.ts b/apps/api/src/__tests__/routes-providers.test.ts new file mode 100644 index 0000000..f361529 --- /dev/null +++ b/apps/api/src/__tests__/routes-providers.test.ts @@ -0,0 +1,110 @@ +import { describe, it, expect, vi, beforeEach } from "vitest"; +import Fastify from "fastify"; +import { paginationPlugin } from "../plugins/pagination.js"; +import { errorHandlerPlugin } from "../plugins/error-handler.js"; + +vi.mock("../graphql/client.js", () => ({ gqlSafe: vi.fn() })); +vi.mock("../rpc/lava.js", () => ({ + fetchAllProviders: vi.fn(), + fetchProvidersForSpec: vi.fn(), + fetchAllSpecs: vi.fn(), + fetchProviderAvatar: vi.fn(), + fetchDelegatorRewards: vi.fn(), +})); +vi.mock("../services/health-store.js", () => ({ + readHealthForProvider: vi.fn(), + readHealthMapForProvider: vi.fn(), +})); + +const { gqlSafe } = await import("../graphql/client.js"); +const { fetchAllProviders, fetchAllSpecs, fetchProvidersForSpec } = await import("../rpc/lava.js"); +const { providerRoutes } = await import("../routes/providers.js"); + +async function buildApp() { + const app = Fastify({ logger: false }); + await app.register(errorHandlerPlugin); + await app.register(paginationPlugin); + await app.register(providerRoutes, { prefix: "/providers" }); + return app; +} + +beforeEach(() => vi.resetAllMocks()); + +const MOCK_ALL_PROVIDERS = [ + { + address: "lava@alpha", moniker: "AlphaProvider", identity: "", + totalStake: "2000", totalDelegation: "100", commission: "5", + specs: ["ETH1", "LAVA"], + }, + { + address: "lava@beta", moniker: "BetaProvider", identity: "", + totalStake: "1000", totalDelegation: "50", commission: "10", + specs: ["ETH1"], + }, +]; + +describe("GET /providers", () => { + it("paginates and sorts providers by totalStake desc", async () => { + (fetchAllProviders as ReturnType).mockResolvedValue(MOCK_ALL_PROVIDERS); + (gqlSafe as ReturnType).mockResolvedValue({ + mvRelayDailies: { groupedAggregates: [{ keys: ["lava@alpha"], sum: { cu: "500", relays: "100" } }] }, + }); + + const app = await buildApp(); + const res = await app.inject({ method: "GET", url: "/providers?page=1&limit=10" }); + expect(res.statusCode).toBe(200); + const body = JSON.parse(res.body); + expect(body.data).toHaveLength(2); + expect(body.data[0].provider).toBe("lava@alpha"); // bigger stake first + expect(body.data[0].cuSum30d).toBe("500"); + expect(body.data[1].cuSum30d).toBeNull(); + expect(body.pagination).toEqual({ total: 2, page: 1, limit: 10, pages: 1 }); + }); +}); + +describe("GET /providers/:addr — schema validation", () => { + it("rejects an invalid address pattern with 400", async () => { + (fetchAllSpecs as ReturnType).mockResolvedValue([]); + const app = await buildApp(); + const res = await app.inject({ method: "GET", url: "/providers/notlava" }); + expect(res.statusCode).toBe(400); + }); +}); + +describe("GET /providers/:addr/stakes", () => { + it("returns stakes filtered to the requested provider", async () => { + (fetchAllSpecs as ReturnType).mockResolvedValue([ + { index: "ETH1", name: "Ethereum" }, + ]); + (fetchProvidersForSpec as ReturnType).mockResolvedValue([ + { + address: "lava@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + moniker: "Alpha", identity: "", + stake: { amount: "1000" }, + delegate_total: { amount: "0" }, + delegate_commission: "5", + geolocation: 1, + addons: "", extensions: "", + apiInterfaces: "", + endpoints: [], + }, + { + address: "lava@bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + moniker: "Beta", identity: "", + stake: { amount: "500" }, + delegate_total: { amount: "0" }, + delegate_commission: "10", + geolocation: 1, + addons: "", extensions: "", + apiInterfaces: "", + endpoints: [], + }, + ]); + const app = await buildApp(); + const res = await app.inject({ method: "GET", url: "/providers/lava@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/stakes" }); + expect(res.statusCode).toBe(200); + const body = JSON.parse(res.body); + expect(body.data).toHaveLength(1); + expect(body.data[0].stake).toBe("1000"); + }); +}); diff --git a/apps/api/src/__tests__/routes-search.test.ts b/apps/api/src/__tests__/routes-search.test.ts new file mode 100644 index 0000000..d6ebbcd --- /dev/null +++ b/apps/api/src/__tests__/routes-search.test.ts @@ -0,0 +1,58 @@ +import { describe, it, expect, vi } from "vitest"; +import Fastify from "fastify"; +import { errorHandlerPlugin } from "../plugins/error-handler.js"; + +vi.mock("../rpc/lava.js", () => ({ + fetchAllProviders: vi.fn(), + fetchAllSpecs: vi.fn(), +})); + +const { fetchAllProviders, fetchAllSpecs } = await import("../rpc/lava.js"); +const { searchRoutes } = await import("../routes/search.js"); + +async function buildApp() { + const app = Fastify({ logger: false }); + await app.register(errorHandlerPlugin); + await app.register(searchRoutes); + return app; +} + +const MOCK_PROVIDERS = [ + { address: "lava@abc", moniker: "AlphaProvider", identity: "", totalStake: "1000", totalDelegation: "0", commission: "5", specs: ["ETH1"] }, + { address: "lava@xyz", moniker: "BetaProvider", identity: "", totalStake: "500", totalDelegation: "0", commission: "10", specs: ["LAVA"] }, +]; +const MOCK_SPECS = [ + { index: "ETH1", name: "Ethereum Mainnet" }, + { index: "LAVA", name: "Lava Mainnet" }, +]; + +describe("GET /search", () => { + it("returns combined provider+spec results matching case-insensitive query", async () => { + (fetchAllProviders as ReturnType).mockResolvedValue(MOCK_PROVIDERS); + (fetchAllSpecs as ReturnType).mockResolvedValue(MOCK_SPECS); + const app = await buildApp(); + const res = await app.inject({ method: "GET", url: "/search?q=alpha" }); + expect(res.statusCode).toBe(200); + const body = JSON.parse(res.body); + expect(body.data.some((r: { moniker: string }) => r.moniker === "AlphaProvider")).toBe(true); + expect(body.data.some((r: { moniker: string }) => r.moniker === "BetaProvider")).toBe(false); + }); + + it("matches on spec name", async () => { + (fetchAllProviders as ReturnType).mockResolvedValue(MOCK_PROVIDERS); + (fetchAllSpecs as ReturnType).mockResolvedValue(MOCK_SPECS); + const app = await buildApp(); + const res = await app.inject({ method: "GET", url: "/search?q=ethereum" }); + const body = JSON.parse(res.body); + expect(body.data.some((r: { type: string; name: string }) => r.type === "spec" && r.name === "ETH1")).toBe(true); + }); + + it("returns everything when no query", async () => { + (fetchAllProviders as ReturnType).mockResolvedValue(MOCK_PROVIDERS); + (fetchAllSpecs as ReturnType).mockResolvedValue(MOCK_SPECS); + const app = await buildApp(); + const res = await app.inject({ method: "GET", url: "/search" }); + const body = JSON.parse(res.body); + expect(body.data.length).toBe(MOCK_PROVIDERS.length + MOCK_SPECS.length); + }); +}); diff --git a/apps/api/src/__tests__/routes-specs.test.ts b/apps/api/src/__tests__/routes-specs.test.ts new file mode 100644 index 0000000..150ef65 --- /dev/null +++ b/apps/api/src/__tests__/routes-specs.test.ts @@ -0,0 +1,85 @@ +import { describe, it, expect, vi, beforeEach } from "vitest"; +import Fastify from "fastify"; +import { errorHandlerPlugin } from "../plugins/error-handler.js"; + +vi.mock("../graphql/client.js", () => ({ + gqlSafe: vi.fn(), +})); + +vi.mock("../rpc/lava.js", () => ({ + fetchAllSpecs: vi.fn(), + fetchProvidersForSpec: vi.fn(), +})); + +vi.mock("../services/health-store.js", () => ({ + readHealthSummaryForSpec: vi.fn(), + readHealthByProviderForSpec: vi.fn(), +})); + +const { gqlSafe } = await import("../graphql/client.js"); +const { fetchAllSpecs, fetchProvidersForSpec } = await import("../rpc/lava.js"); +const { readHealthSummaryForSpec } = await import("../services/health-store.js"); +const { specRoutes } = await import("../routes/specs.js"); + +async function buildApp() { + const app = Fastify({ logger: false }); + await app.register(errorHandlerPlugin); + await app.register(specRoutes, { prefix: "/specs" }); + return app; +} + +beforeEach(() => { + vi.resetAllMocks(); +}); + +describe("GET /specs", () => { + it("returns specs sorted by provider count desc, enriched with 30d relay data", async () => { + (fetchAllSpecs as ReturnType).mockResolvedValue([ + { index: "ETH1", name: "Ethereum Mainnet" }, + { index: "LAVA", name: "Lava Mainnet" }, + ]); + (fetchProvidersForSpec as ReturnType) + .mockResolvedValueOnce([{}, {}, {}]) // ETH1 has 3 + .mockResolvedValueOnce([{}]); // LAVA has 1 + (gqlSafe as ReturnType).mockResolvedValue({ + mvRelayDailies: { + groupedAggregates: [ + { keys: ["ETH1"], sum: { cu: "500", relays: "100" } }, + ], + }, + }); + + const app = await buildApp(); + const res = await app.inject({ method: "GET", url: "/specs" }); + expect(res.statusCode).toBe(200); + const body = JSON.parse(res.body); + expect(body.data[0].specId).toBe("ETH1"); // more providers first + expect(body.data[0].providerCount).toBe(3); + expect(body.data[0].relays30d).toBe("100"); + expect(body.data[1].relays30d).toBeNull(); + }); + + it("gracefully returns empty list when indexer is down", async () => { + (fetchAllSpecs as ReturnType).mockResolvedValue([]); + (gqlSafe as ReturnType).mockResolvedValue(null); + const app = await buildApp(); + const res = await app.inject({ method: "GET", url: "/specs" }); + expect(res.statusCode).toBe(200); + expect(JSON.parse(res.body).data).toEqual([]); + }); +}); + +describe("GET /specs/:specId/health", () => { + it("rejects invalid specId via schema validation", async () => { + const app = await buildApp(); + const res = await app.inject({ method: "GET", url: "/specs/$$$bad/health" }); + expect(res.statusCode).toBe(400); + }); + + it("returns empty data gracefully when Redis isn't decorated", async () => { + const app = await buildApp(); + const res = await app.inject({ method: "GET", url: "/specs/ETH1/health" }); + expect(res.statusCode).toBe(200); + expect(JSON.parse(res.body)).toEqual({ data: [] }); + }); +}); diff --git a/apps/web/package.json b/apps/web/package.json index b5be62e..7f95b30 100644 --- a/apps/web/package.json +++ b/apps/web/package.json @@ -8,7 +8,8 @@ "dev": "next dev", "start": "next start", "lint": "next lint", - "typecheck": "tsc --noEmit" + "typecheck": "tsc --noEmit", + "test": "vitest run" }, "dependencies": { "@info/shared": "workspace:*", @@ -31,6 +32,7 @@ "devDependencies": { "@types/react": "^19.0.0", "@types/react-dom": "^19.0.0", - "typescript": "^6.0.0" + "typescript": "^6.0.0", + "vitest": "^4.1.3" } } diff --git a/apps/web/src/lib/__tests__/chain-icons.test.ts b/apps/web/src/lib/__tests__/chain-icons.test.ts new file mode 100644 index 0000000..234ea16 --- /dev/null +++ b/apps/web/src/lib/__tests__/chain-icons.test.ts @@ -0,0 +1,20 @@ +import { describe, it, expect } from "vitest"; +import { getChainIcon } from "../chain-icons.js"; + +describe("getChainIcon", () => { + it("maps known alias specIds to their canonical icon filename", () => { + expect(getChainIcon("ETH1")).toBe("/chains/ethereum.svg"); + expect(getChainIcon("eth1")).toBe("/chains/ethereum.svg"); + expect(getChainIcon("BSC")).toBe("/chains/bsc.svg"); + expect(getChainIcon("COSMOSHUB")).toBe("/chains/cosmos-hub.svg"); + }); + + it("falls back to the lowercased specId for unknown chains", () => { + expect(getChainIcon("NEWCHAIN")).toBe("/chains/newchain.svg"); + }); + + it("is case-insensitive on input", () => { + expect(getChainIcon("FVMT")).toBe("/chains/filecoin.svg"); + expect(getChainIcon("fvmt")).toBe("/chains/filecoin.svg"); + }); +}); diff --git a/apps/web/src/lib/__tests__/format.test.ts b/apps/web/src/lib/__tests__/format.test.ts new file mode 100644 index 0000000..3fa3311 --- /dev/null +++ b/apps/web/src/lib/__tests__/format.test.ts @@ -0,0 +1,61 @@ +import { describe, it, expect } from "vitest"; +import { formatNumber, formatNumberKMB, formatLava, formatLavaKMB } from "../format.js"; + +describe("formatNumber", () => { + it("adds thousand separators", () => { + expect(formatNumber(1234567)).toBe("1,234,567"); + }); + + it("handles bigint input", () => { + expect(formatNumber(1000000n)).toBe("1,000,000"); + }); + + it("returns '0' for invalid input", () => { + expect(formatNumber("not a number")).toBe("0"); + }); +}); + +describe("formatNumberKMB", () => { + it("formats thousands with K", () => { + expect(formatNumberKMB(1500)).toBe("1.50K"); + }); + + it("formats millions with M", () => { + expect(formatNumberKMB(2_500_000)).toBe("2.50M"); + }); + + it("formats billions with B", () => { + expect(formatNumberKMB(3_200_000_000)).toBe("3.20B"); + }); + + it("leaves small numbers unsuffixed (rounded)", () => { + expect(formatNumberKMB(42)).toBe("42"); + }); +}); + +describe("formatLava", () => { + it("converts ulava to LAVA with thousand separators", () => { + // 1,000,000 ulava = 1 LAVA + expect(formatLava("1000000")).toBe("1"); + expect(formatLava("1234567000000")).toBe("1,234,567"); + }); + + it("accepts bigint input", () => { + expect(formatLava(5_000_000n)).toBe("5"); + }); + + it("strips trailing 'ulava' suffix", () => { + expect(formatLava("1000000ulava")).toBe("1"); + }); + + it("returns '0' for invalid input", () => { + expect(formatLava("garbage")).toBe("0"); + }); +}); + +describe("formatLavaKMB", () => { + it("shortens large LAVA amounts", () => { + // 2,500,000,000,000 ulava = 2,500,000 LAVA = 2.50M LAVA + expect(formatLavaKMB("2500000000000")).toBe("2.50M"); + }); +}); diff --git a/apps/web/vitest.config.ts b/apps/web/vitest.config.ts new file mode 100644 index 0000000..e646917 --- /dev/null +++ b/apps/web/vitest.config.ts @@ -0,0 +1,9 @@ +import { defineConfig } from "vitest/config"; + +export default defineConfig({ + test: { + environment: "node", + globals: true, + include: ["src/**/__tests__/**/*.test.ts", "src/**/*.test.ts"], + }, +}); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5ad4a11..bb54b32 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -139,6 +139,9 @@ importers: typescript: specifier: ^6.0.0 version: 6.0.2 + vitest: + specifier: ^4.1.3 + version: 4.1.3(@types/node@25.5.0)(vite@6.4.2(@types/node@25.5.0)(jiti@2.6.1)(lightningcss@1.32.0)(tsx@4.21.0)(yaml@2.8.3)) packages/shared: dependencies: From c29c521a7901c6dd6330d1442908010cd5d25d56 Mon Sep 17 00:00:00 2001 From: Nimrod Teich Date: Fri, 17 Apr 2026 00:02:54 +0300 Subject: [PATCH 13/25] chore: enable noUncheckedIndexedAccess across workspace MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Tighten TypeScript to flag silent undefined-from-indexed-access bugs (arr[i] → T | undefined, Map.get → T | undefined). Produced 57 latent issues — all fixed in this commit, mostly via explicit ! on indices the surrounding loop guarantees are in-bounds and destructure defaults on .split() outputs. --- apps/api/src/bin/precompute.ts | 3 +- apps/api/src/graphql/client.ts | 2 +- apps/api/src/plugins/cache.ts | 2 +- apps/api/src/routes/all-providers-apr.ts | 3 +- apps/api/src/routes/burn-rate.ts | 4 +-- apps/api/src/routes/index.ts | 2 +- apps/api/src/routes/optimizer-metrics.ts | 3 +- .../src/routes/provider-claimable-rewards.ts | 4 +-- .../src/routes/provider-estimated-rewards.ts | 4 +-- apps/api/src/routes/provider-rewards.ts | 6 ++-- apps/api/src/routes/providers.ts | 3 +- apps/api/src/routes/specs.ts | 3 +- apps/api/src/rpc/apr.ts | 29 ++++++++++--------- apps/api/src/rpc/rewards.ts | 2 +- apps/api/src/services/grpc-probe.ts | 7 ++++- apps/api/src/services/health-probe-loop.ts | 4 +-- apps/api/src/services/health-store.ts | 6 ++-- apps/web/src/app/provider/[lavaid]/page.tsx | 2 +- apps/web/src/components/data/IndexChart.tsx | 17 +++++------ .../components/data/OptimizerMetricsChart.tsx | 6 ++-- apps/web/src/components/layout/SearchBar.tsx | 3 +- apps/web/tsconfig.json | 1 + tsconfig.base.json | 1 + 23 files changed, 65 insertions(+), 52 deletions(-) diff --git a/apps/api/src/bin/precompute.ts b/apps/api/src/bin/precompute.ts index 62bbd06..aa20997 100644 --- a/apps/api/src/bin/precompute.ts +++ b/apps/api/src/bin/precompute.ts @@ -56,7 +56,8 @@ async function recomputeAllProvidersApr(redis: Redis): Promise { const relay30d = new Map(); if (relayData) { for (const agg of relayData.mvRelayDailies.groupedAggregates) { - relay30d.set(agg.keys[0], { cu: agg.sum.cu, relays: agg.sum.relays }); + const provider = agg.keys[0]; + if (provider) relay30d.set(provider, { cu: agg.sum.cu, relays: agg.sum.relays }); } } diff --git a/apps/api/src/graphql/client.ts b/apps/api/src/graphql/client.ts index c145def..84dbbe9 100644 --- a/apps/api/src/graphql/client.ts +++ b/apps/api/src/graphql/client.ts @@ -27,7 +27,7 @@ export async function gql(query: string, variables?: Record }; - if (json.errors?.length) throw new Error(json.errors[0].message); + if (json.errors?.length) throw new Error(json.errors[0]!.message); return json.data as T; } diff --git a/apps/api/src/plugins/cache.ts b/apps/api/src/plugins/cache.ts index f4a4929..3715e68 100644 --- a/apps/api/src/plugins/cache.ts +++ b/apps/api/src/plugins/cache.ts @@ -16,7 +16,7 @@ function resolveTtl(request: FastifyRequest): number | undefined { function buildCacheKey(request: FastifyRequest): string { // Normalize: collapse double slashes, strip trailing slash, sort query params - const [path, qs] = request.url.split("?", 2); + const [path = "", qs] = request.url.split("?", 2); const normalizedPath = path.replace(/\/+/g, "/").replace(/\/$/, "") || "/"; const normalizedQs = qs ? qs.split("&").sort().join("&") diff --git a/apps/api/src/routes/all-providers-apr.ts b/apps/api/src/routes/all-providers-apr.ts index 06c669f..da7e0f1 100644 --- a/apps/api/src/routes/all-providers-apr.ts +++ b/apps/api/src/routes/all-providers-apr.ts @@ -38,7 +38,8 @@ export async function allProvidersAprRoutes(app: FastifyInstance) { const relay30d = new Map(); if (relayData) { for (const agg of relayData.mvRelayDailies.groupedAggregates) { - relay30d.set(agg.keys[0], { cu: agg.sum.cu, relays: agg.sum.relays }); + const provider = agg.keys[0]; + if (provider) relay30d.set(provider, { cu: agg.sum.cu, relays: agg.sum.relays }); } } diff --git a/apps/api/src/routes/burn-rate.ts b/apps/api/src/routes/burn-rate.ts index 739eb04..4e9d552 100644 --- a/apps/api/src/routes/burn-rate.ts +++ b/apps/api/src/routes/burn-rate.ts @@ -98,10 +98,10 @@ export async function burnRateRoutes(app: FastifyInstance) { const valid = resolved.filter((b): b is NonNullable => b !== null); for (let i = 0; i < valid.length; i++) { - const b = valid[i]; + const b = valid[i]!; const prev = i === 0 ? BigInt(latestSupply.toString()) - : BigInt(valid[i - 1].supply); + : BigInt(valid[i - 1]!.supply); const supplyDiff = prev - BigInt(b.supply); blocks.push({ ...b, supply_diff: supplyDiff.toString() }); } diff --git a/apps/api/src/routes/index.ts b/apps/api/src/routes/index.ts index 64c2683..0cf8687 100644 --- a/apps/api/src/routes/index.ts +++ b/apps/api/src/routes/index.ts @@ -156,7 +156,7 @@ export async function indexRoutes(app: FastifyInstance) { }> = []; for (const [key, v] of byDayChain) { - const [date, chainId] = key.split("|"); + const [date = "", chainId = ""] = key.split("|"); result.push({ date, chainId, cu: v.cu.toString(), diff --git a/apps/api/src/routes/optimizer-metrics.ts b/apps/api/src/routes/optimizer-metrics.ts index 28daee7..3ad6158 100644 --- a/apps/api/src/routes/optimizer-metrics.ts +++ b/apps/api/src/routes/optimizer-metrics.ts @@ -96,7 +96,8 @@ function aggregate(rows: NormalizedMetric[], keyFn: (r: NormalizedMetric) => str const result: NormalizedMetric[] = []; for (const [, group] of groups) { - const base = { ...group[0] }; + if (group.length === 0) continue; + const base: NormalizedMetric = { ...group[0]! }; for (const k of METRIC_KEYS) { const values = group.map((r) => r[k]).filter((v): v is number => v != null); (base as Record)[k] = values.length > 0 ? avg(values) : null; diff --git a/apps/api/src/routes/provider-claimable-rewards.ts b/apps/api/src/routes/provider-claimable-rewards.ts index f9fff2c..218260b 100644 --- a/apps/api/src/routes/provider-claimable-rewards.ts +++ b/apps/api/src/routes/provider-claimable-rewards.ts @@ -30,8 +30,8 @@ export async function providerClaimableRewardsRoutes(app: FastifyInstance) { batch.map((p) => fetchDelegatorRewards(p.address)), ); for (let j = 0; j < batch.length; j++) { - const addr = batch[j].address; - const entries = await processClaimableRewards(results[j], addr); + const addr = batch[j]!.address; + const entries = await processClaimableRewards(results[j]!, addr); if (entries.length > 0) { out[addr] = { rewards: entries, timestamp: ts }; } diff --git a/apps/api/src/routes/provider-estimated-rewards.ts b/apps/api/src/routes/provider-estimated-rewards.ts index 206d7c4..f23e8f8 100644 --- a/apps/api/src/routes/provider-estimated-rewards.ts +++ b/apps/api/src/routes/provider-estimated-rewards.ts @@ -152,9 +152,9 @@ export async function providerEstimatedRewardsRoutes(app: FastifyInstance) { ); for (let j = 0; j < batch.length; j++) { - const addr = batch[j]; + const addr = batch[j]!; const provider = providerMap.get(addr)!; - let rewards = rewardResults[j]; + let rewards = rewardResults[j]!; if (spec) rewards = rewards.filter((r) => r.spec === spec); if (rewards.length === 0) continue; diff --git a/apps/api/src/routes/provider-rewards.ts b/apps/api/src/routes/provider-rewards.ts index 6c85f57..5b1133e 100644 --- a/apps/api/src/routes/provider-rewards.ts +++ b/apps/api/src/routes/provider-rewards.ts @@ -231,8 +231,8 @@ export async function providerRewardsRoutes(app: FastifyInstance) { if (groupBy === "spec") { // One row per (provider, spec). Keys: [chainId, provider]. const rows = groups.map((agg) => { - const spec = agg.keys[0]; - const provider = agg.keys[1]; + const spec = agg.keys[0] ?? ""; + const provider = agg.keys[1] ?? ""; return { provider, spec, @@ -275,7 +275,7 @@ export async function providerRewardsRoutes(app: FastifyInstance) { // Default: one row per provider. Keys: [provider]. const rows = groups.map((agg) => { - const provider = agg.keys[0]; + const provider = agg.keys[0] ?? ""; return { provider, moniker: providerMap.get(provider) ?? "", diff --git a/apps/api/src/routes/providers.ts b/apps/api/src/routes/providers.ts index 038e121..4132e84 100644 --- a/apps/api/src/routes/providers.ts +++ b/apps/api/src/routes/providers.ts @@ -63,7 +63,8 @@ export async function providerRoutes(app: FastifyInstance) { const relayMap = new Map(); if (relayData) { for (const agg of relayData.mvRelayDailies.groupedAggregates) { - relayMap.set(agg.keys[0], { cu: agg.sum.cu, relays: agg.sum.relays }); + const provider = agg.keys[0]; + if (provider) relayMap.set(provider, { cu: agg.sum.cu, relays: agg.sum.relays }); } } diff --git a/apps/api/src/routes/specs.ts b/apps/api/src/routes/specs.ts index dbfc212..9e37ad8 100644 --- a/apps/api/src/routes/specs.ts +++ b/apps/api/src/routes/specs.ts @@ -38,7 +38,8 @@ export async function specRoutes(app: FastifyInstance) { const relayMap = new Map(); if (relayData) { for (const agg of relayData.mvRelayDailies.groupedAggregates) { - relayMap.set(agg.keys[0], { cu: agg.sum.cu, relays: agg.sum.relays }); + const key = agg.keys[0]; + if (key) relayMap.set(key, { cu: agg.sum.cu, relays: agg.sum.relays }); } } diff --git a/apps/api/src/rpc/apr.ts b/apps/api/src/rpc/apr.ts index 78c9b5d..154deed 100644 --- a/apps/api/src/rpc/apr.ts +++ b/apps/api/src/rpc/apr.ts @@ -25,15 +25,15 @@ const APR_MIN = 1e-11; /** Percentile calculation matching jsinfo `CalculatePercentile` */ function calculatePercentile(values: number[], rank: number): number { if (values.length === 0 || rank < 0 || rank > 1) return 0; - if (values.length === 1) return values[0]; + if (values.length === 1) return values[0]!; const sorted = [...values].sort((a, b) => a - b); const pos = Math.floor((sorted.length - 1) * rank); if (sorted.length % 2 === 0) { - return sorted[pos] + (sorted[pos + 1] - sorted[pos]) * rank; + return sorted[pos]! + (sorted[pos + 1]! - sorted[pos]!) * rank; } - return sorted[pos]; + return sorted[pos]!; } /** APR from monthly reward: (1 + rewardUsd/investedUsd)^12 - 1 */ @@ -94,9 +94,11 @@ async function getWeightedApr(redis: Redis, type: string, address: string): Prom let weightedSum = 0; let weightSum = 0; for (let i = 0; i < history.records.length && i < APR_WEIGHTS.length; i++) { - const avg = history.records[i].aprSum / history.records[i].count; - weightedSum += avg * APR_WEIGHTS[i]; - weightSum += APR_WEIGHTS[i]; + const record = history.records[i]!; + const weight = APR_WEIGHTS[i]!; + const avg = record.aprSum / record.count; + weightedSum += avg * weight; + weightSum += weight; } return weightSum > 0 ? weightedSum / weightSum : null; } catch { @@ -165,13 +167,14 @@ async function collectEntityAprs( ); for (let j = 0; j < batch.length; j++) { - const currentApr = calculateApr(rewards[j].totalUsd, investedUsd); + const addr = batch[j]!; + const currentApr = calculateApr(rewards[j]!.totalUsd, investedUsd); if (currentApr <= 0) continue; let finalApr = currentApr; if (redis) { - await storeApr(redis, type, batch[j], currentApr); - const weighted = await getWeightedApr(redis, type, batch[j]); + await storeApr(redis, type, addr, currentApr); + const weighted = await getWeightedApr(redis, type, addr); if (weighted !== null) finalApr = weighted; } @@ -252,11 +255,11 @@ export async function computeAllProvidersApr( ]); for (let j = 0; j < batch.length; j++) { - const addr = batch[j]; + const addr = batch[j]!; const provider = providerMap.get(addr)!; - const rewards = rewardResults[j]; - const rewardsLastMonth = rewardsLastMonthResults[j]; - const avatar = avatarResults[j]; + const rewards = rewardResults[j]!; + const rewardsLastMonth = rewardsLastMonthResults[j]!; + const avatar = avatarResults[j] ?? null; const currentApr = calculateApr(rewards.totalUsd, investedUsd); let finalApr = currentApr; diff --git a/apps/api/src/rpc/rewards.ts b/apps/api/src/rpc/rewards.ts index b286b65..a7b422c 100644 --- a/apps/api/src/rpc/rewards.ts +++ b/apps/api/src/rpc/rewards.ts @@ -85,7 +85,7 @@ async function processRewardTokens( /** Strip trailing zeros from a decimal string (matching jsinfo FormatTokenAmount) */ function formatTokenStr(s: string): string { - const [whole, frac] = s.split("."); + const [whole = "", frac] = s.split("."); if (!frac) return whole; const trimmed = frac.replace(/0+$/, ""); return trimmed ? `${whole}.${trimmed}` : whole; diff --git a/apps/api/src/services/grpc-probe.ts b/apps/api/src/services/grpc-probe.ts index 0ae96d3..34b6f9d 100644 --- a/apps/api/src/services/grpc-probe.ts +++ b/apps/api/src/services/grpc-probe.ts @@ -63,7 +63,12 @@ export async function probeProvider( lavaLatestBlock: number; }>((resolve, reject) => { const deadline = new Date(Date.now() + PROBE_TIMEOUT_MS); - client.probe( + const probe = client.probe as ( + req: unknown, + opts: unknown, + cb: (err: grpc.ServiceError | null, response: unknown) => void, + ) => void; + probe( { guid, specId, apiInterface, withVerifications: false }, { deadline }, (err: grpc.ServiceError | null, response: unknown) => { diff --git a/apps/api/src/services/health-probe-loop.ts b/apps/api/src/services/health-probe-loop.ts index 8b67562..f491e5e 100644 --- a/apps/api/src/services/health-probe-loop.ts +++ b/apps/api/src/services/health-probe-loop.ts @@ -61,7 +61,7 @@ function isPublicEndpoint(iPPORT: string): boolean { if (host.startsWith("10.")) return false; if (host.startsWith("192.168.")) return false; if (host.startsWith("172.")) { - const second = parseInt(host.split(".")[1], 10); + const second = parseInt(host.split(".")[1] ?? "", 10); if (second >= 16 && second <= 31) return false; } if (host.startsWith("169.254.")) return false; @@ -72,7 +72,7 @@ function shuffle(arr: T[]): T[] { const a = [...arr]; for (let i = a.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); - [a[i], a[j]] = [a[j], a[i]]; + [a[i], a[j]] = [a[j]!, a[i]!]; } return a; } diff --git a/apps/api/src/services/health-store.ts b/apps/api/src/services/health-store.ts index fb2deb0..6951d75 100644 --- a/apps/api/src/services/health-store.ts +++ b/apps/api/src/services/health-store.ts @@ -76,7 +76,7 @@ async function readFromIndex( if (val) { records.push(JSON.parse(val) as HealthRecord); } else { - staleKeys.push(batch[j]); + staleKeys.push(batch[j]!); } } } @@ -149,7 +149,7 @@ export async function readHealthMapForProvider( const unhealthyCount = interfaces.filter((i) => i.status !== "healthy").length; const oldestTimestamp = recs.reduce( (oldest, r) => (r.timestamp < oldest ? r.timestamp : oldest), - recs[0].timestamp, + recs[0]!.timestamp, ); result.set(spec, { @@ -192,7 +192,7 @@ export async function readHealthByProviderForSpec( const unhealthyCount = interfaces.filter((i) => i.status !== "healthy").length; const oldestTimestamp = recs.reduce( (oldest, r) => (r.timestamp < oldest ? r.timestamp : oldest), - recs[0].timestamp, + recs[0]!.timestamp, ); result.set(provider, { diff --git a/apps/web/src/app/provider/[lavaid]/page.tsx b/apps/web/src/app/provider/[lavaid]/page.tsx index 22372d6..231151d 100644 --- a/apps/web/src/app/provider/[lavaid]/page.tsx +++ b/apps/web/src/app/provider/[lavaid]/page.tsx @@ -174,7 +174,7 @@ const { data: delegatorRewards } = useApi<{ data: DelegatorReward[] }>(`/provide if (vals.length === 0) return null; const counts = new Map(); for (const v of vals) counts.set(v, (counts.get(v) ?? 0) + 1); - const best = [...counts.entries()].sort((a, b) => b[1] - a[1])[0][0]; + const best = [...counts.entries()].sort((a, b) => b[1] - a[1])[0]![0]; return Number(best) + "%"; }, [provider]); diff --git a/apps/web/src/components/data/IndexChart.tsx b/apps/web/src/components/data/IndexChart.tsx index b06c8df..4421f08 100644 --- a/apps/web/src/components/data/IndexChart.tsx +++ b/apps/web/src/components/data/IndexChart.tsx @@ -141,17 +141,14 @@ export function IndexChart({ const existing = byDay.get(p.date); if (existing) { - existing[p.chainId] = - ((existing[p.chainId] as number) || 0) + relays; - (existing as Record).totalRelays += relays; + const rec = existing as Record; + existing[p.chainId] = (Number(existing[p.chainId]) || 0) + relays; + rec.totalRelays = (rec.totalRelays ?? 0) + relays; if (p.qosSync != null) { - (existing as Record)._qSW += - (p.qosSync ?? 0) * relays; - (existing as Record)._qAW += - (p.qosAvailability ?? 0) * relays; - (existing as Record)._qLW += - (p.qosLatency ?? 0) * relays; - (existing as Record)._w += relays; + rec._qSW = (rec._qSW ?? 0) + (p.qosSync ?? 0) * relays; + rec._qAW = (rec._qAW ?? 0) + (p.qosAvailability ?? 0) * relays; + rec._qLW = (rec._qLW ?? 0) + (p.qosLatency ?? 0) * relays; + rec._w = (rec._w ?? 0) + relays; } } else { byDay.set(p.date, { diff --git a/apps/web/src/components/data/OptimizerMetricsChart.tsx b/apps/web/src/components/data/OptimizerMetricsChart.tsx index 05a13cd..7ef218d 100644 --- a/apps/web/src/components/data/OptimizerMetricsChart.tsx +++ b/apps/web/src/components/data/OptimizerMetricsChart.tsx @@ -159,7 +159,7 @@ function ConsumerDropdown({ consumers, selected, onChange }: { consumers: string /* ─── Helpers ─── */ function formatDateParam(d: Date): string { - return d.toISOString().split("T")[0]; + return d.toISOString().split("T")[0]!; } function daysAgo(n: number): Date { @@ -392,7 +392,7 @@ export function ProviderOptimizerChart({ providerId }: { providerId: string }) { tickFormatter={formatTimestamp} > - + @@ -491,7 +491,7 @@ export function ChainOptimizerChart({ specId, providerInfo }: { specId: string; topProviders.map((p, i) => ({ key: p.slice(0, 10), label: providerMap.get(p)?.moniker || p.slice(0, 10) + "...", - color: PROVIDER_COLORS[i % PROVIDER_COLORS.length], + color: PROVIDER_COLORS[i % PROVIDER_COLORS.length]!, })), [topProviders, providerMap]); diff --git a/apps/web/src/components/layout/SearchBar.tsx b/apps/web/src/components/layout/SearchBar.tsx index 6c165db..3dd4863 100644 --- a/apps/web/src/components/layout/SearchBar.tsx +++ b/apps/web/src/components/layout/SearchBar.tsx @@ -258,7 +258,8 @@ function DesktopSearch() { setActiveIndex((i) => (i > 0 ? i - 1 : flatResults.length - 1)); } else if (e.key === "Enter" && activeIndex >= 0) { e.preventDefault(); - navigate(flatResults[activeIndex]); + const result = flatResults[activeIndex]; + if (result) navigate(result); } else if (e.key === "Escape") { setOpen(false); inputRef.current?.blur(); diff --git a/apps/web/tsconfig.json b/apps/web/tsconfig.json index 444ad3a..d4c27bb 100644 --- a/apps/web/tsconfig.json +++ b/apps/web/tsconfig.json @@ -12,6 +12,7 @@ "noEmit": true, "incremental": true, "strict": true, + "noUncheckedIndexedAccess": true, "esModuleInterop": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true, diff --git a/tsconfig.base.json b/tsconfig.base.json index cf8439e..d235eea 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -5,6 +5,7 @@ "moduleResolution": "Node16", "lib": ["ES2022"], "strict": true, + "noUncheckedIndexedAccess": true, "esModuleInterop": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true, From e057f5d87071a4d4088c30bcb626d1fb844af78c Mon Sep 17 00:00:00 2001 From: Nimrod Teich Date: Fri, 17 Apr 2026 00:08:57 +0300 Subject: [PATCH 14/25] =?UTF-8?q?refactor:=20drop=20API=20pagination=20?= =?UTF-8?q?=E2=80=94=20providers=20fit=20in=20a=20single=20payload?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Network-wide provider count sits in the tens, so paginating the list response at the HTTP layer buys nothing and makes client filtering/sorting awkward. Return the full provider array from GET /providers and let the frontend paginate with TanStack Table if it wants to. Changes: - Delete plugins/pagination.ts + the app.register call in main.ts. - Strip request.pagination usage from routes/providers.ts (both GET / and GET /:addr/health) and simplify readHealthForProvider to return the full HealthRecord[] array. - Remove PaginatedResponse/PaginationParams from @info/shared/types; ApiError stays. - Drop the limit=10000 query param on the frontend /providers page. - Update affected tests (11 files). - Ignore .next.old dev artifact directories. --- .gitignore | 3 + apps/api/src/__tests__/health-store.test.ts | 25 ++------- apps/api/src/__tests__/plugins.test.ts | 52 ----------------- .../routes-all-providers-apr.test.ts | 2 - apps/api/src/__tests__/routes-apr.test.ts | 2 - .../src/__tests__/routes-burn-rate.test.ts | 2 - apps/api/src/__tests__/routes-health.test.ts | 2 - .../routes-provider-claimable-rewards.test.ts | 2 - .../routes-provider-estimated-rewards.test.ts | 2 - .../__tests__/routes-provider-rewards.test.ts | 2 - .../src/__tests__/routes-providers.test.ts | 7 +-- apps/api/src/__tests__/routes-supply.test.ts | 2 - apps/api/src/__tests__/routes-tvl.test.ts | 2 - .../routes-validators-and-rewards.test.ts | 2 - apps/api/src/__tests__/test-helpers.ts | 2 - apps/api/src/main.ts | 2 - apps/api/src/plugins/pagination.ts | 56 ------------------- apps/api/src/routes/providers.ts | 44 +++++---------- apps/api/src/services/health-store.ts | 9 +-- apps/web/src/app/providers/page.tsx | 34 +---------- packages/shared/src/__tests__/types.test.ts | 19 ------- packages/shared/src/types/api.ts | 17 ------ 22 files changed, 28 insertions(+), 262 deletions(-) delete mode 100644 apps/api/src/plugins/pagination.ts diff --git a/.gitignore b/.gitignore index b2b303a..1da6bd2 100644 --- a/.gitignore +++ b/.gitignore @@ -36,3 +36,6 @@ CLAUDE.local.md # pnpm store .pnpm-store/ + +# Next.js dev artifacts (old/backup directories) +.next.old/ diff --git a/apps/api/src/__tests__/health-store.test.ts b/apps/api/src/__tests__/health-store.test.ts index 8f728a3..9ae31c2 100644 --- a/apps/api/src/__tests__/health-store.test.ts +++ b/apps/api/src/__tests__/health-store.test.ts @@ -71,34 +71,19 @@ describe("health-store", () => { describe("readHealthForProvider", () => { it("returns empty for unknown provider", async () => { - const result = await readHealthForProvider(redis as any, "lava@unknown", 1, 20); - expect(result.data).toEqual([]); - expect(result.total).toBe(0); + const result = await readHealthForProvider(redis as any, "lava@unknown"); + expect(result).toEqual([]); }); it("returns all health records for a provider", async () => { await writeHealthStatus(redis as any, "lava@test", "ETH1", "jsonrpc", "2", "healthy", { latency: 10 }); await writeHealthStatus(redis as any, "lava@test", "LAVA", "grpc", "1", "unhealthy", { message: "timeout" }); - const result = await readHealthForProvider(redis as any, "lava@test", 1, 20); - expect(result.total).toBe(2); - expect(result.data).toHaveLength(2); - const specs = result.data.map((r) => r.spec).sort(); + const result = await readHealthForProvider(redis as any, "lava@test"); + expect(result).toHaveLength(2); + const specs = result.map((r) => r.spec).sort(); expect(specs).toEqual(["ETH1", "LAVA"]); }); - - it("paginates correctly", async () => { - await writeHealthStatus(redis as any, "lava@test", "ETH1", "jsonrpc", "2", "healthy", {}); - await writeHealthStatus(redis as any, "lava@test", "LAVA", "grpc", "1", "healthy", {}); - await writeHealthStatus(redis as any, "lava@test", "BSC", "jsonrpc", "2", "healthy", {}); - - const page1 = await readHealthForProvider(redis as any, "lava@test", 1, 2); - expect(page1.data).toHaveLength(2); - expect(page1.total).toBe(3); - - const page2 = await readHealthForProvider(redis as any, "lava@test", 2, 2); - expect(page2.data).toHaveLength(1); - }); }); describe("readHealthSummaryForSpec", () => { diff --git a/apps/api/src/__tests__/plugins.test.ts b/apps/api/src/__tests__/plugins.test.ts index 9d91742..68cb036 100644 --- a/apps/api/src/__tests__/plugins.test.ts +++ b/apps/api/src/__tests__/plugins.test.ts @@ -1,57 +1,5 @@ import { describe, it, expect } from "vitest"; import { buildTestApp } from "./test-helpers.js"; -import { parsePagination } from "../plugins/pagination.js"; - -describe("pagination plugin", () => { - it("parses default values", () => { - const p = parsePagination({}); - expect(p.page).toBe(1); - expect(p.limit).toBe(20); - expect(p.offset).toBe(0); - expect(p.sort).toBeNull(); - expect(p.order).toBe("asc"); - }); - - it("parses custom values", () => { - const p = parsePagination({ page: "3", limit: "50", sort: "name", order: "desc" }); - expect(p.page).toBe(3); - expect(p.limit).toBe(50); - expect(p.offset).toBe(100); - expect(p.sort).toBe("name"); - expect(p.order).toBe("desc"); - }); - - it("clamps page to 1-4000", () => { - expect(parsePagination({ page: "0" }).page).toBe(1); - expect(parsePagination({ page: "-5" }).page).toBe(1); - expect(parsePagination({ page: "5000" }).page).toBe(4000); - }); - - it("clamps limit to 1-100", () => { - expect(parsePagination({ limit: "0" }).limit).toBe(20); // 0 is falsy → falls to default 20 - expect(parsePagination({ limit: "200" }).limit).toBe(100); - expect(parsePagination({ limit: "-1" }).limit).toBe(1); - expect(parsePagination({ limit: "1" }).limit).toBe(1); - expect(parsePagination({ limit: "100" }).limit).toBe(100); - }); - - it("defaults order to asc for invalid values", () => { - expect(parsePagination({ order: "invalid" }).order).toBe("asc"); - expect(parsePagination({ order: "" }).order).toBe("asc"); - }); - - it("handles NaN gracefully", () => { - const p = parsePagination({ page: "abc", limit: "xyz" }); - expect(p.page).toBe(1); - expect(p.limit).toBe(20); - }); - - it("calculates offset correctly", () => { - expect(parsePagination({ page: "1", limit: "10" }).offset).toBe(0); - expect(parsePagination({ page: "2", limit: "10" }).offset).toBe(10); - expect(parsePagination({ page: "5", limit: "25" }).offset).toBe(100); - }); -}); describe("error handler plugin", () => { it("returns 404 for unknown routes", async () => { diff --git a/apps/api/src/__tests__/routes-all-providers-apr.test.ts b/apps/api/src/__tests__/routes-all-providers-apr.test.ts index e685cdb..37e96e1 100644 --- a/apps/api/src/__tests__/routes-all-providers-apr.test.ts +++ b/apps/api/src/__tests__/routes-all-providers-apr.test.ts @@ -1,6 +1,5 @@ import { describe, it, expect, vi } from "vitest"; import Fastify from "fastify"; -import { paginationPlugin } from "../plugins/pagination.js"; import { errorHandlerPlugin } from "../plugins/error-handler.js"; vi.mock("../rpc/lava.js", () => ({ @@ -17,7 +16,6 @@ const { allProvidersAprRoutes } = await import("../routes/all-providers-apr.js") async function buildApp() { const app = Fastify({ logger: false }); await app.register(errorHandlerPlugin); - await app.register(paginationPlugin); await app.register(allProvidersAprRoutes, { prefix: "/all_providers_apr" }); return app; } diff --git a/apps/api/src/__tests__/routes-apr.test.ts b/apps/api/src/__tests__/routes-apr.test.ts index 47af496..ad8c30f 100644 --- a/apps/api/src/__tests__/routes-apr.test.ts +++ b/apps/api/src/__tests__/routes-apr.test.ts @@ -1,6 +1,5 @@ import { describe, it, expect, vi } from "vitest"; import Fastify from "fastify"; -import { paginationPlugin } from "../plugins/pagination.js"; import { errorHandlerPlugin } from "../plugins/error-handler.js"; vi.mock("../rpc/lava.js", () => ({ @@ -13,7 +12,6 @@ const { aprRoutes } = await import("../routes/apr.js"); async function buildApp() { const app = Fastify({ logger: false }); await app.register(errorHandlerPlugin); - await app.register(paginationPlugin); await app.register(aprRoutes); return app; } diff --git a/apps/api/src/__tests__/routes-burn-rate.test.ts b/apps/api/src/__tests__/routes-burn-rate.test.ts index 60f11a7..10ae761 100644 --- a/apps/api/src/__tests__/routes-burn-rate.test.ts +++ b/apps/api/src/__tests__/routes-burn-rate.test.ts @@ -1,6 +1,5 @@ import { describe, it, expect, vi, beforeEach } from "vitest"; import Fastify from "fastify"; -import { paginationPlugin } from "../plugins/pagination.js"; import { errorHandlerPlugin } from "../plugins/error-handler.js"; vi.mock("../rpc/lava.js", () => ({ @@ -19,7 +18,6 @@ const { burnRateRoutes } = await import("../routes/burn-rate.js"); async function buildApp() { const app = Fastify({ logger: false }); await app.register(errorHandlerPlugin); - await app.register(paginationPlugin); await app.register(burnRateRoutes); return app; } diff --git a/apps/api/src/__tests__/routes-health.test.ts b/apps/api/src/__tests__/routes-health.test.ts index 0cef145..d35e8b8 100644 --- a/apps/api/src/__tests__/routes-health.test.ts +++ b/apps/api/src/__tests__/routes-health.test.ts @@ -1,6 +1,5 @@ import { describe, it, expect, vi } from "vitest"; import Fastify from "fastify"; -import { paginationPlugin } from "../plugins/pagination.js"; import { errorHandlerPlugin } from "../plugins/error-handler.js"; vi.mock("../rpc/lava.js", () => ({ @@ -13,7 +12,6 @@ const { healthRoutes } = await import("../routes/health.js"); async function buildApp() { const app = Fastify({ logger: false }); await app.register(errorHandlerPlugin); - await app.register(paginationPlugin); await app.register(healthRoutes); return app; } diff --git a/apps/api/src/__tests__/routes-provider-claimable-rewards.test.ts b/apps/api/src/__tests__/routes-provider-claimable-rewards.test.ts index f20cd7e..b3301db 100644 --- a/apps/api/src/__tests__/routes-provider-claimable-rewards.test.ts +++ b/apps/api/src/__tests__/routes-provider-claimable-rewards.test.ts @@ -1,6 +1,5 @@ import { describe, it, expect, vi, beforeEach } from "vitest"; import Fastify from "fastify"; -import { paginationPlugin } from "../plugins/pagination.js"; import { errorHandlerPlugin } from "../plugins/error-handler.js"; vi.mock("../rpc/lava.js", () => ({ @@ -22,7 +21,6 @@ const { providerClaimableRewardsRoutes } = await import("../routes/provider-clai async function buildApp() { const app = Fastify({ logger: false }); await app.register(errorHandlerPlugin); - await app.register(paginationPlugin); await app.register(providerClaimableRewardsRoutes); return app; } diff --git a/apps/api/src/__tests__/routes-provider-estimated-rewards.test.ts b/apps/api/src/__tests__/routes-provider-estimated-rewards.test.ts index a0cadbe..f51a4b6 100644 --- a/apps/api/src/__tests__/routes-provider-estimated-rewards.test.ts +++ b/apps/api/src/__tests__/routes-provider-estimated-rewards.test.ts @@ -1,6 +1,5 @@ import { describe, it, expect, vi, beforeEach } from "vitest"; import Fastify from "fastify"; -import { paginationPlugin } from "../plugins/pagination.js"; import { errorHandlerPlugin } from "../plugins/error-handler.js"; vi.mock("../rpc/lava.js", () => ({ @@ -24,7 +23,6 @@ const { providerEstimatedRewardsRoutes } = await import("../routes/provider-esti async function buildApp() { const app = Fastify({ logger: false }); await app.register(errorHandlerPlugin); - await app.register(paginationPlugin); await app.register(providerEstimatedRewardsRoutes); return app; } diff --git a/apps/api/src/__tests__/routes-provider-rewards.test.ts b/apps/api/src/__tests__/routes-provider-rewards.test.ts index 891eec6..8083d88 100644 --- a/apps/api/src/__tests__/routes-provider-rewards.test.ts +++ b/apps/api/src/__tests__/routes-provider-rewards.test.ts @@ -1,6 +1,5 @@ import { describe, it, expect, vi, beforeEach } from "vitest"; import Fastify from "fastify"; -import { paginationPlugin } from "../plugins/pagination.js"; import { errorHandlerPlugin } from "../plugins/error-handler.js"; vi.mock("../graphql/client.js", () => ({ @@ -19,7 +18,6 @@ const { providerRewardsRoutes } = await import("../routes/provider-rewards.js"); async function buildApp() { const app = Fastify({ logger: false }); await app.register(errorHandlerPlugin); - await app.register(paginationPlugin); await app.register(providerRewardsRoutes); return app; } diff --git a/apps/api/src/__tests__/routes-providers.test.ts b/apps/api/src/__tests__/routes-providers.test.ts index f361529..34ee137 100644 --- a/apps/api/src/__tests__/routes-providers.test.ts +++ b/apps/api/src/__tests__/routes-providers.test.ts @@ -1,6 +1,5 @@ import { describe, it, expect, vi, beforeEach } from "vitest"; import Fastify from "fastify"; -import { paginationPlugin } from "../plugins/pagination.js"; import { errorHandlerPlugin } from "../plugins/error-handler.js"; vi.mock("../graphql/client.js", () => ({ gqlSafe: vi.fn() })); @@ -23,7 +22,6 @@ const { providerRoutes } = await import("../routes/providers.js"); async function buildApp() { const app = Fastify({ logger: false }); await app.register(errorHandlerPlugin); - await app.register(paginationPlugin); await app.register(providerRoutes, { prefix: "/providers" }); return app; } @@ -44,21 +42,20 @@ const MOCK_ALL_PROVIDERS = [ ]; describe("GET /providers", () => { - it("paginates and sorts providers by totalStake desc", async () => { + it("returns the full list sorted by totalStake desc, enriched with 30d relay data", async () => { (fetchAllProviders as ReturnType).mockResolvedValue(MOCK_ALL_PROVIDERS); (gqlSafe as ReturnType).mockResolvedValue({ mvRelayDailies: { groupedAggregates: [{ keys: ["lava@alpha"], sum: { cu: "500", relays: "100" } }] }, }); const app = await buildApp(); - const res = await app.inject({ method: "GET", url: "/providers?page=1&limit=10" }); + const res = await app.inject({ method: "GET", url: "/providers" }); expect(res.statusCode).toBe(200); const body = JSON.parse(res.body); expect(body.data).toHaveLength(2); expect(body.data[0].provider).toBe("lava@alpha"); // bigger stake first expect(body.data[0].cuSum30d).toBe("500"); expect(body.data[1].cuSum30d).toBeNull(); - expect(body.pagination).toEqual({ total: 2, page: 1, limit: 10, pages: 1 }); }); }); diff --git a/apps/api/src/__tests__/routes-supply.test.ts b/apps/api/src/__tests__/routes-supply.test.ts index 8ca7b4e..e3657b5 100644 --- a/apps/api/src/__tests__/routes-supply.test.ts +++ b/apps/api/src/__tests__/routes-supply.test.ts @@ -1,6 +1,5 @@ import { describe, it, expect, vi } from "vitest"; import Fastify from "fastify"; -import { paginationPlugin } from "../plugins/pagination.js"; import { errorHandlerPlugin } from "../plugins/error-handler.js"; vi.mock("../rpc/lava.js", () => ({ @@ -14,7 +13,6 @@ const { supplyRoutes } = await import("../routes/supply.js"); async function buildApp() { const app = Fastify({ logger: false }); await app.register(errorHandlerPlugin); - await app.register(paginationPlugin); await app.register(supplyRoutes, { prefix: "/supply" }); return app; } diff --git a/apps/api/src/__tests__/routes-tvl.test.ts b/apps/api/src/__tests__/routes-tvl.test.ts index 20910ab..3853838 100644 --- a/apps/api/src/__tests__/routes-tvl.test.ts +++ b/apps/api/src/__tests__/routes-tvl.test.ts @@ -1,6 +1,5 @@ import { describe, it, expect, vi } from "vitest"; import Fastify from "fastify"; -import { paginationPlugin } from "../plugins/pagination.js"; import { errorHandlerPlugin } from "../plugins/error-handler.js"; vi.mock("../rpc/lava.js", () => ({ @@ -13,7 +12,6 @@ const { tvlRoutes } = await import("../routes/tvl.js"); async function buildApp() { const app = Fastify({ logger: false }); await app.register(errorHandlerPlugin); - await app.register(paginationPlugin); await app.register(tvlRoutes); return app; } diff --git a/apps/api/src/__tests__/routes-validators-and-rewards.test.ts b/apps/api/src/__tests__/routes-validators-and-rewards.test.ts index 92c0774..595466a 100644 --- a/apps/api/src/__tests__/routes-validators-and-rewards.test.ts +++ b/apps/api/src/__tests__/routes-validators-and-rewards.test.ts @@ -1,6 +1,5 @@ import { describe, it, expect, vi, beforeEach } from "vitest"; import Fastify from "fastify"; -import { paginationPlugin } from "../plugins/pagination.js"; import { errorHandlerPlugin } from "../plugins/error-handler.js"; vi.mock("../rpc/lava.js", () => ({ @@ -13,7 +12,6 @@ const { validatorsAndRewardsRoutes } = await import("../routes/validators-and-re async function buildApp() { const app = Fastify({ logger: false }); await app.register(errorHandlerPlugin); - await app.register(paginationPlugin); await app.register(validatorsAndRewardsRoutes); return app; } diff --git a/apps/api/src/__tests__/test-helpers.ts b/apps/api/src/__tests__/test-helpers.ts index 9500353..36e1dda 100644 --- a/apps/api/src/__tests__/test-helpers.ts +++ b/apps/api/src/__tests__/test-helpers.ts @@ -1,5 +1,4 @@ import Fastify, { type FastifyInstance } from "fastify"; -import { paginationPlugin } from "../plugins/pagination.js"; import { errorHandlerPlugin } from "../plugins/error-handler.js"; /** @@ -8,6 +7,5 @@ import { errorHandlerPlugin } from "../plugins/error-handler.js"; export async function buildTestApp(): Promise { const app = Fastify({ logger: false }); await app.register(errorHandlerPlugin); - await app.register(paginationPlugin); return app; } diff --git a/apps/api/src/main.ts b/apps/api/src/main.ts index c79426c..33e7076 100644 --- a/apps/api/src/main.ts +++ b/apps/api/src/main.ts @@ -5,7 +5,6 @@ import rateLimit from "@fastify/rate-limit"; import compress from "@fastify/compress"; import { redisPlugin } from "./plugins/redis.js"; import { cachePlugin } from "./plugins/cache.js"; -import { paginationPlugin } from "./plugins/pagination.js"; import { errorHandlerPlugin } from "./plugins/error-handler.js"; import { swaggerPlugin } from "./plugins/swagger.js"; import { healthProbePlugin } from "./plugins/health-probe.js"; @@ -56,7 +55,6 @@ async function main() { await app.register(redisPlugin); await app.register(relaysDbPlugin); await app.register(cachePlugin); - await app.register(paginationPlugin); await app.register(healthRoutes); await app.register(indexRoutes, { prefix: "/index" }); diff --git a/apps/api/src/plugins/pagination.ts b/apps/api/src/plugins/pagination.ts deleted file mode 100644 index 2aee287..0000000 --- a/apps/api/src/plugins/pagination.ts +++ /dev/null @@ -1,56 +0,0 @@ -import fp from "fastify-plugin"; -import type { FastifyInstance, FastifyRequest } from "fastify"; - -/** Default max rows per page. Override per-route via config.maxLimit. */ -const DEFAULT_MAX_LIMIT = 100; - -export interface PaginationQuery { - page?: string; - limit?: string; - sort?: string; - order?: string; -} - -export interface ParsedPagination { - page: number; - limit: number; - offset: number; - sort: string | null; - order: "asc" | "desc"; -} - -/** - * Parse pagination from query params. - * maxLimit caps the upper bound (default 100, configurable per-route via config.maxLimit). - */ -export function parsePagination(query: PaginationQuery, maxLimit = DEFAULT_MAX_LIMIT): ParsedPagination { - const page = Math.max(1, Math.min(4000, parseInt(query.page ?? "1", 10) || 1)); - const limit = Math.max(1, Math.min(maxLimit, parseInt(query.limit ?? "20", 10) || 20)); - const offset = (page - 1) * limit; - const sort = query.sort ?? null; - const order = query.order === "desc" ? "desc" : "asc"; - return { page, limit, offset, sort, order }; -} - -declare module "fastify" { - interface FastifyRequest { - pagination: ParsedPagination; - } - interface FastifyContextConfig { - maxLimit?: number; - } -} - -/** - * Pagination plugin. Parses ?page, ?limit, ?sort, ?order from query string. - * Per-route upper bound via config.maxLimit (default 100). - */ -export const paginationPlugin = fp(async (app: FastifyInstance) => { - app.decorateRequest("pagination", { - getter() { - const req = this as FastifyRequest; - const maxLimit = req.routeOptions.config?.maxLimit ?? DEFAULT_MAX_LIMIT; - return parsePagination(req.query as PaginationQuery, maxLimit); - }, - }); -}); diff --git a/apps/api/src/routes/providers.ts b/apps/api/src/routes/providers.ts index 4132e84..6e8aa54 100644 --- a/apps/api/src/routes/providers.ts +++ b/apps/api/src/routes/providers.ts @@ -1,6 +1,6 @@ import type { FastifyInstance } from "fastify"; import { weightedQos } from "@info/shared/utils"; -import type { PaginatedResponse, ProviderListItem } from "@info/shared/types"; +import type { ProviderListItem } from "@info/shared/types"; import { gqlSafe } from "../graphql/client.js"; import { fetchAllProviders, @@ -20,23 +20,13 @@ const addrSchema = { }; export async function providerRoutes(app: FastifyInstance) { - // GET /providers — chain RPC + indexer relay data + // GET /providers — full provider list with 30d relay stats. + // Network-wide provider count sits in the tens, so pagination buys nothing + // and complicates client filtering; return the full array sorted by stake. app.get("/", { - schema: { - tags: ["Providers"], - summary: "Paginated provider list with 30d relay stats", - querystring: { - type: "object" as const, - properties: { - page: { type: "integer" as const, default: 1 }, - limit: { type: "integer" as const, default: 20 }, - }, - }, - }, + schema: { tags: ["Providers"], summary: "All providers with 30d relay stats, sorted by total stake desc" }, config: { cacheTTL: 300 }, - }, async (request): Promise> => { - const { page, limit, offset } = request.pagination; - + }, async (): Promise<{ data: ProviderListItem[] }> => { const since = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000) .toISOString() .slice(0, 10); @@ -68,15 +58,13 @@ export async function providerRoutes(app: FastifyInstance) { } } - const total = providers.length; providers.sort((a, b) => { const diff = BigInt(b.totalStake) - BigInt(a.totalStake); return diff > 0n ? 1 : diff < 0n ? -1 : 0; }); - const paged = providers.slice(offset, offset + limit); return { - data: paged.map((p): ProviderListItem => { + data: providers.map((p): ProviderListItem => { const relay = relayMap.get(p.address); return { provider: p.address, @@ -90,7 +78,6 @@ export async function providerRoutes(app: FastifyInstance) { relaySum30d: relay?.relays ?? null, }; }), - pagination: { total, page, limit, pages: Math.ceil(total / limit) }, }; }); @@ -186,28 +173,23 @@ export async function providerRoutes(app: FastifyInstance) { return { data: results.filter(Boolean) }; }); - // GET /providers/:addr/health — from Redis + // GET /providers/:addr/health — from Redis (per-interface probe records) app.get<{ Params: { addr: string } }>("/:addr/health", { schema: { ...addrSchema, tags: ["Providers"], - summary: "Provider health probe results (paginated)", + summary: "Provider health probe results", }, config: { cacheTTL: 30 }, }, async (request) => { const { addr } = request.params; - const { page, limit } = request.pagination; - - if (!app.redis) { - return { data: [], pagination: { total: 0, page, limit, pages: 0 } }; - } + if (!app.redis) return { data: [] }; try { - const result = await readHealthForProvider(app.redis, addr, page, limit); - const total = result.total; - return { data: result.data, pagination: { total, page, limit, pages: Math.ceil(total / limit) } }; + const data = await readHealthForProvider(app.redis, addr); + return { data }; } catch { - return { data: [], pagination: { total: 0, page, limit, pages: 0 } }; + return { data: [] }; } }); diff --git a/apps/api/src/services/health-store.ts b/apps/api/src/services/health-store.ts index 6951d75..e9625c6 100644 --- a/apps/api/src/services/health-store.ts +++ b/apps/api/src/services/health-store.ts @@ -92,15 +92,10 @@ async function readFromIndex( export async function readHealthForProvider( redis: Redis, provider: string, - page: number, - limit: number, -): Promise<{ data: HealthRecord[]; total: number }> { +): Promise { const records = await readFromIndex(redis, providerIndexKey(provider)); records.sort((a, b) => b.timestamp.localeCompare(a.timestamp)); - - const total = records.length; - const offset = (page - 1) * limit; - return { data: records.slice(offset, offset + limit), total }; + return records; } export interface InterfaceHealth { diff --git a/apps/web/src/app/providers/page.tsx b/apps/web/src/app/providers/page.tsx index ca8d582..77ccdf1 100644 --- a/apps/web/src/app/providers/page.tsx +++ b/apps/web/src/app/providers/page.tsx @@ -5,13 +5,12 @@ import { useReactTable, getCoreRowModel, getSortedRowModel, - getPaginationRowModel, flexRender, type ColumnDef, type SortingState, } from "@tanstack/react-table"; import { ChevronUp, ChevronDown, ChevronsUpDown } from "lucide-react"; -import type { PaginatedResponse, ProviderListItem } from "@info/shared/types"; +import type { ProviderListItem } from "@info/shared/types"; import { useApi } from "@/hooks/use-api"; import { Loading } from "@/components/data/Loading"; import { ProviderLink } from "@/components/data/ProviderLink"; @@ -63,7 +62,7 @@ const columns: ColumnDef[] = [ ]; function ProvidersContent() { -const { data: resp, isLoading } = useApi>("/providers?limit=10000"); + const { data: resp, isLoading } = useApi<{ data: ProviderListItem[] }>("/providers"); const providers = useMemo(() => resp?.data ?? [], [resp]); const [sorting, setSorting] = useState([ @@ -77,15 +76,13 @@ const { data: resp, isLoading } = useApi>("/ onSortingChange: setSorting, getCoreRowModel: getCoreRowModel(), getSortedRowModel: getSortedRowModel(), - getPaginationRowModel: getPaginationRowModel(), - initialState: { pagination: { pageSize: 20 } }, }); if (isLoading) return ; return (
-
+

Active Providers ({providers.length}) @@ -139,31 +136,6 @@ const { data: resp, isLoading } = useApi>("/

- - {table.getPageCount() > 1 && ( -
- - Page {table.getState().pagination.pageIndex + 1} of {table.getPageCount()} - {" "}({providers.length} providers) - -
- - -
-
- )}
diff --git a/packages/shared/src/__tests__/types.test.ts b/packages/shared/src/__tests__/types.test.ts index 60b915c..324e294 100644 --- a/packages/shared/src/__tests__/types.test.ts +++ b/packages/shared/src/__tests__/types.test.ts @@ -3,11 +3,6 @@ import { PROVIDER_STATUS, HEALTH_STATUS, EVENT_TYPES, - type ProviderStatus, - type HealthStatus, - type EventType, - type PaginatedResponse, - type PaginationParams, type ApiError, type HealthCheckResult, type BlockchainEvent, @@ -77,20 +72,6 @@ describe("IGNORED_EVENT_TYPES", () => { }); describe("type shapes (compile-time verification via runtime construction)", () => { - it("PaginatedResponse shape", () => { - const resp: PaginatedResponse<{ id: number }> = { - data: [{ id: 1 }], - pagination: { total: 1, page: 1, limit: 20, pages: 1 }, - }; - expect(resp.data).toHaveLength(1); - expect(resp.pagination.total).toBe(1); - }); - - it("PaginationParams shape", () => { - const params: PaginationParams = { page: 1, limit: 20, sort: "name", order: "asc" }; - expect(params.page).toBe(1); - }); - it("ApiError shape", () => { const err: ApiError = { error: "NotFound", message: "Not found", statusCode: 404 }; expect(err.statusCode).toBe(404); diff --git a/packages/shared/src/types/api.ts b/packages/shared/src/types/api.ts index 82fcd92..0d67830 100644 --- a/packages/shared/src/types/api.ts +++ b/packages/shared/src/types/api.ts @@ -1,20 +1,3 @@ -export interface PaginatedResponse { - data: T[]; - pagination: { - total: number; - page: number; - limit: number; - pages: number; - }; -} - -export interface PaginationParams { - page?: number; - limit?: number; - sort?: string; - order?: "asc" | "desc"; -} - export interface ApiError { error: string; message?: string; From 7fda35e2a5b4e8d90b7570760a7be6a5b6a6cb7f Mon Sep 17 00:00:00 2001 From: Nimrod Teich Date: Fri, 17 Apr 2026 00:09:38 +0300 Subject: [PATCH 15/25] style(web): use 1_000_000n literal instead of BigInt(1e6) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit BigInt(1e6) round-trips through a Number at runtime — defeats the point of BigInt and costs a few nanoseconds per call site. The literal 1_000_000n is both clearer and slightly faster. --- apps/web/src/lib/format.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/web/src/lib/format.ts b/apps/web/src/lib/format.ts index 64d29e1..9ac5bf4 100644 --- a/apps/web/src/lib/format.ts +++ b/apps/web/src/lib/format.ts @@ -19,7 +19,7 @@ export function formatNumberKMB(value: number | string): string { export function formatLava(ulava: string | bigint | number): string { try { const value = typeof ulava === "bigint" ? ulava : BigInt(String(ulava).replace(/ulava$/, "")); - const lava = Number(value / BigInt(1e6)); + const lava = Number(value / 1_000_000n); return new Intl.NumberFormat("en-US").format(lava); } catch { return "0"; @@ -30,7 +30,7 @@ export function formatLava(ulava: string | bigint | number): string { export function formatLavaKMB(ulava: string | bigint | number): string { try { const value = typeof ulava === "bigint" ? ulava : BigInt(String(ulava).replace(/ulava$/, "")); - const lava = Number(value / BigInt(1e6)); + const lava = Number(value / 1_000_000n); return formatNumberKMB(lava); } catch { return "0"; From 1bfc601fd0f885bb90aae65b32a960de948851d3 Mon Sep 17 00:00:00 2001 From: Nimrod Teich Date: Fri, 17 Apr 2026 00:14:41 +0300 Subject: [PATCH 16/25] =?UTF-8?q?refactor:=20single=20source=20for=20ulava?= =?UTF-8?q?=E2=86=92LAVA=20conversion?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Four call sites were hand-rolling the same BigInt(value / 1_000_000n) math: web/lib/format.ts (both formatLava + formatLavaKMB), api/routes/ supply.ts, api/rpc/pricing.ts, and api/bin/precompute.ts. Keep the existing ulavaToLava helper in @info/shared/utils/formatting.ts (returns a decimal string with no trailing zeros) and add a second variant ulavaToLavaNumber that returns a Number for arithmetic — used by the TVL math in pricing.ts and the display helpers in web/lib/format.ts. No behavior change; tests still pass. --- apps/api/src/bin/precompute.ts | 4 ++-- apps/api/src/routes/supply.ts | 9 +++------ apps/api/src/rpc/pricing.ts | 8 ++++---- apps/web/src/lib/format.ts | 12 +++++------- packages/shared/src/utils/formatting.ts | 26 ++++++++++++++++++++----- 5 files changed, 35 insertions(+), 24 deletions(-) diff --git a/apps/api/src/bin/precompute.ts b/apps/api/src/bin/precompute.ts index aa20997..77b862c 100644 --- a/apps/api/src/bin/precompute.ts +++ b/apps/api/src/bin/precompute.ts @@ -22,6 +22,7 @@ */ import { Redis } from "ioredis"; import pino from "pino"; +import { ulavaToLava } from "@info/shared/utils"; import { gqlSafe } from "../graphql/client.js"; import { fetchCirculatingSupply } from "../rpc/supply.js"; import { computeAPR, computeAllProvidersApr } from "../rpc/apr.js"; @@ -31,8 +32,7 @@ import { writePrecomputed } from "../services/precompute-store.js"; const log = pino({ name: "precompute" }); async function recomputeCirculatingSupply(redis: Redis): Promise { - const supply = await fetchCirculatingSupply(); - const value = (Number(supply / 1_000_000n) + Number(supply % 1_000_000n) / 1_000_000).toString(); + const value = ulavaToLava(await fetchCirculatingSupply()); await writePrecomputed(redis, "supply.circulating", value); } diff --git a/apps/api/src/routes/supply.ts b/apps/api/src/routes/supply.ts index 0d17051..8988139 100644 --- a/apps/api/src/routes/supply.ts +++ b/apps/api/src/routes/supply.ts @@ -1,18 +1,16 @@ import type { FastifyInstance } from "fastify"; +import { ulavaToLava } from "@info/shared/utils"; import { fetchTotalSupply, fetchCirculatingSupply } from "../rpc/lava.js"; import { readPrecomputed } from "../services/precompute-store.js"; -const ULAVA_TO_LAVA = 1_000_000n; - export async function supplyRoutes(app: FastifyInstance) { // GET /supply/total — total token supply in lava, cached 5 min app.get("/total", { schema: { tags: ["Supply"], summary: "Total LAVA supply (plain text)" }, config: { cacheTTL: 300, rateLimit: { max: 20, timeWindow: "1 minute" } }, }, async (_request, reply) => { - const total = await fetchTotalSupply(); reply.header("Content-Type", "text/plain"); - return (total / ULAVA_TO_LAVA).toString(); + return ulavaToLava(await fetchTotalSupply()); }); // GET /supply/circulating — total - locked vesting - reward pools, in lava. @@ -25,7 +23,6 @@ export async function supplyRoutes(app: FastifyInstance) { reply.header("Content-Type", "text/plain"); const cached = await readPrecomputed(app.redis, "supply.circulating"); if (cached) return cached.value; - const circulating = await fetchCirculatingSupply(); - return (circulating / ULAVA_TO_LAVA).toString(); + return ulavaToLava(await fetchCirculatingSupply()); }); } diff --git a/apps/api/src/rpc/pricing.ts b/apps/api/src/rpc/pricing.ts index 2752b85..f1d0e02 100644 --- a/apps/api/src/rpc/pricing.ts +++ b/apps/api/src/rpc/pricing.ts @@ -1,3 +1,4 @@ +import { ulavaToLavaNumber } from "@info/shared/utils"; import { fetchRest } from "./rest.js"; import { fetchStakingPool } from "./supply.js"; @@ -280,12 +281,11 @@ export async function computeTVL(): Promise<{ tvl: string }> { ? BigInt(Math.round((arbitrumUsd / lavaPrice) * 1_000_000)) : 0n; - // Sum all components in ulava, then convert to USD - // Use BigInt division to avoid Number precision loss for large ulava sums + // Sum all components in ulava, then convert to USD via shared helper + // (full precision: integer-divide + remainder, avoiding Number overflow) const totalUlava = bondedTokens + rewardPools + subscriptions + osmosis + baseUlava + arbitrumUlava; - const totalLava = Number(totalUlava / 1_000_000n) + Number(totalUlava % 1_000_000n) / 1_000_000; - const tvlUsd = totalLava * lavaPrice; + const tvlUsd = ulavaToLavaNumber(totalUlava) * lavaPrice; return { tvl: tvlUsd.toFixed(4) }; } diff --git a/apps/web/src/lib/format.ts b/apps/web/src/lib/format.ts index 9ac5bf4..bbed93c 100644 --- a/apps/web/src/lib/format.ts +++ b/apps/web/src/lib/format.ts @@ -1,3 +1,5 @@ +import { ulavaToLavaNumber } from "@info/shared/utils"; + /** Format number with thousand separators */ export function formatNumber(value: number | string | bigint): string { const n = typeof value === "bigint" ? Number(value) : Number(value); @@ -15,12 +17,10 @@ export function formatNumberKMB(value: number | string): string { return n.toFixed(0); } -/** Convert ulava to LAVA with formatting */ +/** Convert ulava to LAVA with thousand-separator formatting */ export function formatLava(ulava: string | bigint | number): string { try { - const value = typeof ulava === "bigint" ? ulava : BigInt(String(ulava).replace(/ulava$/, "")); - const lava = Number(value / 1_000_000n); - return new Intl.NumberFormat("en-US").format(lava); + return new Intl.NumberFormat("en-US").format(ulavaToLavaNumber(ulava)); } catch { return "0"; } @@ -29,9 +29,7 @@ export function formatLava(ulava: string | bigint | number): string { /** Convert ulava to LAVA with K/M/B shorthand */ export function formatLavaKMB(ulava: string | bigint | number): string { try { - const value = typeof ulava === "bigint" ? ulava : BigInt(String(ulava).replace(/ulava$/, "")); - const lava = Number(value / 1_000_000n); - return formatNumberKMB(lava); + return formatNumberKMB(ulavaToLavaNumber(ulava)); } catch { return "0"; } diff --git a/packages/shared/src/utils/formatting.ts b/packages/shared/src/utils/formatting.ts index bd79e56..84b5b18 100644 --- a/packages/shared/src/utils/formatting.ts +++ b/packages/shared/src/utils/formatting.ts @@ -1,15 +1,31 @@ const ULAVA_DECIMALS = 6; -const ULAVA_DIVISOR = 10 ** ULAVA_DECIMALS; +const ULAVA_DIVISOR = 1_000_000n; -/** Convert ulava (micro-lava) to LAVA with proper decimal formatting */ +/** + * Convert ulava (micro-lava) to LAVA as a decimal string (no trailing zeros). + * Preserves full fractional precision by doing integer-divide + remainder, + * so it handles supply-sized amounts (>2^53) correctly. + * + * Example: ulavaToLava("1500000") → "1.5"; ulavaToLava("1000000") → "1". + */ export function ulavaToLava(ulava: bigint | number | string): string { - const value = typeof ulava === "bigint" ? ulava : BigInt(ulava); - const whole = value / BigInt(ULAVA_DIVISOR); - const fraction = value % BigInt(ULAVA_DIVISOR); + const value = typeof ulava === "bigint" ? ulava : BigInt(String(ulava).replace(/ulava$/, "")); + const whole = value / ULAVA_DIVISOR; + const fraction = value % ULAVA_DIVISOR; const fractionStr = fraction.toString().padStart(ULAVA_DECIMALS, "0").replace(/0+$/, ""); return fractionStr ? `${whole}.${fractionStr}` : whole.toString(); } +/** + * Same as ulavaToLava but returns a Number (for arithmetic/formatting). + * Accurate for amounts up to ~9 quadrillion LAVA, which is well beyond + * any on-chain value we work with. + */ +export function ulavaToLavaNumber(ulava: bigint | number | string): number { + const value = typeof ulava === "bigint" ? ulava : BigInt(String(ulava).replace(/ulava$/, "")); + return Number(value / ULAVA_DIVISOR) + Number(value % ULAVA_DIVISOR) / 1_000_000; +} + /** Format a large number with commas */ export function formatNumber(n: number | bigint): string { return n.toLocaleString("en-US"); From 02125414d0244c6c94ca2c94755fea2adc032d2e Mon Sep 17 00:00:00 2001 From: Nimrod Teich Date: Fri, 17 Apr 2026 00:18:44 +0300 Subject: [PATCH 17/25] refactor(api): normalize error-response shape via sendApiError helper Route handlers were emitting at least three different error shapes: reply.status(400).send({ error: msg }) reply.status(503); return { error: msg } reply.status(400).send({ error: 'Error - bad X: ...' }) Add sendApiError(reply, status, message) in plugins/error-handler.ts that returns the canonical ApiError shape ({ error, message, statusCode }) matching what the global error handler already produces for thrown errors. Every 4xx/5xx return now goes through this helper. Callers updated: routes/optimizer-metrics.ts, provider-rewards.ts, provider-estimated-rewards.ts, health.ts. Test assertions updated to check body.message instead of body.error for the detail text. --- .../routes-optimizer-metrics.test.ts | 6 ++--- .../routes-provider-estimated-rewards.test.ts | 2 +- .../__tests__/routes-provider-rewards.test.ts | 8 +++--- apps/api/src/plugins/error-handler.ts | 12 +++++++++ apps/api/src/routes/health.ts | 6 ++--- apps/api/src/routes/optimizer-metrics.ts | 25 +++++++------------ .../src/routes/provider-estimated-rewards.ts | 3 ++- apps/api/src/routes/provider-rewards.ts | 13 ++++------ 8 files changed, 38 insertions(+), 37 deletions(-) diff --git a/apps/api/src/__tests__/routes-optimizer-metrics.test.ts b/apps/api/src/__tests__/routes-optimizer-metrics.test.ts index 8bcaa94..c4f67d7 100644 --- a/apps/api/src/__tests__/routes-optimizer-metrics.test.ts +++ b/apps/api/src/__tests__/routes-optimizer-metrics.test.ts @@ -26,7 +26,7 @@ describe("GET /providers/:addr/optimizer-metrics", () => { url: "/providers/lava@abc/optimizer-metrics", }); expect(res.statusCode).toBe(503); - expect(JSON.parse(res.body).error).toContain("not configured"); + expect(JSON.parse(res.body).message).toContain("not configured"); }); it("rejects invalid `from` date with 400", async () => { @@ -36,7 +36,7 @@ describe("GET /providers/:addr/optimizer-metrics", () => { url: "/providers/lava@abc/optimizer-metrics?from=not-a-date", }); expect(res.statusCode).toBe(400); - expect(JSON.parse(res.body).error).toContain("invalid from"); + expect(JSON.parse(res.body).message).toContain("invalid from"); }); it("rejects invalid `to` date with 400", async () => { @@ -46,7 +46,7 @@ describe("GET /providers/:addr/optimizer-metrics", () => { url: "/providers/lava@abc/optimizer-metrics?to=2025-02-30", }); expect(res.statusCode).toBe(400); - expect(JSON.parse(res.body).error).toContain("invalid to"); + expect(JSON.parse(res.body).message).toContain("invalid to"); }); }); diff --git a/apps/api/src/__tests__/routes-provider-estimated-rewards.test.ts b/apps/api/src/__tests__/routes-provider-estimated-rewards.test.ts index f51a4b6..8e0c713 100644 --- a/apps/api/src/__tests__/routes-provider-estimated-rewards.test.ts +++ b/apps/api/src/__tests__/routes-provider-estimated-rewards.test.ts @@ -199,7 +199,7 @@ describe("GET /provider-estimated-rewards", () => { url: "/provider-estimated-rewards?spec=!", }); expect(res.statusCode).toBe(400); - expect(JSON.parse(res.body).error).toMatch(/bad spec format/); + expect(JSON.parse(res.body).message).toMatch(/bad spec format/); }); it("excludes providers whose filtered rewards become empty", async () => { diff --git a/apps/api/src/__tests__/routes-provider-rewards.test.ts b/apps/api/src/__tests__/routes-provider-rewards.test.ts index 8083d88..4c03e0e 100644 --- a/apps/api/src/__tests__/routes-provider-rewards.test.ts +++ b/apps/api/src/__tests__/routes-provider-rewards.test.ts @@ -209,7 +209,7 @@ describe("GET /provider-rewards (default groupBy=provider)", () => { url: "/provider-rewards?from=2025-02-30&to=2025-04-01", }); expect(res.statusCode).toBe(400); - expect(JSON.parse(res.body).error).toMatch(/bad from date/); + expect(JSON.parse(res.body).message).toMatch(/bad from date/); }); it("rejects calendar-invalid dates (Apr 31)", async () => { @@ -219,7 +219,7 @@ describe("GET /provider-rewards (default groupBy=provider)", () => { url: "/provider-rewards?from=2025-01-01&to=2025-04-31", }); expect(res.statusCode).toBe(400); - expect(JSON.parse(res.body).error).toMatch(/bad to date/); + expect(JSON.parse(res.body).message).toMatch(/bad to date/); }); it("rejects date range exceeding 6 months", async () => { @@ -229,7 +229,7 @@ describe("GET /provider-rewards (default groupBy=provider)", () => { url: "/provider-rewards?from=2024-01-01&to=2025-01-01", }); expect(res.statusCode).toBe(400); - expect(JSON.parse(res.body).error).toMatch(/6 months/); + expect(JSON.parse(res.body).message).toMatch(/6 months/); }); it("swaps dates when to < from", async () => { @@ -251,7 +251,7 @@ describe("GET /provider-rewards (default groupBy=provider)", () => { url: "/provider-rewards?from=2025-01-01&to=2025-04-01&specs=a", }); expect(res.statusCode).toBe(400); - expect(JSON.parse(res.body).error).toMatch(/bad spec format/); + expect(JSON.parse(res.body).message).toMatch(/bad spec format/); }); it("returns zero adjusted rewards when qosCu is zero", async () => { diff --git a/apps/api/src/plugins/error-handler.ts b/apps/api/src/plugins/error-handler.ts index 9db3043..e19012f 100644 --- a/apps/api/src/plugins/error-handler.ts +++ b/apps/api/src/plugins/error-handler.ts @@ -1,5 +1,17 @@ import fp from "fastify-plugin"; import type { FastifyInstance, FastifyError, FastifyRequest, FastifyReply } from "fastify"; +import type { ApiError } from "@info/shared/types"; + +/** Emit the standard ApiError shape from a route handler for 4xx/5xx responses. */ +export function sendApiError(reply: FastifyReply, statusCode: number, message: string): ApiError { + reply.status(statusCode); + const error = statusCode === 400 ? "Bad Request" + : statusCode === 404 ? "Not Found" + : statusCode === 503 ? "Service Unavailable" + : "Error"; + const body: ApiError = { error, message, statusCode }; + return body; +} export const errorHandlerPlugin = fp(async (app: FastifyInstance) => { app.setErrorHandler((error: FastifyError, request: FastifyRequest, reply: FastifyReply) => { diff --git a/apps/api/src/routes/health.ts b/apps/api/src/routes/health.ts index 6ec14c6..6440bde 100644 --- a/apps/api/src/routes/health.ts +++ b/apps/api/src/routes/health.ts @@ -1,5 +1,6 @@ import type { FastifyInstance } from "fastify"; import { fetchLatestBlockHeight } from "../rpc/lava.js"; +import { sendApiError } from "../plugins/error-handler.js"; export async function healthRoutes(app: FastifyInstance) { app.get("/health", { @@ -15,10 +16,7 @@ export async function healthRoutes(app: FastifyInstance) { if (process.env.NODE_ENV !== "production") { app.delete("/cache", async (_request, reply) => { const client = app.redis; - if (!client) { - reply.status(404); - return { error: "Redis not connected" }; - } + if (!client) return sendApiError(reply, 404, "Redis not connected"); await client.flushdb(); return { cleared: true }; diff --git a/apps/api/src/routes/optimizer-metrics.ts b/apps/api/src/routes/optimizer-metrics.ts index 3ad6158..e24167e 100644 --- a/apps/api/src/routes/optimizer-metrics.ts +++ b/apps/api/src/routes/optimizer-metrics.ts @@ -1,5 +1,6 @@ import type { FastifyInstance } from "fastify"; import { parseYMD } from "@info/shared/utils"; +import { sendApiError } from "../plugins/error-handler.js"; interface MetricRow { hourly_timestamp: Date; @@ -155,18 +156,15 @@ export async function optimizerMetricsRoutes(app: FastifyInstance) { }, config: { cacheTTL: 21600 }, }, async (request, reply) => { - if (!app.relaysDb) { - reply.status(503); - return { error: "Optimizer metrics not configured" }; - } + if (!app.relaysDb) return sendApiError(reply, 503, "Optimizer metrics not configured"); try { const { addr } = request.params as { addr: string }; const query = request.query as { from?: string; to?: string; consumer?: string; chain_id?: string }; const fromRaw = query.from ? parseYMD(query.from) : defaultFrom(); - if (!fromRaw) return reply.status(400).send({ error: "invalid from (expected YYYY-MM-DD)" }); + if (!fromRaw) return sendApiError(reply, 400, "invalid from (expected YYYY-MM-DD)"); const to = query.to ? parseYMD(query.to) : new Date(); - if (!to) return reply.status(400).send({ error: "invalid to (expected YYYY-MM-DD)" }); + if (!to) return sendApiError(reply, 400, "invalid to (expected YYYY-MM-DD)"); const from = clampFrom(fromRaw); const conditions = [ @@ -198,8 +196,7 @@ export async function optimizerMetricsRoutes(app: FastifyInstance) { return { metrics, possibleConsumers, possibleChainIds, filters: { provider: addr, from, to } }; } catch (err) { request.log.error({ err }, "Failed to query optimizer metrics for provider"); - reply.status(503); - return { error: "Optimizer metrics temporarily unavailable" }; + return sendApiError(reply, 503, "Optimizer metrics temporarily unavailable"); } }); @@ -224,18 +221,15 @@ export async function optimizerMetricsRoutes(app: FastifyInstance) { }, config: { cacheTTL: 21600 }, }, async (request, reply) => { - if (!app.relaysDb) { - reply.status(503); - return { error: "Optimizer metrics not configured" }; - } + if (!app.relaysDb) return sendApiError(reply, 503, "Optimizer metrics not configured"); try { const { specId } = request.params as { specId: string }; const query = request.query as { from?: string; to?: string; consumer?: string }; const fromRaw = query.from ? parseYMD(query.from) : defaultFrom(); - if (!fromRaw) return reply.status(400).send({ error: "invalid from (expected YYYY-MM-DD)" }); + if (!fromRaw) return sendApiError(reply, 400, "invalid from (expected YYYY-MM-DD)"); const to = query.to ? parseYMD(query.to) : new Date(); - if (!to) return reply.status(400).send({ error: "invalid to (expected YYYY-MM-DD)" }); + if (!to) return sendApiError(reply, 400, "invalid to (expected YYYY-MM-DD)"); const from = clampFrom(fromRaw); const conditions = [ @@ -264,8 +258,7 @@ export async function optimizerMetricsRoutes(app: FastifyInstance) { return { metrics, possibleConsumers, providers, filters: { specId, from, to } }; } catch (err) { request.log.error({ err }, "Failed to query optimizer metrics for spec"); - reply.status(503); - return { error: "Optimizer metrics temporarily unavailable" }; + return sendApiError(reply, 503, "Optimizer metrics temporarily unavailable"); } }); } diff --git a/apps/api/src/routes/provider-estimated-rewards.ts b/apps/api/src/routes/provider-estimated-rewards.ts index f23e8f8..8262f6e 100644 --- a/apps/api/src/routes/provider-estimated-rewards.ts +++ b/apps/api/src/routes/provider-estimated-rewards.ts @@ -8,6 +8,7 @@ import { prewarmPriceCache, type RewardsBySpecEntry, } from "../rpc/lava.js"; +import { sendApiError } from "../plugins/error-handler.js"; // ── Helpers ────────────────────────────────────────────────────────────────── @@ -120,7 +121,7 @@ export async function providerEstimatedRewardsRoutes(app: FastifyInstance) { let spec: string | undefined; if (q.spec !== undefined) { if (!validateSpecId(q.spec)) { - return reply.status(400).send({ error: `Error - bad spec format: ${q.spec}` }); + return sendApiError(reply, 400, `bad spec format: ${q.spec}`); } spec = q.spec.toUpperCase(); } diff --git a/apps/api/src/routes/provider-rewards.ts b/apps/api/src/routes/provider-rewards.ts index 5b1133e..4535384 100644 --- a/apps/api/src/routes/provider-rewards.ts +++ b/apps/api/src/routes/provider-rewards.ts @@ -2,6 +2,7 @@ import type { FastifyInstance } from "fastify"; import { parseYMD } from "@info/shared/utils"; import { gqlSafe } from "../graphql/client.js"; import { fetchProvidersForSpec, fetchAllProviderMonikers } from "../rpc/lava.js"; +import { sendApiError } from "../plugins/error-handler.js"; // ── Helpers ────────────────────────────────────────────────────────────────── @@ -131,17 +132,15 @@ export async function providerRewardsRoutes(app: FastifyInstance) { // ── Validate spec IDs ────────────────────────────────────────── if (q.specs) { for (const s of q.specs) { - if (!validateSpecId(s)) { - return reply.status(400).send({ error: `Error - bad spec format: ${s}` }); - } + if (!validateSpecId(s)) return sendApiError(reply, 400, `bad spec format: ${s}`); } } // ── Validate & normalize dates ───────────────────────────────── let dateFrom = parseYMD(q.from); - if (!dateFrom) return reply.status(400).send({ error: "Error - bad from date format" }); + if (!dateFrom) return sendApiError(reply, 400, "bad from date format (expected YYYY-MM-DD)"); let dateTo = parseYMD(q.to); - if (!dateTo) return reply.status(400).send({ error: "Error - bad to date format" }); + if (!dateTo) return sendApiError(reply, 400, "bad to date format (expected YYYY-MM-DD)"); if (dateTo < dateFrom) { [dateFrom, dateTo] = [dateTo, dateFrom]; @@ -151,9 +150,7 @@ export async function providerRewardsRoutes(app: FastifyInstance) { const to = dateTo.toISOString().slice(0, 10); if (dateTo.getTime() - dateFrom.getTime() > MAX_RANGE_MS) { - return reply.status(400).send({ - error: "Error - date range should not exceed 6 months", - }); + return sendApiError(reply, 400, "date range should not exceed 6 months"); } // Past windows are immutable (MV refreshes catch up to yesterday within a From 57a48016df0b96e7fd8514b44d9a5939d5a08659 Mon Sep 17 00:00:00 2001 From: Nimrod Teich Date: Fri, 17 Apr 2026 00:19:51 +0300 Subject: [PATCH 18/25] refactor: move CHAIN_DISPLAY_NAMES to @info/shared MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The display-name override map (ETH1 → "Ethereum Mainnet" etc.) lived in apps/api/src/rpc/specs.ts alongside fetchAllSpecs. Both the API and the frontend routinely need to render a chain's human name, so promote the map and a new chainDisplayName helper to @info/shared/constants where both workspaces can import it. Keep rpc/specs.ts focused on the actual RPC call. --- apps/api/src/rpc/specs.ts | 37 +-------------------- packages/shared/src/constants/chains.ts | 43 +++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 36 deletions(-) diff --git a/apps/api/src/rpc/specs.ts b/apps/api/src/rpc/specs.ts index dc5fc9c..7eac17d 100644 --- a/apps/api/src/rpc/specs.ts +++ b/apps/api/src/rpc/specs.ts @@ -1,41 +1,6 @@ -import { BASE_SPECS } from "@info/shared/constants"; +import { BASE_SPECS, chainDisplayName } from "@info/shared/constants"; import { fetchRest } from "./rest.js"; -function titleCase(s: string): string { - return s.replace(/\b\w/g, (c) => c.toUpperCase()); -} - -/** Display name overrides — keys are specIDs (uppercase) */ -const CHAIN_DISPLAY_NAMES: Record = { - BSC: "BNB Chain Mainnet", - BSCT: "BNB Chain Testnet", - COSMOSHUB: "Cosmos Hub Mainnet", - COSMOSHUBT: "Cosmos Hub Testnet", - ETH1: "Ethereum Mainnet", - FTM4002: "Fantom Testnet", - FVMT: "Filecoin Testnet", - HEDERA: "Hedera Hashgraph Mainnet", - BTC: "Bitcoin Mainnet", - HOL1: "Ethereum Holesky Testnet", - LAVA: "Lava Mainnet", - LAV1: "Lava Testnet", - MOVEMENTT: "Movement Testnet", - OPTMS: "Optimism Sepolia Testnet", - POLYGONA: "Polygon Amoy Testnet", - SEP1: "Ethereum Sepolia Testnet", - SOLANAT: "Solana Testnet", - SONICT: "Sonic Blaze Testnet", - SPARK: "Fuse Testnet", - STRKS: "Starknet Sepolia Testnet", - TRX: "Tron Mainnet", - TRXT: "Tron Shasta Testnet", -}; - -function chainDisplayName(chainID: string, chainName: string): string { - if (CHAIN_DISPLAY_NAMES[chainID]) return CHAIN_DISPLAY_NAMES[chainID]; - return titleCase(chainName); -} - export async function fetchAllSpecs(): Promise> { const data = await fetchRest<{ chainInfoList: Array<{ chainName: string; chainID: string }>; diff --git a/packages/shared/src/constants/chains.ts b/packages/shared/src/constants/chains.ts index 530fc91..686337d 100644 --- a/packages/shared/src/constants/chains.ts +++ b/packages/shared/src/constants/chains.ts @@ -5,6 +5,49 @@ export const BASE_SPECS = new Set([ "ETHERMINT", "TENDERMINT", "IBC", ]); +/** + * Display-name overrides keyed by spec ID (uppercase). + * Used by `chainDisplayName` when the raw chainName from RPC isn't + * user-friendly (e.g. chainName="bsc-mainnet" → "BNB Chain Mainnet"). + */ +export const CHAIN_DISPLAY_NAMES: Record = { + BSC: "BNB Chain Mainnet", + BSCT: "BNB Chain Testnet", + COSMOSHUB: "Cosmos Hub Mainnet", + COSMOSHUBT: "Cosmos Hub Testnet", + ETH1: "Ethereum Mainnet", + FTM4002: "Fantom Testnet", + FVMT: "Filecoin Testnet", + HEDERA: "Hedera Hashgraph Mainnet", + BTC: "Bitcoin Mainnet", + HOL1: "Ethereum Holesky Testnet", + LAVA: "Lava Mainnet", + LAV1: "Lava Testnet", + MOVEMENTT: "Movement Testnet", + OPTMS: "Optimism Sepolia Testnet", + POLYGONA: "Polygon Amoy Testnet", + SEP1: "Ethereum Sepolia Testnet", + SOLANAT: "Solana Testnet", + SONICT: "Sonic Blaze Testnet", + SPARK: "Fuse Testnet", + STRKS: "Starknet Sepolia Testnet", + TRX: "Tron Mainnet", + TRXT: "Tron Shasta Testnet", +}; + +function titleCase(s: string): string { + return s.replace(/\b\w/g, (c) => c.toUpperCase()); +} + +/** + * Canonical display name for a chain: prefers the override map, falls + * back to title-casing the raw chainName from chain RPC. + */ +export function chainDisplayName(chainID: string, chainName: string): string { + if (CHAIN_DISPLAY_NAMES[chainID]) return CHAIN_DISPLAY_NAMES[chainID]; + return titleCase(chainName); +} + /** Chains that use non-standard block numbering (timestamp-based or record-file-based) */ export const NON_STANDARD_BLOCK_CHAINS = [ "HYPERLIQUID", From 0c2ad6bae216b5c8474ca4bf8da069e9d0fa999d Mon Sep 17 00:00:00 2001 From: Nimrod Teich Date: Fri, 17 Apr 2026 00:20:34 +0300 Subject: [PATCH 19/25] docs(api): explain mixed snake/camel casing on AllProviderAprEntry MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The /all_providers_apr response mixes snake_case (e.g. rewards_last_month) with camelCase (moniker, delegateTotal). It looks inconsistent but the snake_case keys are load-bearing — they mirror jsinfo's exact output shape which burn-ui and the lava-ops pipelines parse by name. Document that so the next reader doesn't 'fix' it and break downstream consumers. --- apps/api/src/rpc/apr.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/apps/api/src/rpc/apr.ts b/apps/api/src/rpc/apr.ts index 154deed..cdcfa5f 100644 --- a/apps/api/src/rpc/apr.ts +++ b/apps/api/src/rpc/apr.ts @@ -205,6 +205,12 @@ function formatCommission(commission: string): string { return `${n.toFixed(1)}%`; } +// Field naming is intentionally mixed on this endpoint: the four snake_case +// keys below (`30_days_cu_served`, `30_days_relays_served`, +// `rewards_10k_lava_delegation`, `rewards_last_month`) match jsinfo's +// `/all_providers_apr` shape 1:1. External consumers (burn-ui / lava-ops) +// depend on that exact contract — renaming them to camelCase would be a +// breaking change. The remaining fields were camelCase in jsinfo too. export interface AllProviderAprEntry { address: string; moniker: string; From 308df055899f1c8ec1b6ea1cec7524c0311fbffc Mon Sep 17 00:00:00 2001 From: Nimrod Teich Date: Fri, 17 Apr 2026 00:34:08 +0300 Subject: [PATCH 20/25] refactor(api): consolidate env var defaults in config.ts process.env was read ad-hoc at 22 call sites across rpc/, plugins/, services/, bin/, routes/ and main.ts. Each one re-declared its own default string. Centralize them in apps/api/src/config.ts so: - the full env contract is visible in one place (mirrors CLAUDE.md's table) - defaults are parsed once at startup (not on every request) - helpers envInt/envBool give consistent NaN/parse handling Every consumer now imports `config` and references typed slices: config.server.{port,host,corsOrigins,rateLimitMax} config.lava.{restUrl,rpcUrl,rpcBatchSize} config.indexer.{graphqlUrl,timeoutMs} config.external.{coingeckoApiUrl,keybaseApiUrl} config.redis.url, config.relaysDb.url config.healthProbe.{enabled,region,intervalMs} config.precompute.intervalMs config.isDev / config.isProd No behavior change; tests green. --- apps/api/src/bin/health-probe.ts | 6 +- apps/api/src/bin/precompute.ts | 8 +-- apps/api/src/config.ts | 76 ++++++++++++++++++++++ apps/api/src/graphql/client.ts | 7 +- apps/api/src/main.ts | 23 +++---- apps/api/src/plugins/health-probe.ts | 3 +- apps/api/src/plugins/redis.ts | 3 +- apps/api/src/plugins/relays-db.ts | 3 +- apps/api/src/routes/health.ts | 3 +- apps/api/src/rpc/block.ts | 9 ++- apps/api/src/rpc/keybase.ts | 4 +- apps/api/src/rpc/pricing.ts | 9 ++- apps/api/src/rpc/rest.ts | 15 +++-- apps/api/src/services/health-probe-loop.ts | 3 +- 14 files changed, 123 insertions(+), 49 deletions(-) create mode 100644 apps/api/src/config.ts diff --git a/apps/api/src/bin/health-probe.ts b/apps/api/src/bin/health-probe.ts index c56a911..f687679 100644 --- a/apps/api/src/bin/health-probe.ts +++ b/apps/api/src/bin/health-probe.ts @@ -15,18 +15,18 @@ */ import { Redis } from "ioredis"; import pino from "pino"; +import { config } from "../config.js"; import { runProbeLoop } from "../services/health-probe-loop.js"; const log = pino({ name: "health-probe-bin" }); async function main() { - const redisUrl = process.env.REDIS_URL; - if (!redisUrl) { + if (!config.redis.url) { log.error("REDIS_URL is required"); process.exit(1); } - const redis = new Redis(redisUrl); + const redis = new Redis(config.redis.url); let running = true; const shutdown = (signal: string) => { diff --git a/apps/api/src/bin/precompute.ts b/apps/api/src/bin/precompute.ts index 77b862c..07b13a0 100644 --- a/apps/api/src/bin/precompute.ts +++ b/apps/api/src/bin/precompute.ts @@ -23,6 +23,7 @@ import { Redis } from "ioredis"; import pino from "pino"; import { ulavaToLava } from "@info/shared/utils"; +import { config } from "../config.js"; import { gqlSafe } from "../graphql/client.js"; import { fetchCirculatingSupply } from "../rpc/supply.js"; import { computeAPR, computeAllProvidersApr } from "../rpc/apr.js"; @@ -94,14 +95,13 @@ function sleep(ms: number): Promise { } async function main() { - const redisUrl = process.env.REDIS_URL; - if (!redisUrl) { + if (!config.redis.url) { log.error("REDIS_URL is required"); process.exit(1); } - const intervalMs = parseInt(process.env.PRECOMPUTE_INTERVAL_MS ?? "900000", 10); - const redis = new Redis(redisUrl); + const intervalMs = config.precompute.intervalMs; + const redis = new Redis(config.redis.url); let running = true; const shutdown = (signal: string) => { diff --git a/apps/api/src/config.ts b/apps/api/src/config.ts new file mode 100644 index 0000000..ed8b8d1 --- /dev/null +++ b/apps/api/src/config.ts @@ -0,0 +1,76 @@ +/** + * Single source of truth for env-var defaults. + * + * Import `config` (default) for plain values or pull specific slices via + * destructure. Every default is also documented in CLAUDE.md's env-var table. + * + * Parse once at startup — changes to process.env mid-process are NOT reflected. + */ + +function env(name: string): string | undefined { + return process.env[name]; +} + +function envInt(name: string, fallback: number): number { + const raw = process.env[name]; + const n = raw !== undefined ? parseInt(raw, 10) : NaN; + return Number.isFinite(n) ? n : fallback; +} + +function envBool(name: string): boolean { + return process.env[name] === "true"; +} + +export const config = { + env: env("NODE_ENV") ?? "production", + isDev: env("NODE_ENV") === "development", + isProd: env("NODE_ENV") === "production", + + server: { + port: envInt("API_PORT", 8080), + host: env("API_HOST") ?? "0.0.0.0", + /** true = reflect any origin; array = explicit allowlist. */ + corsOrigins: env("CORS_ORIGINS") + ? env("CORS_ORIGINS")!.split(",").map((o) => o.trim()) + : (true as true), + rateLimitMax: envInt("RATE_LIMIT_MAX", 100), + }, + + lava: { + restUrl: env("LAVA_REST_URL") ?? "https://lava.rest.lava.build", + rpcUrl: env("LAVA_RPC_URL") ?? "https://lava.tendermintrpc.lava.build:443", + /** Max concurrent chain-RPC calls per batch loop. Public endpoints should stay modest (~5). */ + rpcBatchSize: Math.max(1, envInt("RPC_BATCH_SIZE", 25)), + }, + + indexer: { + graphqlUrl: env("INDEXER_GRAPHQL_URL") ?? "http://localhost:3000", + timeoutMs: envInt("INDEXER_TIMEOUT_MS", 15_000), + }, + + external: { + coingeckoApiUrl: env("COINGECKO_API_URL") ?? "https://api.coingecko.com/api/v3", + keybaseApiUrl: env("KEYBASE_API_URL") ?? "https://keybase.io/_/api/1.0", + }, + + redis: { + url: env("REDIS_URL"), + }, + + relaysDb: { + url: env("RELAYS_DB_URL"), + }, + + healthProbe: { + enabled: envBool("ENABLE_HEALTH_PROBE"), + region: env("HEALTH_PROBE_REGION") ?? "Local", + intervalMs: envInt("HEALTH_PROBE_INTERVAL_MS", 30_000), + }, + + precompute: { + /** How often the standalone precompute worker recomputes heavy endpoints. */ + intervalMs: envInt("PRECOMPUTE_INTERVAL_MS", 900_000), // 15 min + }, +} as const; + +export default config; diff --git a/apps/api/src/graphql/client.ts b/apps/api/src/graphql/client.ts index 84dbbe9..e94fd84 100644 --- a/apps/api/src/graphql/client.ts +++ b/apps/api/src/graphql/client.ts @@ -1,9 +1,8 @@ import pino from "pino"; import { Agent, setGlobalDispatcher } from "undici"; +import { config } from "../config.js"; const logger = pino({ name: "graphql" }); -const INDEXER_GRAPHQL_URL = process.env.INDEXER_GRAPHQL_URL ?? "http://localhost:3000"; -const GQL_TIMEOUT_MS = parseInt(process.env.INDEXER_TIMEOUT_MS ?? "15000", 10); // Shared connection pool — reuses TCP/TLS across outbound requests so each // call saves the ~50-100ms handshake cost. Applies to all fetch() in the @@ -17,11 +16,11 @@ setGlobalDispatcher( ); export async function gql(query: string, variables?: Record): Promise { - const res = await fetch(INDEXER_GRAPHQL_URL, { + const res = await fetch(config.indexer.graphqlUrl, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ query, variables }), - signal: AbortSignal.timeout(GQL_TIMEOUT_MS), + signal: AbortSignal.timeout(config.indexer.timeoutMs), }); if (!res.ok) throw new Error(`GraphQL request failed: ${res.status}`); diff --git a/apps/api/src/main.ts b/apps/api/src/main.ts index 33e7076..ddae55f 100644 --- a/apps/api/src/main.ts +++ b/apps/api/src/main.ts @@ -3,6 +3,7 @@ import cors from "@fastify/cors"; import helmet from "@fastify/helmet"; import rateLimit from "@fastify/rate-limit"; import compress from "@fastify/compress"; +import { config } from "./config.js"; import { redisPlugin } from "./plugins/redis.js"; import { cachePlugin } from "./plugins/cache.js"; import { errorHandlerPlugin } from "./plugins/error-handler.js"; @@ -26,27 +27,21 @@ import { providerClaimableRewardsRoutes } from "./routes/provider-claimable-rewa import { validatorsAndRewardsRoutes } from "./routes/validators-and-rewards.js"; import { burnRateRoutes } from "./routes/burn-rate.js"; -const PORT = parseInt(process.env.API_PORT ?? "8080", 10); -const HOST = process.env.API_HOST ?? "0.0.0.0"; -const CORS_ORIGINS = process.env.CORS_ORIGINS - ? process.env.CORS_ORIGINS.split(",").map((o) => o.trim()) - : true; // default: allow all in dev; set CORS_ORIGINS in production - async function main() { const app = Fastify({ logger: { - transport: - process.env.NODE_ENV === "development" - ? { target: "pino-pretty" } - : undefined, + transport: config.isDev ? { target: "pino-pretty" } : undefined, }, }); - await app.register(cors, { origin: CORS_ORIGINS, methods: ["GET", "HEAD", "OPTIONS", "DELETE"] }); + await app.register(cors, { + origin: config.server.corsOrigins, + methods: ["GET", "HEAD", "OPTIONS", "DELETE"], + }); await app.register(helmet, { contentSecurityPolicy: false }); // CSP handled by Next.js frontend await app.register(compress, { global: true, threshold: 1024, encodings: ["br", "gzip"] }); await app.register(rateLimit, { - max: parseInt(process.env.RATE_LIMIT_MAX ?? "100", 10), + max: config.server.rateLimitMax, timeWindow: "1 minute", allowList: ["127.0.0.1", "::1"], }); @@ -75,8 +70,8 @@ async function main() { await app.register(healthProbePlugin); - await app.listen({ port: PORT, host: HOST }); - app.log.info(`API server listening on ${HOST}:${PORT}`); + await app.listen({ port: config.server.port, host: config.server.host }); + app.log.info(`API server listening on ${config.server.host}:${config.server.port}`); for (const signal of ["SIGTERM", "SIGINT"] as const) { process.on(signal, () => { diff --git a/apps/api/src/plugins/health-probe.ts b/apps/api/src/plugins/health-probe.ts index cf1ff8e..f883f7d 100644 --- a/apps/api/src/plugins/health-probe.ts +++ b/apps/api/src/plugins/health-probe.ts @@ -1,5 +1,6 @@ import fp from "fastify-plugin"; import type { FastifyInstance } from "fastify"; +import { config } from "../config.js"; import { runProbeLoop } from "../services/health-probe-loop.js"; /** @@ -11,7 +12,7 @@ import { runProbeLoop } from "../services/health-probe-loop.js"; * convenience during local dev: set `ENABLE_HEALTH_PROBE=true` to enable. */ export const healthProbePlugin = fp(async (app: FastifyInstance) => { - if (process.env.ENABLE_HEALTH_PROBE !== "true") return; + if (!config.healthProbe.enabled) return; if (!app.redis) { app.log.warn("Health probe requires Redis — REDIS_URL not set, skipping"); return; diff --git a/apps/api/src/plugins/redis.ts b/apps/api/src/plugins/redis.ts index fae196a..32aca09 100644 --- a/apps/api/src/plugins/redis.ts +++ b/apps/api/src/plugins/redis.ts @@ -1,6 +1,7 @@ import fp from "fastify-plugin"; import type { FastifyInstance } from "fastify"; import { Redis } from "ioredis"; +import { config } from "../config.js"; declare module "fastify" { interface FastifyInstance { @@ -9,7 +10,7 @@ declare module "fastify" { } export const redisPlugin = fp(async (app: FastifyInstance) => { - const url = process.env.REDIS_URL; + const url = config.redis.url; if (!url) { app.decorate("redis", null); return; diff --git a/apps/api/src/plugins/relays-db.ts b/apps/api/src/plugins/relays-db.ts index 6aacd99..fe45bf1 100644 --- a/apps/api/src/plugins/relays-db.ts +++ b/apps/api/src/plugins/relays-db.ts @@ -1,6 +1,7 @@ import fp from "fastify-plugin"; import type { FastifyInstance } from "fastify"; import postgres from "postgres"; +import { config } from "../config.js"; declare module "fastify" { interface FastifyInstance { @@ -9,7 +10,7 @@ declare module "fastify" { } export const relaysDbPlugin = fp(async (app: FastifyInstance) => { - const url = process.env.RELAYS_DB_URL; + const url = config.relaysDb.url; if (!url) { app.decorate("relaysDb", null); app.log.warn("RELAYS_DB_URL not set — optimizer metrics disabled"); diff --git a/apps/api/src/routes/health.ts b/apps/api/src/routes/health.ts index 6440bde..6087318 100644 --- a/apps/api/src/routes/health.ts +++ b/apps/api/src/routes/health.ts @@ -1,4 +1,5 @@ import type { FastifyInstance } from "fastify"; +import { config } from "../config.js"; import { fetchLatestBlockHeight } from "../rpc/lava.js"; import { sendApiError } from "../plugins/error-handler.js"; @@ -13,7 +14,7 @@ export async function healthRoutes(app: FastifyInstance) { return { health: "ok" }; }); - if (process.env.NODE_ENV !== "production") { + if (!config.isProd) { app.delete("/cache", async (_request, reply) => { const client = app.redis; if (!client) return sendApiError(reply, 404, "Redis not connected"); diff --git a/apps/api/src/rpc/block.ts b/apps/api/src/rpc/block.ts index 0d4f67c..ea7c2d3 100644 --- a/apps/api/src/rpc/block.ts +++ b/apps/api/src/rpc/block.ts @@ -1,3 +1,5 @@ +import { config } from "../config.js"; + // Lava mainnet genesis timestamp (block 1) — used to narrow binary search range. const LAVA_GENESIS_UNIX = 1_713_350_000; // ~2024-04-17 @@ -9,8 +11,7 @@ export async function fetchLatestBlockHeight(): Promise<{ height: number; time: string; }> { - const LAVA_RPC_URL = process.env.LAVA_RPC_URL ?? "https://lava.tendermintrpc.lava.build:443"; - const res = await fetch(`${LAVA_RPC_URL}/status`, { + const res = await fetch(`${config.lava.rpcUrl}/status`, { signal: AbortSignal.timeout(10_000), }); if (!res.ok) throw new Error(`RPC ${res.status}`); @@ -29,10 +30,8 @@ export async function fetchBlockAtTimestamp(targetUnix: number): Promise const cached = blockAtTsCache.get(targetUnix); if (cached !== undefined) return cached; - const LAVA_RPC_URL = process.env.LAVA_RPC_URL ?? "https://lava.tendermintrpc.lava.build:443"; - async function blockTime(height: number): Promise { - const res = await fetch(`${LAVA_RPC_URL}/block?height=${height}`, { + const res = await fetch(`${config.lava.rpcUrl}/block?height=${height}`, { signal: AbortSignal.timeout(10_000), }); if (!res.ok) throw new Error(`RPC ${res.status}`); diff --git a/apps/api/src/rpc/keybase.ts b/apps/api/src/rpc/keybase.ts index 9fa94a5..cb75bab 100644 --- a/apps/api/src/rpc/keybase.ts +++ b/apps/api/src/rpc/keybase.ts @@ -1,3 +1,4 @@ +import { config } from "../config.js"; import { fetchRest } from "./rest.js"; import { fetchAllSpecs } from "./specs.js"; @@ -35,9 +36,8 @@ export async function fetchProviderAvatar(provider: string, identityHint?: strin } if (!identity) return null; - const KEYBASE_API_URL = process.env.KEYBASE_API_URL ?? "https://keybase.io/_/api/1.0"; const res = await fetch( - `${KEYBASE_API_URL}/user/lookup.json?key_suffix=${encodeURIComponent(identity)}&fields=pictures`, + `${config.external.keybaseApiUrl}/user/lookup.json?key_suffix=${encodeURIComponent(identity)}&fields=pictures`, { signal: AbortSignal.timeout(10_000) }, ); if (!res.ok) return null; diff --git a/apps/api/src/rpc/pricing.ts b/apps/api/src/rpc/pricing.ts index f1d0e02..92c4ac2 100644 --- a/apps/api/src/rpc/pricing.ts +++ b/apps/api/src/rpc/pricing.ts @@ -1,12 +1,11 @@ import { ulavaToLavaNumber } from "@info/shared/utils"; +import { config } from "../config.js"; import { fetchRest } from "./rest.js"; import { fetchStakingPool } from "./supply.js"; -const COINGECKO_API_URL = process.env.COINGECKO_API_URL ?? "https://api.coingecko.com/api/v3"; - export async function fetchLavaUsdPrice(): Promise { const res = await fetch( - `${COINGECKO_API_URL}/simple/price?ids=lava-network&vs_currencies=usd`, + `${config.external.coingeckoApiUrl}/simple/price?ids=lava-network&vs_currencies=usd`, { signal: AbortSignal.timeout(15_000) }, ); if (!res.ok) throw new Error(`CoinGecko ${res.status}`); @@ -76,7 +75,7 @@ export async function prewarmPriceCache(): Promise { const ids = [...new Set(Object.values(DENOM_COINGECKO_ID))].join(","); try { const res = await fetch( - `https://api.coingecko.com/api/v3/simple/price?ids=${ids}&vs_currencies=usd`, + `${config.external.coingeckoApiUrl}/simple/price?ids=${ids}&vs_currencies=usd`, { signal: AbortSignal.timeout(15_000) }, ); if (!res.ok) return; @@ -102,7 +101,7 @@ export async function fetchTokenUsdPrice(baseDenom: string): Promise { try { const res = await fetch( - `https://api.coingecko.com/api/v3/simple/price?ids=${id}&vs_currencies=usd`, + `${config.external.coingeckoApiUrl}/simple/price?ids=${id}&vs_currencies=usd`, { signal: AbortSignal.timeout(15_000) }, ); if (!res.ok) return cached?.price ?? 0; diff --git a/apps/api/src/rpc/rest.ts b/apps/api/src/rpc/rest.ts index 0f44e77..47e275f 100644 --- a/apps/api/src/rpc/rest.ts +++ b/apps/api/src/rpc/rest.ts @@ -1,13 +1,14 @@ import pino from "pino"; +import { config } from "../config.js"; export const logger = pino({ name: "rpc" }); -export const LAVA_REST_URL = process.env.LAVA_REST_URL ?? "https://lava.rest.lava.build"; - -// Concurrent in-flight RPC calls per batch loop. Operators running dedicated -// RPC endpoints can raise this (env: RPC_BATCH_SIZE). Public endpoints should -// keep a conservative value (~5) to avoid rate limiting. -export const RPC_BATCH_SIZE = Math.max(1, parseInt(process.env.RPC_BATCH_SIZE ?? "25", 10)); +/** + * Concurrent in-flight RPC calls per batch loop. Operators running dedicated + * RPC endpoints can raise this (env: RPC_BATCH_SIZE). Public endpoints should + * keep a conservative value (~5) to avoid rate limiting. + */ +export const RPC_BATCH_SIZE = config.lava.rpcBatchSize; // Request coalescing: concurrent fetches for the same path share one in-flight request const inflightRpc = new Map>(); @@ -21,7 +22,7 @@ export async function fetchRest(path: string, blockHeight?: number): Promise< const headers: Record = {}; if (blockHeight) headers["x-cosmos-block-height"] = String(blockHeight); - const res = await fetch(`${LAVA_REST_URL}${path}`, { + const res = await fetch(`${config.lava.restUrl}${path}`, { headers, signal: AbortSignal.timeout(15_000), }); diff --git a/apps/api/src/services/health-probe-loop.ts b/apps/api/src/services/health-probe-loop.ts index f491e5e..1266699 100644 --- a/apps/api/src/services/health-probe-loop.ts +++ b/apps/api/src/services/health-probe-loop.ts @@ -1,6 +1,7 @@ import type { Redis } from "ioredis"; import pino from "pino"; import { normalizeBlock } from "@info/shared/constants"; +import { config } from "../config.js"; import { fetchAllSpecs, fetchProvidersForSpec, RPC_BATCH_SIZE } from "../rpc/lava.js"; import type { ProviderEndpoint } from "../rpc/lava.js"; import { probeProvider } from "./grpc-probe.js"; @@ -97,7 +98,7 @@ export interface ProbeLoopOptions { */ export async function runProbeLoop(opts: ProbeLoopOptions): Promise { const { redis, isRunning } = opts; - const intervalMs = opts.intervalMs ?? parseInt(process.env.HEALTH_PROBE_INTERVAL_MS ?? "30000", 10); + const intervalMs = opts.intervalMs ?? config.healthProbe.intervalMs; logger.info("Health probe started"); From 9eaff2698d7dc0c0177a92314c07582bdd218402 Mon Sep 17 00:00:00 2001 From: Nimrod Teich Date: Fri, 17 Apr 2026 00:34:51 +0300 Subject: [PATCH 21/25] security(api): validate Keybase identity format before outbound request MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit encodeURIComponent() was already applied so there's no live SSRF, but a future refactor that dropped the encoding would reopen one. Add an explicit regex gate: Keybase identity fingerprints are hex strings up to 64 chars — reject anything else. Belt-and-suspenders against the security review's concern that relying solely on downstream encoding is fragile. --- apps/api/src/rpc/keybase.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/apps/api/src/rpc/keybase.ts b/apps/api/src/rpc/keybase.ts index cb75bab..89f0f0f 100644 --- a/apps/api/src/rpc/keybase.ts +++ b/apps/api/src/rpc/keybase.ts @@ -27,6 +27,15 @@ async function fetchProviderMetadata(provider: string): Promise<{ } } +/** + * Keybase identity fingerprints are hex strings up to 32 chars. Anything + * that doesn't match is either malformed chain data or an attempt to slip + * other characters into the outbound URL. Reject early; encodeURIComponent + * still runs below as defense-in-depth so any future loosening here + * doesn't reopen an SSRF vector. + */ +const KEYBASE_IDENTITY_RE = /^[A-Fa-f0-9]{1,64}$/; + export async function fetchProviderAvatar(provider: string, identityHint?: string): Promise { try { let identity = identityHint; @@ -35,6 +44,7 @@ export async function fetchProviderAvatar(provider: string, identityHint?: strin identity = meta?.description?.identity ?? undefined; } if (!identity) return null; + if (!KEYBASE_IDENTITY_RE.test(identity)) return null; const res = await fetch( `${config.external.keybaseApiUrl}/user/lookup.json?key_suffix=${encodeURIComponent(identity)}&fields=pictures`, From 05bb577b64e84306a7ad8e031f1f8449f25d78ab Mon Sep 17 00:00:00 2001 From: Nimrod Teich Date: Fri, 17 Apr 2026 00:36:52 +0300 Subject: [PATCH 22/25] refactor(api): name cache TTLs instead of scattering magic numbers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 30 routes read cacheTTL from bare seconds — 10, 30, 300, 1800, 7200, 21600, 86_400 — making tiering changes a grep-and-pray exercise. Add CACHE_TTL to config.ts with semantic names: REAL_TIME (10s), HEALTH_PROBE (30s), LIST (300s), SEARCH (600s), APR (1800s), SLOW_MOVING (7200s), OPTIMIZER (6h), HISTORICAL (24h). Every route config + the request.cacheTTL overrides in provider-rewards and provider-estimated-rewards now reference the named values. --- apps/api/src/config.ts | 23 +++++++++++++++++++ apps/api/src/routes/all-providers-apr.ts | 3 ++- apps/api/src/routes/apr.ts | 3 ++- apps/api/src/routes/burn-rate.ts | 3 ++- apps/api/src/routes/health.ts | 4 ++-- apps/api/src/routes/index.ts | 7 +++--- apps/api/src/routes/lava.ts | 3 ++- apps/api/src/routes/optimizer-metrics.ts | 5 ++-- .../src/routes/provider-claimable-rewards.ts | 3 ++- .../src/routes/provider-estimated-rewards.ts | 7 +++--- apps/api/src/routes/provider-rewards.ts | 9 ++++---- apps/api/src/routes/providers.ts | 15 ++++++------ apps/api/src/routes/search.ts | 3 ++- apps/api/src/routes/specs.ts | 9 ++++---- apps/api/src/routes/supply.ts | 5 ++-- apps/api/src/routes/tvl.ts | 3 ++- apps/api/src/routes/validators-and-rewards.ts | 3 ++- 17 files changed, 72 insertions(+), 36 deletions(-) diff --git a/apps/api/src/config.ts b/apps/api/src/config.ts index ed8b8d1..f9a7f39 100644 --- a/apps/api/src/config.ts +++ b/apps/api/src/config.ts @@ -73,4 +73,27 @@ export const config = { }, } as const; +/** + * Per-route cache TTLs (seconds). Group by freshness tier so TTL tweaks are + * one edit — rather than hunting through 30 route files for magic numbers. + */ +export const CACHE_TTL = { + /** Block-height / realtime chain probes. */ + REAL_TIME: 10, + /** Per-provider health records pulled from Redis. */ + HEALTH_PROBE: 30, + /** Default list/aggregate cache window; aligns with MV refresh cadence. */ + LIST: 300, + /** Search results — slightly longer than lists since queries repeat. */ + SEARCH: 600, + /** APR computation (expensive, derived from per-entity RPC fan-out). */ + APR: 1800, + /** Slow-moving validator / claimable rewards data. */ + SLOW_MOVING: 7200, + /** Optimizer metrics — aggregated hourly scores. */ + OPTIMIZER: 21_600, + /** Historical/immutable data (past-block snapshots, avatars). */ + HISTORICAL: 86_400, +} as const; + export default config; diff --git a/apps/api/src/routes/all-providers-apr.ts b/apps/api/src/routes/all-providers-apr.ts index da7e0f1..6034070 100644 --- a/apps/api/src/routes/all-providers-apr.ts +++ b/apps/api/src/routes/all-providers-apr.ts @@ -1,4 +1,5 @@ import type { FastifyInstance } from "fastify"; +import { CACHE_TTL } from "../config.js"; import { computeAllProvidersApr, type AllProviderAprEntry } from "../rpc/lava.js"; import { gqlSafe } from "../graphql/client.js"; import { readPrecomputed } from "../services/precompute-store.js"; @@ -9,7 +10,7 @@ export async function allProvidersAprRoutes(app: FastifyInstance) { // compute when the precompute worker hasn't populated the key yet. app.get("/", { schema: { tags: ["APR"], summary: "Per-provider APR with commission, 30d relays, and reward breakdown" }, - config: { cacheTTL: 1800 }, + config: { cacheTTL: CACHE_TTL.APR }, }, async (request) => { const cached = await readPrecomputed(request.server.redis, "all_providers_apr"); if (cached) return cached.value; diff --git a/apps/api/src/routes/apr.ts b/apps/api/src/routes/apr.ts index fa758df..0cb2cc4 100644 --- a/apps/api/src/routes/apr.ts +++ b/apps/api/src/routes/apr.ts @@ -1,4 +1,5 @@ import type { FastifyInstance } from "fastify"; +import { CACHE_TTL } from "../config.js"; import { computeAPR } from "../rpc/lava.js"; import { readPrecomputed } from "../services/precompute-store.js"; @@ -13,7 +14,7 @@ export async function aprRoutes(app: FastifyInstance) { // compute when the precompute worker hasn't populated the key yet. app.get("/apr", { schema: { tags: ["APR"], summary: "Restaking and staking APR percentiles (80th, capped at 30%)" }, - config: { cacheTTL: 1800 }, + config: { cacheTTL: CACHE_TTL.APR }, }, async (request) => { const cached = await readPrecomputed(request.server.redis, "apr"); if (cached) return cached.value; diff --git a/apps/api/src/routes/burn-rate.ts b/apps/api/src/routes/burn-rate.ts index 4e9d552..36700ea 100644 --- a/apps/api/src/routes/burn-rate.ts +++ b/apps/api/src/routes/burn-rate.ts @@ -1,4 +1,5 @@ import type { FastifyInstance } from "fastify"; +import { CACHE_TTL } from "../config.js"; import { fetchBlockAtTimestamp, fetchLatestBlockHeight, @@ -57,7 +58,7 @@ export async function burnRateRoutes(app: FastifyInstance) { }, }, }, - config: { cacheTTL: 86_400 }, + config: { cacheTTL: CACHE_TTL.HISTORICAL }, }, async (request) => { const q = request.query as { months?: number }; const count = q.months ?? DEFAULT_MONTHS; diff --git a/apps/api/src/routes/health.ts b/apps/api/src/routes/health.ts index 6087318..465acc7 100644 --- a/apps/api/src/routes/health.ts +++ b/apps/api/src/routes/health.ts @@ -1,5 +1,5 @@ import type { FastifyInstance } from "fastify"; -import { config } from "../config.js"; +import { CACHE_TTL, config } from "../config.js"; import { fetchLatestBlockHeight } from "../rpc/lava.js"; import { sendApiError } from "../plugins/error-handler.js"; @@ -29,7 +29,7 @@ export async function healthRoutes(app: FastifyInstance) { tags: ["Health"], summary: "Detailed health status with block staleness check", }, - config: { cacheTTL: 10 }, + config: { cacheTTL: CACHE_TTL.REAL_TIME }, }, async (_request, reply) => { try { const block = await fetchLatestBlockHeight(); diff --git a/apps/api/src/routes/index.ts b/apps/api/src/routes/index.ts index 0cf8687..80f6b86 100644 --- a/apps/api/src/routes/index.ts +++ b/apps/api/src/routes/index.ts @@ -1,4 +1,5 @@ import type { FastifyInstance } from "fastify"; +import { CACHE_TTL } from "../config.js"; import { weightedQos } from "@info/shared/utils"; import { gqlSafe } from "../graphql/client.js"; import { fetchLatestBlockHeight, fetchAllProviders } from "../rpc/lava.js"; @@ -9,7 +10,7 @@ export async function indexRoutes(app: FastifyInstance) { tags: ["Index"], summary: "Dashboard stats — alltime totals, 30d totals, stake, provider count, latest block", }, - config: { cacheTTL: 300 }, + config: { cacheTTL: CACHE_TTL.LIST }, }, async () => { const thirtyDaysAgo = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000) .toISOString() @@ -58,7 +59,7 @@ export async function indexRoutes(app: FastifyInstance) { tags: ["Index"], summary: "Top 20 chains by alltime CU", }, - config: { cacheTTL: 300 }, + config: { cacheTTL: CACHE_TTL.LIST }, }, async () => { const data = await gqlSafe<{ mvRelayDailies: { @@ -94,7 +95,7 @@ export async function indexRoutes(app: FastifyInstance) { }, }, }, - config: { cacheTTL: 300 }, + config: { cacheTTL: CACHE_TTL.LIST }, }, async (request) => { const q = request.query as Record; const to = q.to ? q.to : new Date().toISOString().slice(0, 10); diff --git a/apps/api/src/routes/lava.ts b/apps/api/src/routes/lava.ts index a00f7ba..6683729 100644 --- a/apps/api/src/routes/lava.ts +++ b/apps/api/src/routes/lava.ts @@ -1,11 +1,12 @@ import type { FastifyInstance } from "fastify"; +import { CACHE_TTL } from "../config.js"; import { fetchAllSpecs } from "../rpc/lava.js"; export async function lavaRoutes(app: FastifyInstance) { // GET /lava/specs — all chain specs (consumed by frontend useChainNames hook) app.get("/specs", { schema: { tags: ["Lava"], summary: "All chain specs (raw, for frontend chain name lookup)" }, - config: { cacheTTL: 300 }, + config: { cacheTTL: CACHE_TTL.LIST }, }, async () => { const specs = await fetchAllSpecs(); return { data: specs }; diff --git a/apps/api/src/routes/optimizer-metrics.ts b/apps/api/src/routes/optimizer-metrics.ts index e24167e..1f70a37 100644 --- a/apps/api/src/routes/optimizer-metrics.ts +++ b/apps/api/src/routes/optimizer-metrics.ts @@ -1,4 +1,5 @@ import type { FastifyInstance } from "fastify"; +import { CACHE_TTL } from "../config.js"; import { parseYMD } from "@info/shared/utils"; import { sendApiError } from "../plugins/error-handler.js"; @@ -154,7 +155,7 @@ export async function optimizerMetricsRoutes(app: FastifyInstance) { }, }, }, - config: { cacheTTL: 21600 }, + config: { cacheTTL: CACHE_TTL.OPTIMIZER }, }, async (request, reply) => { if (!app.relaysDb) return sendApiError(reply, 503, "Optimizer metrics not configured"); @@ -219,7 +220,7 @@ export async function optimizerMetricsRoutes(app: FastifyInstance) { }, }, }, - config: { cacheTTL: 21600 }, + config: { cacheTTL: CACHE_TTL.OPTIMIZER }, }, async (request, reply) => { if (!app.relaysDb) return sendApiError(reply, 503, "Optimizer metrics not configured"); diff --git a/apps/api/src/routes/provider-claimable-rewards.ts b/apps/api/src/routes/provider-claimable-rewards.ts index 218260b..d3edb9f 100644 --- a/apps/api/src/routes/provider-claimable-rewards.ts +++ b/apps/api/src/routes/provider-claimable-rewards.ts @@ -1,4 +1,5 @@ import type { FastifyInstance } from "fastify"; +import { CACHE_TTL } from "../config.js"; import { RPC_BATCH_SIZE, fetchAllProviders, @@ -17,7 +18,7 @@ export async function providerClaimableRewardsRoutes(app: FastifyInstance) { tags: ["Provider Rewards"], summary: "Per-provider self-delegation claimable rewards (multi-denom + USD)", }, - config: { cacheTTL: 7200 }, + config: { cacheTTL: CACHE_TTL.SLOW_MOVING }, }, async () => { await prewarmPriceCache(); const providers = await fetchAllProviders(); diff --git a/apps/api/src/routes/provider-estimated-rewards.ts b/apps/api/src/routes/provider-estimated-rewards.ts index 8262f6e..89cb7f4 100644 --- a/apps/api/src/routes/provider-estimated-rewards.ts +++ b/apps/api/src/routes/provider-estimated-rewards.ts @@ -1,4 +1,5 @@ import type { FastifyInstance } from "fastify"; +import { CACHE_TTL } from "../config.js"; import { RPC_BATCH_SIZE, fetchBlockAtTimestamp, @@ -69,7 +70,7 @@ export async function providerEstimatedRewardsRoutes(app: FastifyInstance) { }, }, }, - config: { cacheTTL: 86_400 }, + config: { cacheTTL: CACHE_TTL.HISTORICAL }, }, async (request) => { const q = request.query as { count?: number }; const count = q.count ?? DEFAULT_MONTHLY_BLOCKS; @@ -113,7 +114,7 @@ export async function providerEstimatedRewardsRoutes(app: FastifyInstance) { }, }, }, - config: { cacheTTL: 1800 }, + config: { cacheTTL: CACHE_TTL.APR }, }, async (request, reply) => { const q = request.query as { block?: number; spec?: string }; const block = q.block && q.block > 0 ? q.block : undefined; @@ -127,7 +128,7 @@ export async function providerEstimatedRewardsRoutes(app: FastifyInstance) { } // Historical blocks are immutable, so cache aggressively. - if (block) request.cacheTTL = 86_400; + if (block) request.cacheTTL = CACHE_TTL.HISTORICAL; await prewarmPriceCache(); // Tokens at block N are priced with the CURRENT LAVA price (matches lava-ops diff --git a/apps/api/src/routes/provider-rewards.ts b/apps/api/src/routes/provider-rewards.ts index 4535384..f442f17 100644 --- a/apps/api/src/routes/provider-rewards.ts +++ b/apps/api/src/routes/provider-rewards.ts @@ -1,4 +1,5 @@ import type { FastifyInstance } from "fastify"; +import { CACHE_TTL } from "../config.js"; import { parseYMD } from "@info/shared/utils"; import { gqlSafe } from "../graphql/client.js"; import { fetchProvidersForSpec, fetchAllProviderMonikers } from "../rpc/lava.js"; @@ -9,8 +10,6 @@ import { sendApiError } from "../plugins/error-handler.js"; const SPEC_ID_RE = /^[a-zA-Z][a-zA-Z0-9_-]*$/; const MAX_RANGE_MS = 6 * 30 * 24 * 60 * 60 * 1000; // ~6 months const ONE_DAY_MS = 24 * 60 * 60 * 1000; -const PAST_WINDOW_TTL = 86_400; // 1 day — immutable historical window -const LIVE_WINDOW_TTL = 300; // 5 min — aligned with MV refresh cadence function validateSpecId(s: string): boolean { return s.length > 2 && s.length <= 50 && SPEC_ID_RE.test(s); @@ -118,7 +117,7 @@ export async function providerRewardsRoutes(app: FastifyInstance) { required: ["from", "to"] as const, }, }, - config: { cacheTTL: 300 }, + config: { cacheTTL: CACHE_TTL.LIST }, }, async (request, reply) => { const q = request.query as { specs?: string[]; @@ -161,8 +160,8 @@ export async function providerRewardsRoutes(app: FastifyInstance) { new Date().getUTCDate(), ); request.cacheTTL = dateTo.getTime() <= todayUtc - ONE_DAY_MS - ? PAST_WINDOW_TTL - : LIVE_WINDOW_TTL; + ? CACHE_TTL.HISTORICAL + : CACHE_TTL.LIST; // ── Resolve specs ───────────────────────────────────────────── const specs = q.specs && q.specs.length > 0 ? [...new Set(q.specs)] : null; diff --git a/apps/api/src/routes/providers.ts b/apps/api/src/routes/providers.ts index 6e8aa54..0064ad4 100644 --- a/apps/api/src/routes/providers.ts +++ b/apps/api/src/routes/providers.ts @@ -1,4 +1,5 @@ import type { FastifyInstance } from "fastify"; +import { CACHE_TTL } from "../config.js"; import { weightedQos } from "@info/shared/utils"; import type { ProviderListItem } from "@info/shared/types"; import { gqlSafe } from "../graphql/client.js"; @@ -25,7 +26,7 @@ export async function providerRoutes(app: FastifyInstance) { // and complicates client filtering; return the full array sorted by stake. app.get("/", { schema: { tags: ["Providers"], summary: "All providers with 30d relay stats, sorted by total stake desc" }, - config: { cacheTTL: 300 }, + config: { cacheTTL: CACHE_TTL.LIST }, }, async (): Promise<{ data: ProviderListItem[] }> => { const since = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000) .toISOString() @@ -88,7 +89,7 @@ export async function providerRoutes(app: FastifyInstance) { tags: ["Providers"], summary: "Provider detail — stakes across all specs with health data", }, - config: { cacheTTL: 300 }, + config: { cacheTTL: CACHE_TTL.LIST }, }, async (request) => { const { addr } = request.params; const specs = await fetchAllSpecs(); @@ -146,7 +147,7 @@ export async function providerRoutes(app: FastifyInstance) { tags: ["Providers"], summary: "Provider stakes per spec", }, - config: { cacheTTL: 300 }, + config: { cacheTTL: CACHE_TTL.LIST }, }, async (request) => { const { addr } = request.params; const specs = await fetchAllSpecs(); @@ -180,7 +181,7 @@ export async function providerRoutes(app: FastifyInstance) { tags: ["Providers"], summary: "Provider health probe results", }, - config: { cacheTTL: 30 }, + config: { cacheTTL: CACHE_TTL.HEALTH_PROBE }, }, async (request) => { const { addr } = request.params; if (!app.redis) return { data: [] }; @@ -208,7 +209,7 @@ export async function providerRoutes(app: FastifyInstance) { }, }, }, - config: { cacheTTL: 300 }, + config: { cacheTTL: CACHE_TTL.LIST }, }, async (request) => { const { addr } = request.params; const query = request.query as Record; @@ -311,7 +312,7 @@ export async function providerRoutes(app: FastifyInstance) { }, }, }, - config: { cacheTTL: 86400 }, + config: { cacheTTL: CACHE_TTL.HISTORICAL }, }, async (request) => { const { addr } = request.params; const query = request.query as Record; @@ -326,7 +327,7 @@ export async function providerRoutes(app: FastifyInstance) { tags: ["Providers"], summary: "Delegator rewards from dualstaking module", }, - config: { cacheTTL: 300 }, + config: { cacheTTL: CACHE_TTL.LIST }, }, async (request) => { const { addr } = request.params; const rewards = await fetchDelegatorRewards(addr); diff --git a/apps/api/src/routes/search.ts b/apps/api/src/routes/search.ts index c8f98c0..f7527d6 100644 --- a/apps/api/src/routes/search.ts +++ b/apps/api/src/routes/search.ts @@ -1,4 +1,5 @@ import type { FastifyInstance } from "fastify"; +import { CACHE_TTL } from "../config.js"; import { fetchAllProviders, fetchAllSpecs } from "../rpc/lava.js"; interface SearchResult { @@ -23,7 +24,7 @@ export async function searchRoutes(app: FastifyInstance) { }, }, }, - config: { cacheTTL: 600 }, + config: { cacheTTL: CACHE_TTL.SEARCH }, }, async (request) => { const query = request.query as Record; const q = query.q?.toLowerCase() ?? ""; diff --git a/apps/api/src/routes/specs.ts b/apps/api/src/routes/specs.ts index 9e37ad8..6985a65 100644 --- a/apps/api/src/routes/specs.ts +++ b/apps/api/src/routes/specs.ts @@ -1,4 +1,5 @@ import type { FastifyInstance } from "fastify"; +import { CACHE_TTL } from "../config.js"; import { gqlSafe } from "../graphql/client.js"; import { fetchAllSpecs, fetchProvidersForSpec } from "../rpc/lava.js"; import { readHealthSummaryForSpec, readHealthByProviderForSpec } from "../services/health-store.js"; @@ -15,7 +16,7 @@ export async function specRoutes(app: FastifyInstance) { // GET /specs — chain RPC + indexer relay data app.get("/", { schema: { tags: ["Specs"], summary: "All specs with provider counts and 30d relay data" }, - config: { cacheTTL: 300 }, + config: { cacheTTL: CACHE_TTL.LIST }, }, async () => { const since = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000).toISOString().slice(0, 10); @@ -66,7 +67,7 @@ export async function specRoutes(app: FastifyInstance) { // GET /specs/:specId/stakes — chain RPC + health from Redis app.get<{ Params: { specId: string } }>("/:specId/stakes", { schema: { ...specIdSchema, tags: ["Specs"], summary: "Providers staked on this spec with health" }, - config: { cacheTTL: 300 }, + config: { cacheTTL: CACHE_TTL.LIST }, }, async (request) => { const { specId } = request.params; const providers = await fetchProvidersForSpec(specId); @@ -97,7 +98,7 @@ export async function specRoutes(app: FastifyInstance) { // GET /specs/:specId/health — from Redis app.get<{ Params: { specId: string } }>("/:specId/health", { schema: { ...specIdSchema, tags: ["Specs"], summary: "Health status distribution for a spec" }, - config: { cacheTTL: 30 }, + config: { cacheTTL: CACHE_TTL.HEALTH_PROBE }, }, async (request) => { const { specId } = request.params; @@ -116,7 +117,7 @@ export async function specRoutes(app: FastifyInstance) { // GET /specs/:specId/charts — indexer GraphQL (materialized view) app.get<{ Params: { specId: string } }>("/:specId/charts", { schema: { ...specIdSchema, tags: ["Specs"], summary: "Alltime CU/relays for a chain" }, - config: { cacheTTL: 300 }, + config: { cacheTTL: CACHE_TTL.LIST }, }, async (request) => { const { specId } = request.params; diff --git a/apps/api/src/routes/supply.ts b/apps/api/src/routes/supply.ts index 8988139..76d5042 100644 --- a/apps/api/src/routes/supply.ts +++ b/apps/api/src/routes/supply.ts @@ -1,4 +1,5 @@ import type { FastifyInstance } from "fastify"; +import { CACHE_TTL } from "../config.js"; import { ulavaToLava } from "@info/shared/utils"; import { fetchTotalSupply, fetchCirculatingSupply } from "../rpc/lava.js"; import { readPrecomputed } from "../services/precompute-store.js"; @@ -7,7 +8,7 @@ export async function supplyRoutes(app: FastifyInstance) { // GET /supply/total — total token supply in lava, cached 5 min app.get("/total", { schema: { tags: ["Supply"], summary: "Total LAVA supply (plain text)" }, - config: { cacheTTL: 300, rateLimit: { max: 20, timeWindow: "1 minute" } }, + config: { cacheTTL: CACHE_TTL.LIST, rateLimit: { max: 20, timeWindow: "1 minute" } }, }, async (_request, reply) => { reply.header("Content-Type", "text/plain"); return ulavaToLava(await fetchTotalSupply()); @@ -18,7 +19,7 @@ export async function supplyRoutes(app: FastifyInstance) { // live compute when the precompute worker is down / has no entry yet. app.get("/circulating", { schema: { tags: ["Supply"], summary: "Circulating LAVA supply (plain text)" }, - config: { cacheTTL: 300, rateLimit: { max: 10, timeWindow: "1 minute" } }, + config: { cacheTTL: CACHE_TTL.LIST, rateLimit: { max: 10, timeWindow: "1 minute" } }, }, async (_request, reply) => { reply.header("Content-Type", "text/plain"); const cached = await readPrecomputed(app.redis, "supply.circulating"); diff --git a/apps/api/src/routes/tvl.ts b/apps/api/src/routes/tvl.ts index e6cfebb..88b3ab5 100644 --- a/apps/api/src/routes/tvl.ts +++ b/apps/api/src/routes/tvl.ts @@ -1,11 +1,12 @@ import type { FastifyInstance } from "fastify"; +import { CACHE_TTL } from "../config.js"; import { computeTVL } from "../rpc/lava.js"; export async function tvlRoutes(app: FastifyInstance) { // GET /tvl — computed from chain RPC, cached 5 min app.get("/tvl", { schema: { tags: ["TVL"], summary: "Total Value Locked in USD" }, - config: { cacheTTL: 300 }, + config: { cacheTTL: CACHE_TTL.LIST }, }, async () => { return await computeTVL(); }); diff --git a/apps/api/src/routes/validators-and-rewards.ts b/apps/api/src/routes/validators-and-rewards.ts index a97a3a9..949098a 100644 --- a/apps/api/src/routes/validators-and-rewards.ts +++ b/apps/api/src/routes/validators-and-rewards.ts @@ -1,4 +1,5 @@ import type { FastifyInstance } from "fastify"; +import { CACHE_TTL } from "../config.js"; import { fetchValidatorsWithRewards, type ValidatorWithRewards } from "../rpc/lava.js"; import { readPrecomputed } from "../services/precompute-store.js"; @@ -20,7 +21,7 @@ export async function validatorsAndRewardsRoutes(app: FastifyInstance) { tags: ["Validators"], summary: "All bonded validators with rewards, delegations, and unbonding — matches jsinfo shape", }, - config: { cacheTTL: 7200 }, + config: { cacheTTL: CACHE_TTL.SLOW_MOVING }, }, async (request) => { const cached = await readPrecomputed(request.server.redis, "validators_and_rewards"); if (cached) return { data: cached.value }; From 1d8975365d7e84e40a113bb7fae3950da1470e82 Mon Sep 17 00:00:00 2001 From: Nimrod Teich Date: Fri, 17 Apr 2026 00:37:24 +0300 Subject: [PATCH 23/25] docs(api): note that /validators-and-rewards serves external consumers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The web app never calls it — the route exists for burn-ui and lava-ops pipelines. A grep-driven "dead route" cleanup would remove it; the comment makes that decision explicit. --- apps/api/src/routes/validators-and-rewards.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/apps/api/src/routes/validators-and-rewards.ts b/apps/api/src/routes/validators-and-rewards.ts index 949098a..8c40785 100644 --- a/apps/api/src/routes/validators-and-rewards.ts +++ b/apps/api/src/routes/validators-and-rewards.ts @@ -14,6 +14,10 @@ export async function validatorsAndRewardsRoutes(app: FastifyInstance) { // validator: fetches distribution rewards, outstanding rewards, estimated // rewards, delegations, and unbonding delegations — all converted to USD. // + // Consumers: external (burn-ui dashboards, lava-ops pipelines). NOT called + // from this repo's web frontend. Don't delete it thinking it's dead — the + // /lava_mainnet_validators_and_rewards shape is the public contract. + // // Prefers the precomputed value from bin/precompute.ts; falls back to live // compute when the precompute worker hasn't populated the key yet. app.get("/validators-and-rewards", { From 93d7554724240d6cd96f636df46ad1639c693b7c Mon Sep 17 00:00:00 2001 From: Nimrod Teich Date: Fri, 17 Apr 2026 00:38:45 +0300 Subject: [PATCH 24/25] perf(web): wire global SWRConfig (dedupe + refresh defaults) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit useApi configured refreshInterval inline on every hook instance, so two components pointing at the same URL inside a few ms could still make two HTTP requests and revalidateOnFocus was forcing an extra fetch whenever the user re-focused the tab. Move the defaults to a SwrProvider at the root layout: - dedupingInterval: 10s — coalesce concurrent callers of the same URL - refreshInterval: 5min — matches API cache TTL - revalidateOnFocus: false - keepPreviousData: true — smooth network-toggle transitions Per-call overrides are still honored (just none exist yet). --- apps/web/src/app/layout.tsx | 27 +++++++++++++----------- apps/web/src/app/swr-provider.tsx | 34 +++++++++++++++++++++++++++++++ apps/web/src/hooks/use-api.ts | 13 ++++++------ 3 files changed, 55 insertions(+), 19 deletions(-) create mode 100644 apps/web/src/app/swr-provider.tsx diff --git a/apps/web/src/app/layout.tsx b/apps/web/src/app/layout.tsx index 5ca4941..f728c42 100644 --- a/apps/web/src/app/layout.tsx +++ b/apps/web/src/app/layout.tsx @@ -3,6 +3,7 @@ import { Inter } from "next/font/google"; import { Header } from "@/components/layout/Header"; import { Footer } from "@/components/layout/Footer"; import { ErrorBoundary } from "@/components/data/ErrorBoundary"; +import { SwrProvider } from "./swr-provider"; import "@/styles/globals.css"; @@ -56,19 +57,21 @@ export default function RootLayout({ children }: { children: React.ReactNode }) return ( -
-
- -
-
-
- {children} + +
+
+ +
+
+
+ {children} +
-
-
-
-
-
+ + +