Skip to content
Merged
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 api/__tests__/handlers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ vi.mock("@meridian/stellar-sdk-helpers", () => ({
submitTx: vi.fn(async () => ({ hash: "HASH" })),
fetchAllVaults: vi.fn(async () => [{ id: "blend-usdc-fixed", protocol: "blend" }]),
selectBestVault: vi.fn(() => ({ id: "blend-usdc-fixed" })),
isVaultCacheWarm: vi.fn(() => false),
fetchBlendPositions: vi.fn(async () => [
{ vaultId: "blend-usdc-fixed", shares: 1, deposited: 1, earned: 0, entryTime: 0 },
]),
Expand Down
5 changes: 3 additions & 2 deletions api/v1/vaults/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { VercelRequest, VercelResponse } from "@vercel/node";
import { fetchAllVaults, selectBestVault } from "@meridian/stellar-sdk-helpers";
import { fetchAllVaults, selectBestVault, isVaultCacheWarm } from "@meridian/stellar-sdk-helpers";
import { APP_ADDRESSES } from "@meridian/shared";
import { applyCors } from "../../_lib/middleware";

Expand All @@ -14,14 +14,15 @@ const defindexConfigured = Boolean(process.env.DEFINDEX_VAULT_ID ?? APP_ADDRESSE
export default async function handler(req: VercelRequest, res: VercelResponse) {
if (applyCors(req, res)) return;
try {
const cached = isVaultCacheWarm();
const vaults = await fetchAllVaults();
const best = selectBestVault(vaults, { defindexConfigured });
res.setHeader("Cache-Control", CACHE_CONTROL);
res.json({
vaults,
recommendedVaultId: best?.id ?? null,
updatedAt: new Date().toISOString(),
cached: false,
cached,
});
} catch (err) {
console.error("[vaults] fetch error:", err);
Expand Down
5 changes: 3 additions & 2 deletions apps/api/src/routes/vaults.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { FastifyPluginAsync } from "fastify";
import { selectBestVault } from "@meridian/stellar-sdk-helpers";
import { selectBestVault, isVaultCacheWarm } from "@meridian/stellar-sdk-helpers";
import { APP_ADDRESSES } from "@meridian/shared";
import { fetchAllVaults } from "../services/vaults.js";

Expand All @@ -8,13 +8,14 @@ const defindexConfigured = Boolean(process.env.DEFINDEX_VAULT_ID ?? APP_ADDRESSE
export const vaultsRoute: FastifyPluginAsync = async (app) => {
app.get("/", async (_req, reply) => {
try {
const cached = isVaultCacheWarm();
const vaults = await fetchAllVaults();
const best = selectBestVault(vaults, { defindexConfigured });
return reply.send({
vaults,
recommendedVaultId: best?.id ?? null,
updatedAt: new Date().toISOString(),
cached: false,
cached,
});
} catch (err) {
app.log.error(err, "[vaults] failed to fetch vaults");
Expand Down
5 changes: 5 additions & 0 deletions packages/stellar-sdk-helpers/src/vaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export function clearVaultCache(): void {
vaultCache = null;
}

/** Returns true if a valid cached result exists and will be returned by fetchAllVaults. */
export function isVaultCacheWarm(): boolean {
return vaultCache !== null && Date.now() < vaultCache.expiresAt;
}

// Every vault in the list is backed by live DeFiLlama market data. Protocols
// without a real on-chain rate feed wired up yet are intentionally omitted
// rather than shown with placeholder figures.
Expand Down