opentensor / developer-docs

Developer documentation for Bittensor.
https://docs.bittensor.com
17 stars 20 forks source link

Add a script that allows people to query when the next adjustment interval will be #196

Closed Jackalgirl closed 2 months ago

Jackalgirl commented 2 months ago

By: @rajkaramchedu and @Jackalgirl

Purpose: instructions to download and create a script that allows users to query a subnet and discover the future next adjustment interval block will be (and provide an approximate time to adjustment), using a terminal window.

Instructions:

  1. On a macOS or Linux terminal, install "node" if you don't have it (see https://nodejs.org/en/download for help)
  2. If you have not installed yarn yet, run npm install --global yarn (if you are not root, you may need sudo npm install --global yarn)
  3. Run yarn add @polkadot/api
  4. Run yarn add prompt-sync
  5. Save the following into a file (for example: "bittensor_check-interval.js")
const { ApiPromise, WsProvider } = require("@polkadot/api");
const prompt = require("prompt-sync")();

// If you do not want to be prompted for network when running the script,
// change this variable to "finney" to default to the finney endpoint,
//  or "local" if you want to default to your own subtensor endpoint.

let network = "";

// If you have your own local subtensor and want to use its entry point, specify
// your entrypoint's wss address here (e.g., "wss://127.0.0.1:443")

let localNetworkEntryPoint = "";

if (network === "") {
  network = prompt("Enter network (finney, subvortex, local): ");
}

if ((network === "local" || network === "l") && localNetworkEntryPoint != "") {
  networkEntryPoint = localNetworkEntryPoint;
} else {
  networkEntryPoint = "wss://entrypoint-finney.opentensor.ai:443";
}

let subnet = prompt("Enter subnet netuid to query:");

if (!(Number.isInteger(subnet)) || subnet < 1 || subnet > 32) {
  subnet == 1;
} else {
  subnet == subnet;
}

async function queryBittensorData() {
  const wsProvider = new WsProvider(
    networkEntryPoint  );
  const api = await ApiPromise.create({ provider: wsProvider });

  const [
    lastAdjustmentBlock,
    adjustmentInterval,
    currentBlock,
  ] = await Promise.all([
    api.query.subtensorModule.lastAdjustmentBlock(subnet),
    api.query.subtensorModule.adjustmentInterval(subnet),
    api.query.system.number(),
  ]);

  let nextAdjustmentBlock = lastAdjustmentBlock.toNumber() + adjustmentInterval.toNumber();
  let currentBlockNumber = currentBlock.toNumber();
  let blocksToGo = nextAdjustmentBlock - currentBlockNumber;

  console.log(`Information for subnet`,subnet);
  console.log(`-=-=-=-=-=-=-=-=-=-=-=-=-=-`);
  console.log(`Last Adjustment Block: ${lastAdjustmentBlock.toNumber()}`);
  console.log(`Adjustment Interval: ${adjustmentInterval.toNumber()}`);
  console.log(`Next Adjustment Block: `, nextAdjustmentBlock);
  console.log(`Current Block: `, currentBlockNumber);
  console.log(`Blocks to go:`,blocksToGo,`in approximately`,blocksToGo*12,`seconds (`,blocksToGo*12/60,`minutes).`);
}

queryBittensorData()
  .catch(console.error)
  .finally(() => process.exit());

console.warn = () => {};
  1. You can run this file via node <filename> (for example: node bittensor_check-interval.js)
Jackalgirl commented 2 months ago

I'm still tinkering. Will update the above in a minute (maybe). : )

Done! For now. : )