Skip to content

Commit

Permalink
Added Account Balance/Info contract Info Mirror Node Queries (#641)
Browse files Browse the repository at this point in the history
Signed-off-by: gsstoykov <georgi.stoykov@limechain.tech>
  • Loading branch information
gsstoykov authored Mar 13, 2024
1 parent ed76a66 commit fb37a00
Show file tree
Hide file tree
Showing 32 changed files with 1,026 additions and 31 deletions.
3 changes: 3 additions & 0 deletions src/sdk/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ add_library(${PROJECT_NAME} STATIC
src/TokenNftInfoQuery.cc
src/TokenNftTransfer.cc
src/TokenPauseTransaction.cc
src/TokenRelationship.cc
src/TokenRevokeKycTransaction.cc
src/TokenSupplyType.cc
src/TokenTransfer.cc
Expand Down Expand Up @@ -183,6 +184,8 @@ add_library(${PROJECT_NAME} STATIC
src/impl/HttpClient.cc
src/impl/MirrorNetwork.cc
src/impl/MirrorNode.cc
src/impl/MirrorNodeGateway.cc
src/impl/MirrorNodeRouter.cc
src/impl/Network.cc
src/impl/Node.cc
src/impl/OpenSSLUtils.cc
Expand Down
12 changes: 12 additions & 0 deletions src/sdk/main/include/AccountBalance.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
#define HEDERA_SDK_CPP_ACCOUNT_BALANCE_H_

#include "Hbar.h"
#include "TokenId.h"

#include <cstddef>
#include <memory>
#include <ostream>
#include <string>
#include <unordered_map>
#include <vector>

namespace proto
Expand Down Expand Up @@ -91,6 +93,16 @@ class AccountBalance
* The account or contract balance.
*/
Hbar mBalance;

/**
* Map of tokens with associated values.
*/
std::unordered_map<TokenId, uint64_t> mTokens;

/**
* Map of token decimals with associated values.
*/
std::unordered_map<TokenId, int> mTokenDecimals;
};

} // namespace Hedera
Expand Down
10 changes: 10 additions & 0 deletions src/sdk/main/include/AccountBalanceQuery.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ class AccountBalanceQuery : public Query<AccountBalanceQuery, AccountBalance>
*/
void validateChecksums(const Client& client) const override;

/**
* Fetches token relationship data from Mirror Node and updates the queried data.
*
* This function retrieves token relationship information. It then populates missing
* object fields with token information obtained from the Mirror Node response.
*
* @throws IllegalStateException on bad Mirror Node response state
*/
void fetchTokenInformation(AccountBalance& accountBalance) const;

/**
* Derived from Query. Build a Query protobuf object with this AccountBalanceQuery's data, with the input QueryHeader
* protobuf object.
Expand Down
2 changes: 1 addition & 1 deletion src/sdk/main/include/AccountId.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class AccountId
[[nodiscard]] std::string toSolidityAddress() const;

/**
* @brief Populates the EVM address for an Account using the Mirror Node. This function fetches the EVM address for an
* Populates the EVM address for an Account using the Mirror Node. This function fetches the EVM address for an
* Account from the Mirror Node.
*
* User Note: This Function requires a 3 second sleep if running on testnet environment as the MirrorNode does not
Expand Down
8 changes: 8 additions & 0 deletions src/sdk/main/include/AccountInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@
#include "Key.h"
#include "LedgerId.h"
#include "StakingInfo.h"
#include "TokenId.h"
#include "TokenRelationship.h"

#include <chrono>
#include <cstddef>
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
#include <vector>

namespace proto
Expand Down Expand Up @@ -177,6 +180,11 @@ class AccountInfo
* The staking metadata for the queried account.
*/
StakingInfo mStakingInfo;

/**
* The token relationships mappings for the queried account.
*/
std::unordered_map<TokenId, TokenRelationship> mTokenRelationships;
};

} // namespace Hedera
Expand Down
10 changes: 10 additions & 0 deletions src/sdk/main/include/AccountInfoQuery.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ class AccountInfoQuery : public Query<AccountInfoQuery, AccountInfo>
*/
void validateChecksums(const Client& client) const override;

/**
* Fetches token relationship data from Mirror Node and updates the queried data.
*
* This function retrieves token relationship information. It then populates missing
* object fields with token information obtained from the Mirror Node response.
*
* @throws IllegalStateException on bad Mirror Node response state
*/
void fetchTokenInformation(AccountInfo& accountInfo) const;

/**
* Derived from Query. Build a Query protobuf object with this AccountInfoQuery's data, with the input QueryHeader
* protobuf object.
Expand Down
7 changes: 7 additions & 0 deletions src/sdk/main/include/ContractInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include "Key.h"
#include "LedgerId.h"
#include "StakingInfo.h"
#include "TokenId.h"
#include "TokenRelationship.h"

#include <chrono>
#include <cstdint>
Expand Down Expand Up @@ -165,6 +167,11 @@ class ContractInfo
* The staking metadata for this contract.
*/
StakingInfo mStakingInfo;

/**
* The token relationships mappings for the queried account.
*/
std::unordered_map<TokenId, TokenRelationship> mTokenRelationships;
};

} // namespace Hedera
Expand Down
10 changes: 10 additions & 0 deletions src/sdk/main/include/ContractInfoQuery.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ class ContractInfoQuery : public Query<ContractInfoQuery, ContractInfo>
*/
void validateChecksums(const Client& client) const override;

/**
* Fetches token relationship data from Mirror Node and updates the queried data.
*
* This function retrieves token relationship information. It then populates missing
* object fields with token information obtained from the Mirror Node response.
*
* @throws IllegalStateException on bad Mirror Node response state
*/
void fetchTokenInformation(ContractInfo& contractInfo) const;

/**
* Derived from Query. Build a Query protobuf object with this ContractInfoQuery's data, with the input QueryHeader
* protobuf object.
Expand Down
15 changes: 15 additions & 0 deletions src/sdk/main/include/Executable.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,16 @@ class Executable
[[maybe_unused]] const Client& client,
[[maybe_unused]] const ProtoResponseType& response);

/**
* Gets the mirror node resolution for the query.
*
* This function returns the mirror node resolution by accessing the first element
* of the vector mMirrorNodeIds.
*
* @return The mirror node resolution as a std::string.
*/
[[nodiscard]] std::string getMirrorNodeResolution() const { return mMirrorNodeIds[0]; }

private:
/**
* Construct a ProtoRequestType object from this Executable, based on the node account ID at the given index.
Expand Down Expand Up @@ -423,6 +433,11 @@ class Executable
*/
std::vector<AccountId> mNodeAccountIds;

/**
* The list of node IDs of the mirror nodes with which query should be attempted.
*/
std::vector<std::string> mMirrorNodeIds;

/**
* The callback to be called before a request is sent. The callback will receive the protobuf of the request and
* return the protobuf of the request. This allows the callback to read, copy, and/or modify the request that will be
Expand Down
122 changes: 122 additions & 0 deletions src/sdk/main/include/TokenRelationship.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*-
*
* Hedera C++ SDK
*
* Copyright (C) 2020 - 2023 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef HEDERA_SDK_CPP_TOKEN_RELATIONSHIP_H_
#define HEDERA_SDK_CPP_TOKEN_RELATIONSHIP_H_

#include "TokenId.h"

#include <iostream>
#include <optional>
#include <string>

namespace Hedera
{
/**
* Represents the relationship between an account and a token.
*
* This class encapsulates information about the account's relationship with a specific token,
* including the token ID, symbol, balance, KYC status, freeze status, and whether the relationship is created
* implicitly.
*/
class TokenRelationship
{
public:
/**
* Constructor for TokenRelationship.
*
* @param tokenId The unique token ID.
* @param symbol The symbol of the token.
* @param balance The balance of the account in the smallest denomination for fungible common tokens,
* or the number of NFTs held by the account for non-fungible unique tokens.
* @param decimals The token decimals
* @param kycStatus The KYC status of the account (optional).
* @param freezeStatus The freeze status of the account (optional).
* @param automaticAssociation Specifies if the relationship is created implicitly.
*/
TokenRelationship(const TokenId& tokenId,
const std::string& symbol,
uint64_t balance,
uint32_t decimals,
std::string_view kycStatus,
std::string_view freezeStatus,
bool automaticAssociation);

/**
* Converts the TokenRelationship object to a string representation.
*
* @return A string representation of the TokenRelationship.
*/
std::string toString() const;

/**
* The unique token ID.
*/
TokenId mTokenId;

/**
* The symbol of the token.
*/
std::string mSymbol;

/**
* The balance of the account.
*/
uint64_t mBalance;

/**
* The token decimals.
*/
uint32_t mDecimals;

/**
* The KYC status of the account (optional).
*/
std::optional<bool> mKycStatus;

/**
* The freeze status of the account (optional).
*/
std::optional<bool> mFreezeStatus;

/**
* Specifies if the relationship is created implicitly.
*/
bool mAutomaticAssociation;

private:
/**
* Sets the KYC status based on the provided string.
*
* @param kycStatusString The string representation of KYC status ("GRANTED", "REVOKED", or "NOT_APPLICABLE").
* @throws std::invalid_argument if the provided string is not a valid KYC status.
*/
void setKycStatus(std::string_view kycStatusString);

/**
* Sets the freeze status based on the provided string.
*
* @param freezeStatus The string representation of freeze status ("FROZEN", "UNFROZEN", or "NOT_APPLICABLE").
* @throws std::invalid_argument if the provided string is not a valid freeze status.
*/
void setFreezeStatus(std::string_view freezeStatus);
};
} // namespace Hedera

#endif // HEDERA_SDK_CPP_TOKEN_RELATIONSHIP_H_
60 changes: 60 additions & 0 deletions src/sdk/main/include/exceptions/CURLException.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*-
*
* Hedera C++ SDK
*
* Copyright (C) 2020 - 2023 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef HEDERA_SDK_CPP_CURL_EXCEPTION_H_
#define HEDERA_SDK_CPP_CURL_EXCEPTION_H_

#include <exception>
#include <string_view>

namespace Hedera
{
/**
* Exception that encompasses all unrecoverable CURL errors.
*/
class CURLException : public std::exception
{
public:
/**
* Construct with a message.
*
* @param msg The error message to further describe this exception.
*/
explicit CURLException(std::string_view msg)
: mError(msg)
{
}

/**
* Get the descriptor message for this error.
*
* @return The descriptor message for this error.
*/
[[nodiscard]] const char* what() const noexcept override { return mError.data(); };

private:
/**
* Descriptive error message.
*/
std::string_view mError;
};

} // namespace Hedera

#endif // HEDERA_SDK_CPP_CURL_EXCEPTION_H_
Loading

0 comments on commit fb37a00

Please sign in to comment.