From 82f533d58291248f53a790fead04b5a3a8993ffe Mon Sep 17 00:00:00 2001 From: Marco van Dijk Date: Fri, 22 Apr 2022 01:38:14 +0200 Subject: [PATCH] some prework for graph view --- backend/src/routes/livepeer.js | 38 ++++++++++++++++++++++++-- src/actions/livepeer.js | 28 +++++++++++++++++++ src/pages/loadingScreen.js | 4 ++- src/pages/stats.js | 2 ++ src/reducers/livepeer/livepeerstate.js | 6 ++++ src/util/livepeer.js | 18 ++++++++++++ 6 files changed, 92 insertions(+), 4 deletions(-) diff --git a/backend/src/routes/livepeer.js b/backend/src/routes/livepeer.js index 55bb640..3d6f244 100644 --- a/backend/src/routes/livepeer.js +++ b/backend/src/routes/livepeer.js @@ -13,6 +13,9 @@ import UnbondEvent from "../models/UnbondEvent"; import UpdateEvent from "../models/UpdateEvent"; import WithdrawFeesEvent from "../models/WithdrawFeesEvent"; import WithdrawStakeEvent from "../models/WithdrawStakeEvent"; +import MonthlyStat from "../models/monthlyStat"; +import CommissionDataPoint from "../models/CommissionDataPoint"; +import TotalStakeDataPoint from "../models/TotalStakeDataPoint"; const apiRouter = express.Router(); import { @@ -38,9 +41,6 @@ const fs = require('fs'); // Used for the livepeer thegraph API import { request, gql } from 'graphql-request'; -import MonthlyStat from "../models/monthlyStat"; -import CommissionDataPoint from "../models/CommissionDataPoint"; -import TotalStakeDataPoint from "../models/TotalStakeDataPoint"; // Gets ETH, LPT and other coin info let CoinMarketCap = require('coinmarketcap-api'); @@ -173,6 +173,8 @@ let unbondEventCache = []; let stakeEventCache = []; let monthlyStatCache = []; +let commissionDataPointCache = []; +let totalStakeDataPoint = []; apiRouter.get("/getAllMonthlyStats", async (req, res) => { try { @@ -182,6 +184,22 @@ apiRouter.get("/getAllMonthlyStats", async (req, res) => { } }); +apiRouter.get("/getAllCommissions", async (req, res) => { + try { + res.send(commissionDataPointCache); + } catch (err) { + res.status(400).send(err); + } +}); + +apiRouter.get("/getAllTotalStakes", async (req, res) => { + try { + res.send(totalStakeDataPoint); + } catch (err) { + res.status(400).send(err); + } +}); + apiRouter.get("/getAllUpdateEvents", async (req, res) => { try { res.send(updateEventCache); @@ -1368,6 +1386,20 @@ const initSync = async function () { testScores: 1, _id: 0 }); + // Get all graph data points + commissionDataPointCache = await CommissionDataPoint.find({}, { + address: 1, + feeCommission: 1, + rewardCommission: 1, + timestamp: 1, + _id: 0 + }); + totalStakeDataPoint = await TotalStakeDataPoint.find({}, { + address: 1, + totalStake: 1, + timestamp: 1, + _id: 0 + }); } // Does the actual looping over last parsed block -> latest block in chain diff --git a/src/actions/livepeer.js b/src/actions/livepeer.js index 9477843..b5c0311 100644 --- a/src/actions/livepeer.js +++ b/src/actions/livepeer.js @@ -17,6 +17,8 @@ export const SET_ALL_ORCH_SCORES = "SET_ALL_ORCH_SCORES"; export const SET_ALL_ORCH_INFO = "SET_ALL_ORCH_INFO"; export const SET_ALL_DEL_INFO = "SET_ALL_DEL_INFO"; export const SET_ALL_MONTHLY_STATS = "SET_ALL_MONTHLY_STATS"; +export const SET_ALL_COMMISSIONS = "SET_ALL_COMMISSIONS"; +export const SET_ALL_TOTAL_STAKES = "SET_ALL_TOTAL_STAKES"; export const SET_ALL_UPDATE_EVENTS = "SET_ALL_UPDATE_EVENTS"; export const SET_ALL_REWARD_EVENTS = "SET_ALL_REWARD_EVENTS"; export const SET_ALL_CLAIM_EVENTS = "SET_ALL_CLAIM_EVENTS"; @@ -79,6 +81,14 @@ const setAllMonthlyStats = message => ({ type: SET_ALL_MONTHLY_STATS, message }); +const setAllCommissions = message => ({ + type: SET_ALL_COMMISSIONS, message +}); + +const setAllTotalStakes = message => ({ + type: SET_ALL_TOTAL_STAKES, message +}); + const setAllUpdateEvents = message => ({ type: SET_ALL_UPDATE_EVENTS, message }); @@ -257,6 +267,24 @@ export const getAllMonthlyStats = () => async dispatch => { return dispatch(receiveErrors(data)); }; +export const getAllCommissions = () => async dispatch => { + const response = await apiUtil.getAllCommissions(); + const data = await response.json(); + if (response.ok) { + return dispatch(setAllCommissions(data)); + } + return dispatch(receiveErrors(data)); +}; + +export const getAllTotalStakes = () => async dispatch => { + const response = await apiUtil.getAllTotalStakes(); + const data = await response.json(); + if (response.ok) { + return dispatch(setAllTotalStakes(data)); + } + return dispatch(receiveErrors(data)); +}; + export const getAllUpdateEvents = () => async dispatch => { const response = await apiUtil.getAllUpdateEvents(); const data = await response.json(); diff --git a/src/pages/loadingScreen.js b/src/pages/loadingScreen.js index 486f608..c9dfec9 100644 --- a/src/pages/loadingScreen.js +++ b/src/pages/loadingScreen.js @@ -9,7 +9,7 @@ import { getAllDelInfo, getAllMonthlyStats, getAllUpdateEvents, getAllRewardEvents, getAllClaimEvents, getAllWithdrawStakeEvents, getAllWithdrawFeesEvents, getAllTransferTicketEvents, getAllRedeemTicketEvents, getAllActivateEvents, - getAllUnbondEvents, getAllStakeEvents, + getAllUnbondEvents, getAllStakeEvents, getAllCommissions, getAllTotalStakes } from "../actions/livepeer"; import { login } from "../actions/session"; @@ -55,6 +55,8 @@ const Startup = (obj) => { dispatch(getAllDelInfo()); dispatch(getAllOrchScores()); dispatch(getAllMonthlyStats()); + dispatch(getAllCommissions()); + dispatch(getAllTotalStakes()); dispatch(getAllUpdateEvents()); dispatch(getAllRewardEvents()); dispatch(getAllClaimEvents()); diff --git a/src/pages/stats.js b/src/pages/stats.js index ea5ea62..fce1944 100644 --- a/src/pages/stats.js +++ b/src/pages/stats.js @@ -5,6 +5,7 @@ import { useSelector, useDispatch } from 'react-redux'; import { Accordion } from '@mantine/core'; import ScrollContainer from 'react-indiana-drag-scroll'; import WinnerMonth from '../components/WinnerMonth'; +// import Graphs from '../components/Graphs'; const Stats = (obj) => { const livepeer = useSelector((state) => state.livepeerstate); @@ -57,6 +58,7 @@ const Stats = (obj) => { content: { padding: 0, paddingTop: '1em', paddingBottom: '1em' }, contentInner: { padding: 0 }, }}> + {/* */} { livepeer.monthlyStats.slice(0).reverse().map(function (data, i) { let thisMonth = ""; diff --git a/src/reducers/livepeer/livepeerstate.js b/src/reducers/livepeer/livepeerstate.js index 5f8bb97..bc42e08 100644 --- a/src/reducers/livepeer/livepeerstate.js +++ b/src/reducers/livepeer/livepeerstate.js @@ -15,6 +15,8 @@ import { SET_ALL_DEL_INFO, CACHE_ORCHESTRATOR, SET_ALL_MONTHLY_STATS, + SET_ALL_COMMISSIONS, + SET_ALL_TOTAL_STAKES, SET_ALL_UPDATE_EVENTS, SET_ALL_REWARD_EVENTS, SET_ALL_CLAIM_EVENTS, @@ -109,6 +111,10 @@ export default (state = { return { ...state, delInfo: message }; case SET_ALL_MONTHLY_STATS: return { ...state, monthlyStats: message }; + case SET_ALL_COMMISSIONS: + return { ...state, allCommissions: message }; + case SET_ALL_TOTAL_STAKES: + return { ...state, allTotalStakes: message }; case SET_ALL_UPDATE_EVENTS: return { ...state, updateEvents: message }; case SET_ALL_REWARD_EVENTS: diff --git a/src/util/livepeer.js b/src/util/livepeer.js index 2e0e3d8..02925c9 100644 --- a/src/util/livepeer.js +++ b/src/util/livepeer.js @@ -154,6 +154,24 @@ export const getAllMonthlyStats = () => ( }) ); +export const getAllCommissions = () => ( + fetch("api/livepeer/getAllCommissions", { + method: "GET", + headers: { + "Content-Type": "application/json" + } + }) +); + +export const getAllTotalStakes = () => ( + fetch("api/livepeer/getAllTotalStakes", { + method: "GET", + headers: { + "Content-Type": "application/json" + } + }) +); + export const getAllUpdateEvents = () => ( fetch("api/livepeer/getAllUpdateEvents", { method: "GET",