Skip to content

Commit

Permalink
Add NanoSpeed API as first class to get the conf time on the home pag…
Browse files Browse the repository at this point in the history
…e to bypass innacurate confirmation_history RPC
  • Loading branch information
tombertrand committed Nov 6, 2023
1 parent b75cd87 commit 4397817
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
2 changes: 2 additions & 0 deletions server/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const REDIS_RICH_LIST = "RICH_LIST";
const PARTICIPANTS = "PARTICIPANTS";
const NANOTICKER_STATS = "NANOTICKER_STATS";
const NANOTPS_STATS = "NANOTPS_STATS";
const NANOSPEED_STATS = "NANOSPEED_STATS";
const NANOBROWSERQUEST_PLAYERS = "NANOBROWSERQUEST_PLAYERS";
const NANOBROWSERQUEST_LEADERBOARD = "NANOBROWSERQUEST_LEADERBOARD";
const YOUTUBE_PLAYLIST = "YOUTUBE_PLAYLIST";
Expand Down Expand Up @@ -112,6 +113,7 @@ module.exports = {
PARTICIPANTS,
NANOTICKER_STATS,
NANOTPS_STATS,
NANOSPEED_STATS,
NANOBROWSERQUEST_PLAYERS,
NANOBROWSERQUEST_LEADERBOARD,
YOUTUBE_PLAYLIST,
Expand Down
22 changes: 22 additions & 0 deletions server/cron/nanospeed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const cron = require("node-cron");
const fetch = require("node-fetch");
const { Sentry } = require("../sentry");
const { nodeCache } = require("../client/cache");
const { NANOSPEED_STATS } = require("../constants");

const doNanoSpeedStats = async () => {
try {
const res = await fetch("https://api.nanospeed.info/conf-times-box-global?period=1D");

const { q90: avgConfTimep90, median } = await res.json();

nodeCache.set(NANOSPEED_STATS, { avgConfTimep90, median });
} catch (err) {
Sentry.captureException(err);
}
};

// Every 15 smins
cron.schedule("*/15 * * * *", async () => {
doNanoSpeedStats();
});
4 changes: 4 additions & 0 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require("./cron/coingeckoStats");
require("./cron/btcTransactionFees");
require("./cron/nanotickerStats");
require("./cron/nanotpsStats");
require("./cron/nanospeed");
require("./cron/nanobrowserquestStats");
require("./cron/2minersStats");
require("./cron/youtubePlaylist");
Expand All @@ -37,6 +38,7 @@ const {
NANOBROWSERQUEST_PLAYERS,
NANOBROWSERQUEST_LEADERBOARD,
NANOTPS_STATS,
NANOSPEED_STATS,
} = require("./constants");
const {
getBtcTransactionFees,
Expand Down Expand Up @@ -145,6 +147,7 @@ app.get("/api/market-statistics", async (req, res) => {
const cachedConfirmations48h = nodeCache.get(TOTAL_CONFIRMATIONS_48H);
const cachedVolume48h = nodeCache.get(TOTAL_VOLUME_48H);
const nanotpsStats = nodeCache.get(NANOTPS_STATS);
const nanoSpeedStats = nodeCache.get(NANOSPEED_STATS);

const { btcTransactionFees24h, btcTransactionFees48h } = await getBtcTransactionFees();
const { marketStats, priceStats } = await getCoingeckoStats({
Expand All @@ -161,6 +164,7 @@ app.get("/api/market-statistics", async (req, res) => {
[BITCOIN_TOTAL_TRANSACTION_FEES_48H]: btcTransactionFees48h,
[BITCOIN_TOTAL_TRANSACTION_FEES_48H]: btcTransactionFees48h,
[NANOTPS_STATS]: nanotpsStats,
[NANOSPEED_STATS]: nanoSpeedStats,
...marketStats,
priceStats: {
...{ bitcoin: { usd: 0 } },
Expand Down
11 changes: 11 additions & 0 deletions src/api/contexts/MarketStatistics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const TOTAL_VOLUME_48H = "TOTAL_VOLUME_48H";
export const BITCOIN_TOTAL_TRANSACTION_FEES_24H = "BITCOIN_TOTAL_TRANSACTION_FEES_24H";
export const BITCOIN_TOTAL_TRANSACTION_FEES_48H = "BITCOIN_TOTAL_TRANSACTION_FEES_48H";
export const NANOTPS_STATS = "NANOTPS_STATS";
export const NANOSPEED_STATS = "NANOSPEED_STATS";

export interface Response {
[TOTAL_CONFIRMATIONS_24H]: number;
Expand All @@ -20,6 +21,7 @@ export interface Response {
[BITCOIN_TOTAL_TRANSACTION_FEES_24H]: number;
[BITCOIN_TOTAL_TRANSACTION_FEES_48H]: number;
[NANOTPS_STATS]: any;
[NANOTPS_STATS]: any;
marketCapRank: number;
marketCapRank24h: number;
marketCap: number;
Expand All @@ -30,6 +32,10 @@ export interface Response {
totalSupply: number;
circulatingSupply: number;
priceStats: any;
NANOSPEED_STATS: {
median: number | null;
avgConfTimep90: number | null;
};
}

export interface Context {
Expand All @@ -51,7 +57,12 @@ export const MarketStatisticsContext = React.createContext<Context>({
[TOTAL_VOLUME_48H]: 0,
[BITCOIN_TOTAL_TRANSACTION_FEES_24H]: 0,
[BITCOIN_TOTAL_TRANSACTION_FEES_48H]: 0,

NANOTPS_STATS: {},
[NANOSPEED_STATS]: {
median: null,
avgConfTimep90: null,
},
marketCapRank: 0,
marketCapRank24h: 0,
marketCap: 0,
Expand Down
7 changes: 5 additions & 2 deletions src/pages/Home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@ const HomePage = () => {
volume24h,
priceStats,
NANOTPS_STATS = {},
NANOSPEED_STATS: { median = null } = {},
} = marketStatistics;

const { send } = NANOTPS_STATS;

const { count } = React.useContext(BlockCountContext);
const { confirmation_stats: { average = 0 } = {} } = React.useContext(ConfirmationHistoryContext);

let medianConfTime = median || average;
const { representatives } = React.useContext(RepresentativesContext);
const {
nodeStatus: { ledgerSize },
Expand Down Expand Up @@ -148,10 +151,10 @@ const HomePage = () => {
}
/>
<LoadingStatistic
isLoading={!average}
isLoading={!medianConfTime}
title={t("pages.home.avgConfirmationTime")}
tooltip={t<string>("tooltips.avgConfirmationTime")}
value={new BigNumber(average).dividedBy(1000).toNumber()}
value={new BigNumber(medianConfTime).dividedBy(1000).toNumber()}
/>

{/* {!isSmallAndLower ? (
Expand Down

0 comments on commit 4397817

Please sign in to comment.