Skip to content

Commit

Permalink
update perp controller for new stats
Browse files Browse the repository at this point in the history
  • Loading branch information
slasher125 committed Jan 13, 2023
1 parent 0103b5f commit ede007e
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 19 deletions.
7 changes: 7 additions & 0 deletions migrations/1673621162936_perp-remove-not-null-constraint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* eslint-disable camelcase */

exports.shorthands = undefined;

exports.up = (pgm) => {};

exports.down = (pgm) => {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* eslint-disable camelcase */

exports.shorthands = undefined;

exports.up = (pgm) => {};

exports.down = (pgm) => {};
78 changes: 59 additions & 19 deletions src/controllers/perpController.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ const getPerp = async () => {
"fundingTimePrevious",
"openInterest",
"indexPrice",
"fundingRate7dAverage"
"fundingRate7dAverage",
"fundingRate7dSum",
"fundingRate30dAverage",
"fundingRate30dSum"
FROM
(
SELECT
Expand All @@ -38,36 +41,73 @@ const getPerp = async () => {
) AS main
JOIN (
SELECT
marketplace,
market,
round(avg("fundingRatePrevious"), 5) AS "fundingRate7dAverage"
weeklyStats.marketplace,
weeklyStats.market,
"fundingRate7dAverage",
"fundingRate7dSum",
"fundingRate30dAverage",
"fundingRate30dSum"
FROM
(
SELECT
DISTINCT ON (marketplace, market, "fundingTimePrevious") *
marketplace,
market,
round(avg("fundingRatePrevious"), 10) AS "fundingRate7dAverage",
round(sum("fundingRatePrevious"), 10) AS "fundingRate7dSum"
FROM
$<perpTable:name>
WHERE
"fundingTimePrevious" IS NOT NULL
AND timestamp >= NOW() - INTERVAL '$<fundingRate7dAverageAge> DAY'
ORDER BY
(
SELECT
DISTINCT ON (marketplace, market, "fundingTimePrevious") *
FROM
$<perpTable:name>
WHERE
"fundingTimePrevious" IS NOT NULL
AND timestamp >= NOW() - INTERVAL '$<ageWeeklyStats> DAY'
ORDER BY
marketplace,
market,
"fundingTimePrevious" DESC
) AS main
GROUP BY
marketplace,
market
) AS weeklyStats
JOIN (
SELECT
marketplace,
market,
"fundingTimePrevious" DESC
) AS main
GROUP BY
marketplace,
market
) AS avg7d ON avg7d.marketplace = main.marketplace
AND avg7d.market = main.market
round(avg("fundingRatePrevious"), 10) AS "fundingRate30dAverage",
round(sum("fundingRatePrevious"), 10) AS "fundingRate30dSum"
FROM
(
SELECT
DISTINCT ON (marketplace, market, "fundingTimePrevious") *
FROM
$<perpTable:name>
WHERE
"fundingTimePrevious" IS NOT NULL
AND timestamp >= NOW() - INTERVAL '$<ageMonthlyStats> DAY'
ORDER BY
marketplace,
market,
"fundingTimePrevious" DESC
) AS main
GROUP BY
marketplace,
market
) AS monthlyStats ON weeklyStats.marketplace = monthlyStats.marketplace
AND weeklyStats.market = monthlyStats.market
) AS stats ON stats.marketplace = main.marketplace
AND stats.market = main.market
`,
{ compress: true }
);

const response = await conn.query(query, {
perpTable: tableName,
age: 3,
fundingRate7dAverageAge: 7,
age: 3, // last 3 hours
ageWeeklyStats: 7,
ageMonthlyStats: 30,
});

if (!response) {
Expand Down

0 comments on commit ede007e

Please sign in to comment.