Skip to content

Commit

Permalink
MPT integration with DEX
Browse files Browse the repository at this point in the history
* Change Issue in STAmount to variant<Issue,MPTIssue>
* Implement direct MPT payment via steps
* Path-finding is not yet fully supported for MPT
  • Loading branch information
gregtatcam committed Mar 11, 2024
1 parent b00eab6 commit d12105b
Show file tree
Hide file tree
Showing 91 changed files with 4,333 additions and 1,430 deletions.
3 changes: 3 additions & 0 deletions Builds/CMake/RippledCore.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ target_sources (xrpl_core PRIVATE
src/ripple/protocol/impl/Indexes.cpp
src/ripple/protocol/impl/InnerObjectFormats.cpp
src/ripple/protocol/impl/Issue.cpp
src/ripple/protocol/impl/PathAsset.cpp
src/ripple/protocol/impl/STIssue.cpp
src/ripple/protocol/impl/Keylet.cpp
src/ripple/protocol/impl/LedgerFormats.cpp
Expand Down Expand Up @@ -263,6 +264,7 @@ install (
src/ripple/protocol/NFTSyntheticSerializer.h
src/ripple/protocol/NFTokenID.h
src/ripple/protocol/NFTokenOfferID.h
src/ripple/protocol/PathAsset.h
src/ripple/protocol/Protocol.h
src/ripple/protocol/PublicKey.h
src/ripple/protocol/Quality.h
Expand Down Expand Up @@ -527,6 +529,7 @@ target_sources (rippled PRIVATE
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/MPTEndpointStep.cpp
src/ripple/app/paths/impl/PaySteps.cpp
src/ripple/app/paths/impl/XRPEndpointStep.cpp
src/ripple/app/rdb/backend/detail/impl/Node.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/ripple/app/ledger/AcceptedLedgerTx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ AcceptedLedgerTx::AcceptedLedgerTx(
auto const amount = mTxn->getFieldAmount(sfTakerGets);

// If the offer create is not self funded then add the owner balance
if (account != amount.issue().account)
if (account != amount.asset().account())
{
auto const ownerFunds = accountFunds(
*ledger,
Expand Down
54 changes: 36 additions & 18 deletions src/ripple/app/ledger/OrderBookDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,28 @@ OrderBookDB::update(std::shared_ptr<ReadView const> const& ledger)
{
Book book;

book.in.currency = sle->getFieldH160(sfTakerPaysCurrency);
book.in.account = sle->getFieldH160(sfTakerPaysIssuer);
book.out.currency = sle->getFieldH160(sfTakerGetsCurrency);
book.out.account = sle->getFieldH160(sfTakerGetsIssuer);
if (sle->isFieldPresent(sfTakerPaysCurrency))
{
Issue iss;
iss.currency = sle->getFieldH160(sfTakerPaysCurrency);
iss.account = sle->getFieldH160(sfTakerPaysIssuer);
book.in = iss;
}
else
{
book.in = sle->getFieldH192(sfTakerPaysMPT);
}
if (sle->isFieldPresent(sfTakerGetsCurrency))
{
Issue iss;
iss.currency = sle->getFieldH160(sfTakerGetsCurrency);
iss.account = sle->getFieldH160(sfTakerGetsIssuer);
book.out = iss;
}
else
{
book.out = sle->getFieldH192(sfTakerGetsMPT);
}

allBooks[book.in].insert(book.out);

Expand All @@ -129,18 +147,18 @@ OrderBookDB::update(std::shared_ptr<ReadView const> const& ledger)
}
else if (sle->getType() == ltAMM)
{
auto const issue1 = (*sle)[sfAsset];
auto const issue2 = (*sle)[sfAsset2];
auto addBook = [&](Issue const& in, Issue const& out) {
auto const asset1 = (*sle)[sfAsset];
auto const asset2 = (*sle)[sfAsset2];
auto addBook = [&](Asset const& in, Asset const& out) {
allBooks[in].insert(out);

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

++cnt;
};
addBook(issue1, issue2);
addBook(issue2, issue1);
addBook(asset1, asset2);
addBook(asset2, asset1);
}
}
}
Expand Down Expand Up @@ -179,39 +197,39 @@ OrderBookDB::addOrderBook(Book const& book)

// return list of all orderbooks that want this issuerID and currencyID
std::vector<Book>
OrderBookDB::getBooksByTakerPays(Issue const& issue)
OrderBookDB::getBooksByTakerPays(Asset const& asset)
{
std::vector<Book> ret;

{
std::lock_guard sl(mLock);

if (auto it = allBooks_.find(issue); it != allBooks_.end())
if (auto it = allBooks_.find(asset); it != allBooks_.end())
{
ret.reserve(it->second.size());

for (auto const& gets : it->second)
ret.push_back(Book(issue, gets));
ret.push_back(Book(asset, gets));
}
}

return ret;
}

int
OrderBookDB::getBookSize(Issue const& issue)
OrderBookDB::getBookSize(Asset const& asset)
{
std::lock_guard sl(mLock);
if (auto it = allBooks_.find(issue); it != allBooks_.end())
if (auto it = allBooks_.find(asset); it != allBooks_.end())
return static_cast<int>(it->second.size());
return 0;
}

bool
OrderBookDB::isBookToXRP(Issue const& issue)
OrderBookDB::isBookToXRP(Asset const& asset)
{
std::lock_guard sl(mLock);
return xrpBooks_.count(issue) > 0;
return xrpBooks_.count(asset) > 0;
}

BookListeners::pointer
Expand Down Expand Up @@ -274,8 +292,8 @@ OrderBookDB::processTxn(
data->isFieldPresent(sfTakerGets))
{
auto listeners = getBookListeners(
{data->getFieldAmount(sfTakerGets).issue(),
data->getFieldAmount(sfTakerPays).issue()});
{data->getFieldAmount(sfTakerGets).asset(),
data->getFieldAmount(sfTakerPays).asset()});
if (listeners)
listeners->publish(jvObj, havePublished);
}
Expand Down
10 changes: 5 additions & 5 deletions src/ripple/app/ledger/OrderBookDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ class OrderBookDB
/** @return a list of all orderbooks that want this issuerID and currencyID.
*/
std::vector<Book>
getBooksByTakerPays(Issue const&);
getBooksByTakerPays(Asset const&);

/** @return a count of all orderbooks that want this issuerID and
currencyID. */
int
getBookSize(Issue const&);
getBookSize(Asset const&);

bool
isBookToXRP(Issue const&);
isBookToXRP(Asset const&);

BookListeners::pointer
getBookListeners(Book const&);
Expand All @@ -71,10 +71,10 @@ class OrderBookDB
Application& app_;

// Maps order books by "issue in" to "issue out":
hardened_hash_map<Issue, hardened_hash_set<Issue>> allBooks_;
hardened_hash_map<Asset, hardened_hash_set<Asset>> allBooks_;

// does an order book to XRP exist
hash_set<Issue> xrpBooks_;
hash_set<Asset> xrpBooks_;

std::recursive_mutex mLock;

Expand Down
6 changes: 3 additions & 3 deletions src/ripple/app/misc/AMMHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ changeSpotPriceQuality(
if (nTakerPays <= 0)
return std::nullopt;
auto const takerPays = toAmount<TIn>(
getIssue(pool.in), nTakerPays, Number::rounding_mode::upward);
getAsset(pool.in), nTakerPays, Number::rounding_mode::upward);
// should not fail
if (auto const amounts =
TAmounts<TIn, TOut>{
Expand Down Expand Up @@ -229,7 +229,7 @@ swapAssetIn(
std::uint16_t tfee)
{
return toAmount<TOut>(
getIssue(pool.out),
getAsset(pool.out),
pool.out - (pool.in * pool.out) / (pool.in + assetIn * feeMult(tfee)),
Number::rounding_mode::downward);
}
Expand All @@ -251,7 +251,7 @@ swapAssetOut(
std::uint16_t tfee)
{
return toAmount<TIn>(
getIssue(pool.in),
getAsset(pool.in),
((pool.in * pool.out) / (pool.out - assetOut) - pool.in) /
feeMult(tfee),
Number::rounding_mode::upward);
Expand Down
18 changes: 9 additions & 9 deletions src/ripple/app/misc/AMMUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ std::pair<STAmount, STAmount>
ammPoolHolds(
ReadView const& view,
AccountID const& ammAccountID,
Issue const& issue1,
Issue const& issue2,
Asset const& asset1,
Asset const& asset2,
FreezeHandling freezeHandling,
beast::Journal const j);

Expand All @@ -52,8 +52,8 @@ Expected<std::tuple<STAmount, STAmount, STAmount>, TER>
ammHolds(
ReadView const& view,
SLE const& ammSle,
std::optional<Issue> const& optIssue1,
std::optional<Issue> const& optIssue2,
std::optional<Asset> const& optAsset1,
std::optional<Asset> const& optAsset2,
FreezeHandling freezeHandling,
beast::Journal const j);

Expand All @@ -62,8 +62,8 @@ ammHolds(
STAmount
ammLPHolds(
ReadView const& view,
Currency const& cur1,
Currency const& cur2,
Asset const& asset1,
Asset const& asset2,
AccountID const& ammAccount,
AccountID const& lpAccount,
beast::Journal const j);
Expand Down Expand Up @@ -91,16 +91,16 @@ STAmount
ammAccountHolds(
ReadView const& view,
AccountID const& ammAccountID,
Issue const& issue);
Asset const& asset);

/** Delete trustlines to AMM. If all trustlines are deleted then
* AMM object and account are deleted. Otherwise tecIMPCOMPLETE is returned.
*/
TER
deleteAMMAccount(
Sandbox& view,
Issue const& asset,
Issue const& asset2,
Asset const& asset,
Asset const& asset2,
beast::Journal j);

/** Initialize Auction and Voting slots and set the trading/discounted fee.
Expand Down
21 changes: 10 additions & 11 deletions src/ripple/app/misc/NetworkOPs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3155,7 +3155,7 @@ NetworkOPsImp::transJson(
auto const amount = transaction->getFieldAmount(sfTakerGets);

// If the offer create is not self funded then add the owner balance
if (account != amount.issue().account)
if (account != amount.asset().account())
{
auto const ownerFunds = accountFunds(
*ledger,
Expand Down Expand Up @@ -4332,8 +4332,8 @@ NetworkOPsImp::getBookPage(

ReadView const& view = *lpLedger;

bool const bGlobalFreeze = isGlobalFrozen(view, book.out.account) ||
isGlobalFrozen(view, book.in.account);
bool const bGlobalFreeze = isGlobalFrozen(view, book.out.account()) ||
isGlobalFrozen(view, book.in.account());

bool bDone = false;
bool bDirectAdvance = true;
Expand All @@ -4343,7 +4343,7 @@ NetworkOPsImp::getBookPage(
unsigned int uBookEntry;
STAmount saDirRate;

auto const rate = transferRate(view, book.out.account);
auto const rate = transferRate(view, book.out.account());
auto viewJ = app_.journal("View");

while (!bDone && iLimit-- > 0)
Expand Down Expand Up @@ -4391,7 +4391,7 @@ NetworkOPsImp::getBookPage(
STAmount saOwnerFunds;
bool firstOwnerOffer(true);

if (book.out.account == uOfferOwnerID)
if (book.out.account() == uOfferOwnerID)
{
// If an offer is selling issuer's own IOUs, it is fully
// funded.
Expand Down Expand Up @@ -4420,8 +4420,7 @@ NetworkOPsImp::getBookPage(
saOwnerFunds = accountHolds(
view,
uOfferOwnerID,
book.out.currency,
book.out.account,
book.out,
fhZERO_IF_FROZEN,
viewJ);

Expand All @@ -4442,9 +4441,9 @@ NetworkOPsImp::getBookPage(

if (rate != parityRate
// Have a tranfer fee.
&& uTakerID != book.out.account
&& uTakerID != book.out.account()
// Not taking offers of own IOUs.
&& book.out.account != uOfferOwnerID)
&& book.out.account() != uOfferOwnerID)
// Offer owner not issuing ownfunds
{
// Need to charge a transfer fee to offer owner.
Expand All @@ -4467,7 +4466,7 @@ NetworkOPsImp::getBookPage(
std::min(
saTakerPays,
multiply(
saTakerGetsFunded, saDirRate, saTakerPays.issue()))
saTakerGetsFunded, saDirRate, saTakerPays.asset()))
.setJson(jvOffer[jss::taker_pays_funded]);
}

Expand Down Expand Up @@ -4618,7 +4617,7 @@ NetworkOPsImp::getBookPage(
// going on here?
std::min(
saTakerPays,
multiply(saTakerGetsFunded, saDirRate, saTakerPays.issue()))
multiply(saTakerGetsFunded, saDirRate, saTakerPays.asset()))
.setJson(jvOffer[jss::taker_pays_funded]);
}

Expand Down
8 changes: 4 additions & 4 deletions src/ripple/app/misc/impl/AMMHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ ammAssetIn(
auto const b = 2 * d / t2 - 1 / f1;
auto const c = d * d - f2 * f2;
return toSTAmount(
asset1Balance.issue(), asset1Balance * solveQuadraticEq(a, b, c));
asset1Balance.asset(), asset1Balance * solveQuadraticEq(a, b, c));
}

/* Equation 7:
Expand Down Expand Up @@ -120,7 +120,7 @@ withdrawByTokens(
auto const f = getFee(tfee);
Number const t1 = lpTokens / lptAMMBalance;
auto const b = assetBalance * (t1 * t1 - t1 * (2 - f)) / (t1 * f - 1);
return toSTAmount(assetBalance.issue(), b);
return toSTAmount(assetBalance.asset(), b);
}

Number
Expand Down Expand Up @@ -169,9 +169,9 @@ adjustAmountsByLPTokens(
if (amount2)
{
Number const fr = lpTokensActual / lpTokens;
auto const amountActual = toSTAmount(amount.issue(), fr * amount);
auto const amountActual = toSTAmount(amount.asset(), fr * amount);
auto const amount2Actual =
toSTAmount(amount2->issue(), fr * *amount2);
toSTAmount(amount2->asset(), fr * *amount2);
return std::make_tuple(
amountActual < amount ? amountActual : amount,
amount2Actual < amount2 ? amount2Actual : amount2,
Expand Down
Loading

0 comments on commit d12105b

Please sign in to comment.