From 2c537b0f4c49456859dd28f50765e99233ae9d2a Mon Sep 17 00:00:00 2001 From: Marco van Dijk Date: Thu, 3 Mar 2022 00:54:38 +0100 Subject: [PATCH] Added endpoint to O subgraph --- backend/package.json | 1 + backend/src/routes/livepeer.js | 68 +++++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/backend/package.json b/backend/package.json index 7ba6eb3..70690fe 100644 --- a/backend/package.json +++ b/backend/package.json @@ -22,6 +22,7 @@ "esm": "^3.2.20", "express": "^4.17.1", "express-session": "^1.17.0", + "graphql-request": "^4.0.0", "install": "^0.13.0", "joi": "^14.3.1", "mongoose": "^5.12.3", diff --git a/backend/src/routes/livepeer.js b/backend/src/routes/livepeer.js index 9868d17..5155a9f 100644 --- a/backend/src/routes/livepeer.js +++ b/backend/src/routes/livepeer.js @@ -5,6 +5,8 @@ const apiRouter = express.Router(); import { API_CMC, API_L1_HTTP, API_L2_HTTP, API_L2_WS } from "../config"; +const https = require('https'); +import { request, gql } from 'graphql-request'; // Get ETH price & LPT coin prices const CoinMarketCap = require('coinmarketcap-api'); @@ -42,6 +44,54 @@ let claimTicketCostL2 = 0; let withdrawFeeCostL1 = 0; let withdrawFeeCostL2 = 0; +// Update info from thegraph every 5 minutes +const timeoutTheGraph = 300000; +let theGraphGet = 0; +// Address of O info we are want to display +const orchQuery = gql` + { + transcoders(where: {id: "0x847791cbf03be716a7fe9dc8c9affe17bd49ae5e"}) { + activationRound + deactivationRound + active + lastRewardRound { + id + length + startBlock + endBlock + mintableTokens + volumeETH + volumeUSD + totalActiveStake + totalSupply + participationRate + movedStake + newStake + } + rewardCut + feeShare + pricePerSegment + pendingPricePerSegment + pendingFeeShare + pendingRewardCut + totalStake + totalVolumeETH + totalVolumeUSD + serviceURI + delegators { + id + bondedAmount + startRound + } + delegator { + id + bondedAmount + startRound + } + } + } + `; +let orchestratorCache = {}; // Listen to smart contract emitters. Resync with DB every 5 minutes const timeoutEvents = 300000; @@ -221,8 +271,9 @@ apiRouter.get("/getEvents", async (req, res) => { transactionUrl: 1, name: 1, data: 1, - _id: 0}); - eventsGet = now; + _id: 0 + }); + eventsGet = now; } res.send(eventsCache); } catch (err) { @@ -230,4 +281,17 @@ apiRouter.get("/getEvents", async (req, res) => { } }); +apiRouter.get("/getOrchestrator", async (req, res) => { + try { + const now = new Date().getTime(); + // Update cmc once their data has expired + if (now - theGraphGet > timeoutTheGraph) { + orchestratorCache = JSON.stringify(await request("https://api.thegraph.com/subgraphs/name/livepeer/arbitrum-one", orchQuery)); + } + res.send(orchestratorCache); + } catch (err) { + res.status(400).send(err); + } +}); + export default apiRouter; \ No newline at end of file