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
1 change: 1 addition & 0 deletions src/routes/funding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ export function fundingRoutes(): Hono {
// causes a PostgREST 400. Downstream defaults assetIndex to 0. Same fix as crank.ts (e471efb).
.select("funding_rate, net_lp_pos, symbol, last_price")
.eq("slab_address", slab)
.eq("network", getNetwork())
.single();

if (statsError && statsError.code !== "PGRST116") {
Expand Down
3 changes: 2 additions & 1 deletion src/routes/insurance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
import { Hono } from "hono";
import { validateSlab } from "../middleware/validateSlab.js";
import { getSupabase, createLogger, truncateErrorMessage } from "@percolator/shared";
import { getSupabase, getNetwork, createLogger, truncateErrorMessage } from "@percolator/shared";

const logger = createLogger("api:insurance");

Expand Down Expand Up @@ -40,6 +40,7 @@ export function insuranceRoutes(): Hono {
.from("market_stats")
.select("insurance_balance, insurance_fee_revenue, total_open_interest")
.eq("slab_address", slab)
.eq("network", getNetwork())
.single();

if (statsError && statsError.code !== "PGRST116") {
Expand Down
1 change: 1 addition & 0 deletions src/routes/markets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export function marketRoutes(): Hono {
.from("market_stats")
.select("slab_address, total_open_interest, total_accounts, last_crank_slot, last_price, mark_price, index_price, funding_rate, net_lp_pos, lp_sum_abs, lp_max_abs, insurance_balance, insurance_fee_revenue, volume_24h, updated_at")
.eq("slab_address", slab)
.eq("network", getNetwork())
.single();
if (error && error.code !== "PGRST116") throw error;
return c.json({ stats: data ?? null });
Expand Down
3 changes: 2 additions & 1 deletion src/routes/open-interest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import { Hono } from "hono";
import { validateSlab } from "../middleware/validateSlab.js";
import { cacheMiddleware } from "../middleware/cache.js";
import { getSupabase, createLogger, truncateErrorMessage } from "@percolator/shared";
import { getSupabase, getNetwork, createLogger, truncateErrorMessage } from "@percolator/shared";

/**
* GH#1458: Phantom OI guard for history records.
Expand Down Expand Up @@ -64,6 +64,7 @@ export function openInterestRoutes(): Hono {
.from("market_stats")
.select("total_open_interest, net_lp_pos, lp_sum_abs, lp_max_abs")
.eq("slab_address", slab)
.eq("network", getNetwork())
.single();

if (statsError && statsError.code !== "PGRST116") {
Expand Down
195 changes: 68 additions & 127 deletions tests/routes/insurance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { insuranceRoutes } from "../../src/routes/insurance.js";
// Mock @percolator/shared
vi.mock("@percolator/shared", () => ({
getSupabase: vi.fn(),
getNetwork: vi.fn(() => "devnet"),
getConnection: vi.fn(),
createLogger: vi.fn(() => ({
info: vi.fn(),
Expand Down Expand Up @@ -42,6 +43,36 @@ describe("insurance routes", () => {
vi.mocked(getSupabase).mockReturnValue(mockSupabase);
});

function mockInsuranceQueries(
mockStats: unknown,
mockHistory: unknown[] = [],
statsError: unknown = null,
historyError: unknown = null
) {
const statsBuilder: any = {};
statsBuilder.select = vi.fn(() => statsBuilder);
statsBuilder.eq = vi.fn(() => statsBuilder);
statsBuilder.single = vi.fn().mockResolvedValue({
data: mockStats,
error: statsError,
});

const historyBuilder: any = {};
historyBuilder.select = vi.fn(() => historyBuilder);
historyBuilder.eq = vi.fn(() => historyBuilder);
historyBuilder.order = vi.fn(() => historyBuilder);
historyBuilder.limit = vi.fn().mockResolvedValue({
data: mockHistory,
error: historyError,
});

mockSupabase.from.mockImplementation((table: string) => {
if (table === "market_stats") return statsBuilder;
if (table === "insurance_history") return historyBuilder;
return mockSupabase;
});
}

describe("GET /insurance/:slab", () => {
it("should return current insurance balance and history", async () => {
const mockStats = {
Expand All @@ -63,28 +94,7 @@ describe("insurance routes", () => {
},
];

mockSupabase.from.mockImplementation((table: string) => {
if (table === "market_stats") {
return {
select: vi.fn(() => ({
eq: vi.fn(() => ({
single: vi.fn().mockResolvedValue({ data: mockStats, error: null }),
})),
})),
};
} else if (table === "insurance_history") {
return {
select: vi.fn(() => ({
eq: vi.fn(() => ({
order: vi.fn(() => ({
limit: vi.fn().mockResolvedValue({ data: mockHistory, error: null }),
})),
})),
})),
};
}
return mockSupabase;
});
mockInsuranceQueries(mockStats, mockHistory);

const app = insuranceRoutes();
const res = await app.request("/insurance/11111111111111111111111111111111");
Expand All @@ -99,22 +109,7 @@ describe("insurance routes", () => {
});

it("should return 404 when market not found", async () => {
mockSupabase.from.mockImplementation((table: string) => {
if (table === "market_stats") {
return {
select: vi.fn(() => ({
eq: vi.fn(() => ({
single: vi.fn().mockResolvedValue({
data: null,
error: { code: "PGRST116" }
}),
})),
})),
};
}
return mockSupabase;
});

mockInsuranceQueries(null, [], { code: "PGRST116" });
const app = insuranceRoutes();
const res = await app.request("/insurance/11111111111111111111111111111111");

Expand All @@ -139,28 +134,7 @@ describe("insurance routes", () => {
total_open_interest: null,
};

mockSupabase.from.mockImplementation((table: string) => {
if (table === "market_stats") {
return {
select: vi.fn(() => ({
eq: vi.fn(() => ({
single: vi.fn().mockResolvedValue({ data: mockStats, error: null }),
})),
})),
};
} else if (table === "insurance_history") {
return {
select: vi.fn(() => ({
eq: vi.fn(() => ({
order: vi.fn(() => ({
limit: vi.fn().mockResolvedValue({ data: [], error: null }),
})),
})),
})),
};
}
return mockSupabase;
});
mockInsuranceQueries(mockStats, []);

const app = insuranceRoutes();
const res = await app.request("/insurance/11111111111111111111111111111111");
Expand All @@ -173,21 +147,7 @@ describe("insurance routes", () => {
});

it("should handle database errors", async () => {
mockSupabase.from.mockImplementation((table: string) => {
if (table === "market_stats") {
return {
select: vi.fn(() => ({
eq: vi.fn(() => ({
single: vi.fn().mockResolvedValue({
data: null,
error: new Error("Database error")
}),
})),
})),
};
}
return mockSupabase;
});
mockInsuranceQueries(null, [], new Error("Database error"));

const app = insuranceRoutes();
const res = await app.request("/insurance/11111111111111111111111111111111");
Expand All @@ -205,30 +165,31 @@ describe("insurance routes", () => {
};

let limitCalled = false;

const statsBuilder: any = {};
statsBuilder.select = vi.fn(() => statsBuilder);
statsBuilder.eq = vi.fn(() => statsBuilder);
statsBuilder.single = vi.fn().mockResolvedValue({
data: mockStats,
error: null,
});

const historyBuilder: any = {};
historyBuilder.select = vi.fn(() => historyBuilder);
historyBuilder.eq = vi.fn(() => historyBuilder);
historyBuilder.order = vi.fn(() => historyBuilder);
historyBuilder.limit = vi.fn((n: number) => {
expect(n).toBe(100);
limitCalled = true;
return Promise.resolve({
data: [],
error: null,
});
});

mockSupabase.from.mockImplementation((table: string) => {
if (table === "market_stats") {
return {
select: vi.fn(() => ({
eq: vi.fn(() => ({
single: vi.fn().mockResolvedValue({ data: mockStats, error: null }),
})),
})),
};
} else if (table === "insurance_history") {
return {
select: vi.fn(() => ({
eq: vi.fn(() => ({
order: vi.fn(() => ({
limit: vi.fn((n: number) => {
expect(n).toBe(100);
limitCalled = true;
return Promise.resolve({ data: [], error: null });
}),
})),
})),
})),
};
}
if (table === "market_stats") return statsBuilder;
if (table === "insurance_history") return historyBuilder;
return mockSupabase;
});

Expand Down Expand Up @@ -260,38 +221,18 @@ describe("insurance routes", () => {
}

it("allows valid non-blocked slabs through to DB layer", async () => {
mockSupabase.from.mockImplementation((table: string) => {
if (table === "market_stats") {
return {
select: vi.fn(() => ({
eq: vi.fn(() => ({
single: vi.fn().mockResolvedValue({
data: {
insurance_balance: "1000000000",
insurance_fee_revenue: "50000000",
total_open_interest: "5000000000",
},
error: null,
}),
})),
})),
};
} else if (table === "insurance_history") {
return {
select: vi.fn(() => ({
eq: vi.fn(() => ({
order: vi.fn(() => ({
limit: vi.fn().mockResolvedValue({ data: [], error: null }),
})),
})),
})),
};
}
return mockSupabase;
});
mockInsuranceQueries(
{
insurance_balance: "1000000000",
insurance_fee_revenue: "50000000",
total_open_interest: "5000000000",
},
[]
);

const app = insuranceRoutes();
const res = await app.request("/insurance/11111111111111111111111111111111");

expect(res.status).toBe(200);
});
});
Expand Down
Loading
Loading