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
44 std::uint16_t prefix,
45 uint256 const& parentHash,
46 uint256 const& ammID)
47{
49 auto const hash = sha512Half(prefix, parentHash, ammID);
50 rsh(hash.data(), hash.size());
51 return AccountID{static_cast<ripesha_hasher::result_type>(rsh)};
52}
53
55ammLPTCurrency(Currency const& cur1, Currency const& cur2)
56{
57 // AMM LPToken is 0x03 plus 19 bytes of the hash
58 std::int32_t constexpr AMMCurrencyCode = 0x03;
59 auto const [minC, maxC] = std::minmax(cur1, cur2);
60 auto const hash = sha512Half(minC, maxC);
61 Currency currency;
62 *currency.begin() = AMMCurrencyCode;
64 hash.begin(), hash.begin() + currency.size() - 1, currency.begin() + 1);
65 return currency;
66}
67
68Issue
70 Currency const& cur1,
71 Currency const& cur2,
73{
74 return Issue(ammLPTCurrency(cur1, cur2), ammAccountID);
75}
76
79 Issue const& issue,
81{
82 if (badCurrency() == issue.currency)
83 return temBAD_CURRENCY;
84 if (isXRP(issue) && issue.account.isNonZero())
85 return temBAD_ISSUER;
86 if (pair && issue != pair->first && issue != pair->second)
87 return temBAD_AMM_TOKENS;
88 return tesSUCCESS;
89}
90
93 Issue const& issue1,
94 Issue const& issue2,
96{
97 if (issue1 == issue2)
98 return temBAD_AMM_TOKENS;
99 if (auto const res = invalidAMMAsset(issue1, pair))
100 return res;
101 if (auto const res = invalidAMMAsset(issue2, pair))
102 return res;
103 return tesSUCCESS;
104}
105
106NotTEC
108 STAmount const& amount,
110 bool validZero)
111{
112 if (auto const res = invalidAMMAsset(amount.issue(), pair))
113 return res;
114 if (amount < beast::zero || (!validZero && amount == beast::zero))
115 return temBAD_AMOUNT;
116 return tesSUCCESS;
117}
118
121{
122 // It should be impossible for expiration to be < TOTAL_TIME_SLOT_SECS,
123 // but check just to be safe
124 auto const expiration = auctionSlot[sfExpiration];
125 XRPL_ASSERT(
126 expiration >= TOTAL_TIME_SLOT_SECS,
127 "ripple::ammAuctionTimeSlot : minimum expiration");
128 if (expiration >= TOTAL_TIME_SLOT_SECS)
129 {
130 if (auto const start = expiration - TOTAL_TIME_SLOT_SECS;
131 current >= start)
132 {
133 if (auto const diff = current - start; diff < TOTAL_TIME_SLOT_SECS)
134 return diff / AUCTION_SLOT_INTERVAL_DURATION;
135 }
136 }
137 return std::nullopt;
138}
139
140bool
141ammEnabled(Rules const& rules)
142{
143 return rules.enabled(featureAMM) && rules.enabled(fixUniversalNumber);
144}
145
146} // 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:130
Issue const & issue() const
Definition: STAmount.h:487
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: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:107
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:91
std::optional< std::uint8_t > ammAuctionTimeSlot(std::uint64_t current, STObject const &auctionSlot)
Get time slot of the auction slot.
Definition: AMMCore.cpp:120
NotTEC invalidAMMAsset(Issue const &issue, std::optional< std::pair< Issue, Issue > > const &pair=std::nullopt)
Definition: AMMCore.cpp:78
Currency ammLPTCurrency(Currency const &cur1, Currency const &cur2)
Calculate Liquidity Provider Token (LPT) Currency.
Definition: AMMCore.cpp:55
bool ammEnabled(Rules const &)
Return true if required AMM amendments are enabled.
Definition: AMMCore.cpp:141
Issue ammLPTIssue(Currency const &cur1, Currency const &cur2, AccountID const &ammAccountID)
Calculate LPT Issue from AMM asset pair.
Definition: AMMCore.cpp:69
@ 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:43
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:92
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:225
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:137