Add the fixAMMOfferRounding amendment: (#4983)

* Fix AMM offer rounding and low quality LOB offer blocking AMM:

A single-path AMM offer with account offer on DEX, is always generated
starting with the takerPays first, which is rounded up, and then
the takerGets, which is rounded down. This rounding ensures that the pool's
product invariant is maintained. However, when one of the offer's side
is XRP, this rounding can result in the AMM offer having a lower
quality, potentially causing offer generation to fail if the quality
is lower than the account's offer quality.

To address this issue, the proposed fix adjusts the offer generation process
to start with the XRP side first and always rounds it down. This results
in a smaller offer size, improving the offer's quality. Regardless if the offer
has XRP or not, the rounding is done so that the offer size is minimized.
This change still ensures the product invariant, as the other generated
side is the exact result of the swap-in or swap-out equations.

If a liquidity can be provided by both AMM and LOB offer on offer crossing
then AMM offer is generated so that it matches LOB offer quality. If LOB
offer quality is less than limit quality then generated AMM offer quality
is also less than limit quality and the offer doesn't cross. To address
this issue, if LOB quality is better than limit quality then use LOB
quality to generate AMM offer. Otherwise, don't use the quality to generate
AMM offer. In this case, limitOut() function in StrandFlow limits
the out amount to match strand's quality to limit quality and consume
maximum AMM liquidity.
This commit is contained in:
Gregory Tsipenyuk
2024-05-14 15:28:38 -04:00
committed by GitHub
parent 244ac5e024
commit 2705109592
13 changed files with 987 additions and 223 deletions

View File

@@ -94,7 +94,7 @@ private:
sendmax(BTC(1'000)),
txflags(tfPartialPayment));
if (!features[fixAMMRounding])
if (!features[fixAMMv1_1])
{
BEAST_EXPECT(ammCarol.expectBalances(
STAmount{BTC, UINT64_C(1'001'000000374812), -12},
@@ -720,7 +720,7 @@ private:
auto const jrr = env.rpc("json", "submit", to_string(payment));
BEAST_EXPECT(jrr[jss::result][jss::status] == "success");
BEAST_EXPECT(jrr[jss::result][jss::engine_result] == "tesSUCCESS");
if (!features[fixAMMRounding])
if (!features[fixAMMv1_1])
{
BEAST_EXPECT(ammAlice.expectBalances(
STAmount(XTS, UINT64_C(101'010101010101), -12),
@@ -1291,17 +1291,34 @@ private:
env(offer(cam, B_BUX(30), A_BUX(30)));
// AMM is consumed up to the first cam Offer quality
BEAST_EXPECT(ammCarol.expectBalances(
STAmount{A_BUX, UINT64_C(309'3541659651605), -13},
STAmount{B_BUX, UINT64_C(320'0215509984417), -13},
ammCarol.tokens()));
BEAST_EXPECT(expectOffers(
env,
cam,
1,
{{Amounts{
STAmount{B_BUX, UINT64_C(20'0215509984417), -13},
STAmount{A_BUX, UINT64_C(20'0215509984417), -13}}}}));
if (!features[fixAMMv1_1])
{
BEAST_EXPECT(ammCarol.expectBalances(
STAmount{A_BUX, UINT64_C(309'3541659651605), -13},
STAmount{B_BUX, UINT64_C(320'0215509984417), -13},
ammCarol.tokens()));
BEAST_EXPECT(expectOffers(
env,
cam,
1,
{{Amounts{
STAmount{B_BUX, UINT64_C(20'0215509984417), -13},
STAmount{A_BUX, UINT64_C(20'0215509984417), -13}}}}));
}
else
{
BEAST_EXPECT(ammCarol.expectBalances(
STAmount{A_BUX, UINT64_C(309'3541659651604), -13},
STAmount{B_BUX, UINT64_C(320'0215509984419), -13},
ammCarol.tokens()));
BEAST_EXPECT(expectOffers(
env,
cam,
1,
{{Amounts{
STAmount{B_BUX, UINT64_C(20'0215509984419), -13},
STAmount{A_BUX, UINT64_C(20'0215509984419), -13}}}}));
}
}
void
@@ -1427,7 +1444,7 @@ private:
using namespace jtx;
FeatureBitset const all{supported_amendments()};
testRmFundedOffer(all);
testRmFundedOffer(all - fixAMMRounding);
testRmFundedOffer(all - fixAMMv1_1);
testEnforceNoRipple(all);
testFillModes(all);
testOfferCrossWithXRP(all);
@@ -1441,28 +1458,17 @@ private:
testOfferCreateThenCross(all);
testSellFlagExceedLimit(all);
testGatewayCrossCurrency(all);
testGatewayCrossCurrency(all - fixAMMRounding);
// testPartialCross
// testXRPDirectCross
// testDirectCross
testGatewayCrossCurrency(all - fixAMMv1_1);
testBridgedCross(all);
// testSellOffer
testSellWithFillOrKill(all);
testTransferRateOffer(all);
testSelfIssueOffer(all);
testBadPathAssert(all);
testSellFlagBasic(all);
testDirectToDirectPath(all);
// testSelfCrossLowQualityOffer
// testOfferInScaling
// testOfferInScalingWithXferRate
// testOfferThresholdWithReducedFunds
// testTinyOffer
// testSelfPayXferFeeOffer
// testSelfPayXferFeeOffer
testDirectToDirectPath(all - fixAMMv1_1);
testRequireAuth(all);
testMissingAuth(all);
// testRCSmoketest
}
void
@@ -2317,7 +2323,7 @@ private:
txflags(tfNoRippleDirect | tfPartialPayment | tfLimitQuality));
env.close();
if (!features[fixAMMRounding])
if (!features[fixAMMv1_1])
{
// alice buys 77.2727USD with 75.5555GBP and pays 25% tr fee
// on 75.5555GBP
@@ -2364,7 +2370,7 @@ private:
env(offer(alice, EUR(100), USD(100)));
env.close();
if (!features[fixAMMRounding])
if (!features[fixAMMv1_1])
{
// 95.2380USD is swapped in for 100EUR
BEAST_EXPECT(amm.expectBalances(
@@ -2417,7 +2423,7 @@ private:
env(pay(gw, dan, USD(1'000)));
AMM ammDan(env, dan, USD(1'000), EUR(1'050));
if (!features[fixAMMRounding])
if (!features[fixAMMv1_1])
{
// alice -> bob -> gw -> carol. $50 should have transfer fee;
// $10, no fee
@@ -2486,7 +2492,7 @@ private:
// alice buys 107.1428USD with 120GBP and pays 25% tr fee on 120GBP
// 1,000 - 120*1.25 = 850GBP
BEAST_EXPECT(expectLine(env, alice, GBP(850)));
if (!features[fixAMMRounding])
if (!features[fixAMMv1_1])
{
// 120GBP is swapped in for 107.1428USD
BEAST_EXPECT(amm.expectBalances(
@@ -2575,7 +2581,7 @@ private:
env.close();
BEAST_EXPECT(expectLine(env, alice, GBP(850)));
if (!features[fixAMMRounding])
if (!features[fixAMMv1_1])
{
// alice buys 107.1428EUR with 120GBP and pays 25% tr fee on
// 120GBP 1,000 - 120*1.25 = 850GBP 120GBP is swapped in for
@@ -2692,7 +2698,7 @@ private:
txflags(tfNoRippleDirect | tfPartialPayment | tfLimitQuality));
env.close();
if (!features[fixAMMRounding])
if (!features[fixAMMv1_1])
{
// alice buys 28.125USD with 24GBP and pays 25% tr fee
// on 24GBP
@@ -2749,7 +2755,7 @@ private:
txflags(tfNoRippleDirect | tfPartialPayment | tfLimitQuality));
env.close();
if (!features[fixAMMRounding])
if (!features[fixAMMv1_1])
{
// alice buys 70.4210EUR with 70.4210GBP via the offer
// and pays 25% tr fee on 70.4210GBP
@@ -2841,7 +2847,7 @@ private:
txflags(tfNoRippleDirect | tfPartialPayment | tfLimitQuality));
env.close();
if (!features[fixAMMRounding])
if (!features[fixAMMv1_1])
{
// alice buys 53.3322EUR with 56.3368GBP via the amm
// and pays 25% tr fee on 56.3368GBP
@@ -2919,7 +2925,7 @@ private:
txflags(tfNoRippleDirect | tfPartialPayment | tfLimitQuality));
env.close();
if (!features[fixAMMRounding])
if (!features[fixAMMv1_1])
{
// alice buys 53.3322EUR with 107.5308GBP
// 25% on 86.0246GBP is paid in tr fee
@@ -2990,7 +2996,7 @@ private:
txflags(tfNoRippleDirect | tfPartialPayment | tfLimitQuality));
env.close();
if (!features[fixAMMRounding])
if (!features[fixAMMv1_1])
{
// 108.1481GBP is swapped in for 97.5935EUR
BEAST_EXPECT(amm1.expectBalances(
@@ -3163,8 +3169,12 @@ private:
// Alice offers to buy 1000 XRP for 1000 USD. She takes Bob's first
// offer, removes 999 more as unfunded, then hits the step limit.
env(offer(alice, USD(1'000), XRP(1'000)));
env.require(
balance(alice, STAmount{USD, UINT64_C(2'050126257867561), -15}));
if (!features[fixAMMv1_1])
env.require(balance(
alice, STAmount{USD, UINT64_C(2'050126257867561), -15}));
else
env.require(balance(
alice, STAmount{USD, UINT64_C(2'050125257867587), -15}));
env.require(owners(alice, 2));
env.require(balance(bob, USD(0)));
env.require(owners(bob, 1'001));
@@ -3270,7 +3280,7 @@ private:
env(offer(bob, XRP(100), USD(100)));
env(offer(bob, XRP(1'000), USD(100)));
AMM ammDan(env, dan, XRP(1'000), USD(1'100));
if (!features[fixAMMRounding])
if (!features[fixAMMv1_1])
{
env(pay(alice, carol, USD(10'000)),
paths(XRP),
@@ -4080,9 +4090,9 @@ private:
testBookStep(all);
testBookStep(all | ownerPaysFee);
testTransferRate(all | ownerPaysFee);
testTransferRate((all - fixAMMRounding) | ownerPaysFee);
testTransferRate((all - fixAMMv1_1) | ownerPaysFee);
testTransferRateNoOwnerFee(all);
testTransferRateNoOwnerFee(all - fixAMMRounding);
testTransferRateNoOwnerFee(all - fixAMMv1_1);
testLimitQuality();
testXRPPathLoop();
}
@@ -4093,6 +4103,7 @@ private:
using namespace jtx;
FeatureBitset const all{supported_amendments()};
testStepLimit(all);
testStepLimit(all - fixAMMv1_1);
}
void
@@ -4101,7 +4112,7 @@ private:
using namespace jtx;
FeatureBitset const all{supported_amendments()};
test_convert_all_of_an_asset(all);
test_convert_all_of_an_asset(all - fixAMMRounding);
test_convert_all_of_an_asset(all - fixAMMv1_1);
}
void