Skip to content

Commit

Permalink
Fix adjustAmountsByLPTokens()
Browse files Browse the repository at this point in the history
  • Loading branch information
gregtatcam committed May 4, 2024
1 parent c31ff5b commit 0e3b555
Show file tree
Hide file tree
Showing 5 changed files with 330 additions and 186 deletions.
21 changes: 17 additions & 4 deletions src/ripple/app/misc/impl/AMMHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,18 +178,31 @@ adjustAmountsByLPTokens(
lpTokensActual);
}

bool const ammRoundingEnabled = [&]() {
if (auto const& rules = getCurrentTransactionRules();
rules && rules->enabled(fixAMMRounding))
return true;
return false;
}();

// Single trade
auto const amountActual = [&]() {
if (isDeposit)
return ammAssetIn(
amountBalance, lptAMMBalance, lpTokensActual, tfee);
else
else if (!ammRoundingEnabled)
return withdrawByTokens(
amountBalance, lptAMMBalance, lpTokens, tfee);
else
return withdrawByTokens(
amountBalance, lptAMMBalance, lpTokensActual, tfee);
}();
return amountActual < amount
? std::make_tuple(amountActual, std::nullopt, lpTokensActual)
: std::make_tuple(amount, std::nullopt, lpTokensActual);
if (!ammRoundingEnabled)
return amountActual < amount
? std::make_tuple(amountActual, std::nullopt, lpTokensActual)
: std::make_tuple(amount, std::nullopt, lpTokensActual);
else
return std::make_tuple(amountActual, std::nullopt, lpTokensActual);
}

assert(lpTokensActual == lpTokens);
Expand Down
20 changes: 10 additions & 10 deletions src/test/app/AMMExtended_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ struct AMMExtended_test : public jtx::AMMTest
{{XRP(10'100), USD(10'000)}},
0,
std::nullopt,
tweakedFeatures);
{tweakedFeatures});

// Immediate or Cancel - cross as much as possible
// and add nothing on the books.
Expand All @@ -257,7 +257,7 @@ struct AMMExtended_test : public jtx::AMMTest
{{XRP(10'100), USD(10'000)}},
0,
std::nullopt,
tweakedFeatures);
{tweakedFeatures});

// tfPassive -- place the offer without crossing it.
testAMM(
Expand All @@ -274,7 +274,7 @@ struct AMMExtended_test : public jtx::AMMTest
{{XRP(10'100), USD(10'000)}},
0,
std::nullopt,
tweakedFeatures);
{tweakedFeatures});

// tfPassive -- cross only offers of better quality.
testAMM(
Expand All @@ -296,7 +296,7 @@ struct AMMExtended_test : public jtx::AMMTest
{{XRP(11'000), USD(9'000)}},
0,
std::nullopt,
tweakedFeatures);
{tweakedFeatures});
}
}

Expand Down Expand Up @@ -430,7 +430,7 @@ struct AMMExtended_test : public jtx::AMMTest
{{XRP(10'000), USD(10'000)}},
0,
std::nullopt,
features);
{features});
}

void
Expand All @@ -453,7 +453,7 @@ struct AMMExtended_test : public jtx::AMMTest
{{XRP(10'000), USD(10'100)}},
0,
std::nullopt,
features);
{features});
}

void
Expand All @@ -477,7 +477,7 @@ struct AMMExtended_test : public jtx::AMMTest
{{XRP(10'100), USD(10'000)}},
0,
std::nullopt,
features);
{features});
}

void
Expand Down Expand Up @@ -643,7 +643,7 @@ struct AMMExtended_test : public jtx::AMMTest
{{XRP(9'900), USD(10'100)}},
0,
std::nullopt,
features);
{features});
}

void
Expand Down Expand Up @@ -952,7 +952,7 @@ struct AMMExtended_test : public jtx::AMMTest
{{XRP(10'000), USD(10'100)}},
0,
std::nullopt,
features);
{features});

// Reverse the order, so the offer in the books is to sell XRP
// in return for USD.
Expand All @@ -973,7 +973,7 @@ struct AMMExtended_test : public jtx::AMMTest
{{XRP(10'100), USD(10'000)}},
0,
std::nullopt,
features);
{features});

{
// Bridged crossing.
Expand Down
Loading

0 comments on commit 0e3b555

Please sign in to comment.