Skip to content

Commit 9db58ff

Browse files
committed
Add class CurrencyAmount.
1 parent 3c4b74c commit 9db58ff

File tree

7 files changed

+178
-4
lines changed

7 files changed

+178
-4
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ set(TDLIB_SOURCE_PART1
393393
td/telegram/ConnectionStateManager.cpp
394394
td/telegram/Contact.cpp
395395
td/telegram/CountryInfoManager.cpp
396+
td/telegram/CurrencyAmount.cpp
396397
td/telegram/DelayDispatcher.cpp
397398
td/telegram/Dependencies.cpp
398399
td/telegram/DeviceTokenManager.cpp
@@ -726,6 +727,7 @@ set(TDLIB_SOURCE_PART2
726727
td/telegram/ConnectionStateManager.h
727728
td/telegram/Contact.h
728729
td/telegram/CountryInfoManager.h
730+
td/telegram/CurrencyAmount.h
729731
td/telegram/CustomEmojiId.h
730732
td/telegram/DelayDispatcher.h
731733
td/telegram/Dependencies.h
@@ -1076,6 +1078,7 @@ set(TDLIB_SOURCE_PART2
10761078
td/telegram/BusinessRecipients.hpp
10771079
td/telegram/BusinessWorkHours.hpp
10781080
td/telegram/ChatReactions.hpp
1081+
td/telegram/CurrencyAmount.hpp
10791082
td/telegram/DialogFilter.hpp
10801083
td/telegram/DialogInviteLink.hpp
10811084
td/telegram/DialogNotificationSettings.hpp

SplitSource.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ function ($matches) use ($needed_std_headers) {
337337
'common_dialog_manager[_(-](?![.]get[(][)])|CommonDialogManager' => 'CommonDialogManager',
338338
'connection_state_manager[_(-](?![.]get[(][)])|ConnectionStateManager' => 'ConnectionStateManager',
339339
'country_info_manager[_(-](?![.]get[(][)])|CountryInfoManager' => 'CountryInfoManager',
340+
'CurrencyAmount' => 'CurrencyAmount',
340341
'CustomEmojiId' => 'CustomEmojiId',
341342
'device_token_manager[_(-](?![.]get[(][)])|DeviceTokenManager' => 'DeviceTokenManager',
342343
'DialogAction[^M]' => 'DialogAction',

td/generate/scheme/td_api.tl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2304,7 +2304,7 @@ factCheck text:formattedText country_code:string = FactCheck;
23042304
//@has_timestamped_media True, if media timestamp entities refers to a media in this message as opposed to a media in the replied message
23052305
//@is_channel_post True, if the message is a channel post. All messages to channels are channel posts, all other messages are not channel posts
23062306
//@is_paid_star_suggested_post True, if the message is a suggested channel post which was paid in Telegram Stars; a warning must be shown if the message is deleted in less then 24 hours after sending
2307-
//@is_paid_ton_suggested_post True, if the message is a suggested channel post which was paid in toncoins; a warning must be shown if the message is deleted in less then 24 hours after sending
2307+
//@is_paid_ton_suggested_post True, if the message is a suggested channel post which was paid in Toncoins; a warning must be shown if the message is deleted in less then 24 hours after sending
23082308
//@contains_unread_mention True, if the message contains an unread mention for the current user
23092309
//@date Point in time (Unix timestamp) when the message was sent; 0 for scheduled messages
23102310
//@edit_date Point in time (Unix timestamp) when the message was last edited; 0 for scheduled messages
@@ -4469,7 +4469,7 @@ messageChecklistTasksAdded checklist_message_id:int53 tasks:vector<checklistTask
44694469
//@description A suggested post was published for 24 hours and payment for the post was received
44704470
//@suggested_post_message_id Identifier of the message with the checklist; can be 0 if the message was deleted
44714471
//@star_amount The amount of received Telegram Stars
4472-
//@ton_amount The amount of received toncoins; in the smallest units of the cryptocurrency
4472+
//@ton_amount The amount of received Toncoins; in the smallest units of the cryptocurrency
44734473
messageSuggestedPostPaid suggested_post_message_id:int53 star_amount:starAmount ton_amount:int64 = MessageContent;
44744474

44754475
//@description A contact has registered with Telegram

td/telegram/CurrencyAmount.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//
2+
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2025
3+
//
4+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
//
7+
#include "td/telegram/CurrencyAmount.h"
8+
9+
#include "td/utils/logging.h"
10+
11+
namespace td {
12+
13+
CurrencyAmount::CurrencyAmount(telegram_api::object_ptr<telegram_api::StarsAmount> &&amount_ptr, bool allow_negative) {
14+
if (amount_ptr == nullptr) {
15+
return;
16+
}
17+
switch (amount_ptr->get_id()) {
18+
case telegram_api::starsAmount::ID: {
19+
auto star_amount =
20+
StarAmount(telegram_api::move_object_as<telegram_api::starsAmount>(amount_ptr), allow_negative);
21+
if (star_amount == StarAmount()) {
22+
return;
23+
}
24+
type_ = Type::Star;
25+
star_amount_ = star_amount;
26+
break;
27+
}
28+
case telegram_api::starsTonAmount::ID: {
29+
auto ton_amount =
30+
TonAmount(telegram_api::move_object_as<telegram_api::starsTonAmount>(amount_ptr), allow_negative);
31+
if (ton_amount == TonAmount()) {
32+
return;
33+
}
34+
type_ = Type::Ton;
35+
ton_amount_ = ton_amount;
36+
break;
37+
}
38+
default:
39+
UNREACHABLE();
40+
}
41+
}
42+
43+
bool operator==(const CurrencyAmount &lhs, const CurrencyAmount &rhs) {
44+
return lhs.type_ == rhs.type_ && lhs.star_amount_ == rhs.star_amount_ && lhs.ton_amount_ == rhs.ton_amount_;
45+
}
46+
47+
bool operator!=(const CurrencyAmount &lhs, const CurrencyAmount &rhs) {
48+
return !(lhs == rhs);
49+
}
50+
51+
StringBuilder &operator<<(StringBuilder &string_builder, const CurrencyAmount &amount) {
52+
switch (amount.type_) {
53+
case CurrencyAmount::Type::None:
54+
return string_builder << "[Free]";
55+
case CurrencyAmount::Type::Star:
56+
return string_builder << '[' << amount.star_amount_ << " Stars]";
57+
case CurrencyAmount::Type::Ton:
58+
return string_builder << '[' << amount.ton_amount_ << " nanotoncoins]";
59+
default:
60+
UNREACHABLE();
61+
return string_builder;
62+
}
63+
}
64+
65+
} // namespace td

td/telegram/CurrencyAmount.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//
2+
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2025
3+
//
4+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
//
7+
#pragma once
8+
9+
#include "td/telegram/StarAmount.h"
10+
#include "td/telegram/td_api.h"
11+
#include "td/telegram/telegram_api.h"
12+
#include "td/telegram/TonAmount.h"
13+
14+
#include "td/utils/common.h"
15+
#include "td/utils/StringBuilder.h"
16+
17+
namespace td {
18+
19+
class CurrencyAmount {
20+
enum class Type : int32 { None, Star, Ton };
21+
Type type_ = Type::None;
22+
StarAmount star_amount_;
23+
TonAmount ton_amount_;
24+
25+
friend bool operator==(const CurrencyAmount &lhs, const CurrencyAmount &rhs);
26+
27+
friend StringBuilder &operator<<(StringBuilder &string_builder, const CurrencyAmount &amount);
28+
29+
public:
30+
CurrencyAmount() = default;
31+
32+
CurrencyAmount(telegram_api::object_ptr<telegram_api::StarsAmount> &&amount_ptr, bool allow_negative);
33+
34+
StarAmount get_star_amount() const {
35+
return star_amount_;
36+
}
37+
38+
TonAmount get_ton_amount() const {
39+
return ton_amount_;
40+
}
41+
42+
template <class StorerT>
43+
void store(StorerT &storer) const;
44+
45+
template <class ParserT>
46+
void parse(ParserT &parser);
47+
};
48+
49+
bool operator==(const CurrencyAmount &lhs, const CurrencyAmount &rhs);
50+
bool operator!=(const CurrencyAmount &lhs, const CurrencyAmount &rhs);
51+
52+
StringBuilder &operator<<(StringBuilder &string_builder, const CurrencyAmount &amount);
53+
54+
} // namespace td

td/telegram/CurrencyAmount.hpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//
2+
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2025
3+
//
4+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
//
7+
#pragma once
8+
9+
#include "td/telegram/CurrencyAmount.h"
10+
#include "td/telegram/StarAmount.hpp"
11+
#include "td/telegram/TonAmount.hpp"
12+
13+
#include "td/utils/tl_helpers.h"
14+
15+
namespace td {
16+
17+
template <class StorerT>
18+
void CurrencyAmount::store(StorerT &storer) const {
19+
bool has_star_amount = star_amount_ != StarAmount();
20+
bool has_ton_amount = ton_amount_ != TonAmount();
21+
BEGIN_STORE_FLAGS();
22+
STORE_FLAG(has_star_amount);
23+
STORE_FLAG(has_ton_amount);
24+
END_STORE_FLAGS();
25+
td::store(type_, storer);
26+
if (has_star_amount) {
27+
td::store(star_amount_, storer);
28+
}
29+
if (has_ton_amount) {
30+
td::store(ton_amount_, storer);
31+
}
32+
}
33+
34+
template <class ParserT>
35+
void CurrencyAmount::parse(ParserT &parser) {
36+
bool has_star_amount;
37+
bool has_ton_amount;
38+
BEGIN_PARSE_FLAGS();
39+
PARSE_FLAG(has_star_amount);
40+
PARSE_FLAG(has_ton_amount);
41+
END_PARSE_FLAGS();
42+
td::parse(type_, parser);
43+
if (has_star_amount) {
44+
td::parse(star_amount_, parser);
45+
}
46+
if (has_ton_amount) {
47+
td::parse(ton_amount_, parser);
48+
}
49+
}
50+
51+
} // namespace td

td/telegram/SuggestedPostPrice.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Result<SuggestedPostPrice> SuggestedPostPrice::get_suggested_post_price(
7878
}
7979
if (amount < td->option_manager_->get_option_integer("suggested_post_toncoin_cent_count_min") ||
8080
amount > td->option_manager_->get_option_integer("suggested_post_toncoin_cent_count_max")) {
81-
return Status::Error(400, "Invalid amount of toncoin cents specified");
81+
return Status::Error(400, "Invalid amount of Toncoin cents specified");
8282
}
8383
SuggestedPostPrice result;
8484
result.type_ = Type::Ton;
@@ -133,7 +133,7 @@ StringBuilder &operator<<(StringBuilder &string_builder, const SuggestedPostPric
133133
case SuggestedPostPrice::Type::Star:
134134
return string_builder << '[' << amount.amount_ << " Stars]";
135135
case SuggestedPostPrice::Type::Ton:
136-
return string_builder << '[' << amount.amount_ << " toncoin cents]";
136+
return string_builder << '[' << amount.amount_ << " Toncoin cents]";
137137
default:
138138
UNREACHABLE();
139139
return string_builder;

0 commit comments

Comments
 (0)