Skip to content

Commit

Permalink
Merge branch '00838-tck-c++-implement-json-rpc-method-endpoint-for-to…
Browse files Browse the repository at this point in the history
…kendissociatetransaction' into 00844-tck-c++-tck-c++-implement-json-rpc-method-endpoint-for-tokenfreezetransaction

Signed-off-by: Rob Walworth <[email protected]>
  • Loading branch information
rwalworth committed Dec 3, 2024
2 parents 40aa70b + 357219d commit 5600640
Show file tree
Hide file tree
Showing 6 changed files with 112 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 @@ -12,6 +12,7 @@ namespace Hiero::TCK::TokenService
struct AssociateTokenParams;
struct CreateTokenParams;
struct DeleteTokenParams;
struct DissociateTokenParams;
struct FreezeTokenParams;
struct UpdateTokenParams;

Expand Down Expand Up @@ -39,6 +40,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);

/**
* Freeze a token on an account.
*
Expand Down
2 changes: 1 addition & 1 deletion src/tck/include/token/params/DeleteTokenParams.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct DeleteTokenParams
std::optional<CommonTransactionParams> mCommonTxParams;
};

} // namespace Hedera::TCK::TokenService
} // namespace Hiero::TCK::TokenService

namespace nlohmann
{
Expand Down
62 changes: 62 additions & 0 deletions src/tck/include/token/params/DissociateTokenParams.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_DISSOCIATE_TOKEN_PARAMS_H_
#define HIERO_TCK_CPP_DISSOCIATE_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 `dissociateToken` JSON-RPC method call.
*/
struct DissociateTokenParams
{
/**
* The ID of the account from which to dissociate the token.
*/
std::optional<std::string> mAccountId;

/**
* The IDs of the tokens to dissociate.
*/
std::optional<std::vector<std::string>> mTokenIds;

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

} // namespace Hiero::TCK::TokenService

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

} // namespace nlohmann

#endif // HIERO_TCK_CPP_DISSOCIATE_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/AssociateTokenParams.h"
#include "token/params/CreateTokenParams.h"
#include "token/params/DeleteTokenParams.h"
#include "token/params/DissociateTokenParams.h"
#include "token/params/FreezeTokenParams.h"
#include "token/params/UpdateTokenParams.h"
#include "json/JsonErrorType.h"
Expand Down Expand Up @@ -357,6 +358,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::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::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("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("freezeToken", tckServer.getHandle(&TokenService::freezeToken));
tckServer.add("updateToken", tckServer.getHandle(&TokenService::updateToken));

Expand Down
36 changes: 36 additions & 0 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/AssociateTokenParams.h"
#include "token/params/CreateTokenParams.h"
#include "token/params/DeleteTokenParams.h"
#include "token/params/DissociateTokenParams.h"
#include "token/params/FreezeTokenParams.h"
#include "token/params/UpdateTokenParams.h"
#include "json/JsonErrorType.h"
Expand All @@ -15,6 +16,7 @@
#include <TokenAssociateTransaction.h>
#include <TokenCreateTransaction.h>
#include <TokenDeleteTransaction.h>
#include <TokenDissociateTransaction.h>
#include <TokenFreezeTransaction.h>
#include <TokenId.h>
#include <TokenSupplyType.h>
Expand Down Expand Up @@ -241,6 +243,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<TokenId> 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 freezeToken(const FreezeTokenParams& params)
{
Expand Down

0 comments on commit 5600640

Please sign in to comment.