From 0a2da07f8b851a2bb1c50cafed75f16be28d22bf Mon Sep 17 00:00:00 2001 From: Rob Walworth <110835868+rwalworth@users.noreply.github.com> Date: Wed, 15 Jan 2025 03:04:51 -0500 Subject: [PATCH] feat(tokenFreezeTransaction): Implement JSON-RPC method endpoint for `TokenFreezeTransaction` (#848) Signed-off-by: Rob Walworth Co-authored-by: gsstoykov --- src/tck/include/token/TokenService.h | 9 +++ .../include/token/params/FreezeTokenParams.h | 62 +++++++++++++++++++ src/tck/src/TckServer.cc | 3 + src/tck/src/main.cc | 1 + src/tck/src/token/TokenService.cc | 32 +++++++++- 5 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 src/tck/include/token/params/FreezeTokenParams.h diff --git a/src/tck/include/token/TokenService.h b/src/tck/include/token/TokenService.h index 23c68fd2..e916e421 100644 --- a/src/tck/include/token/TokenService.h +++ b/src/tck/include/token/TokenService.h @@ -13,6 +13,7 @@ struct AssociateTokenParams; struct CreateTokenParams; struct DeleteTokenParams; struct DissociateTokenParams; +struct FreezeTokenParams; struct PauseTokenParams; struct UnpauseTokenParams; struct UpdateTokenFeeScheduleParams; @@ -50,6 +51,14 @@ nlohmann::json deleteToken(const DeleteTokenParams& params); */ nlohmann::json dissociateToken(const DissociateTokenParams& params); +/** + * Freeze a token on an account. + * + * @params The parameters to use to freeze a token. + * @return A JSON response containing the status of the token freeze. + */ +nlohmann::json freezeToken(const FreezeTokenParams& params); + /** * Pause a token. * diff --git a/src/tck/include/token/params/FreezeTokenParams.h b/src/tck/include/token/params/FreezeTokenParams.h new file mode 100644 index 00000000..18aa2c62 --- /dev/null +++ b/src/tck/include/token/params/FreezeTokenParams.h @@ -0,0 +1,62 @@ +// SPDX-License-Identifier: Apache-2.0 +#ifndef HIERO_TCK_CPP_FREEZE_TOKEN_PARAMS_H_ +#define HIERO_TCK_CPP_FREEZE_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 `freezeToken` JSON-RPC method call. + */ +struct FreezeTokenParams +{ + /** + * The ID of the token to freeze. + */ + std::optional mTokenId; + + /** + * The ID of the account to freeze. + */ + std::optional mAccountId; + + /** + * Any parameters common to all transaction types. + */ + std::optional mCommonTxParams; +}; + +} // namespace Hedera::TCK::TokenService + +namespace nlohmann +{ +/** + * JSON serializer template specialization required to convert FreezeTokenParams arguments properly. + */ +template<> +struct [[maybe_unused]] adl_serializer +{ + /** + * Convert a JSON object to a FreezeTokenParams. + * + * @param jsonFrom The JSON object with which to fill the FreezeTokenParams. + * @param params The FreezeTokenParams to fill with the JSON object. + */ + static void from_json(const json& jsonFrom, Hiero::TCK::TokenService::FreezeTokenParams& params) + { + params.mTokenId = Hiero::TCK::getOptionalJsonParameter(jsonFrom, "tokenId"); + params.mAccountId = Hiero::TCK::getOptionalJsonParameter(jsonFrom, "accountId"); + params.mCommonTxParams = + Hiero::TCK::getOptionalJsonParameter(jsonFrom, "commonTransactionParams"); + } +}; + +} // namespace nlohmann + +#endif // HIERO_TCK_CPP_FREEZE_TOKEN_PARAMS_H_ diff --git a/src/tck/src/TckServer.cc b/src/tck/src/TckServer.cc index 662330d9..32d240d5 100644 --- a/src/tck/src/TckServer.cc +++ b/src/tck/src/TckServer.cc @@ -13,6 +13,7 @@ #include "token/params/CreateTokenParams.h" #include "token/params/DeleteTokenParams.h" #include "token/params/DissociateTokenParams.h" +#include "token/params/FreezeTokenParams.h" #include "token/params/PauseTokenParams.h" #include "token/params/UnpauseTokenParams.h" #include "token/params/UpdateTokenFeeScheduleParams.h" @@ -362,6 +363,8 @@ template TckServer::MethodHandle TckServer::getHandle( nlohmann::json (*method)(const TokenService::DissociateTokenParams&)); +template TckServer::MethodHandle TckServer::getHandle( + nlohmann::json (*method)(const TokenService::FreezeTokenParams&)); template TckServer::MethodHandle TckServer::getHandle( nlohmann::json (*method)(const TokenService::PauseTokenParams&)); template TckServer::MethodHandle TckServer::getHandle( diff --git a/src/tck/src/main.cc b/src/tck/src/main.cc index 0333cc1d..efb176ad 100644 --- a/src/tck/src/main.cc +++ b/src/tck/src/main.cc @@ -32,6 +32,7 @@ int main(int argc, char** argv) tckServer.add("createToken", tckServer.getHandle(&TokenService::createToken)); tckServer.add("deleteToken", tckServer.getHandle(&TokenService::deleteToken)); tckServer.add("dissociateToken", tckServer.getHandle(&TokenService::dissociateToken)); + tckServer.add("freezeToken", tckServer.getHandle(&TokenService::freezeToken)); tckServer.add("pauseToken", tckServer.getHandle(&TokenService::pauseToken)); tckServer.add("unpauseToken", tckServer.getHandle(&TokenService::unpauseToken)); tckServer.add("updateTokenFeeSchedule", tckServer.getHandle(&TokenService::updateTokenFeeSchedule)); diff --git a/src/tck/src/token/TokenService.cc b/src/tck/src/token/TokenService.cc index d85879a5..6850296a 100644 --- a/src/tck/src/token/TokenService.cc +++ b/src/tck/src/token/TokenService.cc @@ -6,6 +6,7 @@ #include "token/params/CreateTokenParams.h" #include "token/params/DeleteTokenParams.h" #include "token/params/DissociateTokenParams.h" +#include "token/params/FreezeTokenParams.h" #include "token/params/PauseTokenParams.h" #include "token/params/UnpauseTokenParams.h" #include "token/params/UpdateTokenFeeScheduleParams.h" @@ -20,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -281,6 +283,34 @@ nlohmann::json dissociateToken(const DissociateTokenParams& params) }; } +//----- +nlohmann::json freezeToken(const FreezeTokenParams& params) +{ + TokenFreezeTransaction tokenFreezeTransaction; + tokenFreezeTransaction.setGrpcDeadline(SdkClient::DEFAULT_TCK_REQUEST_TIMEOUT); + + if (params.mTokenId.has_value()) + { + tokenFreezeTransaction.setTokenId(TokenId::fromString(params.mTokenId.value())); + } + + if (params.mAccountId.has_value()) + { + tokenFreezeTransaction.setAccountId(AccountId::fromString(params.mAccountId.value())); + } + + if (params.mCommonTxParams.has_value()) + { + params.mCommonTxParams->fillOutTransaction(tokenFreezeTransaction, SdkClient::getClient()); + } + + return { + {"status", + gStatusToString.at( + tokenFreezeTransaction.execute(SdkClient::getClient()).getReceipt(SdkClient::getClient()).mStatus)} + }; +} + //----- nlohmann::json pauseToken(const PauseTokenParams& params) { @@ -359,7 +389,7 @@ nlohmann::json updateTokenFeeSchedule(const UpdateTokenFeeScheduleParams& params 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()) {