-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jeffrey Tang <[email protected]>
- Loading branch information
1 parent
c439ebf
commit 8ba52b0
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |