Skip to content

Commit

Permalink
feat(tokenBurnTransaction): Implement JSON-RPC method endpoint for `T…
Browse files Browse the repository at this point in the history
…okenBurnTransaction` (#861)

Signed-off-by: Rob Walworth <[email protected]>
  • Loading branch information
rwalworth authored Jan 16, 2025
1 parent 7b0b610 commit 9064070
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/tck/include/token/TokenService.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Hiero::TCK::TokenService
* Forward declarations.
*/
struct AssociateTokenParams;
struct BurnTokenParams;
struct CreateTokenParams;
struct DeleteTokenParams;
struct DissociateTokenParams;
Expand All @@ -31,6 +32,14 @@ struct UpdateTokenParams;
*/
nlohmann::json associateToken(const AssociateTokenParams& params);

/**
* Burn a token.
*
* @param params The parameters to use to burn a token.
* @return A JSON response containing the status of the token burn and the new total supply of the token.
*/
nlohmann::json burnToken(const BurnTokenParams& params);

/**
* Create a token.
*
Expand Down Expand Up @@ -120,6 +129,7 @@ nlohmann::json unpauseToken(const UnpauseTokenParams& params);
nlohmann::json updateTokenFeeSchedule(const UpdateTokenFeeScheduleParams& params);

/**
>>>>>>> main
* Update a token.
*
* @param params The parameters to use to update a token.
Expand Down
68 changes: 68 additions & 0 deletions src/tck/include/token/params/BurnTokenParams.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// SPDX-License-Identifier: Apache-2.0
#ifndef HIERO_TCK_CPP_BURN_TOKEN_PARAMS_H_
#define HIERO_TCK_CPP_BURN_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 `burnToken` JSON-RPC method call.
*/
struct BurnTokenParams
{
/**
* The ID of the token to burn.
*/
std::optional<std::string> mTokenId;

/**
* The amount of fungible tokens to burn.
*/
std::optional<std::string> mAmount;

/**
* The serial numbers of the NFTs to burn.
*/
std::optional<std::vector<std::string>> mSerialNumbers;

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

} // namespace Hedera::TCK::TokenService

namespace nlohmann
{
/**
* JSON serializer template specialization required to convert BurnTokenParams arguments properly.
*/
template<>
struct [[maybe_unused]] adl_serializer<Hiero::TCK::TokenService::BurnTokenParams>
{
/**
* Convert a JSON object to a BurnTokenParams.
*
* @param jsonFrom The JSON object with which to fill the BurnTokenParams.
* @param params The BurnTokenParams to fill with the JSON object.
*/
static void from_json(const json& jsonFrom, Hiero::TCK::TokenService::BurnTokenParams& params)
{
params.mTokenId = Hiero::TCK::getOptionalJsonParameter<std::string>(jsonFrom, "tokenId");
params.mAmount = Hiero::TCK::getOptionalJsonParameter<std::string>(jsonFrom, "amount");
params.mSerialNumbers = Hiero::TCK::getOptionalJsonParameter<std::vector<std::string>>(jsonFrom, "serialNumbers");
params.mCommonTxParams =
Hiero::TCK::getOptionalJsonParameter<Hiero::TCK::CommonTransactionParams>(jsonFrom, "commonTransactionParams");
}
};

} // namespace nlohmann

#endif // HIERO_TCK_CPP_BURN_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 @@ -10,6 +10,7 @@
#include "sdk/params/ResetParams.h"
#include "sdk/params/SetupParams.h"
#include "token/params/AssociateTokenParams.h"
#include "token/params/BurnTokenParams.h"
#include "token/params/CreateTokenParams.h"
#include "token/params/DeleteTokenParams.h"
#include "token/params/DissociateTokenParams.h"
Expand Down Expand Up @@ -361,6 +362,8 @@ template TckServer::MethodHandle TckServer::getHandle<SdkClient::SetupParams>(

template TckServer::MethodHandle TckServer::getHandle<TokenService::AssociateTokenParams>(
nlohmann::json (*method)(const TokenService::AssociateTokenParams&));
template TckServer::MethodHandle TckServer::getHandle<TokenService::BurnTokenParams>(
nlohmann::json (*method)(const TokenService::BurnTokenParams&));
template TckServer::MethodHandle TckServer::getHandle<TokenService::CreateTokenParams>(
nlohmann::json (*method)(const TokenService::CreateTokenParams&));
template TckServer::MethodHandle TckServer::getHandle<TokenService::DeleteTokenParams>(
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 @@ -29,6 +29,7 @@ int main(int argc, char** argv)

// Add the TokenService functions.
tckServer.add("associateToken", tckServer.getHandle(&TokenService::associateToken));
tckServer.add("burnToken", tckServer.getHandle(&TokenService::burnToken));
tckServer.add("createToken", tckServer.getHandle(&TokenService::createToken));
tckServer.add("deleteToken", tckServer.getHandle(&TokenService::deleteToken));
tckServer.add("dissociateToken", tckServer.getHandle(&TokenService::dissociateToken));
Expand Down
42 changes: 42 additions & 0 deletions src/tck/src/token/TokenService.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "key/KeyService.h"
#include "sdk/SdkClient.h"
#include "token/params/AssociateTokenParams.h"
#include "token/params/BurnTokenParams.h"
#include "token/params/CreateTokenParams.h"
#include "token/params/DeleteTokenParams.h"
#include "token/params/DissociateTokenParams.h"
Expand All @@ -21,6 +22,7 @@
#include <AccountId.h>
#include <Status.h>
#include <TokenAssociateTransaction.h>
#include <TokenBurnTransaction.h>
#include <TokenCreateTransaction.h>
#include <TokenDeleteTransaction.h>
#include <TokenDissociateTransaction.h>
Expand Down Expand Up @@ -84,6 +86,46 @@ nlohmann::json associateToken(const AssociateTokenParams& params)
};
}

//-----
nlohmann::json burnToken(const BurnTokenParams& params)
{
TokenBurnTransaction tokenBurnTransaction;
tokenBurnTransaction.setGrpcDeadline(SdkClient::DEFAULT_TCK_REQUEST_TIMEOUT);

if (params.mTokenId.has_value())
{
tokenBurnTransaction.setTokenId(TokenId::fromString(params.mTokenId.value()));
}

if (params.mAmount.has_value())
{
tokenBurnTransaction.setAmount(internal::EntityIdHelper::getNum(params.mAmount.value()));
}

if (params.mSerialNumbers.has_value())
{
std::vector<uint64_t> serialNumbers;
for (const std::string& serialNumber : params.mSerialNumbers.value())
{
serialNumbers.push_back(internal::EntityIdHelper::getNum(serialNumber));
}

tokenBurnTransaction.setSerialNumbers(serialNumbers);
}

if (params.mCommonTxParams.has_value())
{
params.mCommonTxParams->fillOutTransaction(tokenBurnTransaction, SdkClient::getClient());
}

const TransactionReceipt txReceipt =
tokenBurnTransaction.execute(SdkClient::getClient()).getReceipt(SdkClient::getClient());
return {
{"status", gStatusToString.at(txReceipt.mStatus) },
{ "newTotalSupply", std::to_string(txReceipt.mNewTotalSupply.value())}
};
}

//-----
nlohmann::json createToken(const CreateTokenParams& params)
{
Expand Down

0 comments on commit 9064070

Please sign in to comment.