Skip to content

Commit

Permalink
fix: update for changes to tokencreate tck
Browse files Browse the repository at this point in the history
Signed-off-by: Rob Walworth <robert.walworth@swirldslabs.com>
  • Loading branch information
rwalworth committed Oct 18, 2024
1 parent 71c0b3e commit 9b72d82
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 59 deletions.
18 changes: 15 additions & 3 deletions src/sdk/main/include/impl/EntityIdHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#ifndef HEDERA_SDK_CPP_IMPL_ENTITY_ID_HELPER_H_
#define HEDERA_SDK_CPP_IMPL_ENTITY_ID_HELPER_H_

#include <charconv>
#include <cstddef>
#include <string>
#include <string_view>
Expand Down Expand Up @@ -55,12 +56,23 @@ template<typename EntityType>
[[nodiscard]] EntityType fromSolidityAddress(const std::vector<std::byte>& address);

/**
* Convert a string to a uint64_t. E.g. "123" will return 123.
* Convert a string to a number type. E.g. "123" will return 123.
*
* @param str The string to convert.
* @return The uint64_t contained in the string.
* @return The number type contained in the string.
*/
[[nodiscard]] uint64_t getNum(std::string_view str);
template<typename T = uint64_t>
[[nodiscard]] T getNum(std::string_view str)
{
T num;
if (auto result = std::from_chars(str.data(), str.data() + str.size(), num);
result.ec != std::errc() || result.ptr != str.data() + str.size())
{
throw std::invalid_argument("Input entity ID string is malformed");

Check failure on line 71 in src/sdk/main/include/impl/EntityIdHelper.h

View workflow job for this annotation

GitHub Actions / Code / Build (Linux, linux-x64)

'invalid_argument' is not a member of 'std'; did you mean 'std::errc::invalid_argument'?
}

return num;
}

/**
* Get the shard from an entity ID.
Expand Down
14 changes: 0 additions & 14 deletions src/sdk/main/src/impl/EntityIdHelper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
#include "impl/HexConverter.h"
#include "impl/Utilities.h"

#include <charconv>
#include <cstddef>
#include <stdexcept>
#include <vector>
Expand All @@ -55,19 +54,6 @@ EntityType fromSolidityAddress(const std::vector<std::byte>& address)
*internal::Utilities::toTypePtr<uint64_t>(address.data() + 12));
}

//-----
uint64_t getNum(std::string_view str)
{
uint64_t num;
if (auto result = std::from_chars(str.data(), str.data() + str.size(), num);
result.ec != std::errc() || result.ptr != str.data() + str.size())
{
throw std::invalid_argument("Input entity ID string is malformed");
}

return num;
}

//-----
uint64_t getShardNum(std::string_view id)
{
Expand Down
122 changes: 98 additions & 24 deletions src/tck/include/CustomFeeSerializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
#include "CustomFractionalFee.h"
#include "CustomRoyaltyFee.h"
#include "JsonRpcException.h"
#include "impl/EntityIdHelper.h"

#include <nlohmann/json.hpp>
#include <stdexcept>

namespace nlohmann
{
Expand Down Expand Up @@ -152,13 +154,22 @@ struct [[maybe_unused]] adl_serializer<std::shared_ptr<Hedera::CustomFee>>
"invalid parameters: amount is REQUIRED for fixedFee fee types.");
}

if (!jsonFrom["fixedFee"]["amount"].is_number_integer())
if (!jsonFrom["fixedFee"]["amount"].is_string())
{
throw Hedera::TCK::JsonRpcException(Hedera::TCK::JsonErrorType::INVALID_PARAMS,
"invalid parameters: amount MUST be an int64.");
"invalid parameters: amount MUST be a string.");
}

fixedFee->setAmount(jsonFrom["fixedFee"]["amount"].get<int64_t>());
try
{
fixedFee->setAmount(
Hedera::internal::EntityIdHelper::getNum<int64_t>(jsonFrom["fixedFee"]["amount"].get<std::string>()));
}
catch (const std::invalid_argument&)
{
fixedFee->setAmount(
Hedera::internal::EntityIdHelper::getNum(jsonFrom["fixedFee"]["amount"].get<std::string>()));
}

if (jsonFrom["fixedFee"].contains("denominatingTokenId"))
{
Expand Down Expand Up @@ -187,27 +198,45 @@ struct [[maybe_unused]] adl_serializer<std::shared_ptr<Hedera::CustomFee>>
"invalid parameters: numerator is REQUIRED for fractionalFee fee types.");
}

if (!jsonFrom["fractionalFee"]["numerator"].is_number_integer())
if (!jsonFrom["fractionalFee"]["numerator"].is_string())
{
throw Hedera::TCK::JsonRpcException(Hedera::TCK::JsonErrorType::INVALID_PARAMS,
"invalid parameters: numerator MUST be an int64.");
"invalid parameters: numerator MUST be a string.");
}

fractionalFee->setNumerator(jsonFrom["fractionalFee"]["numerator"].get<int64_t>());
try
{
fractionalFee->setNumerator(
Hedera::internal::EntityIdHelper::getNum<int64_t>(jsonFrom["fractionalFee"]["numerator"].get<std::string>()));
}
catch (const std::invalid_argument&)
{
fractionalFee->setNumerator(
Hedera::internal::EntityIdHelper::getNum(jsonFrom["fractionalFee"]["numerator"].get<std::string>()));
}

if (!jsonFrom["fractionalFee"].contains("denominator"))
{
throw Hedera::TCK::JsonRpcException(Hedera::TCK::JsonErrorType::INVALID_PARAMS,
"invalid parameters: denominator is REQUIRED for fractionalFee fee types.");
}

if (!jsonFrom["fractionalFee"]["denominator"].is_number_integer())
if (!jsonFrom["fractionalFee"]["denominator"].is_string())
{
throw Hedera::TCK::JsonRpcException(Hedera::TCK::JsonErrorType::INVALID_PARAMS,
"invalid parameters: denominator MUST be an int64.");
"invalid parameters: denominator MUST be a string.");
}

fractionalFee->setDenominator(jsonFrom["fractionalFee"]["denominator"].get<int64_t>());
try
{
fractionalFee->setDenominator(Hedera::internal::EntityIdHelper::getNum<int64_t>(
jsonFrom["fractionalFee"]["denominator"].get<std::string>()));
}
catch (const std::invalid_argument&)
{
fractionalFee->setDenominator(
Hedera::internal::EntityIdHelper::getNum(jsonFrom["fractionalFee"]["denominator"].get<std::string>()));
}

if (!jsonFrom["fractionalFee"].contains("minimumAmount"))
{
Expand All @@ -216,13 +245,22 @@ struct [[maybe_unused]] adl_serializer<std::shared_ptr<Hedera::CustomFee>>
"invalid parameters: minimumAmount is REQUIRED for fractionalFee fee types.");
}

if (!jsonFrom["fractionalFee"]["minimumAmount"].is_number_integer())
if (!jsonFrom["fractionalFee"]["minimumAmount"].is_string())
{
throw Hedera::TCK::JsonRpcException(Hedera::TCK::JsonErrorType::INVALID_PARAMS,
"invalid parameters: minimumAmount MUST be an int64.");
"invalid parameters: minimumAmount MUST be a string.");
}

fractionalFee->setMinimumAmount(jsonFrom["fractionalFee"]["minimumAmount"].get<int64_t>());
try
{
fractionalFee->setMinimumAmount(Hedera::internal::EntityIdHelper::getNum<int64_t>(
jsonFrom["fractionalFee"]["minimumAmount"].get<std::string>()));
}
catch (const std::invalid_argument&)
{
fractionalFee->setMinimumAmount(
Hedera::internal::EntityIdHelper::getNum(jsonFrom["fractionalFee"]["minimumAmount"].get<std::string>()));
}

if (!jsonFrom["fractionalFee"].contains("maximumAmount"))
{
Expand All @@ -231,13 +269,22 @@ struct [[maybe_unused]] adl_serializer<std::shared_ptr<Hedera::CustomFee>>
"invalid parameters: maximumAmount is REQUIRED for fractionalFee fee types.");
}

if (!jsonFrom["fractionalFee"]["maximumAmount"].is_number_integer())
if (!jsonFrom["fractionalFee"]["maximumAmount"].is_string())
{
throw Hedera::TCK::JsonRpcException(Hedera::TCK::JsonErrorType::INVALID_PARAMS,
"invalid parameters: maximumAmount MUST be an int64.");
"invalid parameters: maximumAmount MUST be a string.");
}

fractionalFee->setMaximumAmount(jsonFrom["fractionalFee"]["maximumAmount"].get<int64_t>());
try
{
fractionalFee->setMaximumAmount(Hedera::internal::EntityIdHelper::getNum<int64_t>(
jsonFrom["fractionalFee"]["maximumAmount"].get<std::string>()));
}
catch (const std::invalid_argument&)
{
fractionalFee->setMaximumAmount(
Hedera::internal::EntityIdHelper::getNum(jsonFrom["fractionalFee"]["maximumAmount"].get<std::string>()));
}

if (!jsonFrom["fractionalFee"].contains("assessmentMethod"))
{
Expand Down Expand Up @@ -274,27 +321,45 @@ struct [[maybe_unused]] adl_serializer<std::shared_ptr<Hedera::CustomFee>>
"invalid parameters: numerator is REQUIRED for royaltyFee fee types.");
}

if (!jsonFrom["royaltyFee"]["numerator"].is_number_integer())
if (!jsonFrom["royaltyFee"]["numerator"].is_string())
{
throw Hedera::TCK::JsonRpcException(Hedera::TCK::JsonErrorType::INVALID_PARAMS,
"invalid parameters: numerator MUST be an int64.");
"invalid parameters: numerator MUST be a string.");
}

royaltyFee->setNumerator(jsonFrom["royaltyFee"]["numerator"].get<int64_t>());
try
{
royaltyFee->setNumerator(
Hedera::internal::EntityIdHelper::getNum<int64_t>(jsonFrom["royaltyFee"]["numerator"].get<std::string>()));
}
catch (const std::invalid_argument&)
{
royaltyFee->setNumerator(
Hedera::internal::EntityIdHelper::getNum(jsonFrom["royaltyFee"]["numerator"].get<std::string>()));
}

if (!jsonFrom["royaltyFee"].contains("denominator"))
{
throw Hedera::TCK::JsonRpcException(Hedera::TCK::JsonErrorType::INVALID_PARAMS,
"invalid parameters: denominator is REQUIRED for royaltyFee fee types.");
}

if (!jsonFrom["royaltyFee"]["denominator"].is_number_integer())
if (!jsonFrom["royaltyFee"]["denominator"].is_string())
{
throw Hedera::TCK::JsonRpcException(Hedera::TCK::JsonErrorType::INVALID_PARAMS,
"invalid parameters: denominator MUST be an int64.");
"invalid parameters: denominator MUST be a string.");
}

royaltyFee->setDenominator(jsonFrom["royaltyFee"]["denominator"].get<int64_t>());
try
{
royaltyFee->setDenominator(
Hedera::internal::EntityIdHelper::getNum<int64_t>(jsonFrom["royaltyFee"]["denominator"].get<std::string>()));
}
catch (const std::invalid_argument&)
{
royaltyFee->setDenominator(
Hedera::internal::EntityIdHelper::getNum(jsonFrom["royaltyFee"]["denominator"].get<std::string>()));
}

if (jsonFrom["royaltyFee"].contains("fallbackFee"))
{
Expand All @@ -306,13 +371,22 @@ struct [[maybe_unused]] adl_serializer<std::shared_ptr<Hedera::CustomFee>>
"invalid parameters: amount is REQUIRED for a fallback fee.");
}

if (!jsonFrom["royaltyFee"]["fallbackFee"]["amount"].is_number_integer())
if (!jsonFrom["royaltyFee"]["fallbackFee"]["amount"].is_string())
{
throw Hedera::TCK::JsonRpcException(Hedera::TCK::JsonErrorType::INVALID_PARAMS,
"invalid parameters: amount MUST be an int64.");
"invalid parameters: amount MUST be a string.");
}

fallbackFee.setAmount(jsonFrom["royaltyFee"]["fallbackFee"]["amount"].get<int64_t>());
try
{
fallbackFee.setAmount(Hedera::internal::EntityIdHelper::getNum(
jsonFrom["royaltyFee"]["fallbackFee"]["amount"].get<std::string>()));
}
catch (const std::invalid_argument&)
{
fallbackFee.setAmount(Hedera::internal::EntityIdHelper::getNum<int64_t>(
jsonFrom["royaltyFee"]["fallbackFee"]["amount"].get<std::string>()));
}

if (jsonFrom["royaltyFee"]["fallbackFee"].contains("denominatingTokenId"))
{
Expand Down
8 changes: 4 additions & 4 deletions src/tck/include/SdkClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,21 @@ nlohmann::json createAccount(const std::optional<std::string>& key,
nlohmann::json createToken(const std::optional<std::string>& name,
const std::optional<std::string>& symbol,
const std::optional<uint32_t>& decimals,
const std::optional<uint64_t>& initialSupply,
const std::optional<std::string>& initialSupply,
const std::optional<std::string>& treasuryAccountId,
const std::optional<std::string>& adminKey,
const std::optional<std::string>& kycKey,
const std::optional<std::string>& freezeKey,
const std::optional<std::string>& wipeKey,
const std::optional<std::string>& supplyKey,
const std::optional<bool>& freezeDefault,
const std::optional<int64_t>& expirationTime,
const std::optional<std::string>& expirationTime,
const std::optional<std::string>& autoRenewAccountId,
const std::optional<int64_t>& autoRenewPeriod,
const std::optional<std::string>& autoRenewPeriod,
const std::optional<std::string>& memo,
const std::optional<std::string>& tokenType,
const std::optional<std::string>& supplyType,
const std::optional<int64_t>& maxSupply,
const std::optional<std::string>& maxSupply,
const std::optional<std::string>& feeScheduleKey,
const std::optional<std::vector<std::shared_ptr<CustomFee>>>& customFees,
const std::optional<std::string>& pauseKey,
Expand Down
7 changes: 4 additions & 3 deletions src/tck/src/KeyHelper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,21 @@ KeyRequest::KeyRequest(const std::string& type,
//-----
std::shared_ptr<Hedera::Key> getHederaKey(const std::string& key)
{
const std::vector<std::byte> keyBytes = internal::HexConverter::hexToBytes(key);
try
{
return PublicKey::fromStringDer(key);
return PublicKey::fromBytesDer(keyBytes);
}
catch (const BadKeyException&)
{
try
{
return PrivateKey::fromStringDer(key);
return PrivateKey::fromBytesDer(keyBytes);
}
catch (const BadKeyException&)
{
proto::Key protoKey;
protoKey.ParseFromString(internal::Utilities::byteVectorToString(internal::HexConverter::hexToBytes(key)));
protoKey.ParseFromString(internal::Utilities::byteVectorToString(keyBytes));
return Hedera::Key::fromProtobuf(protoKey);
}
}
Expand Down
Loading

0 comments on commit 9b72d82

Please sign in to comment.