Skip to content
This repository has been archived by the owner on Sep 26, 2024. It is now read-only.

Commit

Permalink
feat(fronted): Add fee and delegators to validators
Browse files Browse the repository at this point in the history
  • Loading branch information
shelegdmitriy committed May 5, 2021
1 parent 256eb68 commit 4a3e2a4
Show file tree
Hide file tree
Showing 23 changed files with 634 additions and 538 deletions.
14 changes: 7 additions & 7 deletions backend/src/db-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const getSyncedGenesis = async (options) => {
const queryGenesisAccountCount = async () => {
return await querySingleRow(
[
`SELECT
`SELECT
COUNT(*)
FROM accounts
WHERE created_by_receipt_id IS NULL`,
Expand Down Expand Up @@ -336,7 +336,7 @@ const queryDeletedAccountsCountAggregatedByDate = async () => {
DATE_TRUNC('day', TO_TIMESTAMP(receipts.included_in_block_timestamp / 1000000000)) AS date,
COUNT(deleted_by_receipt_id) AS deleted_accounts_count_by_date
FROM accounts
JOIN receipts ON receipts.receipt_id = accounts.deleted_by_receipt_id
JOIN receipts ON receipts.receipt_id = accounts.deleted_by_receipt_id
WHERE included_in_block_timestamp < ((CAST(EXTRACT(EPOCH FROM NOW()) AS bigint) / (60 * 60 * 24)) * 60 * 60 * 24 * 1000 * 1000 * 1000)
GROUP BY date
ORDER BY date`,
Expand Down Expand Up @@ -532,13 +532,13 @@ const queryPartnerUniqueUserAmount = async () => {
const queryBridgeTokenHolders = async (bridgeTokenContractId) => {
return await queryRows(
[
`SELECT
`SELECT
DISTINCT transactions.signer_account_id AS holder
FROM receipts
JOIN action_receipt_actions ON receipts.receipt_id = action_receipt_actions.receipt_id
JOIN transactions ON transactions.transaction_hash = receipts.originated_from_transaction_hash
FROM receipts
JOIN action_receipt_actions ON receipts.receipt_id = action_receipt_actions.receipt_id
JOIN transactions ON transactions.transaction_hash = receipts.originated_from_transaction_hash
WHERE receipts.receiver_account_id = :bridge_token_contract_id
AND action_receipt_actions.action_kind = 'FUNCTION_CALL'
AND action_receipt_actions.action_kind = 'FUNCTION_CALL'
AND action_receipt_actions.args->> 'method_name' = 'mint'`,
{ bridge_token_contract_id: bridgeTokenContractId },
],
Expand Down
34 changes: 32 additions & 2 deletions backend/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ const {
} = require("./config");
const { DS_LEGACY_SYNC_BACKEND, DS_INDEXER_BACKEND } = require("./consts");

const { nearRpc, queryFinalTimestamp, queryNodeStats } = require("./near");
const {
nearRpc,
queryFinalTimestamp,
queryNodeStats,
fetchValidatorsInfo,
} = require("./near");

const {
syncNewNearcoreState,
Expand Down Expand Up @@ -52,6 +57,9 @@ const {
aggregateLiveAccountsCountByDate,
} = require("./stats");

let validatorsInfo = new Map();
let proposalsInfo = new Map();

async function startLegacySync() {
console.log("Starting NEAR Explorer legacy syncing service...");

Expand Down Expand Up @@ -257,15 +265,35 @@ async function main() {
if (wamp.session) {
let {
currentValidators,
proposals,
currentProposals,
seatPrice,
totalStake,
epochStartHeight,
} = await queryNodeStats();
let validators = await addNodeInfo(currentValidators);
let proposals = await addNodeInfo(currentProposals);
let onlineValidatingNodes = pickOnlineValidatingNode(validators);
let onlineNodes = await queryOnlineNodes();

let validatorExtendedData = await fetchValidatorsInfo(
validators,
validatorsInfo
);
let proposalsExtendedData = await fetchValidatorsInfo(
proposals,
proposalsInfo
);

if (!onlineNodes) {
onlineNodes = [];
}

if (validatorsInfo) {
validators = validatorExtendedData;
}
if (!proposalsInfo) {
proposals = proposalsExtendedData;
}
wampPublish(
"nodes",
{ onlineNodes, validators, proposals, onlineValidatingNodes },
Expand All @@ -278,6 +306,8 @@ async function main() {
seatPriceAmount: seatPrice,
onlineNodeAmount: onlineNodes.length,
proposalAmount: proposals.length,
totalStakeAmount: totalStake,
epochStartHeightAmount: epochStartHeight,
},
wamp
);
Expand Down
78 changes: 68 additions & 10 deletions backend/src/near.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
const nearApi = require("near-api-js");

const BN = require("bn.js");

const { nearRpcUrl } = require("./config");

const nearRpc = new nearApi.providers.JsonRpcProvider(nearRpcUrl);

let seatPrice = null;
let totalStake = null;

// TODO: Provide an equivalent method in near-api-js, so we don't need to hack it around.
nearRpc.callViewMethod = async function (contractName, methodName, args) {
const account = new nearApi.Account({ provider: this });
Expand All @@ -16,13 +21,37 @@ const queryFinalTimestamp = async () => {
};

const queryNodeStats = async () => {
let nodes = await nearRpc.sendJsonRpc("validators", [null]);
let proposals = nodes.current_proposals;
let seatPrice = nearApi.validators
.findSeatPrice(nodes.current_validators, nodes.numSeats)
.toString();
let currentValidators = getCurrentNodes(nodes);
return { currentValidators, proposals, seatPrice };
let genesisConfig = await nearRpc.sendJsonRpc(
"EXPERIMENTAL_protocol_config",
{ finality: "final" }
);
let epochStatus = await nearRpc.sendJsonRpc("validators", [null]);
let numSeats =
genesisConfig.num_block_producer_seats +
genesisConfig.avg_hidden_validator_seats_per_shard.reduce((a, b) => a + b);
let currentProposals = epochStatus.current_proposals;
let currentValidators = getCurrentNodes(epochStatus);

if (!seatPrice) {
seatPrice = nearApi.validators
.findSeatPrice(epochStatus.current_validators, numSeats)
.toString();
}

if (!totalStake) {
totalStake = currentValidators
.reduce((acc, node) => acc.add(new BN(node.stake)), new BN(0))
.toString();
}
let { epoch_start_height: epochStartHeight } = epochStatus;

return {
currentValidators,
currentProposals,
seatPrice,
totalStake,
epochStartHeight,
};
};

const signNewValidators = (newValidators) => {
Expand All @@ -37,9 +66,37 @@ const signRemovedValidators = (removedValidators) => {
}
};

const getCurrentNodes = (nodes) => {
let currentValidators = nodes.current_validators;
let nextValidators = nodes.next_validators;
const fetchValidatorsInfo = async (validators, validatorArray) => {
for (let i = 0; i < validators.length; i++) {
const { account_id } = validators[i];
let validator = validatorArray.get(account_id);
let fee = validator?.fee;
let delegators = validator?.delegators;

if (fee) {
validators[i].fee = fee;
}
if (delegators) {
validators[i].delegators = delegators;
}
fee = await nearRpc.callViewMethod(
account_id,
"get_reward_fee_fraction",
{}
);
delegators = await nearRpc.callViewMethod(
account_id,
"get_number_of_accounts",
{}
);
validatorArray.set(account_id, { fee, delegators });
}
return validators;
};

const getCurrentNodes = (epochStatus) => {
let currentValidators = epochStatus.current_validators;
let nextValidators = epochStatus.next_validators;
const {
newValidators,
removedValidators,
Expand All @@ -53,3 +110,4 @@ const getCurrentNodes = (nodes) => {
exports.nearRpc = nearRpc;
exports.queryFinalTimestamp = queryFinalTimestamp;
exports.queryNodeStats = queryNodeStats;
exports.fetchValidatorsInfo = fetchValidatorsInfo;
4 changes: 0 additions & 4 deletions backend/src/wamp.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,6 @@ wampHandlers["get-account-details"] = async ([accountId]) => {
.callViewMethod(lockupAccountId, "get_locked_amount", {})
.then((balance) => new BN(balance))
.catch(ignore_if_does_not_exist),
nearRpc
.callViewMethod(lockupAccountId, "get_reward_fee_fraction", {})
.then((fee) => fee)
.catch(ignore_if_does_not_exist),
nearRpc
.callViewMethod(lockupAccountId, "get_staking_pool_account_id", {})
.catch(ignore_if_does_not_exist),
Expand Down
1 change: 0 additions & 1 deletion frontend/src/components/dashboard/DashboardNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const DashboardNode = () => (
}
loading={typeof stats.onlineNodeAmount === "undefined"}
text={stats.onlineNodeAmount?.toLocaleString()}
href={"/nodes/online-nodes"}
/>
</Col>
<Col xs="6" md="12">
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/nodes/NodeNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class NodeNav extends React.PureComponent<Props> {
</a>
</Link>
</Col>
<Col
{/* <Col
xs="auto"
className={`node-selector pt-2 pb-2 ${
role === "online-nodes" ? `node-selected` : ""
Expand All @@ -50,7 +50,7 @@ class NodeNav extends React.PureComponent<Props> {
</Badge>
</a>
</Link>
</Col>
</Col> */}
<Col
xs="auto"
className={`node-selector pt-2 pb-2 ${
Expand Down
39 changes: 31 additions & 8 deletions frontend/src/components/nodes/NodesCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,18 @@ class NodesCard extends React.PureComponent {
<Col xs="6" md="4">
<Row noGutters>
<Col xs="12" className="nodes-card-title">
Circulating Supply
Total Supply
</Col>
<Col xs="12" className="nodes-card-text">
{" "}
000 / 0000
{context.epochStartBlock?.totalSupply ? (
<Balance
round
amount={context.epochStartBlock.totalSupply}
/>
) : (
"--"
)}
<NearBadge />
</Col>
</Row>
Expand All @@ -56,11 +63,18 @@ class NodesCard extends React.PureComponent {
<Col xs="12" md="3">
<Row noGutters>
<Col xs="12" className="nodes-card-title">
Active Stake
Total Stake
</Col>
<Col xs="12" className="nodes-card-text">
{" "}
000.000
{context.totalStakeAmount ? (
<Balance
roundSuffix="M"
round
amount={context.totalStakeAmount}
/>
) : (
"-"
)}
<NearBadge />
</Col>
</Row>
Expand All @@ -73,7 +87,10 @@ class NodesCard extends React.PureComponent {
</Col>
<Col xs="12" className="nodes-card-text">
{context.seatPriceAmount ? (
<Balance amount={context.seatPriceAmount} />
<Balance
roundSuffix="M"
amount={context.seatPriceAmount}
/>
) : (
"-"
)}
Expand All @@ -89,7 +106,8 @@ class NodesCard extends React.PureComponent {
box-shadow: 0px 2px 2px rgba(17, 22, 24, 0.04);
border-radius: 8px;
padding: 48px 32px;
margin-top: 50px;
margin-top: 16px;
// margin-top: 50px;
}
.nodes-card-title {
Expand All @@ -102,12 +120,17 @@ class NodesCard extends React.PureComponent {
.nodes-card-text {
font-weight: 900;
font-size: 31px;
// font-size: 31px;
font-size: 16px;
line-height: 130%;
color: #272729;
font-feature-settings: "zero", on;
}
.nodes-card-badge {
margin-left: 10px;
}
.nodes-card-text.validating {
color: #00c08b;
}
Expand Down
Loading

0 comments on commit 4a3e2a4

Please sign in to comment.