Skip to content

Commit 9388a5a

Browse files
authored
ds dex state (#2859)
1 parent d1b3da3 commit 9388a5a

File tree

11 files changed

+220
-31
lines changed

11 files changed

+220
-31
lines changed

public/samples/DataStreams/StreamsUpkeep.sol

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ contract StreamsUpkeep is ILogAutomation, StreamsLookupCompatibleInterface {
6161
error InvalidReportVersion(uint16 version); // Thrown when an unsupported report version is provided to verifyReport.
6262

6363
/**
64-
* @dev Represents a data report from a Data Streams stream for v3 schema (crypto streams).
64+
* @dev Represents a data report from a Data Streams stream for v3 schema (used for crypto and DEX State Price streams).
6565
* The `price`, `bid`, and `ask` values are carried to either 8 or 18 decimal places, depending on the stream.
66+
* `bid`, and `ask` values are not available for DEX State Price streams.
6667
* For more information, see https://docs.chain.link/data-streams/crypto-streams and https://docs.chain.link/data-streams/reference/report-schema
6768
*/
6869
struct ReportV3 {
@@ -73,8 +74,8 @@ contract StreamsUpkeep is ILogAutomation, StreamsLookupCompatibleInterface {
7374
uint192 linkFee; // Base cost to validate a transaction using the report, denominated in LINK.
7475
uint32 expiresAt; // Latest timestamp where the report can be verified onchain.
7576
int192 price; // DON consensus median price (8 or 18 decimals).
76-
int192 bid; // Simulated price impact of a buy order up to the X% depth of liquidity utilisation (8 or 18 decimals).
77-
int192 ask; // Simulated price impact of a sell order up to the X% depth of liquidity utilisation (8 or 18 decimals).
77+
int192 bid; // Simulated price impact of a buy order up to the X% depth of liquidity utilisation (8 or 18 decimals). Note: not available for DEX State Price streams.
78+
int192 ask; // Simulated price impact of a sell order up to the X% depth of liquidity utilisation (8 or 18 decimals). Note: not available for DEX State Price streams.
7879
}
7980

8081
/**

public/samples/DataStreams/StreamsUpkeepRegistrar.sol

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ contract StreamsUpkeepRegistrar is
8888
AutomationRegistrarInterface public immutable i_registrar;
8989

9090
/**
91-
* @dev Represents a data report from a Data Streams feed for v3 schema (crypto feeds).
92-
* The `price`, `bid`, and `ask` values are carried to either 8 or 18 decimal places, depending on the feed.
91+
* @dev Represents a data report from a stream for v3 schema (crypto and DEX State Price streams).
92+
* The `price`, `bid`, and `ask` values are carried to either 8 or 18 decimal places, depending on the stream.
93+
* `bid`, and `ask` values are not available for DEX State Price streams.
9394
* For more information, see https://docs.chain.link/data-streams/crypto-streams and https://docs.chain.link/data-streams/reference/report-schema
9495
*/
9596
struct ReportV3 {
@@ -100,8 +101,8 @@ contract StreamsUpkeepRegistrar is
100101
uint192 linkFee; // Base cost to validate a transaction using the report, denominated in LINK.
101102
uint32 expiresAt; // Latest timestamp where the report can be verified onchain.
102103
int192 price; // DON consensus median price (8 or 18 decimals).
103-
int192 bid; // Simulated price impact of a buy order up to the X% depth of liquidity utilisation (8 or 18 decimals).
104-
int192 ask; // Simulated price impact of a sell order up to the X% depth of liquidity utilisation (8 or 18 decimals).
104+
int192 bid; // Simulated price impact of a buy order up to the X% depth of liquidity utilisation (8 or 18 decimals). Note: not available for DEX State Price streams.
105+
int192 ask; // Simulated price impact of a sell order up to the X% depth of liquidity utilisation (8 or 18 decimals). Note: not available for DEX State Price streams.
105106
}
106107

107108
/**

src/config/sidebar.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,13 @@ export const SIDEBAR: Partial<Record<Sections, SectionEntry[]>> = {
348348
url: "data-streams/crypto-streams",
349349
children: [
350350
{
351-
title: "Report Schema v3",
351+
title: "Report Schema v3 (Crypto)",
352352
url: "data-streams/reference/report-schema",
353353
},
354+
{
355+
title: "Report Schema v3 (DEX State Price)",
356+
url: "data-streams/reference/report-schema-v3-dex",
357+
},
354358
],
355359
},
356360
{
@@ -416,6 +420,10 @@ export const SIDEBAR: Partial<Record<Sections, SectionEntry[]>> = {
416420
title: "Liquidity-Weighted Bid and Ask prices",
417421
url: "data-streams/concepts/liquidity-weighted-prices",
418422
},
423+
{
424+
title: "DEX State Price Streams",
425+
url: "data-streams/concepts/dex-state-price-streams",
426+
},
419427
],
420428
},
421429
{
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
section: dataStreams
3+
title: "DEX State Price Streams"
4+
whatsnext:
5+
{
6+
"Find the list of available Stream IDs.": "/data-streams/crypto-streams",
7+
"Find the schema of data to expect from Data Streams reports.": "/data-streams/reference/report-schema-v3-dex",
8+
}
9+
---
10+
11+
import { Aside } from "@components"
12+
13+
DEX State Price Streams are streams tailored for assets that derive most—if not all—of their liquidity from **decentralized exchanges (DEXs)**. While standard crypto price streams often incorporate centralized exchange (CEX) data and focus on assets with high trading volumes, these DEX-centric streams incorporate onchain market data to more accurately reflect the conditions under which certain "long-tail" or newly launched tokens trade.
14+
15+
<Aside type="caution" title="Disclaimer">
16+
17+
DEX state can introduce risks inherent to DeFi. Consumers of State Pricing must perform their own risk assessments, considering factors like market depth, underlying asset security, and bridge dependencies, and then adjust protocol parameters accordingly. You are responsible for auditing your code and applications prior to using DEX State Price Streams in a production environment and also ensuring that they meet the requirements for your use case. You are also responsible for implementing measures to mitigate risk factors inherent to DeFi. See [Risk Mitigation](#risk-mitigation) section below for more information. Neither Chainlink Labs, the Chainlink Foundation, nor Chainlink node operators are responsible for unintended outputs that are generated due to errors in code or failure to properly mitigate any dependencies or risk factors.
18+
19+
</Aside>
20+
21+
## Why a Separate Methodology?
22+
23+
- **Liquidity Distribution**: Many emerging or specialized tokens have limited CEX listings but deep onchain liquidity in Automated Market Maker (AMM) pools.
24+
25+
- **DEX-Specific Dynamics**: AMMs use liquidity pools rather than traditional order books, meaning trading volume and liquidity conditions are captured differently than in centralized order-book markets.
26+
27+
- **Volume vs. Liquidity Mismatch**: Some assets may have relatively low trading volume but significant liquidity depth in DEXs. Traditional trade-based pricing algorithms are often not well optimized in these cases due to sparse activity and cannot provide continuous streaming prices. DEX State Price Streams address this by leveraging the current state of onchain liquidity to determine the execution price a trader would receive. This enables more accurate, real-time pricing even for low-volume assets.
28+
29+
- **Mitigating Manipulation**: Observing onchain "end of block" states and applying filters to aggregated DEX data can significantly reduce the impact of block-level price manipulation.
30+
31+
## High-Level Outline of the DEX State Price Methodology
32+
33+
1. **Asset Selection**
34+
35+
- Focuses on DEX-dominant or DEX-exclusive assets, generally those where 80% or more of trading volume is on decentralized exchanges.
36+
- Uses preliminary screens to identify relevant liquidity pools (e.g., top 95% of trading volume).
37+
38+
1. **Onchain Pool Monitoring**
39+
40+
- Selects pools across multiple DEX protocols to capture an aggregate view of token price and liquidity.
41+
- Gathers end-of-block state information (reserves, ticks, or pool-specific data) rather than raw trade-by-trade events to protect against temporary price swings or intra-block MEV.
42+
43+
1. **Weighted Aggregation**
44+
45+
- Aggregates individual pool prices using a volume-based weighting scheme.
46+
- Each pool's long-term trading volume or liquidity depth influences its weight, helping ensure more dominant pools have a proportionally greater impact on the final price.
47+
48+
1. **Outlier & Time-Lag Filters**
49+
50+
- Applies outlier detection to remove anomalous values that deviate significantly from broader market activity.
51+
- Introduces a mild time-lag filter to smooth short-term volatility and mitigate sudden spikes or dips due to low-liquidity trades.
52+
53+
1. **Liquidity Metrics**
54+
55+
- Incorporates advanced liquidity metrics (e.g., market depth, liquidity distribution across pools) to refine the weighting and guard against abrupt liquidity shifts.
56+
- An emergency pause mechanism can temporarily suspend price reporting if a severe liquidity drop is detected.
57+
58+
1. **Final DEX State Price**
59+
60+
- The result is a price that reflects both onchain market activity and relevant liquidity conditions, providing a robust solution for DEX-based assets.
61+
62+
## How to Use DEX State Price Streams
63+
64+
You can use DEX State Price streams like other Chainlink Data streams. The key distinction lies in the report schema you need to configure, specifically the [Report Schema V3 (DEX)](/data-streams/reference/report-schema-v3-dex). In this schema, the `bid` and `ask` fields have the same value as the `price` field. This is due to DEX-based markets depending on continuous liquidity pools instead of order-book mechanics to establish the reference price. Aside from this difference, the integration process follows the same technical steps as any Chainlink Data stream.
65+
66+
## Risk Mitigation
67+
68+
Although DEX State Price Streams services are powered by Chainlink decentralized oracle networks—which have a long track record of being highly secure, reliable, and accurate—users must ensure that they understand each feed’s unique update parameters (and the liquidity/volume profiles of the corresponding assets) and implement relevant [risk mitigation techniques](/data-feeds/selecting-data-feeds#risk-mitigation) based on the intended use case.
69+
70+
Utilizing DEX state introduces specific risks inherent to DeFi:
71+
72+
- **Smart Contract Risk**: Smart contracts, operating on certain blockchains like Ethereum, are autonomous code segments designed to execute transactions in a decentralized manner without the need for intermediaries. Despite their simplicity and intended security, they remain susceptible to bugs and exploitations. A flaw within the smart contract code can be exploited by malicious entities to manipulate the state price or other liquidity metrics. Such occurrences may lead to oracle price abnormality and financial losses for lending protocols.
73+
74+
- **Layer 2 and Cross-chain Bridge Hack Risk**: A bridge in the context of DeFi consists of smart contracts that enable the transfer of assets between different blockchains or layers. Bridges hold significant reserves of tokens to facilitate these transfers. However, they can become targets for hacks. An unauthorized withdrawal from the bridge depletes the reserves necessary for users to redeem their assets. This can lead to the implied price of the tokens—essentially the price that is expected based on available reserves and pool ratios—no longer accurately reflecting the true state or market price of the tokens.
75+
76+
- **External Dependency Risk**: Some DEX pool state prices rely on external exchange rates, which can come from an oracle, a ratio of queryable balances, or another calculation method. Bad actors might attempt to manipulate these exchange rates to influence the outcome of the state price or profit from the price discrepancies. This can lead to unintended consequences, such as liquidations, loss of funds, or arbitrage opportunities for attackers.
77+
78+
**Consumers of State Pricing must perform their own risk assessments**. They should consider factors like market depth, underlying asset security, and bridge dependencies, and then adjust protocol parameters (e.g., LTV, liquidation thresholds, supply caps) accordingly. The provided market depth metrics (when available) will aid in this assessment.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
section: dataStreams
3+
date: Last Modified
4+
title: "Report Schemas"
5+
---
6+
7+
import DataStreams from "@features/data-streams/common/DataStreams.astro"
8+
import { PageTabs } from "@components"
9+
10+
<DataStreams section="dsNotes" />
11+
12+
<PageTabs
13+
pages={[
14+
{
15+
name: "Cryptocurrency Report Schema (v3)",
16+
url: "/data-streams/reference/report-schema",
17+
},
18+
{
19+
name: "DEX State Price Report Schema (v3)",
20+
url: "/data-streams/reference/report-schema-v3-dex",
21+
},
22+
{
23+
name: "Real World Asset (RWA) Report Schema (v4)",
24+
url: "/data-streams/reference/report-schema-v4",
25+
},
26+
]}
27+
headerTitle="Available Report Schemas"
28+
headerDescription="Choose the schema version you want to explore."
29+
/>
30+
31+
DEX State Price streams adhere to the report schema outlined below.
32+
33+
## Schema Fields
34+
35+
| Field | Type | Description |
36+
| ----------------------- | --------- | ---------------------------------------------------------------------------------------------------------- |
37+
| `feedID` | `bytes32` | Unique identifier for the data stream |
38+
| `validFromTimestamp` | `uint32` | Start timestamp of price validity period |
39+
| `observationsTimestamp` | `uint32` | End timestamp of price validity period |
40+
| `nativeFee` | `uint192` | Verification cost in native blockchain tokens |
41+
| `linkFee` | `uint192` | Verification cost in LINK tokens |
42+
| `expiresAt` | `uint32` | Timestamp when this report expires |
43+
| `price` | `int192` | DON consensus median [DEX state price](/data-streams/concepts/dex-state-price-streams) (18 decimal places) |
44+
| `bid` | `int192` | N/A, equals `price`. |
45+
| `ask` | `int192` | N/A, equals `price`. |
46+
47+
**Notes**:
48+
49+
- Future DEX State Price streams may use different report schemas.
50+
- The `bid` and `ask` fields exist but contain the same value as the `price` field.

src/content/data-streams/reference/report-schema-v4.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ import { PageTabs } from "@components"
1919
name: "Cryptocurrency Report Schema (v3)",
2020
url: "/data-streams/reference/report-schema",
2121
},
22+
{
23+
name: "DEX State Price Report Schema (v3)",
24+
url: "/data-streams/reference/report-schema-v3-dex",
25+
},
2226
{
2327
name: "Real World Asset (RWA) Report Schema (v4)",
2428
url: "/data-streams/reference/report-schema-v4",

src/content/data-streams/reference/report-schema.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ import { PageTabs } from "@components"
2020
name: "Cryptocurrency Report Schema (v3)",
2121
url: "/data-streams/reference/report-schema",
2222
},
23+
{
24+
name: "DEX State Price Report Schema (v3)",
25+
url: "/data-streams/reference/report-schema-v3-dex",
26+
},
2327
{
2428
name: "Real World Asset (RWA) Report Schema (v4)",
2529
url: "/data-streams/reference/report-schema-v4",

0 commit comments

Comments
 (0)