-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
66 lines (49 loc) · 2.52 KB
/
index.js
File metadata and controls
66 lines (49 loc) · 2.52 KB
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
65
66
const { Client, PrivateKey, AccountCreateTransaction, AccountBalanceQuery, Hbar, TransferTransaction} = require("@hashgraph/sdk");
require("dotenv").config();
async function main() {
//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;
// If we weren't able to grab it, we should throw a new error
if (myAccountId == null ||
myPrivateKey == null ) {
throw new Error("Environment variables myAccountId and myPrivateKey must be present");
}
console.log("accountid: " + myAccountId);
console.log("privacykey: " + myPrivateKey);
// Create our connection to the Hedera network
// The Hedera JS SDK makes this reallyyy easy!
const client = Client.forTestnet();
client.setOperator(myAccountId, myPrivateKey);
//Create new keys
const newAccountPrivateKey = await PrivateKey.generate();
const newAccountPublicKey = newAccountPrivateKey.publicKey;
//Create a new account with 1,000 tinybar starting balance
const newAccountTransactionResponse = await new AccountCreateTransaction()
.setKey(newAccountPublicKey)
.setInitialBalance(Hbar.fromTinybars(1000))
.execute(client);
// Get the new account ID
const getReceipt = await newAccountTransactionResponse.getReceipt(client);
const newAccountId = getReceipt.accountId;
console.log("The new account ID is: " +newAccountId);
//Verify the account balance
const accountBalance = await new AccountBalanceQuery()
.setAccountId(newAccountId)
.execute(client);
console.log("The new account balance is: " +accountBalance.hbars.toTinybars() +" tinybar.");
//Create the transfer transaction
const transferTransactionResponse = await new TransferTransaction()
.addHbarTransfer(myAccountId, Hbar.fromTinybars(-1000))
.addHbarTransfer(newAccountId, Hbar.fromTinybars(1000))
.execute(client);
//Verify the transaction reached consensus
const transactionReceipt = await transferTransactionResponse.getReceipt(client);
console.log("The transfer transaction from my account to the new account was: " + transactionReceipt.status.toString());
//Check the new account's balance
const getNewBalance = await new AccountBalanceQuery()
.setAccountId(newAccountId)
.execute(client);
console.log("The account balance after the transfer is: " +getNewBalance.hbars.toTinybars() +" tinybar.")
}
main();