rippled
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 <ripple/protocol/AMMCore.h>
21 #include <ripple/protocol/Feature.h>
22 #include <ripple/protocol/Rules.h>
23 #include <ripple/protocol/STAmount.h>
24 #include <ripple/protocol/STObject.h>
25 #include <ripple/protocol/digest.h>
26 
27 namespace ripple {
28 
31  std::uint16_t prefix,
32  uint256 const& parentHash,
33  uint256 const& ammID)
34 {
35  ripesha_hasher rsh;
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 
42 ammLPTCurrency(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;
50  std::copy(
51  hash.begin(), hash.begin() + currency.size() - 1, currency.begin() + 1);
52  return currency;
53 }
54 
55 Issue
57  Currency const& cur1,
58  Currency const& cur2,
59  AccountID const& ammAccountID)
60 {
61  return Issue(ammLPTCurrency(cur1, cur2), ammAccountID);
62 }
63 
64 NotTEC
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 
78 NotTEC
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 
93 NotTEC
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 
107 ammAuctionTimeSlot(std::uint64_t current, STObject const& auctionSlot)
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  assert(expiration >= TOTAL_TIME_SLOT_SECS);
113  if (expiration >= TOTAL_TIME_SLOT_SECS)
114  {
115  if (auto const start = expiration - TOTAL_TIME_SLOT_SECS;
116  current >= start)
117  {
118  if (auto const diff = current - start; diff < TOTAL_TIME_SLOT_SECS)
119  return diff / AUCTION_SLOT_INTERVAL_DURATION;
120  }
121  }
122  return std::nullopt;
123 }
124 
125 bool
126 ammEnabled(Rules const& rules)
127 {
128  return rules.enabled(featureAMM) && rules.enabled(fixUniversalNumber);
129 }
130 
131 } // namespace ripple
ripple::badCurrency
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
Definition: UintTypes.cpp:135
ripple::Issue
A currency issued by an account.
Definition: Issue.h:35
ripple::Rules::enabled
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition: Rules.cpp:94
ripple::base_uint::isNonZero
bool isNonZero() const
Definition: base_uint.h:537
ripple::temBAD_CURRENCY
@ temBAD_CURRENCY
Definition: TER.h:88
ripple::STAmount::issue
Issue const & issue() const
Definition: STAmount.h:347
ripple::ammLPTIssue
Issue ammLPTIssue(Currency const &cur1, Currency const &cur2, AccountID const &ammAccountID)
Calculate LPT Issue from AMM asset pair.
Definition: AMMCore.cpp:56
std::pair
ripple::ammEnabled
bool ammEnabled(Rules const &)
Return true if required AMM amendments are enabled.
Definition: AMMCore.cpp:126
ripple::Issue::currency
Currency currency
Definition: Issue.h:38
ripple::temBAD_ISSUER
@ temBAD_ISSUER
Definition: TER.h:91
ripple::ammAccountID
AccountID ammAccountID(std::uint16_t prefix, uint256 const &parentHash, uint256 const &ammID)
Calculate AMM account ID.
Definition: AMMCore.cpp:30
ripple::invalidAMMAmount
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
ripple::base_uint::size
constexpr static std::size_t size()
Definition: base_uint.h:519
ripple::sfExpiration
const SF_UINT32 sfExpiration
ripple::base_uint< 256 >
ripple::temBAD_AMM_TOKENS
@ temBAD_AMM_TOKENS
Definition: TER.h:127
ripple::ripesha_hasher
Returns the RIPEMD-160 digest of the SHA256 hash of the message.
Definition: digest.h:131
std::minmax
T minmax(T... args)
std::array
STL class.
ripple::STAmount
Definition: STAmount.h:45
std::copy
T copy(T... args)
ripple::isXRP
bool isXRP(AccountID const &c)
Definition: AccountID.h:89
ripple::ValStatus::current
@ current
This was a new validation and was added.
ripple::temBAD_AMOUNT
@ temBAD_AMOUNT
Definition: TER.h:87
std::uint16_t
ripple::invalidAMMAssetPair
NotTEC invalidAMMAssetPair(Issue const &issue1, Issue const &issue2, std::optional< std::pair< Issue, Issue >> const &pair=std::nullopt)
Definition: AMMCore.cpp:79
ripple::STObject
Definition: STObject.h:52
ripple::Currency
base_uint< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
Definition: UintTypes.h:56
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::TOTAL_TIME_SLOT_SECS
constexpr std::uint32_t TOTAL_TIME_SLOT_SECS
Definition: AMMCore.h:34
ripple::base_uint::begin
iterator begin()
Definition: base_uint.h:133
ripple::featureAMM
const uint256 featureAMM
ripple::sha512Half
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
Definition: digest.h:216
ripple::AUCTION_SLOT_INTERVAL_DURATION
constexpr std::uint32_t AUCTION_SLOT_INTERVAL_DURATION
Definition: AMMCore.h:40
ripple::fixUniversalNumber
const uint256 fixUniversalNumber
ripple::Rules
Rules controlling protocol behavior.
Definition: Rules.h:33
ripple::invalidAMMAsset
NotTEC invalidAMMAsset(Issue const &issue, std::optional< std::pair< Issue, Issue >> const &pair=std::nullopt)
Definition: AMMCore.cpp:65
std::optional
ripple::ammLPTCurrency
Currency ammLPTCurrency(Currency const &cur1, Currency const &cur2)
Calculate Liquidity Provider Token (LPT) Currency.
Definition: AMMCore.cpp:42
ripple::tesSUCCESS
@ tesSUCCESS
Definition: TER.h:225
ripple::AccountID
base_uint< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition: AccountID.h:47
ripple::Issue::account
AccountID account
Definition: Issue.h:39
ripple::ammAuctionTimeSlot
std::optional< std::uint8_t > ammAuctionTimeSlot(std::uint64_t current, STObject const &auctionSlot)
Get time slot of the auction slot.
Definition: AMMCore.cpp:107
ripple::NotTEC
TERSubset< CanCvtToNotTEC > NotTEC
Definition: TER.h:539