Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Ethernet Square 2.0 how to write the JS code for querying the balance of Validator

Shulou Source: shulou.com Published: 2022-06-01 13:59:30 10月02日 Update

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.

Tags: Balance query certificate code mortgage cycle article program network more and more demand more and more learning help ether highest clear next two public key Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia macOS Redmi Docker vpn