-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
70 lines (59 loc) · 3.08 KB
/
index.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
65
66
67
68
69
70
const {AccountId,Client,PrivateKey,AccountCreateTransaction,AccountBalanceQuery,Hbar,TransferTransaction,} = require("@hashgraph/sdk");
require("dotenv").config();
//STEP 1 import already made account
async function environmentSetup() {
const myAccountId = AccountId.fromString(process.env.MY_ACCOUNT_ID);
const myPrivateKey = PrivateKey.fromString(process.env.MY_PRIVATE_KEY);
if (!myAccountId || !myPrivateKey){
throw new Error("Environment variables MY_ACCOUNT_ID and MY_PRIVATE_KEY are not present!");
}else {
console.log(`myAccountId = ${myAccountId}`);
console.log(`myPrivateKey = ${myPrivateKey}`);
}
//STEP 2 Connect to Testnet
const client = Client.forTestnet();
client.setOperator(myAccountId, myPrivateKey);
client.setDefaultMaxTransactionFee(new Hbar(100));
client.setMaxQueryPayment(new Hbar(50));
console.log("Conection made succesfully!");
//STEP 3 Make New Account
const newAccountPrivateKey = PrivateKey.generateED25519();
const newAccountPublicKey = newAccountPrivateKey.publicKey;
if (!newAccountPrivateKey || !newAccountPublicKey) {
throw new Error("New keys are null");
} else {
console.log("New Keys made succesfully!");
console.log(`newAccountPrivateKey = ${newAccountPrivateKey}`);
console.log(`newAccountPublicKey = ${newAccountPublicKey}`);
}
// Create a new account with 1,000 tinybar starting balance
const newAccount = await new AccountCreateTransaction().setKey(newAccountPublicKey).setInitialBalance(Hbar.fromTinybars(1000)).execute(client);
if (!newAccount) {
throw new Error("New account is null");
} else {
console.log("New account made succesfully!");
console.log(`newAccount = ${newAccount}`);
}
// Get the new account ID
const getReceipt = await newAccount.getReceipt(client);
const newAccountId = getReceipt.accountId;
console.log("\nNew account ID: " + 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 sendHbar = await new TransferTransaction().addHbarTransfer(myAccountId, Hbar.fromTinybars(-1000)).addHbarTransfer(newAccountId, Hbar.fromTinybars(1000)).execute(client);
//Verify the transaction reached consensus
const transactionReceipt = await sendHbar.getReceipt(client);
console.log("The transfer transaction from my account to the new account was: " + transactionReceipt.status.toString());
//Request the cost of the query
const queryCost = await new AccountBalanceQuery().setAccountId(newAccountId).getCost(client);
console.log("The cost of query is: " +queryCost);
//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.")
console.log("End");
client.close();
return newAccountId;
}
environmentSetup();