Last active
August 11, 2023 13:07
-
-
Save ferostabio/a881d7fdbcc779e468bdd165918635a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import express, { Request, Response } from "express"; | |
import axios from "axios"; | |
import { createClient } from "redis"; | |
const FIFTEEN_MINUTES_IN_MILLISECONDS = 15 * 60 * 1000; | |
const THREE_DAYS_IN_SECONDS = 3 * 24 * 60 * 60; | |
const PORT = Number(process.env.PORT) || 3000; | |
const REDIS_URL: string = process.env.REDIS_URL ?? ""; | |
export enum DEFILLAMA_URL { | |
LEND_BORROW = "https://yields.llama.fi/lendBorrow", | |
POOLS = "https://yields.llama.fi/pools", | |
} | |
type DefillamaUri = { | |
lendBorrow: string; | |
pools: string; | |
}; | |
const client = createClient({ | |
url: REDIS_URL, | |
}); | |
client.on("error", (err: Error) => { | |
console.error("Redis error:", err); | |
}); | |
const app = express(); | |
app.get("/llama_financials", async (req: Request, res: Response) => { | |
try { | |
const [lendBorrows, pools] = await client | |
.multi() | |
.get("lendBorrows") | |
.get("pools") | |
.exec(); | |
const responseData = { | |
lendBorrows: | |
typeof lendBorrows === "string" ? JSON.parse(lendBorrows) : null, | |
pools: typeof pools === "string" ? JSON.parse(pools) : null, | |
}; | |
res.json(responseData); | |
} catch (err) { | |
res.status(500).json({ error: "Error retrieving data" }); | |
} | |
}); | |
app.listen(PORT, async () => { | |
console.log(`Server is running on ${PORT}`); | |
await client.connect(); | |
console.log("Connected to Redis"); | |
setInterval(fetchDataAndSaveToRedis, FIFTEEN_MINUTES_IN_MILLISECONDS); | |
fetchDataAndSaveToRedis(); | |
}); | |
function defillamaUri(): DefillamaUri { | |
// Not sure if this is even the right approach for the proxy | |
const defillamaproxy = process.env.DEFILLAMA_PROXY; | |
const uri = { | |
lendBorrow: defillamaproxy | |
? `${defillamaproxy}lendBorrow` | |
: DEFILLAMA_URL.LEND_BORROW, | |
pools: defillamaproxy ? `${defillamaproxy}pools` : DEFILLAMA_URL.POOLS, | |
}; | |
return uri; | |
} | |
async function fetchDataAndSaveToRedis(): Promise<void> { | |
const uri = defillamaUri(); | |
try { | |
const [lendBorrows, pools] = await Promise.all([ | |
axios.get(uri.lendBorrow).then(({ data }) => data), | |
axios.get(uri.pools).then(({ data }) => data.data), | |
]); | |
client.set("lendBorrows", JSON.stringify(lendBorrows), { | |
EX: THREE_DAYS_IN_SECONDS, | |
}); | |
client.set("pools", JSON.stringify(pools), { EX: THREE_DAYS_IN_SECONDS }); | |
console.log("Data fetched and saved to Redis."); | |
} catch (error) { | |
console.error("Error fetching data:", error); // This try/catch is the whole reason we're doing this | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment