In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Ethernet Square 2.0 query Validator balance of the JS code how to write, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.
One of the basic requirements of ETH 2.0 mortgages is to monitor their verifier balances, and we will learn how to use Infura's ETH 2.0 Beacon Chain API to write programs to query verifier (Validator) balances.
As we get closer to the zero phase launch of ETH 2.0, there is a growing interest in participating in mortgages on witti and the upcoming altona testing website-more than 65 per cent of respondents said they expected to mortgage their ETH holdings, according to the survey.
First we need to introduce the axio library and create a HTTP client to load the Infura project ID from the environment variables:
Import axios from 'axios';const instance = axios.create ({baseURL:' https://witti.infura.io/', auth: {username: process.env.INFURA_PROJECT_ID, password:',},})
We need to define some network-related parameters. The first two help us calculate the current cycle (epoch), and the last one is used to convert the balance unit from gwei to the more familiar eth:
Const SECONDS_PER_SLOT = 12th Const SLOTS_PER_EPOCH = 32th Const GWEI_PER_ETH = 1000000000
Next, we will query the creation data of the network to calculate the current cycle:
(async () = > {let response = await instance.get ('node/genesis_time') const genesisTime = response.data; const now = Math.floor (Date.now () / 1000) const currentEpoch = Math.floor ((now-genesisTime) / (SECONDS_PER_SLOT * SLOTS_PER_EPOCH)
With this code, we can query the current balance of the verifier (Validator). Here we use the public key of the verifier with the highest balance at the time of writing:
Response = await instance.post ('beacon/validators', {epoch: currentEpoch.toString (), pubkeys: ["0xb0bfa93603f81ecaf3c450b793e71f7372822cd2002534364493e4e497fb5df16d252ae2fdc6bbc0e74d849f918db393"],}); const balanceGwei = response.data [0] .balance; const balance = balanceGwei / GWEI_PER_ETH; console.log (`You have ${balance} 2.0`);}) ()
When we run this program, we can see the following output:
You have 32.500178823 v2.0
Here is the complete code for querying the balance of ETH 2.0 verifiers:
Import axios from 'axios';const instance = axios.create ({baseURL:' https://witti.infura.io/', auth: {username: process.env.INFURA_PROJECT_ID, password:',},}); const SECONDS_PER_SLOT = 12 const SECONDS_PER_SLOT Const SLOTS_PER_EPOCH = 32 const SECONDS_PER_SLOT Const GWEI_PER_ETH = 100000000; (async () = > {let response = await instance.get ('node/genesis_time') const genesisTime = response.data Const now = Math.floor (Date.now () / 1000) const currentEpoch = Math.floor ((now-genesisTime) / (SECONDS_PER_SLOT * SLOTS_PER_EPOCH)) response = await instance.post ('beacon/validators', {epoch: currentEpoch.toString (), pubkeys: ["0xb0bfa93603f81ecaf3c450b793e71f7372822cd2002534364493e4e497fb5df16d252ae2fdc6bbc0e74d849f918db393"],}); const balanceGwei = response.data [0] .balance; const balance = balanceGwei / GWEI_PER_ETH; console.log (`You have ${balance} 2.0`) Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.