Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion apps/api/src/__tests__/routes-index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ vi.mock("../rpc/lava.js", () => ({
fetchLatestBlockHeight: vi.fn(),
fetchAllProviders: vi.fn(),
}));
vi.mock("../rpc/supply.js", () => ({
fetchStakingPool: vi.fn(),
}));

const { gqlSafe } = await import("../graphql/client.js");
const { fetchLatestBlockHeight, fetchAllProviders } = await import("../rpc/lava.js");
const { fetchStakingPool } = await import("../rpc/supply.js");
const { indexRoutes } = await import("../routes/index.js");

async function buildApp() {
Expand All @@ -33,14 +37,18 @@ describe("GET /index/stats", () => {
(fetchAllProviders as ReturnType<typeof vi.fn>).mockResolvedValue([
{ address: "lava@a", totalStake: "1000", totalDelegation: "500" },
]);
(fetchStakingPool as ReturnType<typeof vi.fn>).mockResolvedValue({
bonded_tokens: "381000000000000",
not_bonded_tokens: "999",
});

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");
expect(body.totalStake).toBe("381000000000000");
});
});

Expand Down
9 changes: 4 additions & 5 deletions apps/api/src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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";
import { fetchStakingPool } from "../rpc/supply.js";

export async function indexRoutes(app: FastifyInstance) {
app.get("/stats", {
Expand All @@ -22,7 +23,7 @@ export async function indexRoutes(app: FastifyInstance) {
};
} | null;

const [allTimeData, last30dData, latestBlock, providers] = await Promise.all([
const [allTimeData, last30dData, latestBlock, providers, stakingPool] = await Promise.all([
gqlSafe<RelayAgg>(`{
allMvRelayDailies {
aggregates { sum { cu relays } }
Expand All @@ -35,12 +36,10 @@ export async function indexRoutes(app: FastifyInstance) {
}`, { since: thirtyDaysAgo }, null),
fetchLatestBlockHeight(),
fetchAllProviders(),
fetchStakingPool(),
]);

const totalStake = providers.reduce(
(sum, p) => sum + BigInt(p.totalStake) + BigInt(p.totalDelegation),
0n,
);
const totalStake = BigInt(stakingPool.bonded_tokens);

return {
totalCu: allTimeData?.allMvRelayDailies.aggregates.sum.cu ?? null,
Expand Down