Skip to content

Commit

Permalink
Introduce AMM support (XLS-30d)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregtatcam committed Sep 6, 2022
1 parent 67cfbf5 commit ad655ce
Show file tree
Hide file tree
Showing 59 changed files with 8,661 additions and 22 deletions.
14 changes: 14 additions & 0 deletions Builds/CMake/RippledCore.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ target_sources (xrpl_core PRIVATE
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 @@ -212,6 +213,7 @@ 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
Expand Down Expand Up @@ -376,6 +378,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/AMM.cpp
src/ripple/app/misc/impl/AMM_formulae.cpp
src/ripple/app/misc/CanonicalTXSet.cpp
src/ripple/app/misc/FeeVoteImpl.cpp
src/ripple/app/misc/HashRouter.cpp
Expand All @@ -401,6 +405,7 @@ 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/BookStep.cpp
src/ripple/app/paths/impl/DirectStep.cpp
src/ripple/app/paths/impl/PaySteps.cpp
Expand All @@ -417,6 +422,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 @@ -582,6 +592,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 @@ -686,6 +697,7 @@ 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/Check_test.cpp
src/test/app/CrossingLimits_test.cpp
src/test/app/DeliverMin_test.cpp
Expand Down Expand Up @@ -825,6 +837,7 @@ 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/Env.cpp
src/test/jtx/impl/JSONRPCClient.cpp
src/test/jtx/impl/ManualTimeKeeper.cpp
Expand Down Expand Up @@ -943,6 +956,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
3 changes: 3 additions & 0 deletions Builds/levelization/results/loops.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ Loop: ripple.nodestore ripple.overlay
Loop: ripple.overlay ripple.rpc
ripple.rpc ~= ripple.overlay

Loop: test.jtx test.rpc
test.rpc > test.jtx

Loop: test.jtx test.toplevel
test.toplevel > test.jtx

Expand Down
2 changes: 1 addition & 1 deletion Builds/levelization/results/ordering.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ test.jtx > ripple.json
test.jtx > ripple.ledger
test.jtx > ripple.net
test.jtx > ripple.protocol
test.jtx > ripple.rpc
test.jtx > ripple.server
test.ledger > ripple.app
test.ledger > ripple.basics
Expand Down Expand Up @@ -204,7 +205,6 @@ test.rpc > ripple.overlay
test.rpc > ripple.protocol
test.rpc > ripple.resource
test.rpc > ripple.rpc
test.rpc > test.jtx
test.rpc > test.nodestore
test.rpc > test.toplevel
test.server > ripple.app
Expand Down
160 changes: 160 additions & 0 deletions src/ripple/app/misc/AMM.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2022 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#ifndef RIPPLE_APP_MISC_AMM_H_INLCUDED
#define RIPPLE_APP_MISC_AMM_H_INLCUDED

#include <ripple/beast/utility/Journal.h>
#include <ripple/protocol/Quality.h>
#include <ripple/protocol/STAmount.h>
#include <ripple/protocol/STArray.h>
#include <ripple/protocol/STLedgerEntry.h>
#include <ripple/protocol/TER.h>
#include <ripple/protocol/TxFlags.h>
#include <ripple/protocol/digest.h>

namespace ripple {

class ReadView;
class ApplyView;
class Sandbox;
class STLedgerEntry;
class NetClock;
class STObject;
class Rules;

/** Calculate AMM account ID.
*/
template <typename... Args>
AccountID
calcAccountID(Args const&... args)
{
ripesha_hasher rsh;
auto hash = sha512Half(args...);
rsh(hash.data(), hash.size());
return AccountID{static_cast<ripesha_hasher::result_type>(rsh)};
}

/** Calculate AMM group hash. The ltAMM object
* contains all AMM's for the same issues.
*/
uint256
calcAMMGroupHash(Issue const& issue1, Issue const& issue2);

/** Calculate Liquidity Provider Token (LPT) Currency.
*/
Currency
calcLPTCurrency(AccountID const& ammAccountID);

/** Calculate LPT Issue.
*/
Issue
calcLPTIssue(AccountID const& ammAccountID);

/** Get AMM pool balances.
*/
std::pair<STAmount, STAmount>
ammPoolHolds(
ReadView const& view,
AccountID const& ammAccountID,
Issue const& issue1,
Issue const& issue2,
beast::Journal const j);

/** Get AMM pool and LP token balances. If both optIssue are
* provided then they are used as the AMM token pair issues.
* Otherwise the missing issues are fetched from ammSle.
*/
std::tuple<STAmount, STAmount, STAmount>
ammHolds(
ReadView const& view,
SLE const& ammSle,
std::optional<Issue> const& optIssue1,
std::optional<Issue> const& optIssue2,
beast::Journal const j);

/** Get the balance of LP tokens.
*/
STAmount
lpHolds(
ReadView const& view,
AccountID const& ammAccountID,
AccountID const& lpAccount,
beast::Journal const j);

/** Validate the amount.
* If zero is false and amount is beast::zero then invalid amount.
* Return error code if invalid amount.
*/
std::optional<TEMcodes>
invalidAmount(std::optional<STAmount> const& a, bool zero = false);

/** Check if the line is frozen from the issuer.
*/
bool
isFrozen(ReadView const& view, std::optional<STAmount> const& a);

/** Get AMM SLE and verify that the AMM account exists.
* Return null if SLE not found or AMM account doesn't exist.
*/
std::shared_ptr<STLedgerEntry const>
getAMMSle(ReadView const& view, uint256 ammID);

std::shared_ptr<STLedgerEntry>
getAMMSle(Sandbox& view, uint256 ammID);

/** Check if the account requires authorization.
* Return true if issuer's account, account, and trust line exist
* and the account requires authorization.
*/
bool
requireAuth(ReadView const& view, Issue const& issue, AccountID const& account);

/** Get AMM trading fee for the given account. The fee is discounted
* if the account is the auction slot owner or one of the slot's authorized
* accounts.
*/
std::uint16_t
getTradingFee(SLE const& ammSle, AccountID const& account);

/** Get Issue from sfToken1/sfToken2 fields.
*/
std::pair<Issue, Issue>
getTokensIssue(SLE const& ammSle);

/** Send w/o fees. Either from or to must be AMM account.
*/
TER
ammSend(
ApplyView& view,
AccountID const& from,
AccountID const& to,
STAmount const& amount,
beast::Journal j);

/** Get time slot of the auction slot.
*/
std::uint16_t
timeSlot(NetClock::time_point const& clock, STObject const& auctionSlot);

bool
ammRequiredAmendments(Rules const&);

} // namespace ripple

#endif // RIPPLE_APP_MISC_AMM_H_INLCUDED
Loading

0 comments on commit ad655ce

Please sign in to comment.