rippled
AMMHelpers.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/app/misc/AMMHelpers.h>
21 
22 namespace ripple {
23 
24 STAmount
26  STAmount const& asset1,
27  STAmount const& asset2,
28  Issue const& lptIssue)
29 {
30  auto const tokens = root2(asset1 * asset2);
31  return toSTAmount(lptIssue, tokens);
32 }
33 
34 /*
35  * Equation 3:
36  * t = T * [(b/B - (sqrt(f2**2 - b/(B*f1)) - f2)) /
37  * (1 + sqrt(f2**2 - b/(B*f1)) - f2)]
38  * where f1 = 1 - tfee, f2 = (1 - tfee/2)/f1
39  */
40 STAmount
42  STAmount const& asset1Balance,
43  STAmount const& asset1Deposit,
44  STAmount const& lptAMMBalance,
45  std::uint16_t tfee)
46 {
47  auto const f1 = feeMult(tfee);
48  auto const f2 = feeMultHalf(tfee) / f1;
49  Number const r = asset1Deposit / asset1Balance;
50  auto const c = root2(f2 * f2 + r / f1) - f2;
51  auto const t = lptAMMBalance * (r - c) / (1 + c);
52  return toSTAmount(lptAMMBalance.issue(), t);
53 }
54 
55 /* Equation 4 solves equation 3 for b:
56  * Let f1 = 1 - tfee, f2 = (1 - tfee/2)/f1, t1 = t/T, t2 = 1 + t1, R = b/B
57  * then
58  * t1 = [R - sqrt(f2**2 + R/f1) + f2] / [1 + sqrt(f2**2 + R/f1] - f2] =>
59  * sqrt(f2**2 + R/f1)*(t1 + 1) = R + f2 + t1*f2 - t1 =>
60  * sqrt(f2**2 + R/f1)*t2 = R + t2*f2 - t1 =>
61  * sqrt(f2**2 + R/f1) = R/t2 + f2 - t1/t2, let d = f2 - t1/t2 =>
62  * sqrt(f2**2 + R/f1) = R/t2 + d =>
63  * f2**2 + R/f1 = (R/t2)**2 +2*d*R/t2 + d**2 =>
64  * (R/t2)**2 + R*(2*d/t2 - 1/f1) + d**2 - f2**2 = 0
65  */
66 STAmount
68  STAmount const& asset1Balance,
69  STAmount const& lptAMMBalance,
70  STAmount const& lpTokens,
71  std::uint16_t tfee)
72 {
73  auto const f1 = feeMult(tfee);
74  auto const f2 = feeMultHalf(tfee) / f1;
75  auto const t1 = lpTokens / lptAMMBalance;
76  auto const t2 = 1 + t1;
77  auto const d = f2 - t1 / t2;
78  auto const a = 1 / (t2 * t2);
79  auto const b = 2 * d / t2 - 1 / f1;
80  auto const c = d * d - f2 * f2;
81  return toSTAmount(
82  asset1Balance.issue(), asset1Balance * solveQuadraticEq(a, b, c));
83 }
84 
85 /* Equation 7:
86  * t = T * (c - sqrt(c**2 - 4*R))/2
87  * where R = b/B, c = R*fee + 2 - fee
88  */
89 STAmount
91  STAmount const& asset1Balance,
92  STAmount const& asset1Withdraw,
93  STAmount const& lptAMMBalance,
94  std::uint16_t tfee)
95 {
96  Number const fr = asset1Withdraw / asset1Balance;
97  auto const f1 = getFee(tfee);
98  auto const c = fr * f1 + 2 - f1;
99  auto const t = lptAMMBalance * (c - root2(c * c - 4 * fr)) / 2;
100  return toSTAmount(lptAMMBalance.issue(), t);
101 }
102 
103 /* Equation 8 solves equation 7 for b:
104  * c - 2*t/T = sqrt(c**2 - 4*R) =>
105  * c**2 - 4*c*t/T + 4*t**2/T**2 = c**2 - 4*R =>
106  * -4*c*t/T + 4*t**2/T**2 = -4*R =>
107  * -c*t/T + t**2/T**2 = -R -=>
108  * substitute c = R*f + 2 - f =>
109  * -(t/T)*(R*f + 2 - f) + (t/T)**2 = -R, let t1 = t/T =>
110  * -t1*R*f -2*t1 +t1*f +t1**2 = -R =>
111  * R = (t1**2 + t1*(f - 2)) / (t1*f - 1)
112  */
113 STAmount
115  STAmount const& assetBalance,
116  STAmount const& lptAMMBalance,
117  STAmount const& lpTokens,
118  std::uint16_t tfee)
119 {
120  auto const f = getFee(tfee);
121  Number const t1 = lpTokens / lptAMMBalance;
122  auto const b = assetBalance * (t1 * t1 - t1 * (2 - f)) / (t1 * f - 1);
123  return toSTAmount(assetBalance.issue(), b);
124 }
125 
126 Number
127 square(Number const& n)
128 {
129  return n * n;
130 }
131 
132 STAmount
134  STAmount const& lptAMMBalance,
135  STAmount const& lpTokens,
136  bool isDeposit)
137 {
138  // Force rounding downward to ensure adjusted tokens are less or equal
139  // to requested tokens.
140  saveNumberRoundMode rm(Number::setround(Number::rounding_mode::downward));
141  if (isDeposit)
142  return (lptAMMBalance + lpTokens) - lptAMMBalance;
143  return (lpTokens - lptAMMBalance) + lptAMMBalance;
144 }
145 
148  STAmount const& amountBalance,
149  STAmount const& amount,
150  std::optional<STAmount> const& amount2,
151  STAmount const& lptAMMBalance,
152  STAmount const& lpTokens,
153  std::uint16_t tfee,
154  bool isDeposit)
155 {
156  auto const lpTokensActual =
157  adjustLPTokens(lptAMMBalance, lpTokens, isDeposit);
158 
159  if (lpTokensActual == beast::zero)
160  {
161  auto const amount2Opt =
162  amount2 ? std::make_optional(STAmount{}) : std::nullopt;
163  return std::make_tuple(STAmount{}, amount2Opt, lpTokensActual);
164  }
165 
166  if (lpTokensActual < lpTokens)
167  {
168  // Equal trade
169  if (amount2)
170  {
171  Number const fr = lpTokensActual / lpTokens;
172  auto const amountActual = toSTAmount(amount.issue(), fr * amount);
173  auto const amount2Actual =
174  toSTAmount(amount2->issue(), fr * *amount2);
175  return std::make_tuple(
176  amountActual < amount ? amountActual : amount,
177  amount2Actual < amount2 ? amount2Actual : amount2,
178  lpTokensActual);
179  }
180 
181  // Single trade
182  auto const amountActual = [&]() {
183  if (isDeposit)
184  return ammAssetIn(
185  amountBalance, lptAMMBalance, lpTokensActual, tfee);
186  else
187  return withdrawByTokens(
188  amountBalance, lptAMMBalance, lpTokens, tfee);
189  }();
190  return amountActual < amount
191  ? std::make_tuple(amountActual, std::nullopt, lpTokensActual)
192  : std::make_tuple(amount, std::nullopt, lpTokensActual);
193  }
194 
195  assert(lpTokensActual == lpTokens);
196 
197  return {amount, amount2, lpTokensActual};
198 }
199 
200 Number
201 solveQuadraticEq(Number const& a, Number const& b, Number const& c)
202 {
203  return (-b + root2(b * b - 4 * a * c)) / (2 * a);
204 }
205 
206 } // namespace ripple
ripple::lpTokensOut
STAmount lpTokensOut(STAmount const &asset1Balance, STAmount const &asset1Withdraw, STAmount const &lptAMMBalance, std::uint16_t tfee)
Definition: AMMHelpers.cpp:90
std::make_tuple
T make_tuple(T... args)
ripple::root2
Number root2(Number f)
Definition: Number.cpp:689
ripple::Issue
A currency issued by an account.
Definition: Issue.h:35
ripple::solveQuadraticEq
Number solveQuadraticEq(Number const &a, Number const &b, Number const &c)
Definition: AMMHelpers.cpp:201
ripple::lpTokensIn
STAmount lpTokensIn(STAmount const &asset1Balance, STAmount const &asset1Deposit, STAmount const &lptAMMBalance, std::uint16_t tfee)
Definition: AMMHelpers.cpp:41
ripple::STAmount::issue
Issue const & issue() const
Definition: STAmount.h:350
ripple::feeMult
Number feeMult(std::uint16_t tfee)
Get fee multiplier (1 - tfee) @tfee trading fee in basis points.
Definition: AMMCore.h:118
ripple::saveNumberRoundMode
Definition: Number.h:365
std::make_optional
T make_optional(T... args)
ripple::getFee
Number getFee(std::uint16_t tfee)
Convert to the fee from the basis points.
Definition: AMMCore.h:109
ripple::adjustAmountsByLPTokens
std::tuple< STAmount, std::optional< STAmount >, STAmount > adjustAmountsByLPTokens(STAmount const &amountBalance, STAmount const &amount, std::optional< STAmount > const &amount2, STAmount const &lptAMMBalance, STAmount const &lpTokens, std::uint16_t tfee, bool isDeposit)
Definition: AMMHelpers.cpp:147
std::tuple
ripple::square
Number square(Number const &n)
Definition: AMMHelpers.cpp:127
ripple::withdrawByTokens
STAmount withdrawByTokens(STAmount const &assetBalance, STAmount const &lptAMMBalance, STAmount const &lpTokens, std::uint16_t tfee)
Definition: AMMHelpers.cpp:114
ripple::Number
Definition: Number.h:36
ripple::Number::setround
static rounding_mode setround(rounding_mode mode)
Definition: Number.cpp:47
ripple::toSTAmount
STAmount toSTAmount(IOUAmount const &iou, Issue const &iss)
Definition: AmountConversions.h:30
ripple::ammAssetIn
STAmount ammAssetIn(STAmount const &asset1Balance, STAmount const &lptAMMBalance, STAmount const &lpTokens, std::uint16_t tfee)
Definition: AMMHelpers.cpp:67
ripple::STAmount
Definition: STAmount.h:46
std::uint16_t
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::ammLPTokens
STAmount ammLPTokens(STAmount const &asset1, STAmount const &asset2, Issue const &lptIssue)
Definition: AMMHelpers.cpp:25
std::optional< STAmount >
ripple::adjustLPTokens
STAmount adjustLPTokens(STAmount const &lptAMMBalance, STAmount const &lpTokens, bool isDeposit)
Definition: AMMHelpers.cpp:133
ripple::feeMultHalf
Number feeMultHalf(std::uint16_t tfee)
Get fee multiplier (1 - tfee / 2) @tfee trading fee in basis points.
Definition: AMMCore.h:127