|
| 1 | +//------------------------------------------------------------------------------ |
| 2 | +/* |
| 3 | + This file is part of rippled: https://github.com/ripple/rippled |
| 4 | + Copyright (c) 2022 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 | +#ifndef RIPPLE_APP_MISC_AMM_H_INLCUDED |
| 20 | +#define RIPPLE_APP_MISC_AMM_H_INLCUDED |
| 21 | + |
| 22 | +#include <ripple/beast/utility/Journal.h> |
| 23 | +#include <ripple/protocol/Quality.h> |
| 24 | +#include <ripple/protocol/STAmount.h> |
| 25 | +#include <ripple/protocol/STArray.h> |
| 26 | +#include <ripple/protocol/STLedgerEntry.h> |
| 27 | +#include <ripple/protocol/TER.h> |
| 28 | +#include <ripple/protocol/TxFlags.h> |
| 29 | +#include <ripple/protocol/digest.h> |
| 30 | + |
| 31 | +namespace ripple { |
| 32 | + |
| 33 | +class ReadView; |
| 34 | +class ApplyView; |
| 35 | +class Sandbox; |
| 36 | +class STLedgerEntry; |
| 37 | +class NetClock; |
| 38 | +class STObject; |
| 39 | +class Rules; |
| 40 | + |
| 41 | +/** Calculate AMM account ID. |
| 42 | + */ |
| 43 | +template <typename... Args> |
| 44 | +AccountID |
| 45 | +calcAccountID(Args const&... args) |
| 46 | +{ |
| 47 | + ripesha_hasher rsh; |
| 48 | + auto hash = sha512Half(args...); |
| 49 | + rsh(hash.data(), hash.size()); |
| 50 | + return AccountID{static_cast<ripesha_hasher::result_type>(rsh)}; |
| 51 | +} |
| 52 | + |
| 53 | +/** Calculate AMM group hash. The ltAMM object |
| 54 | + * contains all AMM's for the same issues. |
| 55 | + */ |
| 56 | +uint256 |
| 57 | +calcAMMGroupHash(Issue const& issue1, Issue const& issue2); |
| 58 | + |
| 59 | +/** Calculate Liquidity Provider Token (LPT) Currency. |
| 60 | + */ |
| 61 | +Currency |
| 62 | +calcLPTCurrency(AccountID const& ammAccountID); |
| 63 | + |
| 64 | +/** Calculate LPT Issue. |
| 65 | + */ |
| 66 | +Issue |
| 67 | +calcLPTIssue(AccountID const& ammAccountID); |
| 68 | + |
| 69 | +/** Get AMM pool balances. |
| 70 | + */ |
| 71 | +std::pair<STAmount, STAmount> |
| 72 | +ammPoolHolds( |
| 73 | + ReadView const& view, |
| 74 | + AccountID const& ammAccountID, |
| 75 | + Issue const& issue1, |
| 76 | + Issue const& issue2, |
| 77 | + beast::Journal const j); |
| 78 | + |
| 79 | +/** Get AMM pool and LP token balances. If both optIssue are |
| 80 | + * provided then they are used as the AMM token pair issues. |
| 81 | + * Otherwise the missing issues are fetched from ammSle. |
| 82 | + */ |
| 83 | +std::tuple<STAmount, STAmount, STAmount> |
| 84 | +ammHolds( |
| 85 | + ReadView const& view, |
| 86 | + SLE const& ammSle, |
| 87 | + std::optional<Issue> const& optIssue1, |
| 88 | + std::optional<Issue> const& optIssue2, |
| 89 | + beast::Journal const j); |
| 90 | + |
| 91 | +/** Get the balance of LP tokens. |
| 92 | + */ |
| 93 | +STAmount |
| 94 | +lpHolds( |
| 95 | + ReadView const& view, |
| 96 | + AccountID const& ammAccountID, |
| 97 | + AccountID const& lpAccount, |
| 98 | + beast::Journal const j); |
| 99 | + |
| 100 | +/** Validate the amount. |
| 101 | + * If zero is false and amount is beast::zero then invalid amount. |
| 102 | + * Return error code if invalid amount. |
| 103 | + */ |
| 104 | +std::optional<TEMcodes> |
| 105 | +invalidAmount(std::optional<STAmount> const& a, bool zero = false); |
| 106 | + |
| 107 | +/** Check if the line is frozen from the issuer. |
| 108 | + */ |
| 109 | +bool |
| 110 | +isFrozen(ReadView const& view, std::optional<STAmount> const& a); |
| 111 | + |
| 112 | +/** Get AMM SLE and verify that the AMM account exists. |
| 113 | + * Return null if SLE not found or AMM account doesn't exist. |
| 114 | + */ |
| 115 | +std::shared_ptr<STLedgerEntry const> |
| 116 | +getAMMSle(ReadView const& view, uint256 ammID); |
| 117 | + |
| 118 | +std::shared_ptr<STLedgerEntry> |
| 119 | +getAMMSle(Sandbox& view, uint256 ammID); |
| 120 | + |
| 121 | +/** Check if the account requires authorization. |
| 122 | + * Return true if issuer's account, account, and trust line exist |
| 123 | + * and the account requires authorization. |
| 124 | + */ |
| 125 | +bool |
| 126 | +requireAuth(ReadView const& view, Issue const& issue, AccountID const& account); |
| 127 | + |
| 128 | +/** Get AMM trading fee for the given account. The fee is discounted |
| 129 | + * if the account is the auction slot owner or one of the slot's authorized |
| 130 | + * accounts. |
| 131 | + */ |
| 132 | +std::uint16_t |
| 133 | +getTradingFee(SLE const& ammSle, AccountID const& account); |
| 134 | + |
| 135 | +/** Get Issue from sfToken1/sfToken2 fields. |
| 136 | + */ |
| 137 | +std::pair<Issue, Issue> |
| 138 | +getTokensIssue(SLE const& ammSle); |
| 139 | + |
| 140 | +/** Send w/o fees. Either from or to must be AMM account. |
| 141 | + */ |
| 142 | +TER |
| 143 | +ammSend( |
| 144 | + ApplyView& view, |
| 145 | + AccountID const& from, |
| 146 | + AccountID const& to, |
| 147 | + STAmount const& amount, |
| 148 | + beast::Journal j); |
| 149 | + |
| 150 | +/** Get time slot of the auction slot. |
| 151 | + */ |
| 152 | +std::uint16_t |
| 153 | +timeSlot(NetClock::time_point const& clock, STObject const& auctionSlot); |
| 154 | + |
| 155 | +bool |
| 156 | +ammRequiredAmendments(Rules const&); |
| 157 | + |
| 158 | +} // namespace ripple |
| 159 | + |
| 160 | +#endif // RIPPLE_APP_MISC_AMM_H_INLCUDED |
0 commit comments