aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorlevlam <levlam@telegram.org>2025-07-08 09:46:19 +0300
committerlevlam <levlam@telegram.org>2025-07-08 09:46:19 +0300
commit1860a65d37fec423963a7c4b6ec58c8d17a6ab1c (patch)
tree416700c647ce1b24f57e6d7affb684d55981ff6a
parentf4833655887182a407ebf2a490aa05909157966e (diff)
Add class TonAmount.
-rw-r--r--CMakeLists.txt3
-rw-r--r--SplitSource.php1
-rw-r--r--td/telegram/TonAmount.cpp45
-rw-r--r--td/telegram/TonAmount.h48
-rw-r--r--td/telegram/TonAmount.hpp26
5 files changed, 123 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 4680784a5..b93f34358 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -638,6 +638,7 @@ set(TDLIB_SOURCE_PART2
td/telegram/ToDoCompletion.cpp
td/telegram/ToDoItem.cpp
td/telegram/ToDoList.cpp
+ td/telegram/TonAmount.cpp
td/telegram/TopDialogCategory.cpp
td/telegram/TopDialogManager.cpp
td/telegram/TranscriptionInfo.cpp
@@ -1026,6 +1027,7 @@ set(TDLIB_SOURCE_PART2
td/telegram/ToDoCompletion.h
td/telegram/ToDoItem.h
td/telegram/ToDoList.h
+ td/telegram/TonAmount.h
td/telegram/TopDialogCategory.h
td/telegram/TopDialogManager.h
td/telegram/TranscriptionInfo.h
@@ -1147,6 +1149,7 @@ set(TDLIB_SOURCE_PART2
td/telegram/ToDoCompletion.hpp
td/telegram/ToDoItem.hpp
td/telegram/ToDoList.hpp
+ td/telegram/TonAmount.hpp
td/telegram/TranscriptionInfo.hpp
td/telegram/VideoNotesManager.hpp
td/telegram/VideosManager.hpp
diff --git a/SplitSource.php b/SplitSource.php
index c071f31c9..2d01ff04f 100644
--- a/SplitSource.php
+++ b/SplitSource.php
@@ -458,6 +458,7 @@ function split_file($file, $chunks, $undo) {
'ToDoCompletion' => 'ToDoCompletion',
'ToDoItem' => 'ToDoItem',
'ToDoList' => 'ToDoList',
+ 'TonAmount' => 'TonAmount',
'TopDialogCategory|get_top_dialog_category' => 'TopDialogCategory',
'top_dialog_manager[_(-](?![.]get[(][)])|TopDialogManager' => 'TopDialogManager',
'translation_manager[_(-](?![.]get[(][)])|TranslationManager' => 'TranslationManager',
diff --git a/td/telegram/TonAmount.cpp b/td/telegram/TonAmount.cpp
new file mode 100644
index 000000000..33a09b249
--- /dev/null
+++ b/td/telegram/TonAmount.cpp
@@ -0,0 +1,45 @@
+//
+// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2025
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+#include "td/telegram/TonAmount.h"
+
+#include "td/utils/logging.h"
+
+namespace td {
+
+TonAmount::TonAmount(telegram_api::object_ptr<telegram_api::starsTonAmount> &&amount, bool allow_negative) {
+ if (amount != nullptr) {
+ ton_amount_ = get_ton_count(amount->amount_, allow_negative);
+ }
+}
+
+bool operator==(const TonAmount &lhs, const TonAmount &rhs) {
+ return lhs.ton_amount_ == rhs.ton_amount_;
+}
+
+int64 TonAmount::get_ton_count(int64 amount, bool allow_negative) {
+ auto max_amount = static_cast<int64>(1) << 51;
+ if (amount < 0) {
+ if (!allow_negative) {
+ LOG(ERROR) << "Receive TON amount = " << amount;
+ return 0;
+ }
+ if (amount < -max_amount) {
+ LOG(ERROR) << "Receive TON amount = " << amount;
+ return -max_amount;
+ }
+ } else if (amount > max_amount) {
+ LOG(ERROR) << "Receive TON amount = " << amount;
+ return max_amount;
+ }
+ return amount;
+}
+
+StringBuilder &operator<<(StringBuilder &string_builder, const TonAmount &ton_amount) {
+ return string_builder << ton_amount.get_ton_amount() << " TON";
+}
+
+} // namespace td
diff --git a/td/telegram/TonAmount.h b/td/telegram/TonAmount.h
new file mode 100644
index 000000000..7c908ef3e
--- /dev/null
+++ b/td/telegram/TonAmount.h
@@ -0,0 +1,48 @@
+//
+// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2025
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+#pragma once
+
+#include "td/telegram/td_api.h"
+#include "td/telegram/telegram_api.h"
+
+#include "td/utils/common.h"
+#include "td/utils/StringBuilder.h"
+
+namespace td {
+
+class TonAmount {
+ int64 ton_amount_ = 0;
+
+ friend bool operator==(const TonAmount &lhs, const TonAmount &rhs);
+
+ public:
+ TonAmount() = default;
+
+ TonAmount(telegram_api::object_ptr<telegram_api::starsTonAmount> &&amount, bool allow_negative);
+
+ int64 get_ton_amount() const {
+ return ton_amount_;
+ }
+
+ static int64 get_ton_count(int64 amount, bool allow_negative);
+
+ template <class StorerT>
+ void store(StorerT &storer) const;
+
+ template <class ParserT>
+ void parse(ParserT &parser);
+};
+
+bool operator==(const TonAmount &lhs, const TonAmount &rhs);
+
+inline bool operator!=(const TonAmount &lhs, const TonAmount &rhs) {
+ return !(lhs == rhs);
+}
+
+StringBuilder &operator<<(StringBuilder &string_builder, const TonAmount &ton_amount);
+
+} // namespace td
diff --git a/td/telegram/TonAmount.hpp b/td/telegram/TonAmount.hpp
new file mode 100644
index 000000000..02efbd1a2
--- /dev/null
+++ b/td/telegram/TonAmount.hpp
@@ -0,0 +1,26 @@
+//
+// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2025
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+#pragma once
+
+#include "td/telegram/TonAmount.h"
+
+#include "td/utils/common.h"
+#include "td/utils/tl_helpers.h"
+
+namespace td {
+
+template <class StorerT>
+void TonAmount::store(StorerT &storer) const {
+ td::store(ton_amount_, storer);
+}
+
+template <class ParserT>
+void TonAmount::parse(ParserT &parser) {
+ td::parse(ton_amount_, parser);
+}
+
+} // namespace td