Skip to content

Commit

Permalink
Merge branch '00858-tck-c++-implement-json-rpc-method-endpoint-for-to…
Browse files Browse the repository at this point in the history
…kenminttransaction' into 00859-tck-c++-implement-json-rpc-method-endpoint-for-tokenburntransaction
  • Loading branch information
rwalworth committed Dec 13, 2024
2 parents b2abb2b + 7d5b761 commit ee5e943
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/tck/include/token/TokenService.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Hiero::TCK::TokenService
struct BurnTokenParams;
struct CreateTokenParams;
struct DeleteTokenParams;
struct MintTokenParams;
struct UpdateTokenParams;

/**
Expand All @@ -38,6 +39,14 @@ nlohmann::json createToken(const CreateTokenParams& params);
*/
nlohmann::json deleteToken(const DeleteTokenParams& params);

/**
* Mint a token.
*
* @param params The parameters to use to mint a token.
* @return A JSON response containing the status of the token mint.
*/
nlohmann::json mintToken(const MintTokenParams& params);

/**
* Update a token.
*
Expand Down
68 changes: 68 additions & 0 deletions src/tck/include/token/params/MintTokenParams.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// SPDX-License-Identifier: Apache-2.0
#ifndef HIERO_TCK_CPP_MINT_TOKEN_PARAMS_H_
#define HIERO_TCK_CPP_MINT_TOKEN_PARAMS_H_

#include "common/CommonTransactionParams.h"
#include "json/JsonUtils.h"

#include <nlohmann/json.hpp>
#include <optional>
#include <string>

namespace Hiero::TCK::TokenService
{
/**
* Struct to hold the arguments for a `mintToken` JSON-RPC method call.
*/
struct MintTokenParams
{
/**
* The ID of the token to mint.
*/
std::optional<std::string> mTokenId;

/**
* The amount of fungible tokens to mint.
*/
std::optional<std::string> mAmount;

/**
* The metadata for the NFTs to mint, as hex strings.
*/
std::optional<std::vector<std::string>> mMetadata;

/**
* Any parameters common to all transaction types.
*/
std::optional<CommonTransactionParams> mCommonTxParams;
};

} // namespace Hedera::TCK::TokenService

namespace nlohmann
{
/**
* JSON serializer template specialization required to convert MintTokenParams arguments properly.
*/
template<>
struct [[maybe_unused]] adl_serializer<Hiero::TCK::TokenService::MintTokenParams>
{
/**
* Convert a JSON object to a MintTokenParams.
*
* @param jsonFrom The JSON object with which to fill the MintTokenParams.
* @param params The MintTokenParams to fill with the JSON object.
*/
static void from_json(const json& jsonFrom, Hiero::TCK::TokenService::MintTokenParams& params)
{
params.mTokenId = Hiero::TCK::getOptionalJsonParameter<std::string>(jsonFrom, "tokenId");
params.mAmount = Hiero::TCK::getOptionalJsonParameter<std::string>(jsonFrom, "amount");
params.mMetadata = Hiero::TCK::getOptionalJsonParameter<std::vector<std::string>>(jsonFrom, "metadata");
params.mCommonTxParams =
Hiero::TCK::getOptionalJsonParameter<Hiero::TCK::CommonTransactionParams>(jsonFrom, "commonTransactionParams");
}
};

} // namespace nlohmann

#endif // HIERO_TCK_CPP_MINT_TOKEN_PARAMS_H_
3 changes: 3 additions & 0 deletions src/tck/src/TckServer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "token/params/BurnTokenParams.h"
#include "token/params/CreateTokenParams.h"
#include "token/params/DeleteTokenParams.h"
#include "token/params/MintTokenParams.h"
#include "token/params/UpdateTokenParams.h"
#include "json/JsonErrorType.h"
#include "json/JsonRpcException.h"
Expand Down Expand Up @@ -356,6 +357,8 @@ template TckServer::MethodHandle TckServer::getHandle<TokenService::CreateTokenP
nlohmann::json (*method)(const TokenService::CreateTokenParams&));
template TckServer::MethodHandle TckServer::getHandle<TokenService::DeleteTokenParams>(
nlohmann::json (*method)(const TokenService::DeleteTokenParams&));
template TckServer::MethodHandle TckServer::getHandle<TokenService::MintTokenParams>(
nlohmann::json (*method)(const TokenService::MintTokenParams&));
template TckServer::MethodHandle TckServer::getHandle<TokenService::UpdateTokenParams>(
nlohmann::json (*method)(const TokenService::UpdateTokenParams&));

Expand Down
1 change: 1 addition & 0 deletions src/tck/src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ int main(int argc, char** argv)
tckServer.add("burnToken", tckServer.getHandle(&TokenService::burnToken));
tckServer.add("createToken", tckServer.getHandle(&TokenService::createToken));
tckServer.add("deleteToken", tckServer.getHandle(&TokenService::deleteToken));
tckServer.add("mintToken", tckServer.getHandle(&TokenService::mintToken));
tckServer.add("updateToken", tckServer.getHandle(&TokenService::updateToken));

// Start listening for requests.
Expand Down
58 changes: 56 additions & 2 deletions src/tck/src/token/TokenService.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "token/params/BurnTokenParams.h"
#include "token/params/CreateTokenParams.h"
#include "token/params/DeleteTokenParams.h"
#include "token/params/MintTokenParams.h"
#include "token/params/UpdateTokenParams.h"
#include "json/JsonErrorType.h"
#include "json/JsonRpcException.h"
Expand All @@ -15,12 +16,14 @@
#include <TokenCreateTransaction.h>
#include <TokenDeleteTransaction.h>
#include <TokenId.h>
#include <TokenMintTransaction.h>
#include <TokenSupplyType.h>
#include <TokenType.h>
#include <TokenUpdateTransaction.h>
#include <TransactionReceipt.h>
#include <TransactionResponse.h>
#include <impl/EntityIdHelper.h>
#include <impl/HexConverter.h>
#include <impl/Utilities.h>

#include <chrono>
Expand Down Expand Up @@ -226,7 +229,7 @@ nlohmann::json createToken(const CreateTokenParams& params)
nlohmann::json deleteToken(const DeleteTokenParams& params)
{
TokenDeleteTransaction tokenDeleteTransaction;
tokenDeleteTransaction.setGrpcDeadline(std::chrono::seconds(SdkClient::DEFAULT_TCK_REQUEST_TIMEOUT));
tokenDeleteTransaction.setGrpcDeadline(SdkClient::DEFAULT_TCK_REQUEST_TIMEOUT);

if (params.mTokenId.has_value())
{
Expand All @@ -245,11 +248,62 @@ nlohmann::json deleteToken(const DeleteTokenParams& params)
};
}

//-----
nlohmann::json mintToken(const MintTokenParams& params)
{
TokenMintTransaction tokenMintTransaction;
tokenMintTransaction.setGrpcDeadline(SdkClient::DEFAULT_TCK_REQUEST_TIMEOUT);

if (params.mTokenId.has_value())
{
tokenMintTransaction.setTokenId(TokenId::fromString(params.mTokenId.value()));
}

if (params.mAmount.has_value())
{
tokenMintTransaction.setAmount(Hiero::internal::EntityIdHelper::getNum(params.mAmount.value()));
}

if (params.mMetadata.has_value())
{
std::vector<std::vector<std::byte>> allMetadata;
for (const std::string& metadata : params.mMetadata.value())
{
allMetadata.push_back(internal::HexConverter::hexToBytes(metadata));
}

tokenMintTransaction.setMetadata(allMetadata);
}

if (params.mCommonTxParams.has_value())
{
params.mCommonTxParams->fillOutTransaction(tokenMintTransaction, SdkClient::getClient());
}

const TransactionReceipt txReceipt =
tokenMintTransaction.execute(SdkClient::getClient()).getReceipt(SdkClient::getClient());

nlohmann::json response = {
{"status", gStatusToString.at(txReceipt.mStatus) },
{ "newTotalSupply", std::to_string(txReceipt.mNewTotalSupply.value())}
};

if (!txReceipt.mSerialNumbers.empty())
{
for (const uint64_t& serialNumber : txReceipt.mSerialNumbers)
{
response["serialNumbers"].push_back(std::to_string(serialNumber));
}
}

return response;
}

//-----
nlohmann::json updateToken(const UpdateTokenParams& params)
{
TokenUpdateTransaction tokenUpdateTransaction;
tokenUpdateTransaction.setGrpcDeadline(std::chrono::seconds(SdkClient::DEFAULT_TCK_REQUEST_TIMEOUT));
tokenUpdateTransaction.setGrpcDeadline(SdkClient::DEFAULT_TCK_REQUEST_TIMEOUT);

if (params.mTokenId.has_value())
{
Expand Down

0 comments on commit ee5e943

Please sign in to comment.