Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
Signed-off-by: Jeffrey Tang <[email protected]>
  • Loading branch information
JeffreyDallas committed Nov 26, 2024
1 parent c439ebf commit 8ba52b0
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions examples/create-topic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import {
Wallet,
LocalProvider,
TopicCreateTransaction,
TopicMessageSubmitTransaction,
} from "@hashgraph/sdk";

import dotenv from "dotenv";

dotenv.config();

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

const provider = new LocalProvider();

const wallet = new Wallet(
process.env.OPERATOR_ID,
process.env.OPERATOR_KEY,
provider,
);

try {
// create topic
let transaction = await new TopicCreateTransaction().freezeWithSigner(
wallet,
);
transaction = await transaction.signWithSigner(wallet);
const createResponse = await transaction.executeWithSigner(wallet);
const createReceipt = await createResponse.getReceiptWithSigner(wallet);

console.log(`topic id = ${createReceipt.topicId.toString()}`);

// send one message
let topicMessageSubmitTransaction =
await new TopicMessageSubmitTransaction({
topicId: createReceipt.topicId,
message: "Hello World",
}).freezeWithSigner(wallet);
topicMessageSubmitTransaction =
await topicMessageSubmitTransaction.signWithSigner(wallet);
const sendResponse =
await topicMessageSubmitTransaction.executeWithSigner(wallet);

const sendReceipt = await sendResponse.getReceiptWithSigner(wallet);

console.log(
`topic sequence number = ${sendReceipt.topicSequenceNumber.toString()}`,
);
} catch (error) {
console.error(error);
}

provider.close();
}

void main();

0 comments on commit 8ba52b0

Please sign in to comment.