XRP Ledger Standards
XLS-0047 Final Amendment

Price Oracles on XRP Ledger

On-chain PriceOracle object to bridge blockchain and external world data for decentralized applications

Author
Gregory Tsipenyuk
Created
2023-08-18
Updated
2026-12-02
Discussion
XRPLF/XRPL-Standards/discussions/129

1. Abstract

This proposal adds an on-chain PriceOracle object to the XRP Ledger. This document outlines a new protocol for price oracles on the XRP Ledger, and provides guidelines for developers and system architects to implement and utilize this solution effectively. This proposal introduces a new on-ledger PriceOracle object and the transactions to create, delete, and update the PriceOracle. It also adds the get_aggregate_price API, to retrieve an aggregate mean, trimmed mean, and median for the provided price oracles. This feature requires an amendment.

2. Motivation

A blockchain oracle is a system or service that acts as a bridge between a blockchain network and the external world, providing off-chain data or information to decentralized applications (dApps) on the blockchain. Oracles are used to bring real-world data, for instance market prices, exchange rates, interest rates, or weather conditions onto the blockchain, enabling dApps to access and utilize information that resides outside the blockchain.

3. Specification

This proposal introduces several new transactions to allow for the creation, update, and deletion of the PriceOracle object.

Terminology:

  • Oracle Provider: A service or technology that enables the integration of external data and real-world events into a blockchain network.
  • dApp (Decentralized Application): An application that is built on a blockchain network and operates using smart contracts or other mechanisms or protocols for their functionality.

3.1. SType: CURRENCY

A new type, STI_CURRENCY, is introduced to support BaseAsset and QuoteAsset fields' type CURRENCY.

3.1.1. SType Value

TODO: content pass.

3.1.2. JSON Representation

This type can represent a standard currency code, XRP, or an arbitrary asset as a 160-bit (40 character) hexadecimal string. This type is generally conformant to the XRPL Currency Codes.

3.1.3. Binary Encoding

TODO: content pass.

3.1.4. Example JSON and Binary Encoding

Below is a JSON example with the BaseAsset representing a CUSIP code 912810RR9 as a 160-bit hexadecimal string and a QuoteAsset representing a standard USD currency code:

{
  "PriceData" : {
    # "912810RR9"
    "BaseAsset" : "3931323831305252390000000000000000000000",
    "QuoteAsset" : "USD",
    "Scale" : 1,
    "SymbolPrice" : 740
  }
}

Binary Encoding Example: TODO: content pass.

3.2. Ledger Entry: PriceOracle

The PriceOracle ledger entry represents the PriceOracle object on XRP Ledger.

3.2.1. Object Identifier

Key Space: 0x52

ID Calculation Algorithm: We compute the PriceOracle object ID as the SHA-512Half of the following values, concatenated in order:

  • The Oracle space key (0x52)
  • The Owner Account ID, Owner.
  • The Oracle Document ID, OracleDocumentID. This field describes a unique Price Oracle instance for the given account. The Oracle Document ID is maintained by the Oracle Provider.

The Owner and OracleDocumentID uniquely identify the PriceOracle object and must be passed to the Oracle transactions.

3.2.2. Fields

Field Name Constant Required Internal Type Default Value Description
LedgerEntryType Yes Yes UINT16 0x0080 Identifies the type of ledger object. The proposal recommends the value 0x0080 as the reserved entry type.
Owner TODO Yes ACCOUNT N/A The account that owns this object and has the update and delete privileges. It is recommended that this account has an associated signer list.
Provider Yes Yes BLOB N/A Identifies an Oracle Provider. It can be URI or any data, for instance chainlink. It is a string of up to 256 ASCII hex encoded characters (0x20-0x7E).
PriceDataSeries No Yes ARRAY N/A An array of up to ten PriceData objects. See PriceDataSeries.
LastUpdateTime No Yes UINT32 N/A The specific point in time when the data was last updated. The LastUpdateTime is represented as Unix Time - the number of seconds since January 1, 1970 (00:00 UTC).
URI No No BLOB N/A An optional URI field to reference price data off-chain. It is limited to 256 bytes.
AssetClass Yes Yes BLOB N/A Describes a type of the assets, for instance "currency", "commodity", "index". It is a string of up to sixteen ASCII hex encoded characters (0x20-0x7E).
PreviousTxnID No Yes HASH256 N/A The hash of the previous transaction to modify this entry (same as on other objects with this field).
PreviousTxnLgrSeq No Yes UINT32 N/A The ledger index of the ledger when this object was most recently updated/created (same as other objects with this field).

Field Details:

3.2.2.1. PriceDataSeries

PriceDataSeries is an array of up to ten PriceData objects, where PriceData represents the price information for a token pair. PriceData includes the following fields:

Field Name Required JSON Type Internal Type Description
BaseAsset Yes string CURRENCY The primary asset within a trading pair.
QuoteAsset Yes string CURRENCY The secondary or quote asset in a trading pair.
AssetPrice No number UINT64 The scaled asset price, which is the price value after applying the scaling factor.
Scale No number UINT8 The price's scaling factor.
  • BaseAsset refers to the primary asset within a trading pair. It is the asset against which the price of the quote asset is quoted. The base asset is usually considered the 'primary' asset and forms the basis for trading. Any valid identifier, such as a stock symbol, bond CUSIP, or currency code, should be allowed and interpreted exactly like other asset identifiers in the ledger. For example, in the pair BTC/USD, BTC is the base asset; in 912810RR9/BTC, 912810RR9 is the base asset. A new type, STI_CURRENCY, is introduced to support the CURRENCY field (see SType: CURRENCY).
  • QuoteAsset represents the secondary or quote asset in a trading pair. It denotes the price of one unit of the base asset. The quote asset's value is expressed in terms of the base asset. Any valid identifier such as a currency or a crypto-currency code, should be allowed and interpreted exactly like other asset identifiers in the ledger. For example, in the pair BTC/USD, USD is the quote asset; in 912810RR9/BTC, BTC is the quote asset. A new enum value STI_CURRENCY is introduced to support the CURRENCY field (see SType: CURRENCY). The BaseAsset and QuoteAsset together form a trading pair, and their relationship determines the price at which one asset can be exchanged for another.
  • AssetPrice is the scaled asset price, which is the price value after applying the scaling factor. This is an optional field. It is not included if the last update transaction didn't include the BaseAsset/QuoteAsset pair.
  • Scale is the price's scaling factor. It represents the price's precision level. For instance, if Scale is 6 and the original price is 0.155 then the scaled price is 155000. Formally, \(scaledPrice = originalPrice*{10}^{scale}\). Valid Scale range is {0-20}. This is an optional field. It is not included if the last update transaction didn't include the BaseAsset/QuoteAsset pair.

3.2.3. Ownership

Owner: Owner. Owner is the account that owns this object and has the update and delete privileges.

Directory Registration: TODO: content pass.

3.2.4. Reserves

Reserve Requirement: Custom

Any PriceOracle with more than five PriceData objects requires two owner reserves. If the Oracle instance has less or equal to five token pairs then the XRP reserve requirements is one, otherwise the XRP reserve requirements is two.

3.2.5. Deletion

Deletion Transactions: OracleDelete

Deletion Conditions:

  1. Only the Owner has the delete privileges.

Account Deletion Blocker: TODO: content pass.

3.2.6. Invariants

TODO: content pass.

3.2.7. RPC Name

TODO: content pass.

3.2.8. Example JSON

{
    "LedgerEntryType": "PriceOracle",
    "Owner": "rsA2LpzuawewSBQXkiju3YQTMzW13pAAdW",
    # "provider"
    "Provider": "70726F7669646572",
    # "currency"
    "AssetClass": "63757272656E6379",
    "PriceDataSeries": [
      {
        "PriceData": {
          "BaseAsset": "XRP",
          "QuoteAsset": "USD",
          "AssetPrice": 74,
          "Scale": 2,
        }
      },
    ],
    "LastUpdateTime": 743609414,
    "PreviousTxnID": "C53ECF838647FA5A4C780377025FEC7999AB4182590510CA461444B207AB74A9",
    "PreviousTxnLgrSeq": 56865244
}

3.3. Transaction: OracleSet

We define a new transaction OracleSet for creating or updating a PriceOracle instance. See Appendix A for the steps the Oracle Provider takes before it submits the transaction to create a new PriceOracle instance.

3.3.1. Fields

Field Name Required JSON Type Internal Type Default Value Description
TransactionType Yes string UINT16 OracleSet Indicates a new transaction type OracleSet.
Account Yes string ACCOUNT N/A The XRPL account that has update and delete privileges on the Oracle being set. This field corresponds to the Owner field on the PriceOracle ledger object.
OracleDocumentID Yes string UINT32 N/A A unique identifier of the Price Oracle for the given Account.
Provider Conditional string BLOB N/A Identifies an Oracle Provider. Provider must be included when creating a new instance of PriceOracle. It can be optionally included on update, in which case it has to match the current Provider value.
URI No string BLOB N/A An optional field to reference the price data off-chain.
AssetClass Conditional string BLOB N/A Describes the asset's type. AssetClass must be included when creating a new instance of PriceOracle. It can be optionally included on update, in which case it has to match the current AssetClass value.
LastUpdateTime Yes number UINT32 N/A The specific point in time when the data was last updated. LastUpdateTime is represented in Unix Time.
PriceDataSeries Yes array ARRAY N/A An array of up to ten PriceData objects. See PriceDataSeries.

Field Details:

3.3.1.1. PriceDataSeries

PriceDataSeries is an array of up to ten PriceData objects, where PriceData represents the price information for a token pair. PriceData includes the following fields:

  • BaseAsset is the asset to be priced.
  • QuoteAsset is the denomination in which the prices are expressed.
  • AssetPrice is the scaled asset price, which is the price value after applying the scaling factor.
  • Scale is the price's scaling factor, with a valid range of values {1-20}. The Scale field should be omitted when the Scale value is 0. An omitted Scale field implies a value of 0.

3.3.2. Transaction Fee

TODO: content pass.

3.3.3. Failure Conditions

3.3.3.1. Data Verification
  1. A required field is missing. (TODO: content pass — result code.)
  2. PriceDataSeries has duplicate token pairs. (TODO: content pass — result code.)
  3. The URI field length exceeds 256 bytes. (TODO: content pass — result code.)
  4. The Provider field length exceeds 256 bytes. (TODO: content pass — result code.)
  5. The AssetClass field length exceeds 16 bytes. (TODO: content pass — result code.)
  6. Transaction's PriceDataSeries array size is empty when creating a new Oracle instance. (TODO: content pass — result code.)
  7. Transaction's PriceDataSeries array size exceeds ten when creating a new Oracle instance. (TODO: content pass — result code.)
3.3.3.2. Protocol-Level Failures
  1. XRP reserve is insufficient (see Reserves). (TODO: content pass — result code.)
  2. Oracle's instance PriceDataSeries array size exceeds ten after updating the Oracle instance. (TODO: content pass — result code.)
  3. PriceDataSeries has array elements with missing AssetPrice and the token pair not matching an existing token pair. (TODO: content pass — result code.)
  4. The Account account doesn't exist. (TODO: content pass — result code.)
  5. The Account is not equal to the Owner field when updating the Oracle instance. (TODO: content pass — result code.)
  6. The transaction is not signed by the Account account or the account's multi signers. (TODO: content pass — result code.)
  7. Provider is not included when creating a new instance of PriceOracle. (TODO: content pass — result code.)
  8. AssetClass is not included when creating a new instance of PriceOracle. (TODO: content pass — result code.)
  9. The Provider field doesn't match the current Provider field on update. (TODO: content pass — result code.)
  10. The AssetClass field doesn't match the current AssetClass field on update. (TODO: content pass — result code.)
  11. The LastUpdateTime field is less than the previous LastUpdateTime. (TODO: content pass — result code.)
  12. The LastUpdateTime field is not in the range of the last close time minus/plus 300 seconds. (TODO: content pass — result code.)

3.3.4. State Changes

An OracleSet transaction uniquely identifies a PriceOracle object with its Account and OracleDocumentID fields.

  1. If such an object does not yet exist in the ledger, it is created. Otherwise, the existing object is updated.
  2. The Provider, URI, and AssetClass fields are copied directly from the transaction, if present. Provider and AssetClass must be included in the transaction if the object is being created.
  3. The PriceDataSeries of the transaction is copied to a newly created PriceOracle object, or updates an existing object, like so:

    1. PriceData objects for (BaseAsset, QuoteAsset) token pairs that appear in the transaction but not the object are copied to the object.
    2. PriceData objects for token pairs that appear in both the transaction and the object are overwritten in the object.
    3. PriceData objects for token pairs that appear in both the transaction and the object and have AssetPrice missing in the transaction are deleted from the object.
    4. PriceData objects for token pairs that appear only in the object have AssetPrice and Scale removed. These fields are omitted from the object to signify that the price is outdated.

    The order of token pairs in the transaction is not important because the token pair uniquely identifies the location of the PriceData object in the PriceDataSeries array of the PriceOracle object.

  4. PreviousTxnID, and PreviousTxnLgrSeq are set in the same manner as for an AccountSet transaction.

  5. The owner reserve of the account is updated according to the difference in the size of the PriceDataSeries before and after the transaction is applied: 0 for missing, 1 for 1 - 5 objects, 2 for 6 - 10 objects.

3.3.5. Example JSON

{
    "TransactionType": "OracleSet",
    "Account": "rsA2LpzuawewSBQXkiju3YQTMzW13pAAdW",
    "OracleDocumentID": 34,
    # "provider"
    "Provider": "70726F7669646572",
    "LastUpdateTime": 743609014,
    # "currency"
    "AssetClass": "63757272656E6379",
    "PriceDataSeries": [
      {
        "PriceData": {
          "BaseAsset": "XRP",
          "QuoteAsset": "USD",
          "AssetPrice": 740,
          "Scale": 3
        }
      }
    ]
}

3.4. Transaction: OracleDelete

We define a new transaction OracleDelete for deleting an Oracle instance.

3.4.1. Fields

Field Name Required JSON Type Internal Type Default Value Description
TransactionType Yes string UINT16 OracleDelete Indicates a new transaction type OracleDelete.
Account Yes string ACCOUNT N/A The account that has the Oracle update and delete privileges. This field corresponds to the Owner field on the PriceOracle ledger object.
OracleDocumentID Yes string UINT32 N/A A unique identifier of the Price Oracle for the given Account.

3.4.2. Transaction Fee

TODO: content pass.

3.4.3. Failure Conditions

3.4.3.1. Data Verification

TODO: content pass.

3.4.3.2. Protocol-Level Failures
  1. Object with the Oracle Object ID doesn't exist. (TODO: content pass — result code.)
  2. The Account account doesn't exist. (TODO: content pass — result code.)
  3. The Account is not equal to the Owner field. (TODO: content pass — result code.)
  4. The transaction is not signed by the Account account or the account's multi signers. (TODO: content pass — result code.)

3.4.4. State Changes

  1. OracleDelete transaction deletes the Oracle object from the ledger.
  2. On success the owner’s reserve requirement is reduced by one or two depending on the PriceDataSeries array size.

3.4.5. Example JSON

{
    "TransactionType": "OracleDelete",
    "Account": "rsA2LpzuawewSBQXkiju3YQTMzW13pAAdW",
    "OracleDocumentID": 34
}

3.5. RPC: ledger_entry

An Oracle object can be retrieved with the ledger_entry API call by specifying the account and oracle_document_id.

3.5.1. Request Fields

Field Name Required JSON Type Description
oracle TODO object Identifies the Oracle object by account and oracle_document_id.
oracle.account Yes string The Oracle's account.
oracle.oracle_document_id Yes number A unique identifier of the Price Oracle for the given Account.

3.5.2. Response Fields

TODO: content pass.

3.5.3. Failure Conditions

TODO: content pass.

3.5.4. Example Request

{
     "method": "ledger_entry",
     "params" : [
        {
          "oracle" : {
            "account": "rsA2LpzuawewSBQXkiju3YQTMzW13pAAdW",
            "oracle_document_id":  34,
          },
          "ledger_index": "validated"
        }
     ]
}

3.5.5. Example Response

{
   "index" : "CF2C20122022DE908C4F521A96DC2C1E5EFFD1EFD47AA244E9EE9A442451162E",
   "ledger_current_index" : 23,
   "node" : {
      "Flags" : 0,
      "LastUpdateTime" : 743609014,
      "LedgerEntryType" : "Oracle",
      "Owner" : "rp847ow9WcPmnNpVHMQV5A4BF6vaL9Abm6",
      # "currency"
      "AssetClass" : "63757272656E6379",
      # "provider"
      "Provider": "70726F7669646572",
      "PreviousTxnID" : "6F120537D0D212FEA6E11A0DCC5410AFCA95BD98D451D046832E6C4C4398164D",
      "PreviousTxnLgrSeq" : 22,
      "PriceDataSeries": [
        {
          "PriceData": {
            "QuoteAsset" : {
               "currency" : "USD"
            },
            "BaseAsset" : {
               "currency" : "XRP"
            },
            "Scale" : 1,
            "AssetPrice" : "740",
          }
        }
      ],
      "index" : "CF2C20122022DE908C4F521A96DC2C1E5EFFD1EFD47AA244E9EE9A442451162E"
   },
   "status" : "success",
   "validated" : true
}

3.6. RPC: get_aggregate_price

get_aggregate_price RPC calculates the aggregate price of the specified PriceOracle objects, and returns three types of price statistics - mean, median, and trimmed mean if trim parameter is included in the request. The PriceOracle objects are identified by the Owner Account (account) and Oracle Document ID (oracle_document_id) fields.

3.6.1. Request Fields

Field Name Required JSON Type Description
ledger_index No string or number (positive integer) The ledger index of the max ledger to use, or a shortcut string to choose a ledger automatically.
ledger_hash No string A 20-byte hex string for the max ledger version to use.
base_asset Yes string The asset to be priced.
quote_asset Yes string The denomination in which the prices are expressed.
oracles Yes array An array of oracle objects to aggregate over. See oracles.
trim No number The percentage of outliers to trim. Valid trim range is 1-25. If this parameter is included then the API returns statistics for the trimmed data.
time_threshold No number Used to define a time range in seconds for filtering out older price data. It's an optional parameter and is 0 by default; i.e. there is no filtering in this case.

Field Details:

3.6.1.1. oracles

oracles is an array of oracle objects to aggregate over. oracle object has two fields:

Field Name Required JSON Type Description
account Yes string The Oracle's account.
oracle_document_id Yes number A unique identifier of the Price Oracle for the given Account.
3.6.1.2. Data Selection

The price data to aggregate is selected based on specific criteria. The most recent Price Oracle object is obtained for the specified oracles. The most recent LastUpdateTime among all objects is chosen as the upper time threshold. A Price Oracle object is included in the aggregation dataset if it satisfies the conditions of containing the specified base_asset/quote_asset pair, including the AssetPrice field, and its LastUpdateTime is within the time range of (upper threshold - time threshold) to the upper threshold. If a Price Oracle object doesn't contain the AssetPrice for the specified token pair, then up to three previous Price Oracle objects are examined and include the most recent one that fulfills the criteria.

3.6.2. Response Fields

On success, the response data contains the following fields:

Field Name Always Present? JSON Type Description
entire_set Yes object An object of the following fields: size, mean, standard_deviation.
entire_set.size Yes number The size of the data set used to calculate the statistics.
entire_set.mean Yes string The simple mean.
entire_set.standard_deviation Yes string The standard deviation.
trimmed_set Conditional object An object, which is included in the response if trim fields is set. The object has the following fields: size, mean, standard_deviation.
trimmed_set.size Conditional number The size of the data set used to calculate the statistics.
trimmed_set.mean Conditional string The simple mean.
trimmed_set.standard_deviation Conditional string The standard deviation.
median Yes string The median.
time Yes number The most recent timestamp out of all LastUpdateTime values.

3.6.3. Failure Conditions

The get_aggregate_price fails if:

  1. The oracles array size is 0. (TODO: content pass — error code.)
  2. The oracles array size is greater than 200. (TODO: content pass — error code.)
  3. The oracles array's object doesn't include account. (TODO: content pass — error code.)
  4. The oracles array's object doesn't include oracle_document_id. (TODO: content pass — error code.)
  5. The oracles array's object account or oracle_document_id field has an invalid value. (TODO: content pass — error code.)
  6. base_asset is missing. (TODO: content pass — error code.)
  7. quote_asset is missing. (TODO: content pass — error code.)
  8. trim contains an invalid uint value. (TODO: content pass — error code.)
  9. time_threshold contains an invalid uint value. (TODO: content pass — error code.)
  10. The resulting data set is empty. (TODO: content pass — error code.)

3.6.4. Example Request

{
"method": "get_aggregate_price",
"params": [
    {
        "ledger_index": "current",
        "base_asset": "XRP",
        "quote_asset": "USD",
        "trim": 20,
        "oracles": [
          {
            "account": "rp047ow9WcPmnNpVHMQV5A4BF6vaL9Abm6",
            "oracle_document_id": 34
          },
          {
            "account": "rp147ow9WcPmnNpVHMQV5A4BF6vaL9Abm7",
            "oracle_document_id": 56
          },
          {
            "account": "rp247ow9WcPmnNpVHMQV5A4BF6vaL9Abm8",
            "oracle_document_id": 2
          },
          {
            "account": "rp347ow9WcPmnNpVHMQV5A4BF6vaL9Abm9",
            "oracle_document_id": 7
          },
          {
            "account": "rp447ow9WcPmnNpVHMQV5A4BF6vaL9Abm0",
            "oracle_document_id": 109
          }
        ]
    }
]
}

3.6.5. Example Response

{
  "entire_set" : {
     "mean" : "74.75",
     "size" : 10,
     "standard_deviation" : "0.1290994448735806"
  },
  "ledger_current_index" : 25,
  "median" : "74.75",
  "status" : "success",
  "trimmed_set" : {
    "mean" : "74.75",
    "size" : 6,
    "standard_deviation" : "0.1290994448735806"
  },
  "validated" : false,
  "time" : 78937648
}

4. Rationale

TODO: content pass.

5. Security Considerations

TODO: content pass.

Appendix

Appendix A: Oracle Provider Setup

Before the transaction can be submitted to create a new PriceOracle instance, the Oracle Provider has to do the following:

  • Create or own the Owner on the XRPL with sufficient XRP balance to meet the XRP reserve and the transaction fee requirements.
  • The Oracle Provider has to publish the Owner account public key so that it can be used for verification by dApp’s.
  • The Oracle Provider has to publish a registry of available Price Oracles with their unique OracleDocumentID. The hash of the Owner and the OracleDocumentID uniquely identifies the Price Oracle on-ledger object.