-
Notifications
You must be signed in to change notification settings - Fork 8
ind liq metrics and mock scripts for dashboard #597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jonthia-drift
merged 3 commits into
master
from
jonthia/be-247-indicative-liquidity-dashboard
Mar 30, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| ENDPOINT=https://api.devnet.solana.com | ||
| WS_ENDPOINT=wss://api.devnet.solana.com | ||
| ENV=devnet | ||
|
|
||
| RUNNING_LOCAL=true | ||
| LOCAL_CACHE=true | ||
| ELASTICACHE_HOST=localhost | ||
| ELASTICACHE_PORT=6379 | ||
| REDIS_CLIENT=DLOB | ||
|
|
||
| METRICS_PORT=9465 | ||
| INDICATIVE_QUOTES_MAX_AGE_MS=1000 | ||
| INDICATIVE_QUOTES_CACHE_TTL_MS=250 | ||
|
|
||
| ENABLE_MOCK_FILL_ENDPOINT=true | ||
| MOCK_ONLY_MODE=true | ||
| MOCK_FILL_PORT=9470 | ||
|
|
||
| MOCK_MARKET_TYPE=perp | ||
| MOCK_MARKET_INDEX=0 | ||
| MOCK_QUOTES_JSON=[{"maker":"good-maker","side":"long","price":100,"size":2},{"maker":"bad-maker","side":"long","price":99,"size":1}] | ||
| MOCK_FILLS_JSON=[{"maker":"good-maker","side":"long","fillPrice":100,"fillSize":1,"oraclePrice":100}] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -139,11 +139,13 @@ src/**.js.map | |
| .idea | ||
|
|
||
| .env | ||
| .env.*.local | ||
| lib | ||
| src/playground.ts | ||
|
|
||
| # redis files | ||
| *.rdb | ||
| *.log | ||
| *.confg | ||
|
|
||
| *.aof* | ||
| *.conf | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,328 @@ | ||
| import { describe, expect, it } from '@jest/globals'; | ||
| import { BASE_PRECISION, PRICE_PRECISION } from '@drift-labs/sdk'; | ||
| import { | ||
| getAbsoluteBpsDiff, | ||
| getCompetitiveLiquidity, | ||
| getIndicativeDirectionBucket, | ||
| getFillPrice, | ||
| getFillSide, | ||
| getFillTimestampMs, | ||
| getIndicativeBpsBucket, | ||
| getQuoteTimestampMs, | ||
| getQuoteValueOnBook, | ||
| getSignedBpsDiff, | ||
| isCompetitivePrice, | ||
| rawPriceToNumber, | ||
| } from '../tradeMetrics'; | ||
|
|
||
| describe('tradeMetrics', () => { | ||
| describe('getFillPrice', () => { | ||
| it('computes the executed unit price', () => { | ||
| expect( | ||
| getFillPrice({ | ||
| baseAssetAmountFilled: 2, | ||
| quoteAssetAmountFilled: 210, | ||
| }) | ||
| ).toBe(105); | ||
| }); | ||
|
|
||
| it('returns undefined for zero base size', () => { | ||
| expect( | ||
| getFillPrice({ | ||
| baseAssetAmountFilled: 0, | ||
| quoteAssetAmountFilled: 210, | ||
| }) | ||
| ).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getFillTimestampMs', () => { | ||
| it('normalizes seconds to milliseconds', () => { | ||
| expect(getFillTimestampMs(1710000000)).toBe(1710000000000); | ||
| }); | ||
|
|
||
| it('leaves millisecond timestamps unchanged', () => { | ||
| expect(getFillTimestampMs(1710000000000)).toBe(1710000000000); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getQuoteTimestampMs', () => { | ||
| it('extracts the quote timestamp when present', () => { | ||
| expect(getQuoteTimestampMs({ ts: 1710000000000 })).toBe(1710000000000); | ||
| }); | ||
|
|
||
| it('returns undefined for missing or invalid timestamps', () => { | ||
| expect(getQuoteTimestampMs(null)).toBeUndefined(); | ||
| expect(getQuoteTimestampMs({ ts: 'bad-ts' })).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getFillSide', () => { | ||
| it('infers the maker side from taker direction first', () => { | ||
| expect(getFillSide({ takerOrderDirection: 'long' })).toBe('short'); | ||
| expect(getFillSide({ takerOrderDirection: 'short' })).toBe('long'); | ||
| }); | ||
|
|
||
| it('falls back to maker direction', () => { | ||
| expect(getFillSide({ makerOrderDirection: 'long' })).toBe('long'); | ||
| expect(getFillSide({ makerOrderDirection: 'short' })).toBe('short'); | ||
| }); | ||
|
|
||
| it('returns undefined when neither side is available', () => { | ||
| expect(getFillSide({})).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('rawPriceToNumber', () => { | ||
| it('converts oracle-offset quotes to absolute prices', () => { | ||
| expect(rawPriceToNumber(2 * PRICE_PRECISION.toNumber(), 100)).toBe(102); | ||
| }); | ||
| }); | ||
|
|
||
| describe('isCompetitivePrice', () => { | ||
| it('treats bid prices at or above the fill price as competitive', () => { | ||
| expect(isCompetitivePrice('long', 101, 100)).toBe(true); | ||
| expect(isCompetitivePrice('long', 99, 100)).toBe(false); | ||
| }); | ||
|
|
||
| it('treats ask prices at or below the fill price as competitive', () => { | ||
| expect(isCompetitivePrice('short', 99, 100)).toBe(true); | ||
| expect(isCompetitivePrice('short', 101, 100)).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('bps bucketing', () => { | ||
| it('computes absolute bps distance', () => { | ||
| expect(getAbsoluteBpsDiff(100.1, 100)).toBeCloseTo(10, 8); | ||
| expect(getAbsoluteBpsDiff(99.9, 100)).toBeCloseTo(10, 8); | ||
| }); | ||
|
|
||
| it('computes signed bps distance', () => { | ||
| expect(getSignedBpsDiff(100.1, 100)).toBeCloseTo(10, 8); | ||
| expect(getSignedBpsDiff(99.9, 100)).toBeCloseTo(-10, 8); | ||
| }); | ||
|
|
||
| it('maps bps distances into the configured buckets', () => { | ||
| expect(getIndicativeBpsBucket(0)).toBe('very_tight'); | ||
| expect(getIndicativeBpsBucket(9.99)).toBe('very_tight'); | ||
| expect(getIndicativeBpsBucket(10)).toBe('tight'); | ||
| expect(getIndicativeBpsBucket(19.99)).toBe('tight'); | ||
| expect(getIndicativeBpsBucket(20)).toBe('moderate'); | ||
| expect(getIndicativeBpsBucket(29.99)).toBe('moderate'); | ||
| expect(getIndicativeBpsBucket(30)).toBe('wide'); | ||
| expect(getIndicativeBpsBucket(49.99)).toBe('wide'); | ||
| expect(getIndicativeBpsBucket(50)).toBe('very_wide'); | ||
| expect(getIndicativeBpsBucket(500)).toBe('very_wide'); | ||
| }); | ||
|
|
||
| it('maps signed bps distances into directional buckets', () => { | ||
| expect(getIndicativeDirectionBucket(10)).toBe('better'); | ||
| expect(getIndicativeDirectionBucket(0)).toBe('equal'); | ||
| expect(getIndicativeDirectionBucket(-10)).toBe('worse'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getCompetitiveLiquidity', () => { | ||
| it('aggregates only bid levels that were competitive for a long maker', () => { | ||
| const liquidity = getCompetitiveLiquidity( | ||
| 'mm-1', | ||
| { | ||
| marketIndex: 0, | ||
| marketType: 'perp', | ||
| oraclePrice: 100, | ||
| }, | ||
| 'long', | ||
| 100, | ||
| { | ||
| ts: 1710000000000, | ||
| quotes: [ | ||
| { | ||
| bid_price: 101 * PRICE_PRECISION.toNumber(), | ||
| bid_size: BASE_PRECISION.toNumber(), | ||
| }, | ||
| { | ||
| bid_price: 100 * PRICE_PRECISION.toNumber(), | ||
| bid_size: 2 * BASE_PRECISION.toNumber(), | ||
| }, | ||
| { | ||
| bid_price: 99 * PRICE_PRECISION.toNumber(), | ||
| bid_size: 4 * BASE_PRECISION.toNumber(), | ||
| }, | ||
| ], | ||
| } | ||
| ); | ||
|
|
||
| expect(liquidity).toEqual({ | ||
| maker: 'mm-1', | ||
| bestPrice: 101, | ||
| size: 3, | ||
| quoteValue: 301, | ||
| quoteTsMs: 1710000000000, | ||
| }); | ||
| }); | ||
|
|
||
| it('aggregates only ask levels that were competitive for a short maker', () => { | ||
| const liquidity = getCompetitiveLiquidity( | ||
| 'mm-2', | ||
| { | ||
| marketIndex: 0, | ||
| marketType: 'perp', | ||
| oraclePrice: 100, | ||
| }, | ||
| 'short', | ||
| 100, | ||
| { | ||
| ts: 1710000000000, | ||
| quotes: [ | ||
| { | ||
| ask_price: 99 * PRICE_PRECISION.toNumber(), | ||
| ask_size: 1.5 * BASE_PRECISION.toNumber(), | ||
| }, | ||
| { | ||
| ask_price: 100 * PRICE_PRECISION.toNumber(), | ||
| ask_size: 0.5 * BASE_PRECISION.toNumber(), | ||
| }, | ||
| { | ||
| ask_price: 101 * PRICE_PRECISION.toNumber(), | ||
| ask_size: 10 * BASE_PRECISION.toNumber(), | ||
| }, | ||
| ], | ||
| } | ||
| ); | ||
|
|
||
| expect(liquidity).toEqual({ | ||
| maker: 'mm-2', | ||
| bestPrice: 99, | ||
| size: 2, | ||
| quoteValue: 198.5, | ||
| quoteTsMs: 1710000000000, | ||
| }); | ||
| }); | ||
|
|
||
| it('supports oracle-offset quotes', () => { | ||
| const liquidity = getCompetitiveLiquidity( | ||
| 'mm-3', | ||
| { | ||
| marketIndex: 0, | ||
| marketType: 'perp', | ||
| oraclePrice: 100, | ||
| }, | ||
| 'long', | ||
| 100, | ||
| { | ||
| ts: 1710000000000, | ||
| quotes: [ | ||
| { | ||
| bid_price: PRICE_PRECISION.toNumber(), | ||
| bid_size: BASE_PRECISION.toNumber(), | ||
| is_oracle_offset: true, | ||
| }, | ||
| ], | ||
| } | ||
| ); | ||
|
|
||
| expect(liquidity?.bestPrice).toBe(101); | ||
| expect(liquidity?.size).toBe(1); | ||
| }); | ||
|
|
||
| it('uses spot market precision when provided', () => { | ||
| const liquidity = getCompetitiveLiquidity( | ||
| 'mm-4', | ||
| { | ||
| marketIndex: 1, | ||
| marketType: 'spot', | ||
| oraclePrice: 10, | ||
| }, | ||
| 'long', | ||
| 10, | ||
| { | ||
| ts: 1710000000000, | ||
| quotes: [ | ||
| { | ||
| bid_price: 10 * PRICE_PRECISION.toNumber(), | ||
| bid_size: 2_000_000, | ||
| }, | ||
| ], | ||
| }, | ||
| 1_000_000 | ||
| ); | ||
|
|
||
| expect(liquidity?.size).toBe(2); | ||
| }); | ||
|
|
||
| it('returns undefined when no levels were competitive', () => { | ||
| expect( | ||
| getCompetitiveLiquidity( | ||
| 'mm-5', | ||
| { | ||
| marketIndex: 0, | ||
| marketType: 'perp', | ||
| oraclePrice: 100, | ||
| }, | ||
| 'long', | ||
| 100, | ||
| { | ||
| ts: 1710000000000, | ||
| quotes: [ | ||
| { | ||
| bid_price: 99 * PRICE_PRECISION.toNumber(), | ||
| bid_size: BASE_PRECISION.toNumber(), | ||
| }, | ||
| ], | ||
| } | ||
| ) | ||
| ).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getQuoteValueOnBook', () => { | ||
| it('sums quote notional on the relevant side', () => { | ||
| expect( | ||
| getQuoteValueOnBook( | ||
| { | ||
| marketIndex: 0, | ||
| marketType: 'perp', | ||
| oraclePrice: 100, | ||
| }, | ||
| 'long', | ||
| { | ||
| ts: 1710000000000, | ||
| quotes: [ | ||
| { | ||
| bid_price: 101 * PRICE_PRECISION.toNumber(), | ||
| bid_size: BASE_PRECISION.toNumber(), | ||
| }, | ||
| { | ||
| bid_price: 100 * PRICE_PRECISION.toNumber(), | ||
| bid_size: 2 * BASE_PRECISION.toNumber(), | ||
| }, | ||
| ], | ||
| } | ||
| ) | ||
| ).toBe(301); | ||
| }); | ||
|
|
||
| it('supports oracle offset prices', () => { | ||
| expect( | ||
| getQuoteValueOnBook( | ||
| { | ||
| marketIndex: 0, | ||
| marketType: 'perp', | ||
| oraclePrice: 100, | ||
| }, | ||
| 'long', | ||
| { | ||
| ts: 1710000000000, | ||
| quotes: [ | ||
| { | ||
| bid_price: PRICE_PRECISION.toNumber(), | ||
| bid_size: BASE_PRECISION.toNumber(), | ||
| is_oracle_offset: true, | ||
| }, | ||
| ], | ||
| } | ||
| ) | ||
| ).toBe(101); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this one is just for the mock=true and mock value mode.