Skip to content

Commit

Permalink
[#1950] Examples related changes (#1953)
Browse files Browse the repository at this point in the history
Signed-off-by: svetoslav-nikol0v <[email protected]>
  • Loading branch information
svetoslav-nikol0v authored Oct 16, 2023
1 parent 8356d64 commit 5c428d4
Show file tree
Hide file tree
Showing 43 changed files with 3,800 additions and 3,319 deletions.
86 changes: 48 additions & 38 deletions examples/account-alias.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ import dotenv from "dotenv";
dotenv.config();

async function main() {
if (process.env.OPERATOR_ID == null || process.env.OPERATOR_KEY == null) {
if (
process.env.OPERATOR_ID == null ||
process.env.OPERATOR_KEY == null ||
process.env.HEDERA_NETWORK == null
) {
throw new Error(
"Environment variables OPERATOR_ID, and OPERATOR_KEY are required."
"Environment variables OPERATOR_ID, HEDERA_NETWORK, and OPERATOR_KEY are required."
);
}

Expand Down Expand Up @@ -84,41 +88,47 @@ async function main() {
).toAccountId(0, 0);

console.log("Transferring some Hbar to the new account");
let transaction = await new TransferTransaction()
.addHbarTransfer(wallet.getAccountId(), new Hbar(10).negated())
.addHbarTransfer(aliasAccountId, new Hbar(10))
.freezeWithSigner(wallet);
transaction = await transaction.signWithSigner(wallet);

const response = await transaction.executeWithSigner(wallet);
await response.getReceiptWithSigner(wallet);

const balance = await new AccountBalanceQuery()
.setNodeAccountIds([response.nodeId])
.setAccountId(aliasAccountId)
.executeWithSigner(wallet);

console.log(`Balances of the new account: ${balance.toString()}`);

const info = await new AccountInfoQuery()
.setNodeAccountIds([response.nodeId])
.setAccountId(aliasAccountId)
.executeWithSigner(wallet);

console.log(`Info about the new account: ${info.toString()}`);

/*
* Note that once an account exists in the ledger, it is assigned a normal AccountId, which can be retrieved
* via an AccountInfoQuery.
*
* Users may continue to refer to the account by its aliasKey AccountId, but they may also
* now refer to it by its normal AccountId
*/

console.log(`The normal account ID: ${info.accountId.toString()}`);
console.log(`The alias key: ${info.aliasKey.toString()}`);

console.log("Example complete!");
try {
let transaction = await new TransferTransaction()
.addHbarTransfer(wallet.getAccountId(), new Hbar(10).negated())
.addHbarTransfer(aliasAccountId, new Hbar(10))
.freezeWithSigner(wallet);
transaction = await transaction.signWithSigner(wallet);

const response = await transaction.executeWithSigner(wallet);
await response.getReceiptWithSigner(wallet);

const balance = await new AccountBalanceQuery()
.setNodeAccountIds([response.nodeId])
.setAccountId(aliasAccountId)
.executeWithSigner(wallet);

console.log(`Balances of the new account: ${balance.toString()}`);

const info = await new AccountInfoQuery()
.setNodeAccountIds([response.nodeId])
.setAccountId(aliasAccountId)
.executeWithSigner(wallet);

console.log(`Info about the new account: ${info.toString()}`);

/*
* Note that once an account exists in the ledger, it is assigned a normal AccountId, which can be retrieved
* via an AccountInfoQuery.
*
* Users may continue to refer to the account by its aliasKey AccountId, but they may also
* now refer to it by its normal AccountId
*/

console.log(`The normal account ID: ${info.accountId.toString()}`);
console.log(`The alias key: ${info.aliasKey.toString()}`);

console.log("Example complete!");
} catch (error) {
console.error(error);
}
}

void main();
void main()
.then(() => process.exit(0))
.catch(() => process.exit(1));
Loading

0 comments on commit 5c428d4

Please sign in to comment.