forked from w3f/polkadot-wiki
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add minStake info for Validators (w3f#3626)
* Add minStake info for Validators Asynchronously populate minStake of validator in the active set. Default to historic values when it is loading * address PR feedback * break-out min stake logic into a new component (equal functionality) * add TODO note * apply human readable filter to default & on-chain result * organization * expose default values via component attributes * improve calculation time significantly * formatting * remove duplicate state setting action Co-authored-by: alfarok <[email protected]>
- Loading branch information
Showing
4 changed files
with
121 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { useState, useEffect } from "react"; | ||
import React from "react"; | ||
import { ApiPromise, WsProvider } from "@polkadot/api"; | ||
import { HumanReadable } from "./utilities/filters"; | ||
|
||
function MinimumStake({ network, defaultValue }) { | ||
const [returnValue, setReturnValue] = useState(''); | ||
|
||
useEffect(async () => { | ||
// Set defaults based on network | ||
let wsUrl = undefined; | ||
if (network === "polkadot") { wsUrl = "wss://rpc.polkadot.io" } | ||
else if (network === "kusama") { wsUrl = "wss://kusama-rpc.polkadot.io/" } | ||
else { return (<div />) } | ||
|
||
// Set default value to render on component | ||
HumanReadable(defaultValue, network, setReturnValue); | ||
// Calculate a more accurate approximation using on-chain data | ||
await CalcValidatorMinStake(network, wsUrl, setReturnValue); | ||
}, []); | ||
|
||
return (returnValue); | ||
} | ||
|
||
async function CalcValidatorMinStake(network, wsUrl, setReturnValue) { | ||
const wsProvider = new WsProvider(wsUrl); | ||
const api = await ApiPromise.create({ provider: wsProvider }) | ||
|
||
const [currentValidators, currentEra] = await Promise.all([ | ||
api.query.session.validators(), | ||
api.query.staking.currentEra(), | ||
]); | ||
|
||
// Get validators stake for current error and first validator | ||
const validatorStake = await api.query.staking.erasStakers(currentEra.toString(), currentValidators[0]) | ||
let validatorMinStake = parseInt(validatorStake['total'].toString()) | ||
|
||
// Iterate era validators | ||
const validators = await api.query.staking.erasStakers.entries(currentEra.toString()); | ||
validators.forEach(([key, validator]) => { | ||
const validatorTotalStake = parseInt(validator.total); | ||
if (validatorTotalStake < validatorMinStake) { | ||
validatorMinStake = validatorTotalStake; | ||
} | ||
}); | ||
|
||
const result = validatorMinStake.toString(); | ||
HumanReadable(result, network, setReturnValue); | ||
} | ||
|
||
export default MinimumStake; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
const Polkadot = "polkadot"; | ||
const Kusama = "kusama"; | ||
const Statemine = "statemine"; | ||
const Statemint = "statemint"; | ||
|
||
const values = { | ||
polkadot: { | ||
precision: 1e10, | ||
symbol: "DOT", | ||
}, | ||
kusama: { | ||
precision: 1e12, | ||
symbol: "KSM", | ||
}, | ||
statemint: { | ||
precision: 1e10, | ||
symbol: "DOT", | ||
}, | ||
statemine: { | ||
precision: 1e12, | ||
symbol: "KSM", | ||
}, | ||
}; | ||
|
||
module.exports = { | ||
|
||
HumanReadable: function (value, network, setReturnValue) { | ||
let decimals = undefined; | ||
if (network === Polkadot || network === Statemint) { | ||
decimals = 3; | ||
} else if (network === Kusama || network === Statemine) { | ||
decimals = 6; | ||
} else { | ||
console.log("Unknown network type found when attempting to apply 'Human Readable' filter"); | ||
return; | ||
} | ||
// String to number | ||
value = parseFloat(value); | ||
// Apply precision | ||
if (Number.isInteger(value / values[network].precision)) { | ||
value = `${value / values[network].precision} ${values[network].symbol}`; | ||
} else { | ||
value = `${(value / values[network].precision).toFixed(decimals)} ${values[network].symbol}`; | ||
} | ||
// Update value | ||
setReturnValue(value.toString()); | ||
}, | ||
|
||
BlocksToDays: function (value, setReturnValue) { | ||
value = (value * 6) / 86400; | ||
// Update value | ||
setReturnValue(value.toString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters