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

Add CreateTopicExample #530

Merged
merged 9 commits into from
Oct 16, 2023
4 changes: 4 additions & 0 deletions sdk/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ set(CREATE_FILE_EXAMPLE_NAME ${PROJECT_NAME}-create-file-example)
set(CREATE_ACCOUNT_THRESHOLD_KEY_EXAMPLE_NAME ${PROJECT_NAME}-create-account-threshold-key-example)
set(CREATE_SIMPLE_CONTRACT_EXAMPLE_NAME ${PROJECT_NAME}-create-simple-contract-example)
set(CREATE_STATEFUL_CONTRACT_EXAMPLE_NAME ${PROJECT_NAME}-create-stateful-contract-example)
set(CREATE_TOPIC_EXAMPLE_NAME ${PROJECT_NAME}-create-topic-example)
set(CONTRACT_NONCES_EXAMPLE_NAME ${PROJECT_NAME}-contract-nonces-example)
set(CUSTOM_FEES_EXAMPLE_NAME ${PROJECT_NAME}-custom-fees-example)
set(DELETE_ACCOUNT_EXAMPLE_NAME ${PROJECT_NAME}-delete-account-example)
Expand Down Expand Up @@ -49,6 +50,7 @@ add_executable(${CREATE_FILE_EXAMPLE_NAME} CreateFileExample.cc)
add_executable(${CREATE_ACCOUNT_THRESHOLD_KEY_EXAMPLE_NAME} CreateAccountThresholdKeyExample.cc)
add_executable(${CREATE_SIMPLE_CONTRACT_EXAMPLE_NAME} CreateSimpleContractExample.cc)
add_executable(${CREATE_STATEFUL_CONTRACT_EXAMPLE_NAME} CreateStatefulContractExample.cc)
add_executable(${CREATE_TOPIC_EXAMPLE_NAME} CreateTopicExample.cc)
add_executable(${CONTRACT_NONCES_EXAMPLE_NAME} ContractNoncesExample.cc)
add_executable(${CUSTOM_FEES_EXAMPLE_NAME} CustomFeesExample.cc)
add_executable(${DELETE_ACCOUNT_EXAMPLE_NAME} DeleteAccountExample.cc)
Expand Down Expand Up @@ -106,6 +108,7 @@ target_link_libraries(${CREATE_FILE_EXAMPLE_NAME} PUBLIC ${PROJECT_NAME})
target_link_libraries(${CREATE_ACCOUNT_THRESHOLD_KEY_EXAMPLE_NAME} PUBLIC ${PROJECT_NAME})
target_link_libraries(${CREATE_SIMPLE_CONTRACT_EXAMPLE_NAME} PUBLIC ${PROJECT_NAME})
target_link_libraries(${CREATE_STATEFUL_CONTRACT_EXAMPLE_NAME} PUBLIC ${PROJECT_NAME})
target_link_libraries(${CREATE_TOPIC_EXAMPLE_NAME} PUBLIC ${PROJECT_NAME})
target_link_libraries(${CONTRACT_NONCES_EXAMPLE_NAME} PUBLIC ${PROJECT_NAME})
target_link_libraries(${CUSTOM_FEES_EXAMPLE_NAME} PUBLIC ${PROJECT_NAME})
target_link_libraries(${DELETE_ACCOUNT_EXAMPLE_NAME} PUBLIC ${PROJECT_NAME})
Expand Down Expand Up @@ -146,6 +149,7 @@ install(TARGETS
${CREATE_ACCOUNT_THRESHOLD_KEY_EXAMPLE_NAME}
${CREATE_SIMPLE_CONTRACT_EXAMPLE_NAME}
${CREATE_STATEFUL_CONTRACT_EXAMPLE_NAME}
${CREATE_TOPIC_EXAMPLE_NAME}
${CONTRACT_NONCES_EXAMPLE_NAME}
${CUSTOM_FEES_EXAMPLE_NAME}
${DELETE_ACCOUNT_EXAMPLE_NAME}
Expand Down
61 changes: 61 additions & 0 deletions sdk/examples/CreateTopicExample.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*-
*
* Hedera C++ SDK
*
* Copyright (C) 2020 - 2023 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "Client.h"
#include "ED25519PrivateKey.h"
#include "Status.h"
#include "TopicCreateTransaction.h"
#include "TopicId.h"
#include "TopicMessageSubmitTransaction.h"
#include "TransactionReceipt.h"
#include "TransactionResponse.h"

#include <iostream>

using namespace Hedera;

int main(int argc, char** argv)
{
if (argc < 3)
{
std::cout << "Please input account ID and private key" << std::endl;
return 1;
}

// Get a client for the Hedera testnet, and set the operator account ID and key such that all generated transactions
// will be paid for by this account and be signed by this key.
Client client = Client::forTestnet();
client.setOperator(AccountId::fromString(argv[1]), ED25519PrivateKey::fromString(argv[2]));

// Create a new topic.
const TopicId topicId = TopicCreateTransaction().execute(client).getReceipt(client).mTopicId.value();
std::cout << "Created new topic with ID " << topicId.toString() << std::endl;

// Submit a message on this topic.
std::cout << "Submitting message: "
<< gStatusToString.at(TopicMessageSubmitTransaction()
.setTopicId(topicId)
.setMessage("Hello world!")
.execute(client)
.getReceipt(client)
.mStatus)
<< std::endl;

return 0;
}
Loading