From a5025e930706bff148bcb8897d50f9c598104526 Mon Sep 17 00:00:00 2001 From: Tyler Shipe Date: Tue, 27 Jul 2021 18:03:22 -0400 Subject: [PATCH] add express routes for queries --- src/collectStats.ts | 8 ++++-- src/index.ts | 60 ++++++++++++++++++++++++++++----------------- 2 files changed, 44 insertions(+), 24 deletions(-) diff --git a/src/collectStats.ts b/src/collectStats.ts index a4a5c26..db0d0c7 100644 --- a/src/collectStats.ts +++ b/src/collectStats.ts @@ -49,6 +49,8 @@ const loadPerpMarkets = async (connection, groupConfig: GroupConfig) => { async function fetchSpotStats() { const mangoGroup = await client.getMangoGroup(groupConfig.publicKey) + const mangoCache = await mangoGroup.loadCache(connection) + // TODO: reduce calls in loadRootBanks await mangoGroup.loadRootBanks(connection) const spotMarketStats = groupConfig.spotMarkets.map((spotMarket, index) => { @@ -63,7 +65,7 @@ async function fetchSpotStats() { depositRate: mangoGroup.getDepositRate(index).toNumber(), borrowRate: mangoGroup.getBorrowRate(index).toNumber(), utilization: totalDeposits.gt(I80F48.fromNumber(0)) ? totalBorrows.div(totalDeposits).toNumber() : 0, - baseOraclePrice: 0, + baseOraclePrice: mangoCache.priceCache[index].price.toNumber(), } }) try { @@ -77,6 +79,8 @@ async function fetchSpotStats() { } async function fetchPerpStats() { + const mangoGroup = await client.getMangoGroup(groupConfig.publicKey) + const mangoCache = await mangoGroup.loadCache(connection) const perpMarkets = await loadPerpMarkets(connection, groupConfig) console.log("6") @@ -88,7 +92,7 @@ async function fetchPerpStats() { longFunding: perpMarket.longFunding.toNumber(), shortFunding: perpMarket.shortFunding.toNumber(), openInterest: perpMarket.openInterest.toNumber(), - baseOraclePrice: 0, + baseOraclePrice: mangoCache.priceCache[index].price.toNumber(), } }) diff --git a/src/index.ts b/src/index.ts index 3f4e104..4488d0d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,44 +10,60 @@ const app = express() app.use(express.json(), cors()) -app.get("/", async (req, res) => { +app.get("/spot", async (req, res) => { try { - const mangoGroup = (req.query.mangoGroup as string) || "" - let stats - if (mangoGroup === "BTC_ETH_USDT" || mangoGroup === "BTC_ETH_SOL_SRM_USDC") { - stats = await sequelize.query( - `SELECT time_bucket('60 minutes', time) AS "hourly", - "symbol", + const mangoGroup = req.query.mangoGroup as string + if (!mangoGroup) { + throw new Error("Missing mangoGroup param") + } + + const stats = await sequelize.query( + `SELECT time_bucket('60 minutes', time) AS "hourly", + "name", avg("totalDeposits")::float AS "totalDeposits", avg("totalBorrows")::float AS "totalBorrows", avg("utilization")::float AS "utilization", avg("depositInterest")::float AS "depositInterest", avg("borrowInterest")::float AS "borrowInterest", min("time") AS "time" - FROM mainnet_stats + FROM spot_market_stats WHERE time > current_date - interval '90' day AND "mangoGroup" = :mangoGroup - GROUP BY "hourly", "symbol" + GROUP BY "hourly", "name" ORDER BY "hourly" ASC;`, - { - replacements: { mangoGroup }, - type: QueryTypes.SELECT, - } - ) - } else { - stats = await PerpMarketStats.findAll({ - order: [["time", "ASC"]], - where: { mangoGroup: { [Op.or]: [null, mangoGroup] } }, - }) - } + { + replacements: { mangoGroup }, + type: QueryTypes.SELECT, + } + ) + res.send(stats) } catch (e) { console.log("Error inserting data", e) } }) -app.get("/current", async (req, res) => { +app.get("/perp/funding_rate", async (req, res) => { try { - const stats = await PerpMarketStats.findAll({ limit: 3, order: [["time", "DESC"]] }) + const market = req.query.market as string + if (!market) { + throw new Error("Missing mangoGroup param") + } + + const stats = await sequelize.query( + `SELECT + "longFunding", + "shortFunding", + "openInterest", + "baseOraclePrice", + "time" + FROM perp_market_stats + WHERE time > NOW() - interval '1 hour' AND "name" = :market`, + { + replacements: { market }, + type: QueryTypes.SELECT, + } + ) + res.send(stats) } catch (e) { console.log("Error inserting data", e)