Skip to content

Commit

Permalink
Introduce AMM support (XLS-30d): (XRPLF#4294)
Browse files Browse the repository at this point in the history
Add AMM functionality:
- InstanceCreate
- Deposit
- Withdraw
- Governance
- Auctioning
- payment engine integration

To support this functionality, add:
- New RPC method, `amm_info`, to fetch pool and LPT balances
- AMM Root Account
- trust line for each IOU AMM token
- trust line to track Liquidity Provider Tokens (LPT)
- `ltAMM` object

The `ltAMM` object tracks:
- fee votes
- auction slot bids
- AMM tokens pair
- total outstanding tokens balance
- `AMMID` to AMM `RootAccountID` mapping

Add new classes to facilitate AMM integration into the payment engine.
`BookStep` uses these classes to infer if AMM liquidity can be consumed.

The AMM formula implementation uses the new Number class added in XRPLF#4192.
IOUAmount and STAmount use Number arithmetic.

Add AMM unit tests for all features.

AMM requires the following amendments:
- featureAMM
- fixUniversalNumber
- featureFlowCross

Notes:
- Current trading fee threshold is 1%
- AMM currency is generated by: 0x03 + 152 bits of sha256{cur1, cur2}
- Current max AMM Offers is 30

---------

Co-authored-by: Howard Hinnant <[email protected]>
  • Loading branch information
2 people authored and ckeshava committed Sep 25, 2023
1 parent 3059d9e commit ae573dc
Show file tree
Hide file tree
Showing 100 changed files with 18,092 additions and 649 deletions.
23 changes: 23 additions & 0 deletions Builds/CMake/RippledCore.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,20 @@ target_sources (xrpl_core PRIVATE
subdir: protocol
#]===============================]
src/ripple/protocol/impl/AccountID.cpp
src/ripple/protocol/impl/AMMCore.cpp
src/ripple/protocol/impl/Book.cpp
src/ripple/protocol/impl/BuildInfo.cpp
src/ripple/protocol/impl/ErrorCodes.cpp
src/ripple/protocol/impl/Feature.cpp
src/ripple/protocol/impl/Indexes.cpp
src/ripple/protocol/impl/InnerObjectFormats.cpp
src/ripple/protocol/impl/Issue.cpp
src/ripple/protocol/impl/STIssue.cpp
src/ripple/protocol/impl/Keylet.cpp
src/ripple/protocol/impl/LedgerFormats.cpp
src/ripple/protocol/impl/PublicKey.cpp
src/ripple/protocol/impl/Quality.cpp
src/ripple/protocol/impl/QualityFunction.cpp
src/ripple/protocol/impl/Rate2.cpp
src/ripple/protocol/impl/Rules.cpp
src/ripple/protocol/impl/SField.cpp
Expand Down Expand Up @@ -223,6 +226,7 @@ install (
install (
FILES
src/ripple/protocol/AccountID.h
src/ripple/protocol/AMMCore.h
src/ripple/protocol/AmountConversions.h
src/ripple/protocol/Book.h
src/ripple/protocol/BuildInfo.h
Expand All @@ -239,12 +243,14 @@ install (
src/ripple/protocol/Protocol.h
src/ripple/protocol/PublicKey.h
src/ripple/protocol/Quality.h
src/ripple/protocol/QualityFunction.h
src/ripple/protocol/Rate.h
src/ripple/protocol/Rules.h
src/ripple/protocol/SField.h
src/ripple/protocol/SOTemplate.h
src/ripple/protocol/STAccount.h
src/ripple/protocol/STAmount.h
src/ripple/protocol/STIssue.h
src/ripple/protocol/STArray.h
src/ripple/protocol/STBase.h
src/ripple/protocol/STBitString.h
Expand Down Expand Up @@ -423,6 +429,8 @@ target_sources (rippled PRIVATE
src/ripple/app/reporting/ReportingETL.cpp
src/ripple/app/reporting/ETLSource.cpp
src/ripple/app/reporting/P2pProxy.cpp
src/ripple/app/misc/impl/AMMHelpers.cpp
src/ripple/app/misc/impl/AMMUtils.cpp
src/ripple/app/misc/CanonicalTXSet.cpp
src/ripple/app/misc/FeeVoteImpl.cpp
src/ripple/app/misc/HashRouter.cpp
Expand All @@ -448,6 +456,8 @@ target_sources (rippled PRIVATE
src/ripple/app/paths/RippleCalc.cpp
src/ripple/app/paths/RippleLineCache.cpp
src/ripple/app/paths/TrustLine.cpp
src/ripple/app/paths/impl/AMMLiquidity.cpp
src/ripple/app/paths/impl/AMMOffer.cpp
src/ripple/app/paths/impl/BookStep.cpp
src/ripple/app/paths/impl/DirectStep.cpp
src/ripple/app/paths/impl/PaySteps.cpp
Expand All @@ -464,6 +474,11 @@ target_sources (rippled PRIVATE
src/ripple/app/rdb/impl/UnitaryShard.cpp
src/ripple/app/rdb/impl/Vacuum.cpp
src/ripple/app/rdb/impl/Wallet.cpp
src/ripple/app/tx/impl/AMMBid.cpp
src/ripple/app/tx/impl/AMMCreate.cpp
src/ripple/app/tx/impl/AMMDeposit.cpp
src/ripple/app/tx/impl/AMMVote.cpp
src/ripple/app/tx/impl/AMMWithdraw.cpp
src/ripple/app/tx/impl/ApplyContext.cpp
src/ripple/app/tx/impl/BookTip.cpp
src/ripple/app/tx/impl/CancelCheck.cpp
Expand Down Expand Up @@ -629,6 +644,7 @@ target_sources (rippled PRIVATE
src/ripple/rpc/handlers/AccountObjects.cpp
src/ripple/rpc/handlers/AccountOffers.cpp
src/ripple/rpc/handlers/AccountTx.cpp
src/ripple/rpc/handlers/AMMInfo.cpp
src/ripple/rpc/handlers/BlackList.cpp
src/ripple/rpc/handlers/BookOffers.cpp
src/ripple/rpc/handlers/CanDelete.cpp
Expand Down Expand Up @@ -735,6 +751,9 @@ if (tests)
src/test/app/AccountDelete_test.cpp
src/test/app/AccountTxPaging_test.cpp
src/test/app/AmendmentTable_test.cpp
src/test/app/AMM_test.cpp
src/test/app/AMMCalc_test.cpp
src/test/app/AMMExtended_test.cpp
src/test/app/Check_test.cpp
src/test/app/Clawback_test.cpp
src/test/app/CrossingLimits_test.cpp
Expand Down Expand Up @@ -877,9 +896,12 @@ if (tests)
src/test/jtx/Env_test.cpp
src/test/jtx/WSClient_test.cpp
src/test/jtx/impl/Account.cpp
src/test/jtx/impl/AMM.cpp
src/test/jtx/impl/AMMTest.cpp
src/test/jtx/impl/Env.cpp
src/test/jtx/impl/JSONRPCClient.cpp
src/test/jtx/impl/ManualTimeKeeper.cpp
src/test/jtx/impl/TestHelpers.cpp
src/test/jtx/impl/WSClient.cpp
src/test/jtx/impl/acctdelete.cpp
src/test/jtx/impl/account_txn_id.cpp
Expand Down Expand Up @@ -995,6 +1017,7 @@ if (tests)
src/test/rpc/AccountSet_test.cpp
src/test/rpc/AccountTx_test.cpp
src/test/rpc/AmendmentBlocked_test.cpp
src/test/rpc/AMMInfo_test.cpp
src/test/rpc/Book_test.cpp
src/test/rpc/DepositAuthorized_test.cpp
src/test/rpc/DeliveredAmount_test.cpp
Expand Down
2 changes: 2 additions & 0 deletions Builds/levelization/results/ordering.txt
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ test.jtx > ripple.json
test.jtx > ripple.ledger
test.jtx > ripple.net
test.jtx > ripple.protocol
test.jtx > ripple.resource
test.jtx > ripple.rpc
test.jtx > ripple.server
test.ledger > ripple.app
test.ledger > ripple.basics
Expand Down
18 changes: 17 additions & 1 deletion src/ripple/app/ledger/OrderBookDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <ripple/app/ledger/LedgerMaster.h>
#include <ripple/app/ledger/OrderBookDB.h>
#include <ripple/app/main/Application.h>
#include <ripple/app/misc/AMMUtils.h>
#include <ripple/app/misc/NetworkOPs.h>
#include <ripple/basics/Log.h>
#include <ripple/core/Config.h>
Expand Down Expand Up @@ -93,7 +94,7 @@ OrderBookDB::update(std::shared_ptr<ReadView const> const& ledger)

JLOG(j_.debug()) << "Beginning update (" << ledger->seq() << ")";

// walk through the entire ledger looking for orderbook entries
// walk through the entire ledger looking for orderbook/AMM entries
int cnt = 0;

try
Expand Down Expand Up @@ -126,6 +127,21 @@ OrderBookDB::update(std::shared_ptr<ReadView const> const& ledger)

++cnt;
}
else if (sle->getType() == ltAMM)
{
auto const issue1 = (*sle)[sfAsset];
auto const issue2 = (*sle)[sfAsset2];
auto addBook = [&](Issue const& in, Issue const& out) {
allBooks[in].insert(out);

if (isXRP(out))
xrpBooks.insert(in);

++cnt;
};
addBook(issue1, issue2);
addBook(issue2, issue1);
}
}
}
catch (SHAMapMissingNode const& mn)
Expand Down
Loading

0 comments on commit ae573dc

Please sign in to comment.