Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[doc] How to use dashd-rpc directly #9

Open
coolaj86 opened this issue Jun 15, 2023 · 0 comments
Open

[doc] How to use dashd-rpc directly #9

coolaj86 opened this issue Jun 15, 2023 · 0 comments

Comments

@coolaj86
Copy link
Member

coolaj86 commented Jun 15, 2023

"use strict";

let Fs = require("node:fs/promises");

let RpcClient = require("@dashevo/dashd-rpc/promise");

// TODO read from JSON config
let config = {
  protocol: "http",
  user: "app",
  pass: "4472bd70ac35b572d768478266e31312",
  host: "10.11.5.101",
  // mainnet=9998, testnet=19998, regtest=19898
  port: 9998,
};

const E_RPC_IN_WARMUP = -28;

async function main() {
  let file = process.argv[2];
  let rpc = new RpcClient(config);

  let ready = await rpc.getBestBlockHash().catch(function (e) {
    if (e.code === E_RPC_IN_WARMUP) {
      console.info("[INFO] RPC is connected.");
      console.warn(`[WARN] RPC is not fully synced: ${e.message}`);
      return;
    }

    throw e;
  });
  if (ready) {
    console.info("[INFO] RPC is ready.");
  }

  let addrs = [
    "XvZy1npHNGeYHCuDnB6upKqdf6XU9q94qu",
    "Xr5ZkoaLAeoFw7GGiNvuwTYZFeDcM9knMg",
  ];
  if (file) {
    let text = await Fs.readFile(file, "utf8");
    let lines = text.split(/["',\s\r\n]/).filter(Boolean);
    addrs = addrs.concat(lines);
  }

  await getUtxos(rpc, addrs);
}

async function getUtxos(rpc, addresses) {
  // getaddressbalance
  // getaddressutxos
  let ret = await rpc.getAddressUtxos({
    addresses: addresses,
  });

  console.log(
    `${addresses.length} addresses with ${ret.result.length} UTXOs between them`,
  );
}

main().catch(function (err) {
  console.error(err.stack);
  process.exit(1);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant