diff --git a/backend/src/routes/livepeer.js b/backend/src/routes/livepeer.js index 7a3ca69..f88364e 100644 --- a/backend/src/routes/livepeer.js +++ b/backend/src/routes/livepeer.js @@ -39,6 +39,8 @@ 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'); @@ -270,7 +272,7 @@ const updateMonthlyReward = async function (blockTime, amount) { var dateObj = new Date(0); dateObj.setUTCSeconds(blockTime); // Determine year, month and name - const thisMonth = dateObj.getMonth() ; + const thisMonth = dateObj.getMonth(); const thisYear = dateObj.getFullYear(); console.log("Updating monthly Reward stats for " + thisYear + "-" + thisMonth); if (!CONF_DISABLE_DB) { @@ -303,7 +305,7 @@ const updateMonthlyClaim = async function (blockTime, fees, rewards) { var dateObj = new Date(0); dateObj.setUTCSeconds(blockTime); // Determine year, month and name - const thisMonth = dateObj.getMonth() ; + const thisMonth = dateObj.getMonth(); const thisYear = dateObj.getFullYear(); console.log("Updating monthly Claim stats for " + thisYear + "-" + thisMonth); if (!CONF_DISABLE_DB) { @@ -338,7 +340,7 @@ const updateMonthlyWithdrawStake = async function (blockTime, amount) { var dateObj = new Date(0); dateObj.setUTCSeconds(blockTime); // Determine year, month and name - const thisMonth = dateObj.getMonth() ; + const thisMonth = dateObj.getMonth(); const thisYear = dateObj.getFullYear(); console.log("Updating monthly WithdrawStake stats for " + thisYear + "-" + thisMonth); if (!CONF_DISABLE_DB) { @@ -371,7 +373,7 @@ const updateMonthlyWithdrawFees = async function (blockTime, amount) { var dateObj = new Date(0); dateObj.setUTCSeconds(blockTime); // Determine year, month and name - const thisMonth = dateObj.getMonth() ; + const thisMonth = dateObj.getMonth(); const thisYear = dateObj.getFullYear(); console.log("Updating monthly WithdrawFees stats for " + thisYear + "-" + thisMonth); if (!CONF_DISABLE_DB) { @@ -404,7 +406,7 @@ const updateMonthlyNewDelegator = async function (blockTime, amount) { var dateObj = new Date(0); dateObj.setUTCSeconds(blockTime); // Determine year, month and name - const thisMonth = dateObj.getMonth() ; + const thisMonth = dateObj.getMonth(); const thisYear = dateObj.getFullYear(); console.log("Updating monthly new Delegator stats for " + thisYear + "-" + thisMonth); if (!CONF_DISABLE_DB) { @@ -437,7 +439,7 @@ const updateMonthlyUnbond = async function (blockTime, amount) { var dateObj = new Date(0); dateObj.setUTCSeconds(blockTime); // Determine year, month and name - const thisMonth = dateObj.getMonth() ; + const thisMonth = dateObj.getMonth(); const thisYear = dateObj.getFullYear(); console.log("Updating monthly new Unbond stats for " + thisYear + "-" + thisMonth); if (!CONF_DISABLE_DB) { @@ -501,7 +503,7 @@ const updateMonthlyActivation = async function (blockTime, amount) { var dateObj = new Date(0); dateObj.setUTCSeconds(blockTime); // Determine year, month and name - const thisMonth = dateObj.getMonth() ; + const thisMonth = dateObj.getMonth(); const thisYear = dateObj.getFullYear(); console.log("Updating monthly new activation stats for " + thisYear + "-" + thisMonth); if (!CONF_DISABLE_DB) { @@ -534,7 +536,7 @@ const updateMonthlyMoveStake = async function (blockTime, amount) { var dateObj = new Date(0); dateObj.setUTCSeconds(blockTime); // Determine year, month and name - const thisMonth = dateObj.getMonth() ; + const thisMonth = dateObj.getMonth(); const thisYear = dateObj.getFullYear(); console.log("Updating monthly stake movement stats for " + thisYear + "-" + thisMonth); if (!CONF_DISABLE_DB) { @@ -567,7 +569,7 @@ const updateMonthlyTicketReceived = async function (blockTime, amount, from, to) var dateObj = new Date(0); dateObj.setUTCSeconds(blockTime); // Determine year, month and name - const thisMonth = dateObj.getMonth() ; + const thisMonth = dateObj.getMonth(); const thisYear = dateObj.getFullYear(); console.log("Updating monthly ticket received stats for " + thisYear + "-" + thisMonth); if (!CONF_DISABLE_DB) { @@ -685,7 +687,7 @@ const updateMonthlyTicketRedeemed = async function (blockTime, amount, address) var dateObj = new Date(0); dateObj.setUTCSeconds(blockTime); // Determine year, month and name - const thisMonth = dateObj.getMonth() ; + const thisMonth = dateObj.getMonth(); const thisYear = dateObj.getFullYear(); console.log("Updating monthly ticket redeemed stats for " + thisYear + "-" + thisMonth); if (!CONF_DISABLE_DB) { @@ -1087,7 +1089,11 @@ const syncEvents = function (toBlock) { let size = events.length; console.log("Parsing " + size + " events"); if (!size) { - lastBlockEvents = toBlock; + if (toBlock == 'latest') { + lastBlockEvents = latestBlockInChain; + } else { + lastBlockEvents = toBlock; + } } for (const event of events) { if (event.blockNumber > lastBlockEvents) { @@ -1141,7 +1147,11 @@ const syncTickets = function (toBlock) { let size = events.length; console.log("Parsing " + size + " tickets"); if (!size) { - lastBlockTickets = toBlock; + if (toBlock == 'latest') { + lastBlockTickets = latestBlockInChain; + } else { + lastBlockTickets = toBlock; + } } for (const event of events) { if (event.blockNumber > lastBlockTickets) { @@ -1247,6 +1257,8 @@ const initSync = async function () { address: 1, fees: 1, rewards: 1, + startRound: 1, + endRound: 1, transactionHash: 1, blockNumber: 1, blockTime: 1, @@ -1373,6 +1385,7 @@ const handleSync = async function () { console.log("Latest L2 Eth block changed to " + latestBlockInChain); } else { // If there are no new blocks, wait for 10 seconds before retrying + console.log("No new blocks. Sleeping for 10 seconds..."); await sleep(10000); continue; } @@ -1381,15 +1394,15 @@ const handleSync = async function () { // Batch requests when sync is large, mark if we are going to reach latestBlockInChain in this round let getFinalTickets = false; let toTickets = 'latest'; - if (latestBlock - lastBlockTickets > 100000) { - toTickets = lastBlockTickets + 100000; + if (latestBlock - lastBlockTickets > 1000000) { + toTickets = lastBlockTickets + 1000000; } else { getFinalTickets = true; } let getFinalEvents = false; let toEvents = 'latest'; - if (latestBlock - lastBlockEvents > 100000) { - toEvents = lastBlockEvents + 100000; + if (latestBlock - lastBlockEvents > 1000000) { + toEvents = lastBlockEvents + 1000000; } else { getFinalEvents = true; } @@ -1404,8 +1417,8 @@ const handleSync = async function () { } else if (!getFinalEvents) { // Start next batch for events toEvents = 'latest'; - if (latestBlock - lastBlockEvents > 100000) { - toEvents = lastBlockEvents + 100000; + if (latestBlock - lastBlockEvents > 1000000) { + toEvents = lastBlockEvents + 1000000; } else { getFinalEvents = true; } @@ -1416,8 +1429,8 @@ const handleSync = async function () { } else if (!getFinalTickets) { // Start next batch for tickets toTickets = 'latest'; - if (latestBlock - lastBlockTickets > 100000) { - toTickets = lastBlockTickets + 100000; + if (latestBlock - lastBlockTickets > 1000000) { + toTickets = lastBlockTickets + 1000000; } else { getFinalTickets = true; } @@ -1635,7 +1648,7 @@ const mutateNewCommissionRates = async function (address, feeCommission, rewardC feeCommission = (100 - (feeCommission / 10000)).toFixed(2); // Create new data point if (!CONF_DISABLE_DB) { - const dbObj = new Event({ + const dbObj = new CommissionDataPoint({ address: address, feeCommission: feeCommission, rewardCommission: rewardCommission, @@ -1698,7 +1711,7 @@ const mutateNewGlobalStake = async function (address, globalStake) { const thisYear = dateObj.getFullYear(); // Create new data point if (!CONF_DISABLE_DB) { - const dbObj = new Event({ + const dbObj = new TotalStakeDataPoint({ address: address, totalStake: globalStake, timestamp: now @@ -2486,12 +2499,10 @@ const mutateTestScoresToDB = async function (scoreObj, month, year) { } // Immediately mutate Monthly statistics object const doc = await MonthlyStat.findOneAndUpdate({ - year: thisYear, - month: thisMonth + year: year, + month: month }, { - $set: { - testScores: scoreObj - } + testScores: scoreObj }, { upsert: true, new: true, @@ -2525,6 +2536,7 @@ const getScoreAtMonthYear = async function (month, year) { const endTime = parseInt(Date.parse(endString) / 1000) // Else get it and cache it const url = "https://leaderboard-serverless.vercel.app/api/aggregated_stats?since=" + startTime + "&until=" + endTime; + console.log("Getting new Orchestrator scores for " + year + "-" + month + " @ " + url); await https.get(url, (res) => { let body = ""; res.on("data", (chunk) => { diff --git a/src/actions/livepeer.js b/src/actions/livepeer.js index c39cb13..964939a 100644 --- a/src/actions/livepeer.js +++ b/src/actions/livepeer.js @@ -176,7 +176,7 @@ export const getOrchestratorInfo = (orchAddr) => async dispatch => { }; export const setCachedOrch = (orchObj) => async dispatch => { - return dispatch(setOrchestratorInfo(orchObj)); + return dispatch(setOrchestratorInfo(orchObj)); }; export const clearOrchestrator = () => async dispatch => { @@ -219,8 +219,12 @@ export const getThreeBoxInfo = async (addr) => { const response = apiUtil.getThreeBox(addr); }; -export const getOrchestratorScores = (year, month) => async dispatch => { - const response = apiUtil.getOrchestratorScores(year, month); +export const getOrchestratorScores = async (year, month) => { + const response = await apiUtil.getOrchestratorScores(year, month); + const data = await response.json(); + if (response.ok) { + return data; + } }; export const getAllOrchScores = () => async dispatch => { diff --git a/src/components/WinnerMonth.js b/src/components/WinnerMonth.js index d0ebc05..4105c38 100644 --- a/src/components/WinnerMonth.js +++ b/src/components/WinnerMonth.js @@ -1,5 +1,5 @@ import React, { useState, useEffect } from 'react' -import { useSelector, useDispatch } from 'react-redux'; +import { useSelector } from 'react-redux'; import { VictoryPie } from 'victory'; import Winner from '../components/WinnerStat'; import { @@ -8,29 +8,18 @@ import { const WinnerMonth = (obj) => { const livepeer = useSelector((state) => state.livepeerstate); - const dispatch = useDispatch(); const [thisScores, setThisScores] = useState(null); - useEffect(() => { - const now = new Date().getTime(); - let wasInCache = false; - // See if it is cached - for (const thisScore of obj.data.testScores) { - if (thisScore.year === obj.data.year && thisScore.month === obj.data.month) { - // Check timeout - if (now - thisScore.timestamp < 360000) { - wasInCache = true; - } - if (!thisScores) { - setThisScores(thisScore); - } + useEffect(async () => { + if (!obj.data.testScores) { + const freshScore = await getOrchestratorScores(obj.data.year, obj.data.month); + if (freshScore) { + setThisScores(freshScore); } + } else { + setThisScores(obj.data.testScores); } - if (!wasInCache) { - dispatch(getOrchestratorScores(obj.year, obj.month)); - dispatch(getAllMonthlyStats()); - } - }, []); + }, [obj.data.testScores]); const getName = (address) => { let thisDomain = null; @@ -70,12 +59,15 @@ const WinnerMonth = (obj) => { // Show all orchs (if latestTotalStake exists) or show only those in winningTicketsReceived let orchList; + let ticketList = obj.data.winningTicketsReceived || []; + let commissionList = obj.data.latestCommission || []; + let stakeList = obj.data.latestTotalStake || []; // Pies for stake overview, if have stake data for that month saved let stakeObj; let totalStakeSum = 0; if (obj.data.latestTotalStake && obj.data.latestTotalStake.length) { - orchList = obj.data.latestTotalStake; + orchList = [...obj.data.latestTotalStake]; let pieList = []; let otherSum = 0; let ticketIdx = obj.data.latestTotalStake.length - 1; @@ -106,39 +98,63 @@ const WinnerMonth = (obj) => { stakeObj =
{obj.data.reactivationCount} Orchestrator reactivated
+🔌 {obj.data.reactivationCount} Orchestrators reactivated
{obj.data.activationCount} Orchestrator activated with an initial stake of {obj.data.activationInitialSum} LPT
+🔧 {obj.data.activationCount} Orchestrator joined with an initial stake of {obj.data.activationInitialSum.toFixed(2)} LPT
{obj.data.unbondCount} delegators unbonded {obj.data.unbondStakeSum} LPT
+📉 {obj.data.unbondCount} delegators unbonded {obj.data.unbondStakeSum.toFixed(2)} LPT
{obj.data.rewardCount} reward calls were made worth {obj.data.rewardAmountSum} LPT
+⌛ {obj.data.rewardCount} reward calls were made worth {obj.data.rewardAmountSum.toFixed(2)} LPT
{obj.data.claimCount} reward claims were made worth {obj.data.claimRewardSum} LPT and {obj.data.claimFeeSum} ETH
+💸 {obj.data.claimRewardSum.toFixed(2)} LPT and {obj.data.claimFeeSum.toFixed(2)} ETH rewards were claimed by {obj.data.claimCount} delegators
{obj.data.withdrawStakeCount} withdraw stake calls were made worth {obj.data.withdrawStakeAmountSum} LPT
+🏦 {obj.data.withdrawStakeAmountSum.toFixed(2)} LPT worth of staking rewards were withdrawn by {obj.data.withdrawStakeCount} delegators
{obj.data.withdrawFeesCount} withdraw fees calls were made worth {obj.data.withdrawFeesAmountSum} ETH
+🏦 {obj.data.withdrawFeesAmountSum.toFixed(2)} ETH worth of transcoding fees were withdrawn by {obj.data.withdrawFeesCount} delegators
{obj.data.bondCount} delegators delegated their first stake worth {obj.data.bondStakeSum} LPT
+📈 {obj.data.bondCount} accounts delegated for the first time for a total of {obj.data.bondStakeSum.toFixed(2)} LPT
Stake got moved around {obj.data.moveStakeCount} times worth {obj.data.moveStakeSum} LPT
+🔄 {obj.data.moveStakeSum.toFixed(2)} LPT stake got moved around in {obj.data.moveStakeCount} transactions
{obj.data.winningTicketsReceivedCount} winning tickets were sent out worth {obj.data.winningTicketsReceivedSum} ETH
+🎫 {obj.data.winningTicketsReceivedCount} winning tickets were sent out worth {obj.data.winningTicketsReceivedSum.toFixed(2)} ETH
{obj.data.winningTicketsRedeemedCount} winning tickets were redeemed worth {obj.data.winningTicketsRedeemedSum} ETH
+🎟️ {obj.data.winningTicketsRedeemedCount} winning tickets were redeemed worth {obj.data.winningTicketsRedeemedSum.toFixed(2)} ETH