diff --git a/src/tck/include/token/TokenService.h b/src/tck/include/token/TokenService.h index 0c07aac9..aa197154 100644 --- a/src/tck/include/token/TokenService.h +++ b/src/tck/include/token/TokenService.h @@ -9,10 +9,20 @@ namespace Hiero::TCK::TokenService /** * Forward declarations. */ +struct AssociateTokenParams; struct CreateTokenParams; struct DeleteTokenParams; +struct DissociateTokenParams; struct UpdateTokenParams; +/** + * Associate an account with tokens. + * + * @param params The parameters to use to associate the account and tokens. + * @return A JSON response containing the status of the token association. + */ +nlohmann::json associateToken(const AssociateTokenParams& params); + /** * Create a token. * @@ -29,6 +39,14 @@ nlohmann::json createToken(const CreateTokenParams& params); */ nlohmann::json deleteToken(const DeleteTokenParams& params); +/** + * Dissociate an account from tokens. + * + * @param params The parameters to use to dissociate the account. + * @return A JSON response containing the status of the account dissociation. + */ +nlohmann::json dissociateToken(const DissociateTokenParams& params); + /** * Update a token. * diff --git a/src/tck/include/token/params/AssociateTokenParams.h b/src/tck/include/token/params/AssociateTokenParams.h new file mode 100644 index 00000000..ea7423f0 --- /dev/null +++ b/src/tck/include/token/params/AssociateTokenParams.h @@ -0,0 +1,63 @@ +// SPDX-License-Identifier: Apache-2.0 +#ifndef HIERO_TCK_CPP_ASSOCIATE_TOKEN_PARAMS_H_ +#define HIERO_TCK_CPP_ASSOCIATE_TOKEN_PARAMS_H_ + +#include "common/CommonTransactionParams.h" +#include "json/JsonUtils.h" + +#include +#include +#include +#include + +namespace Hiero::TCK::TokenService +{ +/** + * Struct to hold the arguments for an `associateToken` JSON-RPC method call. + */ +struct AssociateTokenParams +{ + /** + * The ID of the account with which to associate the tokens. + */ + std::optional mAccountId; + + /** + * The IDs of the tokens to associate. + */ + std::optional> mTokenIds; + + /** + * Any parameters common to all transaction types. + */ + std::optional mCommonTxParams; +}; + +} // namespace Hiero::TCK::TokenService + +namespace nlohmann +{ +/** + * JSON serializer template specialization required to convert AssociateTokenParams arguments properly. + */ +template<> +struct [[maybe_unused]] adl_serializer +{ + /** + * Convert a JSON object to a AssociateTokenParams. + * + * @param jsonFrom The JSON object with which to fill the AssociateTokenParams. + * @param params The AssociateTokenParams to fill with the JSON object. + */ + static void from_json(const json& jsonFrom, Hiero::TCK::TokenService::AssociateTokenParams& params) + { + params.mAccountId = Hiero::TCK::getOptionalJsonParameter(jsonFrom, "accountId"); + params.mTokenIds = Hiero::TCK::getOptionalJsonParameter>(jsonFrom, "tokenIds"); + params.mCommonTxParams = + Hiero::TCK::getOptionalJsonParameter(jsonFrom, "commonTransactionParams"); + } +}; + +} // namespace nlohmann + +#endif // HIERO_TCK_CPP_ASSOCIATE_TOKEN_PARAMS_H_ diff --git a/src/tck/include/token/params/DeleteTokenParams.h b/src/tck/include/token/params/DeleteTokenParams.h index d1ab7ccf..8f30ae6f 100644 --- a/src/tck/include/token/params/DeleteTokenParams.h +++ b/src/tck/include/token/params/DeleteTokenParams.h @@ -27,7 +27,7 @@ struct DeleteTokenParams std::optional mCommonTxParams; }; -} // namespace Hedera::TCK::TokenService +} // namespace Hiero::TCK::TokenService namespace nlohmann { diff --git a/src/tck/include/token/params/DissociateTokenParams.h b/src/tck/include/token/params/DissociateTokenParams.h new file mode 100644 index 00000000..2a696d40 --- /dev/null +++ b/src/tck/include/token/params/DissociateTokenParams.h @@ -0,0 +1,62 @@ +// SPDX-License-Identifier: Apache-2.0 +#ifndef HIERO_TCK_CPP_DISSOCIATE_TOKEN_PARAMS_H_ +#define HIERO_TCK_CPP_DISSOCIATE_TOKEN_PARAMS_H_ + +#include "common/CommonTransactionParams.h" +#include "json/JsonUtils.h" + +#include +#include +#include + +namespace Hiero::TCK::TokenService +{ +/** + * Struct to hold the arguments for a `dissociateToken` JSON-RPC method call. + */ +struct DissociateTokenParams +{ + /** + * The ID of the account from which to dissociate the token. + */ + std::optional mAccountId; + + /** + * The IDs of the tokens to dissociate. + */ + std::optional> mTokenIds; + + /** + * Any parameters common to all transaction types. + */ + std::optional mCommonTxParams; +}; + +} // namespace Hiero::TCK::TokenService + +namespace nlohmann +{ +/** + * JSON serializer template specialization required to convert DissociateTokenParams arguments properly. + */ +template<> +struct [[maybe_unused]] adl_serializer +{ + /** + * Convert a JSON object to a DissociateTokenParams. + * + * @param jsonFrom The JSON object with which to fill the DissociateTokenParams. + * @param params The DissociateTokenParams to fill with the JSON object. + */ + static void from_json(const json& jsonFrom, Hiero::TCK::TokenService::DissociateTokenParams& params) + { + params.mAccountId = Hiero::TCK::getOptionalJsonParameter(jsonFrom, "accountId"); + params.mTokenIds = Hiero::TCK::getOptionalJsonParameter>(jsonFrom, "tokenIds"); + params.mCommonTxParams = + Hiero::TCK::getOptionalJsonParameter(jsonFrom, "commonTransactionParams"); + } +}; + +} // namespace nlohmann + +#endif // HIERO_TCK_CPP_DISSOCIATE_TOKEN_PARAMS_H_ diff --git a/src/tck/src/TckServer.cc b/src/tck/src/TckServer.cc index 537815bd..2dcdb334 100644 --- a/src/tck/src/TckServer.cc +++ b/src/tck/src/TckServer.cc @@ -9,8 +9,10 @@ #include "key/params/GenerateKeyParams.h" #include "sdk/params/ResetParams.h" #include "sdk/params/SetupParams.h" +#include "token/params/AssociateTokenParams.h" #include "token/params/CreateTokenParams.h" #include "token/params/DeleteTokenParams.h" +#include "token/params/DissociateTokenParams.h" #include "token/params/UpdateTokenParams.h" #include "json/JsonErrorType.h" #include "json/JsonRpcException.h" @@ -349,10 +351,14 @@ template TckServer::MethodHandle TckServer::getHandle( template TckServer::MethodHandle TckServer::getHandle( nlohmann::json (*method)(const SdkClient::SetupParams&)); +template TckServer::MethodHandle TckServer::getHandle( + nlohmann::json (*method)(const TokenService::AssociateTokenParams&)); template TckServer::MethodHandle TckServer::getHandle( nlohmann::json (*method)(const TokenService::CreateTokenParams&)); template TckServer::MethodHandle TckServer::getHandle( nlohmann::json (*method)(const TokenService::DeleteTokenParams&)); +template TckServer::MethodHandle TckServer::getHandle( + nlohmann::json (*method)(const TokenService::DissociateTokenParams&)); template TckServer::MethodHandle TckServer::getHandle( nlohmann::json (*method)(const TokenService::UpdateTokenParams&)); diff --git a/src/tck/src/main.cc b/src/tck/src/main.cc index 2871dd80..c76ed45f 100644 --- a/src/tck/src/main.cc +++ b/src/tck/src/main.cc @@ -28,8 +28,10 @@ int main(int argc, char** argv) tckServer.add("updateAccount", tckServer.getHandle(&AccountService::updateAccount)); // Add the TokenService functions. + tckServer.add("associateToken", tckServer.getHandle(&TokenService::associateToken)); tckServer.add("createToken", tckServer.getHandle(&TokenService::createToken)); tckServer.add("deleteToken", tckServer.getHandle(&TokenService::deleteToken)); + tckServer.add("dissociateToken", tckServer.getHandle(&TokenService::dissociateToken)); tckServer.add("updateToken", tckServer.getHandle(&TokenService::updateToken)); // Start listening for requests. diff --git a/src/tck/src/token/TokenService.cc b/src/tck/src/token/TokenService.cc index 51ed420d..b1a69fde 100644 --- a/src/tck/src/token/TokenService.cc +++ b/src/tck/src/token/TokenService.cc @@ -2,16 +2,20 @@ #include "token/TokenService.h" #include "key/KeyService.h" #include "sdk/SdkClient.h" +#include "token/params/AssociateTokenParams.h" #include "token/params/CreateTokenParams.h" #include "token/params/DeleteTokenParams.h" +#include "token/params/DissociateTokenParams.h" #include "token/params/UpdateTokenParams.h" #include "json/JsonErrorType.h" #include "json/JsonRpcException.h" #include #include +#include #include #include +#include #include #include #include @@ -25,9 +29,44 @@ #include #include #include +#include namespace Hiero::TCK::TokenService { +//----- +nlohmann::json associateToken(const AssociateTokenParams& params) +{ + TokenAssociateTransaction tokenAssociateTransaction; + tokenAssociateTransaction.setGrpcDeadline(SdkClient::DEFAULT_TCK_REQUEST_TIMEOUT); + + if (params.mAccountId.has_value()) + { + tokenAssociateTransaction.setAccountId(AccountId::fromString(params.mAccountId.value())); + } + + if (params.mTokenIds.has_value()) + { + std::vector tokenIds; + for (const std::string& tokenId : params.mTokenIds.value()) + { + tokenIds.push_back(TokenId::fromString(tokenId)); + } + + tokenAssociateTransaction.setTokenIds(tokenIds); + } + + if (params.mCommonTxParams.has_value()) + { + params.mCommonTxParams->fillOutTransaction(tokenAssociateTransaction, SdkClient::getClient()); + } + + return { + {"status", + gStatusToString.at( + tokenAssociateTransaction.execute(SdkClient::getClient()).getReceipt(SdkClient::getClient()).mStatus)} + }; +} + //----- nlohmann::json createToken(const CreateTokenParams& params) { @@ -202,6 +241,40 @@ nlohmann::json deleteToken(const DeleteTokenParams& params) }; } +//----- +nlohmann::json dissociateToken(const DissociateTokenParams& params) +{ + TokenDissociateTransaction tokenDissociateTransaction; + tokenDissociateTransaction.setGrpcDeadline(std::chrono::seconds(SdkClient::DEFAULT_TCK_REQUEST_TIMEOUT)); + + if (params.mAccountId.has_value()) + { + tokenDissociateTransaction.setAccountId(AccountId::fromString(params.mAccountId.value())); + } + + if (params.mTokenIds.has_value()) + { + std::vector tokenIds; + for (const std::string& tokenId : params.mTokenIds.value()) + { + tokenIds.push_back(TokenId::fromString(tokenId)); + } + + tokenDissociateTransaction.setTokenIds(tokenIds); + } + + if (params.mCommonTxParams.has_value()) + { + params.mCommonTxParams->fillOutTransaction(tokenDissociateTransaction, SdkClient::getClient()); + } + + return { + {"status", + gStatusToString.at( + tokenDissociateTransaction.execute(SdkClient::getClient()).getReceipt(SdkClient::getClient()).mStatus)} + }; +} + //----- nlohmann::json updateToken(const UpdateTokenParams& params) {