Skip to content

Commit

Permalink
cmd/jsutils: add a tool for validator version
Browse files Browse the repository at this point in the history
  • Loading branch information
blxdyx committed Dec 5, 2023
1 parent 7e6b43a commit 4aa6e2c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
16 changes: 16 additions & 0 deletions cmd/jsutils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
## Requirement

- nodejs: v20.10.0
- npm: v10.2.3

## Prepare
Recommend use [nvm](https://github.com/nvm-sh/nvm) to manage node version.

Install node.js dependency:
```shell script
npm install
```
## Run
```bash
npm run start
```
19 changes: 19 additions & 0 deletions cmd/jsutils/getvalidatorversion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ethers } from "ethers";
const testNet = "https://bsc-prebsc-dataseed.bnbchain.org"
const mainNet = "https://bsc-dataseed.bnbchain.org"
const provider = new ethers.JsonRpcProvider(mainNet);
const valNum = 21;

const main = async () => {
const blockNum = await provider.getBlockNumber();
console.log(blockNum);
for (let i = 0; i < valNum; i++) {
let blockData = await provider.getBlock(blockNum - i);
console.log(blockData.miner);
let major = ethers.toNumber(ethers.dataSlice(blockData.extraData, 2, 3))
let minor = ethers.toNumber(ethers.dataSlice(blockData.extraData, 3, 4))
let patch = ethers.toNumber(ethers.dataSlice(blockData.extraData, 4, 5))
console.log("version = ", major, ".", minor, ".", patch)
}
};
main();
14 changes: 14 additions & 0 deletions cmd/jsutils/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "jsutils",
"version": "1.0.0",
"type": "module",
"description": "jsUtils for bsc",
"main": "index.js",
"scripts": {
"start": "node getvalidatorversion.js"
},
"dependencies": {
"ethers": "^6.2.3"
},
"author": "BNB Chain"
}

0 comments on commit 4aa6e2c

Please sign in to comment.