Skip to content

Commit

Permalink
Merge branch 'main' into 00488-add-accountcreatewithhtsexample
Browse files Browse the repository at this point in the history
  • Loading branch information
deyanzz authored Sep 1, 2023
2 parents ed73ff7 + 6dbc639 commit bf8443c
Show file tree
Hide file tree
Showing 19 changed files with 1,009 additions and 8 deletions.
4 changes: 4 additions & 0 deletions sdk/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set(CONSENSUS_PUB_SUB_EXAMPLE_NAME ${PROJECT_NAME}-consensus-pub-sub-example)
set(CREATE_ACCOUNT_EXAMPLE_NAME ${PROJECT_NAME}-create-account-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(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)
set(DELETE_FILE_EXAMPLE_NAME ${PROJECT_NAME}-delete-file-example)
Expand All @@ -31,6 +32,7 @@ add_executable(${CONSENSUS_PUB_SUB_EXAMPLE_NAME} ConsensusPubSubExample.cc)
add_executable(${CREATE_ACCOUNT_EXAMPLE_NAME} CreateAccountExample.cc)
add_executable(${CREATE_SIMPLE_CONTRACT_EXAMPLE_NAME} CreateSimpleContractExample.cc)
add_executable(${CREATE_STATEFUL_CONTRACT_EXAMPLE_NAME} CreateStatefulContractExample.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)
add_executable(${DELETE_FILE_EXAMPLE_NAME} DeleteFileExample.cc)
Expand Down Expand Up @@ -75,6 +77,7 @@ target_link_libraries(${CONSENSUS_PUB_SUB_EXAMPLE_NAME} PUBLIC ${PROJECT_NAME})
target_link_libraries(${CREATE_ACCOUNT_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(${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})
target_link_libraries(${DELETE_FILE_EXAMPLE_NAME} PUBLIC ${PROJECT_NAME})
Expand Down Expand Up @@ -104,6 +107,7 @@ install(TARGETS
${CREATE_ACCOUNT_EXAMPLE_NAME}
${CREATE_SIMPLE_CONTRACT_EXAMPLE_NAME}
${CREATE_STATEFUL_CONTRACT_EXAMPLE_NAME}
${CONTRACT_NONCES_EXAMPLE_NAME}
${CUSTOM_FEES_EXAMPLE_NAME}
${DELETE_ACCOUNT_EXAMPLE_NAME}
${DELETE_FILE_EXAMPLE_NAME}
Expand Down
133 changes: 133 additions & 0 deletions sdk/examples/ContractNoncesExample.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*-
*
* 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 "ContractCreateTransaction.h"
#include "ContractDeleteTransaction.h"
#include "ContractFunctionResult.h"
#include "ED25519PrivateKey.h"
#include "FileAppendTransaction.h"
#include "FileCreateTransaction.h"
#include "FileDeleteTransaction.h"
#include "FileId.h"
#include "FileInfo.h"
#include "FileInfoQuery.h"
#include "Status.h"
#include "TransactionReceipt.h"
#include "TransactionRecord.h"
#include "TransactionResponse.h"
#include "impl/Utilities.h"

#include <filesystem>
#include <fstream>
#include <iostream>
#include <nlohmann/json.hpp>

using json = nlohmann::json;
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();
const AccountId operatorId = AccountId::fromString(argv[1]);
std::unique_ptr<PrivateKey> operatorKey = ED25519PrivateKey::fromString(argv[2]);
const std::shared_ptr<PublicKey> operatorPublicKey = operatorKey->getPublicKey();
client.setOperator(operatorId, operatorKey.get());

// Contract's bytecode as hex
const std::string mBytecodeHexWithContractNonceInfo =
"6080604052348015600f57600080fd5b50604051601a90603b565b604051809103906000f0801580156035573d6000803e3d6000fd5"
"b50506047565b605c8061009483390190565b603f806100556000396000f3fe6080604052600080fdfea2646970667358221220a201"
"22cbad3457fedcc0600363d6e895f17048f5caa4afdab9e655123737567d64736f6c634300081200336080604052348015600f57600"
"080fd5b50603f80601d6000396000f3fe6080604052600080fdfea264697066735822122053dfd8835e3dc6fedfb8b4806460b9b716"
"3f8a7248bac510c6d6808d9da9d6d364736f6c63430008120033";

// Create the contract's bytecode file
TransactionReceipt txReceipt =
FileCreateTransaction()
.setKeys({ operatorPublicKey.get() })
.setContents(internal::Utilities::stringToByteVector(mBytecodeHexWithContractNonceInfo))
.setMaxTransactionFee(Hbar(2LL))
.execute(client)
.getReceipt(client);
std::cout << "FileCreateTransaction execution completed with status: " << gStatusToString.at(txReceipt.mStatus)
<< std::endl;
if (!txReceipt.mFileId.has_value())
{
std::cout << "No file created!" << std::endl;
return 1;
}

const FileId newFileId = txReceipt.mFileId.value();
std::cout << "Contract bytecode file created with ID " << newFileId.toString() << std::endl;

// Create the actual contract
TransactionResponse contractCreateTxResponse = ContractCreateTransaction()
.setAdminKey(operatorPublicKey.get())
.setGas(100000ULL)
.setBytecodeFileId(newFileId)
.setMemo("[e2e::ContractADeploysContractBInConstructor]")
.execute(client);

TransactionReceipt contractCreateTxReceipt = contractCreateTxResponse.getReceipt(client);
std::cout << "ContractCreateTransaction execution completed with status: "
<< gStatusToString.at(contractCreateTxReceipt.mStatus) << std::endl;
if (!contractCreateTxReceipt.mContractId.has_value())
{
std::cout << "No contract created!" << std::endl;
return 1;
}

const ContractId contractId = contractCreateTxReceipt.mContractId.value();
std::cout << "Smart contract created with ID " << contractId.toString() << std::endl;

// Get contract's function results
ContractFunctionResult contractFunctionResult =
contractCreateTxResponse.getRecord(client).mContractFunctionResult.value();

std::cout << "Contract Nonces: " << std::endl;
for (auto it = contractFunctionResult.mContractNonces.begin(); it != contractFunctionResult.mContractNonces.end();
++it)
{
std::cout << "ContractId: " << (*it).mContractId.toString() << std::endl;
std::cout << "Nonce: " << (*it).mNonce << std::endl << std::endl;
}

std::cout << std::endl << std::endl;

// Now delete the contract
txReceipt = ContractDeleteTransaction()
.setContractId(contractId)
.setTransferAccountId(operatorId)
.setMaxTransactionFee(Hbar(1LL))
.execute(client)
.getReceipt(client);
std::cout << "ContractDeleteTransaction execution completed with status: " << gStatusToString.at(txReceipt.mStatus)
<< std::endl;

return 0;
}
3 changes: 3 additions & 0 deletions sdk/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ add_library(${PROJECT_NAME} STATIC
src/LedgerId.cc
src/Mnemonic.cc
src/MnemonicBIP39.cc
src/NetworkVersionInfo.cc
src/NetworkVersionInfoQuery.cc
src/NftId.cc
src/PrivateKey.cc
src/PublicKey.cc
Expand All @@ -76,6 +78,7 @@ add_library(${PROJECT_NAME} STATIC
src/ScheduleInfo.cc
src/ScheduleInfoQuery.cc
src/ScheduleSignTransaction.cc
src/SemanticVersion.cc
src/StakingInfo.cc
src/Status.cc
src/SubscriptionHandle.cc
Expand Down
95 changes: 95 additions & 0 deletions sdk/main/include/NetworkVersionInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*-
*
* Hedera C++ SDK
*
* Copyright (C) 2020 - 2022 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.
*
*/
#ifndef HEDERA_SDK_CPP_NETWORK_VERSION_INFO_H_
#define HEDERA_SDK_CPP_NETWORK_VERSION_INFO_H_

#include "SemanticVersion.h"

#include <cstddef>
#include <memory>
#include <vector>

namespace proto
{
class NetworkGetVersionInfoResponse;
}

namespace Hedera
{
/**
* Contains information about the network's version.
*/
class NetworkVersionInfo
{
public:
NetworkVersionInfo() = default;

/**
* Construct from a protobuf schema SemanticVersion and a Hedera services SemanticVersion.
*
* @param hapi The SemanticVersion of the protobuf schema.
* @param hedera The SemanticVersion of the Hedera services.
*/
NetworkVersionInfo(const SemanticVersion& hapi, const SemanticVersion& hedera);

/**
* Construct a NetworkVersionInfo object from a NetworkGetVersionInfoResponse protobuf object.
*
* @param proto The NetworkGetVersionInfoResponse protobuf object from which to construct a NetworkVersionInfo object.
* @return The constructed NetworkVersionInfo object.
*/
[[nodiscard]] static NetworkVersionInfo fromProtobuf(const proto::NetworkGetVersionInfoResponse& proto);

/**
* Construct a NetworkVersionInfo object from a byte array.
*
* @param bytes The byte array from which to construct a NetworkVersionInfo object.
* @return The constructed NetworkVersionInfo object.
*/
[[nodiscard]] static NetworkVersionInfo fromBytes(const std::vector<std::byte>& bytes);

/**
* Construct a NetworkGetVersionInfoResponse protobuf object from this NetworkVersionInfo object.
*
* @return A pointer to the created NetworkVersionInfo protobuf object.
*/
[[nodiscard]] std::unique_ptr<proto::NetworkGetVersionInfoResponse> toProtobuf() const;

/**
* Construct a representative byte array from this NetworkVersionInfo object.
*
* @return The byte array representing this NetworkVersionInfo object.
*/
[[nodiscard]] std::vector<std::byte> toBytes() const;

/**
* The version of the protobuf schema in use by the network.
*/
SemanticVersion mProtobufVersion;

/**
* The version of the Hedera services in use by the network.
*/
SemanticVersion mServicesVersion;
};

} // namespace Hedera

#endif // HEDERA_SDK_CPP_NETWORK_VERSION_INFO_H_
83 changes: 83 additions & 0 deletions sdk/main/include/NetworkVersionInfoQuery.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*-
*
* Hedera C++ SDK
*
* Copyright (C) 2020 - 2022 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.
*
*/
#ifndef HEDERA_SDK_CPP_NETWORK_VERSION_INFO_QUERY_H_
#define HEDERA_SDK_CPP_NETWORK_VERSION_INFO_QUERY_H_

#include "Query.h"

namespace Hedera
{
class NetworkVersionInfo;
}

namespace Hedera
{
/**
* Get the deployed versions of Hedera Services and the HAPI proto in semantic version format.
*/
class NetworkVersionInfoQuery : public Query<NetworkVersionInfoQuery, NetworkVersionInfo>
{
private:
/**
* Derived from Executable. Construct a Query protobuf object from this NetworkVersionInfoQuery object.
*
* @param client The Client trying to construct this NetworkVersionInfoQuery.
* @param node The Node to which this NetworkVersionInfoQuery will be sent.
* @return A Query protobuf object filled with this NetworkVersionInfoQuery object's data.
*/
[[nodiscard]] proto::Query makeRequest(const Client& client,
const std::shared_ptr<internal::Node>& node) const override;

/**
* Derived from Executable. Construct a NetworkVersionInfo object from a Response protobuf object.
*
* @param response The Response protobuf object from which to construct a NetworkVersionInfo object.
* @return A NetworkVersionInfo object filled with the Response protobuf object's data.
*/
[[nodiscard]] NetworkVersionInfo mapResponse(const proto::Response& response) const override;

/**
* Derived from Executable. Get the status response code for a submitted NetworkVersionInfoQuery from a Response
* protobuf object.
*
* @param response The Response protobuf object from which to grab the NetworkVersionInfoQuery status response code.
* @return The NetworkVersionInfoQuery status response code of the input Response protobuf object.
*/
[[nodiscard]] Status mapResponseStatus(const proto::Response& response) const override;

/**
* Derived from Executable. Submit this NetworkVersionInfoQuery to a Node.
*
* @param client The Client submitting this NetworkVersionInfoQuery.
* @param deadline The deadline for submitting this NetworkVersionInfoQuery.
* @param node Pointer to the Node to which this NetworkVersionInfoQuery should be submitted.
* @param response Pointer to the Response protobuf object that gRPC should populate with the response information
* from the gRPC server.
* @return The gRPC status of the submission.
*/
[[nodiscard]] grpc::Status submitRequest(const Client& client,
const std::chrono::system_clock::time_point& deadline,
const std::shared_ptr<internal::Node>& node,
proto::Response* response) const override;
};

} // namespace Hedera

#endif // HEDERA_SDK_CPP_NETWORK_VERSION_INFO_QUERY_H_
Loading

0 comments on commit bf8443c

Please sign in to comment.