xrpld
Loading...
Searching...
No Matches
tests/libxrpl/ledger/TokenHelpers.cpp
1#include <xrpl/ledger/helpers/TokenHelpers.h>
2
3#include <xrpl/protocol/AccountID.h>
4#include <xrpl/protocol/Indexes.h>
5#include <xrpl/protocol/Issue.h>
6#include <xrpl/protocol/MPTIssue.h>
7#include <xrpl/protocol/Rate.h>
8#include <xrpl/protocol/STAmount.h>
9#include <xrpl/protocol/UintTypes.h>
10
11#include <gtest/gtest.h>
12
13using namespace xrpl;
14
15namespace {
16
17// A 25% transfer fee.
18Rate const kRate125{1'250'000'000};
19
20AccountID const kIssuer{1};
21
22} // namespace
23
24TEST(TokenHelpers, add_transfer_fee)
25{
26 Issue const usd{toCurrency("USD"), kIssuer};
27 MPTIssue const mpt{makeMptID(1, kIssuer)};
28
29 EXPECT_EQ(addTransferFee(STAmount{usd, 80}, kRate125), STAmount(usd, 100));
30 EXPECT_EQ(addTransferFee(STAmount{usd, 80}, kParityRate), STAmount(usd, 80));
31
32 // Integral MPT: the fee on 1 unit rounds up to a whole unit.
33 EXPECT_EQ(addTransferFee(STAmount{mpt, 80}, kRate125), STAmount(mpt, 100));
34 EXPECT_EQ(addTransferFee(STAmount{mpt, 1}, kRate125), STAmount(mpt, 2));
35 EXPECT_EQ(addTransferFee(STAmount{mpt, 1}, kParityRate), STAmount(mpt, 1));
36}
37
38TEST(TokenHelpers, subtract_transfer_fee)
39{
40 Issue const usd{toCurrency("USD"), kIssuer};
41 MPTIssue const mpt{makeMptID(1, kIssuer)};
42
43 EXPECT_EQ(subtractTransferFee(STAmount{usd, 100}, kRate125), STAmount(usd, 80));
44 EXPECT_EQ(subtractTransferFee(STAmount{usd, 100}, kParityRate), STAmount(usd, 100));
45
46 // Integral MPT: the payout rounds down, to zero for a single unit.
47 EXPECT_EQ(subtractTransferFee(STAmount{mpt, 31}, kRate125), STAmount(mpt, 24));
48 EXPECT_EQ(subtractTransferFee(STAmount{mpt, 1}, kRate125), STAmount(mpt));
49 EXPECT_EQ(subtractTransferFee(STAmount{mpt, 1}, kParityRate), STAmount(mpt, 1));
50}
A currency issued by an account.
Definition Issue.h:18
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
STAmount subtractTransferFee(STAmount const &amount, Rate const &rate)
Returns the amount a receiver gets when amount leaves the sender and the issuer takes the transfer fe...
TEST(FileUtilitiesTest, get_file_contents)
bool toCurrency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
Definition UintTypes.cpp:65
Rate const kParityRate
A transfer rate signifying a 1:1 exchange.
MPTID makeMptID(std::uint32_t const sequence, AccountID const &account)
Definition Indexes.cpp:219
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
STAmount addTransferFee(STAmount const &amount, Rate const &rate)
Returns the amount a sender must part with for amount to reach the receiver once the issuer takes the...
Represents a transfer rate.
Definition Rate.h:21