Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tokenDissociateTransaction): Implement JSON-RPC method endpoint for TokenDissociateTransaction #839

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/sdk/main/src/ContractFunctionParameters.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const std::vector<std::byte> NEGATIVE_PADDING(31, std::byte(0xFF));
{
const std::vector<std::byte>& padding = negative ? NEGATIVE_PADDING : POSITIVE_PADDING;
return internal::Utilities::concatenateVectors({
{ padding.cbegin(), padding.cbegin() + getPaddingAmount(bytes) },
{padding.cbegin(), padding.cbegin() + getPaddingAmount(bytes)},
bytes
});
}
Expand All @@ -63,7 +63,7 @@ const std::vector<std::byte> NEGATIVE_PADDING(31, std::byte(0xFF));
[[nodiscard]] std::vector<std::byte> rightPad(const std::vector<std::byte>& bytes, bool = false)
{
return internal::Utilities::concatenateVectors({
bytes, { POSITIVE_PADDING.cbegin(), POSITIVE_PADDING.cbegin() + getPaddingAmount(bytes) }
bytes, {POSITIVE_PADDING.cbegin(), POSITIVE_PADDING.cbegin() + getPaddingAmount(bytes)}
});
}

Expand Down
18 changes: 18 additions & 0 deletions src/tck/include/token/TokenService.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,18 @@ namespace Hiero::TCK::TokenService
/**
* Forward declarations.
*/
class AssociateTokenParams;
class CreateTokenParams;
class DeleteTokenParams;
struct DissociateTokenParams;

/**
* 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.
Expand All @@ -28,6 +38,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);

} // namespace Hiero::TCK::TokenService

#endif // HIERO_TCK_CPP_TOKEN_SERVICE_H_
63 changes: 63 additions & 0 deletions src/tck/include/token/params/AssociateTokenParams.h
Original file line number Diff line number Diff line change
@@ -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 <nlohmann/json.hpp>
#include <optional>
#include <string>
#include <vector>

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

/**
* The IDs of the tokens to associate.
*/
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 AssociateTokenParams arguments properly.
*/
template<>
struct [[maybe_unused]] adl_serializer<Hiero::TCK::TokenService::AssociateTokenParams>
{
/**
* 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<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_ASSOCIATE_TOKEN_PARAMS_H_
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_
6 changes: 6 additions & 0 deletions src/tck/src/TckServer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,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 "json/JsonErrorType.h"
#include "json/JsonRpcException.h"
#include "json/JsonUtils.h"
Expand Down Expand Up @@ -345,9 +347,13 @@ template TckServer::MethodHandle TckServer::getHandle<SdkClient::ResetParams>(
template TckServer::MethodHandle TckServer::getHandle<SdkClient::SetupParams>(
nlohmann::json (*method)(const SdkClient::SetupParams&));

template TckServer::MethodHandle TckServer::getHandle<TokenService::AssociateTokenParams>(
nlohmann::json (*method)(const TokenService::AssociateTokenParams&));
template TckServer::MethodHandle TckServer::getHandle<TokenService::CreateTokenParams>(
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&));

} // namespace Hiero::TCK
2 changes: 2 additions & 0 deletions src/tck/src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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));

// Start listening for requests.
tckServer.startServer();
Expand Down
73 changes: 73 additions & 0 deletions src/tck/src/token/TokenService.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@
#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 "json/JsonErrorType.h"
#include "json/JsonRpcException.h"

#include <AccountId.h>
#include <Status.h>
#include <TokenAssociateTransaction.h>
#include <TokenCreateTransaction.h>
#include <TokenDeleteTransaction.h>
#include <TokenDissociateTransaction.h>
#include <TokenId.h>
#include <TokenSupplyType.h>
#include <TokenType.h>
Expand All @@ -23,9 +27,44 @@
#include <cstdint>
#include <nlohmann/json.hpp>
#include <string>
#include <vector>

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<TokenId> 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)
{
Expand Down Expand Up @@ -200,4 +239,38 @@ 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)}
};
}

} // namespace Hiero::TCK::TokenService
Loading