Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add transaction status checks to examples #23

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion hcs/script-hcs-topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,17 @@ async function scriptHcsTopic() {

// Get the topic ID
const topicId = topicCreateTxReceipt.topicId;
logger.log('topicId:', topicId.toString());
// Get the topic ID
if (topicCreateTxReceipt.status.toString() === 'SUCCESS') {
theekrystallee marked this conversation as resolved.
Show resolved Hide resolved
logger.log('✅ Topic created successfully. Topic ID:', topicId.toString());
logger.log(
`Transaction was successful. View it at: https://hashscan.io/testnet/transaction/${topicCreateTxId}`,
);
} else {
logger.error(
`❌ Transaction failed with status: ${topicCreateTxReceipt.status}`,
);
}

// NOTE: Publish a message to the Hedera Consensus Service (HCS) topic
await logger.logSection('Publish message to HCS topic');
Expand Down
41 changes: 22 additions & 19 deletions hts/script-hts-ft.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async function scriptHtsFungibleToken() {
logger.logStart('Hello Future World - HTS Fungible Token - start');

// Read in environment variables from `.env` file in parent directory
dotenv.config({ path: '../.env' });
dotenv.config({ path: './.env' });
logger.log('Read .env file');

// Initialize the operator account
Expand All @@ -37,12 +37,12 @@ async function scriptHtsFungibleToken() {
client = Client.forTestnet().setOperator(operatorId, operatorKey);
logger.log('Using account:', operatorIdStr);

//Set the default maximum transaction fee (in HBAR)
// Set the default maximum transaction fee (in HBAR)
client.setDefaultMaxTransactionFee(new Hbar(100));
//Set the default maximum payment for queries (in HBAR)
// Set the default maximum payment for queries (in HBAR)
client.setDefaultMaxQueryPayment(new Hbar(50));

// NOTE: Create a HTS token
// NOTE: Create an HTS token
await logger.logSection('Creating new HTS token');
const tokenCreateTx = await new TokenCreateTransaction()
//Set the transaction memo
Expand All @@ -55,40 +55,42 @@ async function scriptHtsFungibleToken() {
.setTokenSymbol(logger.scriptId.toUpperCase())
// Set the token decimals to 2
.setDecimals(2)
// Set the initial supply of the token to 1,000,000
// Set the initial supply to 1,000,000
.setInitialSupply(1_000_000)
// Configure token access permissions: treasury account, admin, freezing
// Set the treasury account to the operator account. Configure token access permissions: treasury account, admin, freezing
.setTreasuryAccountId(operatorId)
// Set the freeze default value to false
.setFreezeDefault(false)
//Freeze the transaction and prepare for signing
.freezeWith(client);

// Get the transaction ID of the transaction. The SDK automatically generates and assigns a transaction ID when the transaction is created
const tokenCreateTxId = tokenCreateTx.transactionId;
logger.log('The token create transaction ID: ', tokenCreateTxId.toString());

// Sign the transaction with the private key of the treasury account (operator key)
const tokenCreateTxSigned = await tokenCreateTx.sign(operatorKey);

// Submit the transaction to the Hedera Testnet
const tokenCreateTxSubmitted = await tokenCreateTxSigned.execute(client);

// Get the transaction receipt
// Get the transaction receipt and check the status
const tokenCreateTxReceipt = await tokenCreateTxSubmitted.getReceipt(client);

// Get the token ID
const tokenId = tokenCreateTxReceipt.tokenId;
logger.log('tokenId:', tokenId.toString());
if (tokenCreateTxReceipt.status.toString() === 'SUCCESS') {
logger.log('✅ Token created successfully. Token ID:', tokenId.toString());
logger.log(
`Transaction was successful. View it at: https://hashscan.io/testnet/transaction/${tokenCreateTxId}`,
);
} else {
throw new Error(
`❌ Token creation transaction failed with status: ${tokenCreateTxReceipt.status}`,
);
}

client.close();

// NOTE: Verify transactions using Hashscan
// This is a manual step, the code below only outputs the URL to visit

// View your token on HashScan
// Verify transactions using Hashscan
await logger.logSection('View the token on HashScan');
const tokenVerifyHashscanUrl = `https://hashscan.io/testnet/token/${tokenId.toString()}`;
const tokenVerifyHashscanUrl = `https://hashscan.io/testnet/token/${tokenCreateTxReceipt.tokenId.toString()}`;

logger.log(
'Paste URL in browser:\n',
...logger.applyAnsi('URL', tokenVerifyHashscanUrl),
Expand All @@ -99,7 +101,8 @@ async function scriptHtsFungibleToken() {

// NOTE: Verify token using Mirror Node API
await logger.logSection('Get token data from the Hedera Mirror Node');
const tokenVerifyMirrorNodeApiUrl = `https://testnet.mirrornode.hedera.com/api/v1/tokens/${tokenId.toString()}`;

const tokenVerifyMirrorNodeApiUrl = `https://testnet.mirrornode.hedera.com/api/v1/tokens/${tokenCreateTxReceipt.tokenId.toString()}`;
theekrystallee marked this conversation as resolved.
Show resolved Hide resolved
logger.log(
'The token Hedera Mirror Node API URL:\n',
...logger.applyAnsi('URL', tokenVerifyMirrorNodeApiUrl),
Expand Down
17 changes: 12 additions & 5 deletions transfer/script-transfer-hbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,20 @@ async function scriptTransferHbar() {
//Submit the transaction to the Hedera Testnet
const transferTxSubmitted = await transferTxSigned.execute(client);

//Get the transfer transaction receipt
// Check the transaction receipt
const transferTxReceipt = await transferTxSubmitted.getReceipt(client);
const transactionStatus = transferTxReceipt.status;
logger.log(
'The transfer transaction status is:',
transactionStatus.toString(),
);

if (transactionStatus.toString() === 'SUCCESS') {
logger.log('✅ The transfer transaction was successful.');
logger.log(
`View it at: https://hashscan.io/testnet/transaction/${transferTxId}`,
);
} else {
throw new Error(
`❌ The transfer transaction failed with status: ${transactionStatus}`,
);
}

// NOTE: Query HBAR balance using AccountBalanceQuery
const newAccountBalance = new AccountBalanceQuery()
Expand Down