Skip to content

Commit

Permalink
Price Oracle (XLS-47d): (#4789) (#4789)
Browse files Browse the repository at this point in the history
Implement native support for Price Oracles.

 A Price Oracle is used to bring real-world data, such as market prices,
 onto the blockchain, enabling dApps to access and utilize information
 that resides outside the blockchain.

 Add Price Oracle functionality:
 - OracleSet: create or update the Oracle object
 - OracleDelete: delete the Oracle object

 To support this functionality add:
 - New RPC method, `get_aggregate_price`, to calculate aggregate price for a token pair of the specified oracles
 - `ltOracle` object

 The `ltOracle` object maintains:
 - Oracle Owner's account
 - Oracle's metadata
 - Up to ten token pairs with the scaled price
 - The last update time the token pairs were updated

 Add Oracle unit-tests
  • Loading branch information
gregtatcam authored Feb 26, 2024
1 parent d7d15a9 commit e718378
Show file tree
Hide file tree
Showing 40 changed files with 2,865 additions and 7 deletions.
7 changes: 7 additions & 0 deletions Builds/CMake/RippledCore.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ target_sources (xrpl_core PRIVATE
src/ripple/protocol/impl/STArray.cpp
src/ripple/protocol/impl/STBase.cpp
src/ripple/protocol/impl/STBlob.cpp
src/ripple/protocol/impl/STCurrency.cpp
src/ripple/protocol/impl/STInteger.cpp
src/ripple/protocol/impl/STLedgerEntry.cpp
src/ripple/protocol/impl/STObject.cpp
Expand Down Expand Up @@ -553,6 +554,7 @@ target_sources (rippled PRIVATE
src/ripple/app/tx/impl/CreateOffer.cpp
src/ripple/app/tx/impl/CreateTicket.cpp
src/ripple/app/tx/impl/DeleteAccount.cpp
src/ripple/app/tx/impl/DeleteOracle.cpp
src/ripple/app/tx/impl/DepositPreauth.cpp
src/ripple/app/tx/impl/DID.cpp
src/ripple/app/tx/impl/Escrow.cpp
Expand All @@ -566,6 +568,7 @@ target_sources (rippled PRIVATE
src/ripple/app/tx/impl/PayChan.cpp
src/ripple/app/tx/impl/Payment.cpp
src/ripple/app/tx/impl/SetAccount.cpp
src/ripple/app/tx/impl/SetOracle.cpp
src/ripple/app/tx/impl/SetRegularKey.cpp
src/ripple/app/tx/impl/SetSignerList.cpp
src/ripple/app/tx/impl/SetTrust.cpp
Expand Down Expand Up @@ -721,6 +724,7 @@ target_sources (rippled PRIVATE
src/ripple/rpc/handlers/FetchInfo.cpp
src/ripple/rpc/handlers/GatewayBalances.cpp
src/ripple/rpc/handlers/GetCounts.cpp
src/ripple/rpc/handlers/GetAggregatePrice.cpp
src/ripple/rpc/handlers/LedgerAccept.cpp
src/ripple/rpc/handlers/LedgerCleanerHandler.cpp
src/ripple/rpc/handlers/LedgerClosed.cpp
Expand Down Expand Up @@ -840,6 +844,7 @@ if (tests)
src/test/app/NFTokenDir_test.cpp
src/test/app/OfferStream_test.cpp
src/test/app/Offer_test.cpp
src/test/app/Oracle_test.cpp
src/test/app/OversizeMeta_test.cpp
src/test/app/Path_test.cpp
src/test/app/PayChan_test.cpp
Expand Down Expand Up @@ -964,6 +969,7 @@ if (tests)
src/test/jtx/impl/AMMTest.cpp
src/test/jtx/impl/Env.cpp
src/test/jtx/impl/JSONRPCClient.cpp
src/test/jtx/impl/Oracle.cpp
src/test/jtx/impl/TestHelpers.cpp
src/test/jtx/impl/WSClient.cpp
src/test/jtx/impl/acctdelete.cpp
Expand Down Expand Up @@ -1089,6 +1095,7 @@ if (tests)
src/test/rpc/DeliveredAmount_test.cpp
src/test/rpc/Feature_test.cpp
src/test/rpc/GatewayBalances_test.cpp
src/test/rpc/GetAggregatePrice_test.cpp
src/test/rpc/GetCounts_test.cpp
src/test/rpc/JSONRPC_test.cpp
src/test/rpc/KeyGeneration_test.cpp
Expand Down
15 changes: 15 additions & 0 deletions src/ripple/app/tx/impl/DeleteAccount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <ripple/app/tx/impl/DID.h>
#include <ripple/app/tx/impl/DeleteAccount.h>
#include <ripple/app/tx/impl/DeleteOracle.h>
#include <ripple/app/tx/impl/DepositPreauth.h>
#include <ripple/app/tx/impl/SetSignerList.h>
#include <ripple/app/tx/impl/details/NFTokenUtils.h>
Expand Down Expand Up @@ -146,6 +147,18 @@ removeDIDFromLedger(
return DIDDelete::deleteSLE(view, sleDel, account, j);
}

TER
removeOracleFromLedger(
Application&,
ApplyView& view,
AccountID const& account,
uint256 const&,
std::shared_ptr<SLE> const& sleDel,
beast::Journal j)
{
return DeleteOracle::deleteOracle(view, sleDel, account, j);
}

// Return nullptr if the LedgerEntryType represents an obligation that can't
// be deleted. Otherwise return the pointer to the function that can delete
// the non-obligation
Expand All @@ -166,6 +179,8 @@ nonObligationDeleter(LedgerEntryType t)
return removeNFTokenOfferFromLedger;
case ltDID:
return removeDIDFromLedger;
case ltORACLE:
return removeOracleFromLedger;
default:
return nullptr;
}
Expand Down
110 changes: 110 additions & 0 deletions src/ripple/app/tx/impl/DeleteOracle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2023 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.
*/
//==============================================================================

#include <ripple/app/tx/impl/DeleteOracle.h>
#include <ripple/ledger/Sandbox.h>
#include <ripple/ledger/View.h>
#include <ripple/protocol/Feature.h>
#include <ripple/protocol/Rules.h>
#include <ripple/protocol/TxFlags.h>

namespace ripple {

NotTEC
DeleteOracle::preflight(PreflightContext const& ctx)
{
if (!ctx.rules.enabled(featurePriceOracle))
return temDISABLED;

if (auto const ret = preflight1(ctx); !isTesSuccess(ret))
return ret;

if (ctx.tx.getFlags() & tfUniversalMask)
{
JLOG(ctx.j.debug()) << "Oracle Delete: invalid flags.";
return temINVALID_FLAG;
}

return preflight2(ctx);
}

TER
DeleteOracle::preclaim(PreclaimContext const& ctx)
{
if (!ctx.view.exists(keylet::account(ctx.tx.getAccountID(sfAccount))))
return terNO_ACCOUNT;

if (auto const sle = ctx.view.read(keylet::oracle(
ctx.tx.getAccountID(sfAccount), ctx.tx[sfOracleDocumentID]));
!sle)
{
JLOG(ctx.j.debug()) << "Oracle Delete: Oracle does not exist.";
return tecNO_ENTRY;
}
else if (ctx.tx.getAccountID(sfAccount) != sle->getAccountID(sfOwner))
{
// this can't happen because of the above check
JLOG(ctx.j.debug()) << "Oracle Delete: invalid account.";
return tecINTERNAL;
}
return tesSUCCESS;
}

TER
DeleteOracle::deleteOracle(
ApplyView& view,
std::shared_ptr<SLE> const& sle,
AccountID const& account,
beast::Journal j)
{
if (!sle)
return tesSUCCESS;

if (!view.dirRemove(
keylet::ownerDir(account), (*sle)[sfOwnerNode], sle->key(), true))
{
JLOG(j.fatal()) << "Unable to delete Oracle from owner.";
return tefBAD_LEDGER;
}

auto const sleOwner = view.peek(keylet::account(account));
if (!sleOwner)
return tecINTERNAL;

auto const count =
sle->getFieldArray(sfPriceDataSeries).size() > 5 ? -2 : -1;

adjustOwnerCount(view, sleOwner, count, j);

view.erase(sle);

return tesSUCCESS;
}

TER
DeleteOracle::doApply()
{
if (auto sle = ctx_.view().peek(
keylet::oracle(account_, ctx_.tx[sfOracleDocumentID])))
return deleteOracle(ctx_.view(), sle, account_, j_);

return tecINTERNAL;
}

} // namespace ripple
64 changes: 64 additions & 0 deletions src/ripple/app/tx/impl/DeleteOracle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2023 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_TX_DELETEORACLE_H_INCLUDED
#define RIPPLE_TX_DELETEORACLE_H_INCLUDED

#include <ripple/app/tx/impl/Transactor.h>

namespace ripple {

/**
Price Oracle is a system that acts as a bridge between
a blockchain network and the external world, providing off-chain price data
to decentralized applications (dApps) on the blockchain. This implementation
conforms to the requirements specified in the XLS-47d.
The DeleteOracle transactor implements the deletion of Oracle objects.
*/

class DeleteOracle : public Transactor
{
public:
static constexpr ConsequencesFactoryType ConsequencesFactory{Normal};

explicit DeleteOracle(ApplyContext& ctx) : Transactor(ctx)
{
}

static NotTEC
preflight(PreflightContext const& ctx);

static TER
preclaim(PreclaimContext const& ctx);

TER
doApply() override;

static TER
deleteOracle(
ApplyView& view,
std::shared_ptr<SLE> const& sle,
AccountID const& account,
beast::Journal j);
};

} // namespace ripple

#endif // RIPPLE_TX_DELETEORACLE_H_INCLUDED
1 change: 1 addition & 0 deletions src/ripple/app/tx/impl/InvariantCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ LedgerEntryTypesMatch::visitEntry(
case ltXCHAIN_OWNED_CLAIM_ID:
case ltXCHAIN_OWNED_CREATE_ACCOUNT_CLAIM_ID:
case ltDID:
case ltORACLE:
break;
default:
invalidTypeAdded_ = true;
Expand Down
Loading

0 comments on commit e718378

Please sign in to comment.