-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.js
64 lines (52 loc) · 2.7 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import fetchPortalAccountInfoFcn from "./fetchPortalAccountInfoFcn.js";
import { Client, AccountBalanceQuery } from "@hashgraph/sdk";
import { createRequire } from "module";
const require = createRequire(import.meta.url);
require("dotenv").config();
const portalApiKey = process.env.HEDERA_PORTAL_API_KEY;
const operatorPublicKey = process.env.OPERATOR_PBKEY;
const authorizationHeader = `Bearer ${portalApiKey}`;
async function main() {
let url;
let network;
let accountId;
let privateKey;
let keyType;
//
// ====================================
// 1. GET SINGLE ACCOUNT INFO BY PUBLIC KEY
// ====================================
url = `https:/portal.hedera.com/api/account/${operatorPublicKey}`;
const singleAccountInfo = await fetchPortalAccountInfoFcn(url, authorizationHeader);
// console.log(JSON.stringify(singleAccountInfo, null, 2));
// 1.1 Get the account ID and private key for the account
network = singleAccountInfo.network;
accountId = `${singleAccountInfo.realm}.${singleAccountInfo.shard}.${singleAccountInfo.accountNum}`;
privateKey = singleAccountInfo.privateKey;
keyType = singleAccountInfo.keyType;
console.log(`- Obtained account ID: ${accountId} from ${network} with key type: ${keyType}`);
// 1.2 Create a client for the Hedera network and check the balance of the account
const client = Client.forNetwork(network).setOperator(accountId, privateKey);
const accountBalanceCheck = await new AccountBalanceQuery().setAccountId(accountId).execute(client);
console.log(`- Balance of account ${accountId} is: ${accountBalanceCheck.hbars.toString()} \n`);
//
// ====================================
// 2. GET ALL ACCOUNTS INFO
// ====================================
url = `https:/portal.hedera.com/api/account`;
const allAccountsInfo = await fetchPortalAccountInfoFcn(url, authorizationHeader);
// console.log(JSON.stringify(allAccountsInfo, null, 2));
// 2.1 Get the account ID and private key for the 4th account in the array
network = allAccountsInfo.accounts[3].network;
accountId = `${allAccountsInfo.accounts[3].realm}.${allAccountsInfo.accounts[3].shard}.${allAccountsInfo.accounts[3].accountNum}`;
privateKey = allAccountsInfo.accounts[3].privateKey;
keyType = allAccountsInfo.accounts[3].keyType;
console.log(`- Obtained account ID: ${accountId} from ${network} with key type: ${keyType}`);
// 2.2 Update the client to work with the new account info and check the balance of the account
client.setNetwork(network);
client.setOperator(accountId, privateKey);
const accountBalanceCheck1 = await new AccountBalanceQuery().setAccountId(accountId).execute(client);
console.log(`- Balance of account ${accountId} is: ${accountBalanceCheck1.hbars.toString()} \n`);
client.close();
}
main();