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/protocol/AMMCore.h>
21#include <xrpl/protocol/Feature.h>
22#include <xrpl/protocol/Rules.h>
23#include <xrpl/protocol/STAmount.h>
24#include <xrpl/protocol/STObject.h>
25#include <xrpl/protocol/digest.h>
26
27namespace ripple {
28
31 std::uint16_t prefix,
32 uint256 const& parentHash,
33 uint256 const& ammID)
34{
36 auto const hash = sha512Half(prefix, parentHash, ammID);
37 rsh(hash.data(), hash.size());
38 return AccountID{static_cast<ripesha_hasher::result_type>(rsh)};
39}
40
42ammLPTCurrency(Currency const& cur1, Currency const& cur2)
43{
44 // AMM LPToken is 0x03 plus 19 bytes of the hash
45 std::int32_t constexpr AMMCurrencyCode = 0x03;
46 auto const [minC, maxC] = std::minmax(cur1, cur2);
47 auto const hash = sha512Half(minC, maxC);
48 Currency currency;
49 *currency.begin() = AMMCurrencyCode;
51 hash.begin(), hash.begin() + currency.size() - 1, currency.begin() + 1);
52 return currency;
53}
54
55Issue
57 Currency const& cur1,
58 Currency const& cur2,
60{
61 return Issue(ammLPTCurrency(cur1, cur2), ammAccountID);
62}
63
66 Issue const& issue,
68{
69 if (badCurrency() == issue.currency)
70 return temBAD_CURRENCY;
71 if (isXRP(issue) && issue.account.isNonZero())
72 return temBAD_ISSUER;
73 if (pair && issue != pair->first && issue != pair->second)
74 return temBAD_AMM_TOKENS;
75 return tesSUCCESS;
76}
77
80 Issue const& issue1,
81 Issue const& issue2,
83{
84 if (issue1 == issue2)
85 return temBAD_AMM_TOKENS;
86 if (auto const res = invalidAMMAsset(issue1, pair))
87 return res;
88 if (auto const res = invalidAMMAsset(issue2, pair))
89 return res;
90 return tesSUCCESS;
91}
92
95 STAmount const& amount,
97 bool validZero)
98{
99 if (auto const res = invalidAMMAsset(amount.issue(), pair))
100 return res;
101 if (amount < beast::zero || (!validZero && amount == beast::zero))
102 return temBAD_AMOUNT;
103 return tesSUCCESS;
104}
105
108{
109 // It should be impossible for expiration to be < TOTAL_TIME_SLOT_SECS,
110 // but check just to be safe
111 auto const expiration = auctionSlot[sfExpiration];
112 XRPL_ASSERT(
113 expiration >= TOTAL_TIME_SLOT_SECS,
114 "ripple::ammAuctionTimeSlot : minimum expiration");
115 if (expiration >= TOTAL_TIME_SLOT_SECS)
116 {
117 if (auto const start = expiration - TOTAL_TIME_SLOT_SECS;
118 current >= start)
119 {
120 if (auto const diff = current - start; diff < TOTAL_TIME_SLOT_SECS)
121 return diff / AUCTION_SLOT_INTERVAL_DURATION;
122 }
123 }
124 return std::nullopt;
125}
126
127bool
128ammEnabled(Rules const& rules)
129{
130 return rules.enabled(featureAMM) && rules.enabled(fixUniversalNumber);
131}
132
133} // namespace ripple
A currency issued by an account.
Definition: Issue.h:36
AccountID account
Definition: Issue.h:39
Currency currency
Definition: Issue.h:38
Rules controlling protocol behavior.
Definition: Rules.h:35
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition: Rules.cpp:122
Issue const & issue() const
Definition: STAmount.h:487
iterator begin()
Definition: base_uint.h:135
static constexpr std::size_t size()
Definition: base_uint.h:525
bool isNonZero() const
Definition: base_uint.h:544
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:26
base_uint< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition: AccountID.h:49
NotTEC invalidAMMAmount(STAmount const &amount, std::optional< std::pair< Issue, Issue > > const &pair=std::nullopt, bool validZero=false)
Validate the amount.
Definition: AMMCore.cpp:94
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
Definition: UintTypes.cpp:129
std::uint32_t constexpr TOTAL_TIME_SLOT_SECS
Definition: AMMCore.h:34
bool isXRP(AccountID const &c)
Definition: AccountID.h:91
std::optional< std::uint8_t > ammAuctionTimeSlot(std::uint64_t current, STObject const &auctionSlot)
Get time slot of the auction slot.
Definition: AMMCore.cpp:107
NotTEC invalidAMMAsset(Issue const &issue, std::optional< std::pair< Issue, Issue > > const &pair=std::nullopt)
Definition: AMMCore.cpp:65
Currency ammLPTCurrency(Currency const &cur1, Currency const &cur2)
Calculate Liquidity Provider Token (LPT) Currency.
Definition: AMMCore.cpp:42
bool ammEnabled(Rules const &)
Return true if required AMM amendments are enabled.
Definition: AMMCore.cpp:128
Issue ammLPTIssue(Currency const &cur1, Currency const &cur2, AccountID const &ammAccountID)
Calculate LPT Issue from AMM asset pair.
Definition: AMMCore.cpp:56
@ current
This was a new validation and was added.
AccountID ammAccountID(std::uint16_t prefix, uint256 const &parentHash, uint256 const &ammID)
Calculate AMM account ID.
Definition: AMMCore.cpp:30
base_uint< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
Definition: UintTypes.h:56
@ tesSUCCESS
Definition: TER.h:242
NotTEC invalidAMMAssetPair(Issue const &issue1, Issue const &issue2, std::optional< std::pair< Issue, Issue > > const &pair=std::nullopt)
Definition: AMMCore.cpp:79
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:223
TERSubset< CanCvtToNotTEC > NotTEC
Definition: TER.h:587
@ 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
Returns the RIPEMD-160 digest of the SHA256 hash of the message.
Definition: digest.h:135