Skip to content
This repository has been archived by the owner on Jul 22, 2020. It is now read-only.

Commit

Permalink
feat: move Lamport -> Sol calculation into API, use new ratio
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnygleason committed Dec 23, 2019
1 parent 7e08455 commit 8eba3de
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 31 deletions.
9 changes: 9 additions & 0 deletions api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -627,6 +628,7 @@ async function getClusterInfo() {
},
0,
);
let totalStakedSol = lamportsToSol(totalStaked);

const network = {};

Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -760,6 +768,7 @@ async function getClusterInfo() {
inflation,
networkInflationRate,
totalStaked,
totalStakedSol,
network,
clusterNodes,
identities,
Expand Down
8 changes: 5 additions & 3 deletions api/util.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions api/views/accounts/detail.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import _ from 'lodash';
import Base58 from 'base-58';

import {lamportsToSol} from '../../util';
const b58e = Base58.encode;

/**
Expand All @@ -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(),
};

Expand Down
3 changes: 3 additions & 0 deletions api/views/programs/detail.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import _ from 'lodash';
import Base58 from 'base-58';

import {lamportsToSol} from '../../util';
const b58e = Base58.encode;

/**
Expand All @@ -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(),
};

Expand Down
8 changes: 4 additions & 4 deletions api/views/tourdesol/index.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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,
};
Expand All @@ -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);

Expand Down
5 changes: 2 additions & 3 deletions src/v2/components/Validators/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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.',
Expand Down
1 change: 0 additions & 1 deletion src/v2/constants.js
Original file line number Diff line number Diff line change
@@ -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;
8 changes: 1 addition & 7 deletions src/v2/stores/accounts/detail.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down
8 changes: 2 additions & 6 deletions src/v2/stores/nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
});

Expand All @@ -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;
};
Expand Down
8 changes: 1 addition & 7 deletions src/v2/stores/programs/detail.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 8eba3de

Please sign in to comment.