rippled
Loading...
Searching...
No Matches
AMMCore.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2023 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpl/basics/base_uint.h>
21#include <xrpl/beast/utility/Zero.h>
22#include <xrpl/beast/utility/instrumentation.h>
23#include <xrpl/protocol/AMMCore.h>
24#include <xrpl/protocol/AccountID.h>
25#include <xrpl/protocol/Feature.h>
26#include <xrpl/protocol/Issue.h>
27#include <xrpl/protocol/Rules.h>
28#include <xrpl/protocol/SField.h>
29#include <xrpl/protocol/STAmount.h>
30#include <xrpl/protocol/STObject.h>
31#include <xrpl/protocol/TER.h>
32#include <xrpl/protocol/UintTypes.h>
33#include <xrpl/protocol/digest.h>
34
35#include <algorithm>
36#include <cstdint>
37#include <optional>
38#include <utility>
39
40namespace ripple {
41
43ammLPTCurrency(Currency const& cur1, Currency const& cur2)
44{
45 // AMM LPToken is 0x03 plus 19 bytes of the hash
46 std::int32_t constexpr AMMCurrencyCode = 0x03;
47 auto const [minC, maxC] = std::minmax(cur1, cur2);
48 auto const hash = sha512Half(minC, maxC);
49 Currency currency;
50 *currency.begin() = AMMCurrencyCode;
52 hash.begin(), hash.begin() + currency.size() - 1, currency.begin() + 1);
53 return currency;
54}
55
56Issue
58 Currency const& cur1,
59 Currency const& cur2,
60 AccountID const& ammAccountID)
61{
62 return Issue(ammLPTCurrency(cur1, cur2), ammAccountID);
63}
64
67 Issue const& issue,
69{
70 if (badCurrency() == issue.currency)
71 return temBAD_CURRENCY;
72 if (isXRP(issue) && issue.account.isNonZero())
73 return temBAD_ISSUER;
74 if (pair && issue != pair->first && issue != pair->second)
75 return temBAD_AMM_TOKENS;
76 return tesSUCCESS;
77}
78
81 Issue const& issue1,
82 Issue const& issue2,
84{
85 if (issue1 == issue2)
86 return temBAD_AMM_TOKENS;
87 if (auto const res = invalidAMMAsset(issue1, pair))
88 return res;
89 if (auto const res = invalidAMMAsset(issue2, pair))
90 return res;
91 return tesSUCCESS;
92}
93
96 STAmount const& amount,
98 bool validZero)
99{
100 if (auto const res = invalidAMMAsset(amount.issue(), pair))
101 return res;
102 if (amount < beast::zero || (!validZero && amount == beast::zero))
103 return temBAD_AMOUNT;
104 return tesSUCCESS;
105}
106
109{
110 // It should be impossible for expiration to be < TOTAL_TIME_SLOT_SECS,
111 // but check just to be safe
112 auto const expiration = auctionSlot[sfExpiration];
113 XRPL_ASSERT(
114 expiration >= TOTAL_TIME_SLOT_SECS,
115 "ripple::ammAuctionTimeSlot : minimum expiration");
116 if (expiration >= TOTAL_TIME_SLOT_SECS)
117 {
118 if (auto const start = expiration - TOTAL_TIME_SLOT_SECS;
119 current >= start)
120 {
121 if (auto const diff = current - start; diff < TOTAL_TIME_SLOT_SECS)
122 return diff / AUCTION_SLOT_INTERVAL_DURATION;
123 }
124 }
125 return std::nullopt;
126}
127
128bool
129ammEnabled(Rules const& rules)
130{
131 return rules.enabled(featureAMM) && rules.enabled(fixUniversalNumber);
132}
133
134} // namespace ripple
A currency issued by an account.
Definition: Issue.h:33
AccountID account
Definition: Issue.h:36
Currency currency
Definition: Issue.h:35
Rules controlling protocol behavior.
Definition: Rules.h:38
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition: Rules.cpp:130
Issue const & issue() const
Definition: STAmount.h:496
iterator begin()
Definition: base_uint.h:136
static constexpr std::size_t size()
Definition: base_uint.h:526
bool isNonZero() const
Definition: base_uint.h:545
T copy(T... args)
T minmax(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:25
NotTEC invalidAMMAmount(STAmount const &amount, std::optional< std::pair< Issue, Issue > > const &pair=std::nullopt, bool validZero=false)
Validate the amount.
Definition: AMMCore.cpp:95
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
Definition: UintTypes.cpp:133
std::uint32_t constexpr TOTAL_TIME_SLOT_SECS
Definition: AMMCore.h:34
bool isXRP(AccountID const &c)
Definition: AccountID.h:90
std::optional< std::uint8_t > ammAuctionTimeSlot(std::uint64_t current, STObject const &auctionSlot)
Get time slot of the auction slot.
Definition: AMMCore.cpp:108
NotTEC invalidAMMAsset(Issue const &issue, std::optional< std::pair< Issue, Issue > > const &pair=std::nullopt)
Definition: AMMCore.cpp:66
Currency ammLPTCurrency(Currency const &cur1, Currency const &cur2)
Calculate Liquidity Provider Token (LPT) Currency.
Definition: AMMCore.cpp:43
bool ammEnabled(Rules const &)
Return true if required AMM amendments are enabled.
Definition: AMMCore.cpp:129
Issue ammLPTIssue(Currency const &cur1, Currency const &cur2, AccountID const &ammAccountID)
Calculate LPT Issue from AMM asset pair.
Definition: AMMCore.cpp:57
@ current
This was a new validation and was added.
base_uint< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
Definition: UintTypes.h:56
@ tesSUCCESS
Definition: TER.h:244
NotTEC invalidAMMAssetPair(Issue const &issue1, Issue const &issue2, std::optional< std::pair< Issue, Issue > > const &pair=std::nullopt)
Definition: AMMCore.cpp:80
std::uint32_t constexpr AUCTION_SLOT_INTERVAL_DURATION
Definition: AMMCore.h:40
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
Definition: digest.h:224
TERSubset< CanCvtToNotTEC > NotTEC
Definition: TER.h:605
@ temBAD_ISSUER
Definition: TER.h:93
@ temBAD_AMOUNT
Definition: TER.h:89
@ temBAD_CURRENCY
Definition: TER.h:90
@ temBAD_AMM_TOKENS
Definition: TER.h:129