Skip to content

Commit

Permalink
feat(tokenFreezeTransaction): Implement JSON-RPC method endpoint for …
Browse files Browse the repository at this point in the history
…`TokenFreezeTransaction` (#848)

Signed-off-by: Rob Walworth <[email protected]>
Co-authored-by: gsstoykov <[email protected]>
  • Loading branch information
rwalworth and gsstoykov authored Jan 15, 2025
1 parent 52e6ae7 commit 0a2da07
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/tck/include/token/TokenService.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct AssociateTokenParams;
struct CreateTokenParams;
struct DeleteTokenParams;
struct DissociateTokenParams;
struct FreezeTokenParams;
struct PauseTokenParams;
struct UnpauseTokenParams;
struct UpdateTokenFeeScheduleParams;
Expand Down Expand Up @@ -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.
*
Expand Down
62 changes: 62 additions & 0 deletions src/tck/include/token/params/FreezeTokenParams.h
Original file line number Diff line number Diff line change
@@ -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 <nlohmann/json.hpp>
#include <optional>
#include <string>

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<std::string> mTokenId;

/**
* The ID of the account to freeze.
*/
std::optional<std::string> mAccountId;

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

} // namespace Hedera::TCK::TokenService

namespace nlohmann
{
/**
* JSON serializer template specialization required to convert FreezeTokenParams arguments properly.
*/
template<>
struct [[maybe_unused]] adl_serializer<Hiero::TCK::TokenService::FreezeTokenParams>
{
/**
* 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<std::string>(jsonFrom, "tokenId");
params.mAccountId = Hiero::TCK::getOptionalJsonParameter<std::string>(jsonFrom, "accountId");
params.mCommonTxParams =
Hiero::TCK::getOptionalJsonParameter<Hiero::TCK::CommonTransactionParams>(jsonFrom, "commonTransactionParams");
}
};

} // namespace nlohmann

#endif // HIERO_TCK_CPP_FREEZE_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 @@ -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"
Expand Down Expand Up @@ -362,6 +363,8 @@ template TckServer::MethodHandle TckServer::getHandle<TokenService::DeleteTokenP
nlohmann::json (*method)(const TokenService::DeleteTokenParams&));
template TckServer::MethodHandle TckServer::getHandle<TokenService::DissociateTokenParams>(
nlohmann::json (*method)(const TokenService::DissociateTokenParams&));
template TckServer::MethodHandle TckServer::getHandle<TokenService::FreezeTokenParams>(
nlohmann::json (*method)(const TokenService::FreezeTokenParams&));
template TckServer::MethodHandle TckServer::getHandle<TokenService::PauseTokenParams>(
nlohmann::json (*method)(const TokenService::PauseTokenParams&));
template TckServer::MethodHandle TckServer::getHandle<TokenService::UnpauseTokenParams>(
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 @@ -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));
Expand Down
32 changes: 31 additions & 1 deletion src/tck/src/token/TokenService.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -20,6 +21,7 @@
#include <TokenDeleteTransaction.h>
#include <TokenDissociateTransaction.h>
#include <TokenFeeScheduleUpdateTransaction.h>
#include <TokenFreezeTransaction.h>
#include <TokenId.h>
#include <TokenPauseTransaction.h>
#include <TokenSupplyType.h>
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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())
{
Expand Down

0 comments on commit 0a2da07

Please sign in to comment.