Mulders Michiel
Mulders Michiel

Reputation: 98

Is it required to set the mirror network when sending queries using the Hedera SDK?

When using the Hedera SDK, like the Hedera JavaScript SDK, do you need to set the mirror network address when sending queries to a mirror node's API?

I tried setting:

client.setMirrorNetwork("mainnet-public.mirrornode.hedera.com:443")

but I'm unsure if this is needed?

Upvotes: 1

Views: 95

Answers (1)

Pathorn Teng
Pathorn Teng

Reputation: 513

The SDK does come with the list of Consensus Node IPs and Mirror Node address. Usually, you don't need to set it manually. However, if you are connecting to a custom network (localnet), you need to set it by yourself.

The SDK only talks to mirror node when it subscribe to a topic of consensus service. If don't use that, you don't need to set it.

The following code is the example how you can initialize client instance to connect to Hedera network.

const { Client } = require("@hashgraph/sdk");
require("dotenv").config();

//Grab your Hedera testnet account ID and private key from your .env file
const myAccountId = process.env.MY_ACCOUNT_ID;
const myPrivateKey = process.env.MY_PRIVATE_KEY;

async function main() {
  const testnetClient = Client.forTestnet();
  console.log(testnetClient.mirrorNetwork);
  console.log(testnetClient.network);

  const mainnetClient = Client.forMainnet();
  console.log(mainnetClient.mirrorNetwork);
  console.log(mainnetClient.network);

  const previewnetClient = Client.forPreviewnet();
  console.log(previewnetClient.mirrorNetwork);
  console.log(previewnetClient.network);

  const localClient = Client.forLocalNode();
  console.log(localClient.mirrorNetwork);
  console.log(localClient.network);
  localClient.setOperator(myAccountId, myPrivateKey);

  const node = { "127.0.0.1:50211": "0.0.3" };
  const customClient = Client.forNetwork(node).setMirrorNetwork(
    "custom.mirrornode.com:5600"
  );
  console.log(customClient.mirrorNetwork);
  console.log(customClient.network);

  console.log(
    localClient.operatorAccountId.toString(),
    localClient._operator.publicKey
  );
}

main();

Upvotes: 2

Related Questions