From 8eba3deb4f66a3c316fec14c9943a14e21ef64f5 Mon Sep 17 00:00:00 2001 From: Sunny Gleason Date: Mon, 23 Dec 2019 13:13:48 -0500 Subject: [PATCH] feat: move Lamport -> Sol calculation into API, use new ratio --- api/api.js | 9 +++++++++ api/util.js | 8 +++++--- api/views/accounts/detail.js | 3 +++ api/views/programs/detail.js | 3 +++ api/views/tourdesol/index.js | 8 ++++---- src/v2/components/Validators/index.jsx | 5 ++--- src/v2/constants.js | 1 - src/v2/stores/accounts/detail.js | 8 +------- src/v2/stores/nodes.js | 8 ++------ src/v2/stores/programs/detail.js | 8 +------- 10 files changed, 30 insertions(+), 31 deletions(-) diff --git a/api/api.js b/api/api.js index a0793f13..4b51dc10 100644 --- a/api/api.js +++ b/api/api.js @@ -26,6 +26,7 @@ import {FriendlyGet} from './friendlyGet'; import config from './config'; import {addNetworkExplorerRoutes} from './network-explorer'; import {FULLNODE_URL} from './fullnode-url'; +import {lamportsToSol} from './util'; const GLOBAL_STATS_BROADCAST_INTERVAL_MS = 2000; const CLUSTER_INFO_BROADCAST_INTERVAL_MS = 5000; @@ -627,6 +628,7 @@ async function getClusterInfo() { }, 0, ); + let totalStakedSol = lamportsToSol(totalStaked); const network = {}; @@ -699,6 +701,12 @@ async function getClusterInfo() { _.find(voteAccounts.delinquent, x => x.nodePubkey === nodePubkey); node.activatedStake = node.voteStatus && node.voteStatus.activatedStake; node.commission = node.voteStatus && node.voteStatus.commission; + + node.stakedSol = lamportsToSol(node.activatedStake).toFixed(8); + node.stakedSolPercent = (100 * (node.activatedStake / totalStaked)).toFixed( + 3, + ); + node.calcCommission = (100 * (node.commission / 0xff)).toFixed(3); } for (const node of Object.keys(network).sort()) { @@ -760,6 +768,7 @@ async function getClusterInfo() { inflation, networkInflationRate, totalStaked, + totalStakedSol, network, clusterNodes, identities, diff --git a/api/util.js b/api/util.js index aa3cee76..690c33cc 100644 --- a/api/util.js +++ b/api/util.js @@ -1,13 +1,15 @@ -import {Transaction} from '@solana/web3.js'; +import {SOL_LAMPORTS, Transaction} from '@solana/web3.js'; import _ from 'lodash'; import Base58 from 'base-58'; const b58e = Base58.encode; -export const LAMPORT_SOL_RATIO = 0.0000000000582; - const DEFAULT_CUMULATIVE_UPTIME_EPOCHS = 64; const TDS_MAGIC_EPOCH = 10; +export function lamportsToSol(lamports) { + return ((lamports || 0.0) * 1.0) / SOL_LAMPORTS; +} + export function calculateUptimeValues(epochInfo, epochSchedule, uptimeValues) { const {epoch} = epochInfo; const { diff --git a/api/views/accounts/detail.js b/api/views/accounts/detail.js index 72194fd9..a49f2877 100644 --- a/api/views/accounts/detail.js +++ b/api/views/accounts/detail.js @@ -1,5 +1,7 @@ import _ from 'lodash'; import Base58 from 'base-58'; + +import {lamportsToSol} from '../../util'; const b58e = Base58.encode; /** @@ -25,6 +27,7 @@ export class AccountDetailView { data: rawData.accountInfo.data.toString(), executable: rawData.accountInfo.executable, lamports: rawData.accountInfo.lamports, + balance: lamportsToSol(rawData.accountInfo.lamports).toFixed(8), owner: rawData.accountInfo.owner.toBase58(), }; diff --git a/api/views/programs/detail.js b/api/views/programs/detail.js index 03a8820e..5c1aa34f 100644 --- a/api/views/programs/detail.js +++ b/api/views/programs/detail.js @@ -1,5 +1,7 @@ import _ from 'lodash'; import Base58 from 'base-58'; + +import {lamportsToSol} from '../../util'; const b58e = Base58.encode; /** @@ -25,6 +27,7 @@ export class ProgramDetailView { data: rawData.accountInfo.data.toString(), executable: rawData.accountInfo.executable, lamports: rawData.accountInfo.lamports, + balance: lamportsToSol(rawData.accountInfo.lamports).toFixed(8), owner: rawData.accountInfo.owner.toBase58(), }; diff --git a/api/views/tourdesol/index.js b/api/views/tourdesol/index.js index ed95207b..78a66cc4 100644 --- a/api/views/tourdesol/index.js +++ b/api/views/tourdesol/index.js @@ -1,6 +1,6 @@ import {filter, reduce, orderBy} from 'lodash/fp'; -import {calculateUptimeValues, LAMPORT_SOL_RATIO} from '../../util'; +import {calculateUptimeValues, lamportsToSol} from '../../util'; const SLOTS_PER_DAY = (1.0 * 24 * 60 * 60) / 0.8; const TDS_DEFAULT_STAGE_LENGTH_BLOCKS = SLOTS_PER_DAY * 5.0; @@ -81,8 +81,8 @@ export class TourDeSolIndexView { daysLeftInStage, stageDurationBlocks: currentStage && currentStage.duration, networkInflationRate: clusterInfo && clusterInfo.networkInflationRate, - totalSupply: clusterInfo && clusterInfo.supply * LAMPORT_SOL_RATIO, - totalStaked: clusterInfo && clusterInfo.totalStaked * LAMPORT_SOL_RATIO, + totalSupply: lamportsToSol(clusterInfo && clusterInfo.supply), + totalStaked: lamportsToSol(clusterInfo && clusterInfo.totalStaked), activeValidators: activeValidatorsRaw && activeValidatorsRaw.length, inactiveValidators: inactiveValidatorsRaw && inactiveValidatorsRaw.length, }; @@ -99,7 +99,7 @@ export class TourDeSolIndexView { const slot = x.currentSlot; const name = x.identity && x.identity.name; const avatarUrl = x.identity && x.identity.avatarUrl; - const activatedStake = x.activatedStake * LAMPORT_SOL_RATIO; + const activatedStake = lamportsToSol(x.activatedStake); const activatedStakePercent = clusterInfo && 100.0 * (x.activatedStake / clusterInfo.totalStaked); diff --git a/src/v2/components/Validators/index.jsx b/src/v2/components/Validators/index.jsx index a54f1b53..061b24b7 100644 --- a/src/v2/components/Validators/index.jsx +++ b/src/v2/components/Validators/index.jsx @@ -15,11 +15,10 @@ import HelpLink from 'v2/components/HelpLink'; import ValidatorsTable from './Table'; import useStyles from './styles'; -import {LAMPORT_SOL_RATIO} from '../../constants'; const Validators = () => { const classes = useStyles(); - const {supply, validators, fetchClusterInfo, totalStaked} = NodesStore; + const {supply, validators, fetchClusterInfo, totalStakedSol} = NodesStore; const {isLoading} = Socket; useEffect(() => { fetchClusterInfo(); @@ -36,7 +35,7 @@ const Validators = () => { }, { title: 'Staked SOL', - value: (totalStaked * LAMPORT_SOL_RATIO).toFixed(8), + value: totalStakedSol.toFixed(8), changes: '', period: 'since yesterday', helpText: 'The total number of SOL staked to validators and activated.', diff --git a/src/v2/constants.js b/src/v2/constants.js index f3b6d0bb..4d696aa6 100644 --- a/src/v2/constants.js +++ b/src/v2/constants.js @@ -1,4 +1,3 @@ export const MIN_TERM_LEN = 3; -export const LAMPORT_SOL_RATIO = 0.0000000000582; export const TDS_ACTIVE_STAGE = parseInt(process.env.TDS_ACTIVE_STAGE || '0'); export const CONNECTION_TIMEOUT = 10000; diff --git a/src/v2/stores/accounts/detail.js b/src/v2/stores/accounts/detail.js index d0f936a9..ea4cc129 100644 --- a/src/v2/stores/accounts/detail.js +++ b/src/v2/stores/accounts/detail.js @@ -1,11 +1,5 @@ import {action, flow, observable, decorate} from 'mobx'; import {apiGetAccountDetail} from 'v2/api/accounts'; -import {LAMPORT_SOL_RATIO} from 'v2/constants'; - -const extendAccountInfo = (account = {}) => ({ - ...account, - balance: ((account.lamports || 0) * LAMPORT_SOL_RATIO).toFixed(8), -}); class Store { isLoading = true; @@ -22,7 +16,7 @@ class Store { const res = yield apiGetAccountDetail({accountId}); const {accountInfo, programAccounts, timestamp} = res.data; this.accountView = res.data; - this.accountInfo = extendAccountInfo(accountInfo); + this.accountInfo = accountInfo; this.programAccounts.replace(programAccounts); this.timestamp = timestamp; this.setLoading(false); diff --git a/src/v2/stores/nodes.js b/src/v2/stores/nodes.js index ec6f2ccd..4a80d5ce 100644 --- a/src/v2/stores/nodes.js +++ b/src/v2/stores/nodes.js @@ -3,13 +3,8 @@ import {action, computed, decorate, observable, flow} from 'mobx'; import * as API from 'v2/api/stats'; import getUptime from 'v2/utils/getUptime'; -import {LAMPORT_SOL_RATIO} from '../constants'; - -const addNetworkSolInfo = totalStaked => node => ({ +const addNetworkSolInfo = () => node => ({ ...node, - stakedSol: (node.activatedStake * LAMPORT_SOL_RATIO).toFixed(8), - stakedSolPercent: (100 * (node.activatedStake / totalStaked)).toFixed(3), - calcCommission: (100 * (node.commission / 0xff)).toFixed(3), calcUptime: getUptime(node.uptime), }); @@ -24,6 +19,7 @@ class Store { ? map(addNetworkSolInfo(data.totalStaked))(data.network) : []; this.totalStaked = data.totalStaked; + this.totalStakedSol = data.totalStakedSol; this.supply = data.supply; this.networkInflationRate = data.networkInflationRate; }; diff --git a/src/v2/stores/programs/detail.js b/src/v2/stores/programs/detail.js index 9a5af1a7..6b3255aa 100644 --- a/src/v2/stores/programs/detail.js +++ b/src/v2/stores/programs/detail.js @@ -1,11 +1,5 @@ import {action, flow, observable, decorate} from 'mobx'; import {apiGetProgramDetail} from 'v2/api/programs'; -import {LAMPORT_SOL_RATIO} from 'v2/constants'; - -const extendAccountInfo = (account = {}) => ({ - ...account, - balance: ((account.lamports || 0) * LAMPORT_SOL_RATIO).toFixed(8), -}); class Store { isLoading = true; @@ -23,7 +17,7 @@ class Store { const {accountInfo, programAccounts, timestamp} = res.data; this.programView = res.data; - this.accountInfo = extendAccountInfo(accountInfo); + this.accountInfo = accountInfo; this.programAccounts.replace(programAccounts); this.timestamp = timestamp; this.setLoading(false);