Skip to content

Commit

Permalink
488: Add AccountCreateWithHtsExample (#490)
Browse files Browse the repository at this point in the history
Signed-off-by: Rob Walworth <[email protected]>
Co-authored-by: Deyan Zhekov <[email protected]>
  • Loading branch information
rwalworth and deyanzz authored Sep 1, 2023
1 parent 6dbc639 commit f4fe5aa
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 0 deletions.
176 changes: 176 additions & 0 deletions sdk/examples/AccountCreateWithHtsExample.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/*-
*
* 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 "AccountInfo.h"
#include "AccountInfoQuery.h"
#include "Client.h"
#include "ECDSAsecp256k1PrivateKey.h"
#include "ED25519PrivateKey.h"
#include "TokenCreateTransaction.h"
#include "TokenId.h"
#include "TokenMintTransaction.h"
#include "TokenNftInfo.h"
#include "TokenNftInfoQuery.h"
#include "TransactionReceipt.h"
#include "TransactionResponse.h"
#include "TransferTransaction.h"
#include "impl/Utilities.h"

#include <array>
#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;
}

const AccountId operatorAccountId = AccountId::fromString(argv[1]);
const std::shared_ptr<ED25519PrivateKey> operatorPrivateKey = ED25519PrivateKey::fromString(argv[2]);

// 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(operatorAccountId, operatorPrivateKey.get());

// IPFS content identifiers for the NFT metadata
const std::vector<std::vector<std::byte>> CIDs = {
internal::Utilities::stringToByteVector("QmNPCiNA3Dsu3K5FxDPMG5Q3fZRwVTg14EXA92uqEeSRXn"),
internal::Utilities::stringToByteVector("QmZ4dgAgt8owvnULxnKxNe8YqpavtVCXmc1Lt2XajFpJs9"),
internal::Utilities::stringToByteVector("QmPzY5GxevjyfMUF5vEAjtyRoigzWp47MiKAtLBduLMC1T"),
internal::Utilities::stringToByteVector("Qmd3kGgSrAwwSrhesYcY7K54f3qD7MDo38r7Po2dChtQx5"),
internal::Utilities::stringToByteVector("QmWgkKz3ozgqtnvbCLeh7EaR1H8u5Sshx3ZJzxkcrT3jbw")
};

std::cout << "Example 1" << std::endl;
std::cout << "---------" << std::endl;

/**
* Step 1: Create an NFT using the Hedera token service.
*/
TokenId tokenId = TokenCreateTransaction()
.setTokenName("HIP-542 Example Collection")
.setTokenSymbol("HIP-542")
.setTokenType(TokenType::NON_FUNGIBLE_UNIQUE)
.setDecimals(0)
.setInitialSupply(0)
.setMaxSupply(CIDs.size())
.setTreasuryAccountId(operatorAccountId)
.setSupplyType(TokenSupplyType::FINITE)
.setAdminKey(operatorPrivateKey)
.setSupplyKey(operatorPrivateKey)
.execute(client)
.getReceipt(client)
.mTokenId.value();
std::cout << "Created NFT with ID: " << tokenId.toString() << std::endl;

/**
* Step 2: Mint the NFTs.
*/
TransactionReceipt txReceipt =
TokenMintTransaction().setTokenId(tokenId).setMetadata(CIDs).execute(client).getReceipt(client);
std::cout << "Minted " << txReceipt.mSerialNumbers.size() << " NFTs" << std::endl;

/**
* Step 3: Create an ECDSAsecp256k1PublicKey alias.
*/
std::shared_ptr<PublicKey> alias = ECDSAsecp256k1PrivateKey::generatePrivateKey()->getPublicKey();
AccountId aliasAccountId = alias->toAccountId();
std::cout << "Created alias: " << aliasAccountId.toString() << std::endl;

/**
* Step 4: Transfer an NFT to the ECDSAsecp256k1PublicKey alias.
*/
const auto nftId = NftId(tokenId, txReceipt.mSerialNumbers.at(0));
std::cout << "Transferring NFT " << nftId.toString() << " to alias account: "
<< gStatusToString.at(TransferTransaction()
.addNftTransfer(nftId, operatorAccountId, aliasAccountId)
.execute(client)
.setValidateStatus(false)
.getReceipt(client)
.mStatus)
<< std::endl;

/**
* Step 5: Query the NFT to see the owner.
*/
const TokenNftInfo tokenNftInfo = TokenNftInfoQuery().setNftId(nftId).execute(client);
std::cout << "NFT " << nftId.toString() << " owner: " << tokenNftInfo.mAccountId.toString() << std::endl;

/**
* Step 6: Get the new account ID of the alias.
*/
std::cout << "The NFT owner account ID "
<< (AccountInfoQuery().setAccountId(aliasAccountId).execute(client).mAccountId == tokenNftInfo.mAccountId
? "matches "
: "does not match ")
<< "the account ID created by HTS." << std::endl;

std::cout << std::endl;
std::cout << "Example 2" << std::endl;
std::cout << "---------" << std::endl;

/**
* Step 1: Create a fungible token using the Hedera token service.
*/
tokenId = TokenCreateTransaction()
.setTokenName("HIP-542 Token")
.setTokenSymbol("H542")
.setTokenType(TokenType::FUNGIBLE_COMMON)
.setTreasuryAccountId(operatorAccountId)
.setInitialSupply(10000) // Total supply = 10000 / 10 ^ 2
.setDecimals(2)
.setAutoRenewAccountId(operatorAccountId)
.execute(client)
.getReceipt(client)
.mTokenId.value();
std::cout << "Created fungible token with ID: " << tokenId.toString() << std::endl;

/**
* Step 2: Create an ECDSAsecp256k1PublicKey alias.
*/
alias = ECDSAsecp256k1PrivateKey::generatePrivateKey()->getPublicKey();
aliasAccountId = alias->toAccountId();
std::cout << "Created alias: " << aliasAccountId.toString() << std::endl;

/**
* Step 3: Transfer the fungible token to the ECDSAsecp256k1PublicKey alias.
*/
std::cout << "Transferring token " << tokenId.toString() << " to alias account: "
<< gStatusToString.at(TransferTransaction()
.addTokenTransfer(tokenId, operatorAccountId, -10LL)
.addTokenTransfer(tokenId, aliasAccountId, 10LL)
.execute(client)
.setValidateStatus(false)
.getReceipt(client)
.mStatus)
<< std::endl;

/**
* Step 4: Get the new account ID of the alias.
*/
std::cout << "The new account ID of the alias is "
<< AccountInfoQuery().setAccountId(aliasAccountId).execute(client).mAccountId.toString() << std::endl;

return 0;
}
4 changes: 4 additions & 0 deletions sdk/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
set(ACCOUNT_ALIAS_EXAMPLE_NAME ${PROJECT_NAME}-account-alias-example)
set(ACCOUNT_ALLOWANCE_EXAMPLE_NAME ${PROJECT_NAME}-account-allowance-example)
set(ACCOUNT_CREATE_WITH_HTS_EXAMPLE_NAME ${PROJECT_NAME}-account-create-with-hts-example)
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)
Expand All @@ -26,6 +27,7 @@ set(UPDATE_ACCOUNT_PUBLIC_KEY_EXAMPLE_NAME ${PROJECT_NAME}-update-account-public

add_executable(${ACCOUNT_ALIAS_EXAMPLE_NAME} AccountAliasExample.cc)
add_executable(${ACCOUNT_ALLOWANCE_EXAMPLE_NAME} AccountAllowanceExample.cc)
add_executable(${ACCOUNT_CREATE_WITH_HTS_EXAMPLE_NAME} AccountCreateWithHtsExample.cc)
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)
Expand Down Expand Up @@ -70,6 +72,7 @@ endif ()

target_link_libraries(${ACCOUNT_ALIAS_EXAMPLE_NAME} PUBLIC ${PROJECT_NAME})
target_link_libraries(${ACCOUNT_ALLOWANCE_EXAMPLE_NAME} PUBLIC ${PROJECT_NAME})
target_link_libraries(${ACCOUNT_CREATE_WITH_HTS_EXAMPLE_NAME} PUBLIC ${PROJECT_NAME})
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})
Expand Down Expand Up @@ -99,6 +102,7 @@ install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE} DESTINATION ex
install(TARGETS
${ACCOUNT_ALIAS_EXAMPLE_NAME}
${ACCOUNT_ALLOWANCE_EXAMPLE_NAME}
${ACCOUNT_CREATE_WITH_HTS_EXAMPLE_NAME}
${CONSENSUS_PUB_SUB_EXAMPLE_NAME}
${CREATE_ACCOUNT_EXAMPLE_NAME}
${CREATE_SIMPLE_CONTRACT_EXAMPLE_NAME}
Expand Down

0 comments on commit f4fe5aa

Please sign in to comment.