|
| 1 | +import { describe, expect, it } from '@jest/globals'; |
| 2 | +import { BASE_PRECISION, PRICE_PRECISION } from '@drift-labs/sdk'; |
| 3 | +import { |
| 4 | + getAbsoluteBpsDiff, |
| 5 | + getCompetitiveLiquidity, |
| 6 | + getIndicativeDirectionBucket, |
| 7 | + getFillPrice, |
| 8 | + getFillSide, |
| 9 | + getFillTimestampMs, |
| 10 | + getIndicativeBpsBucket, |
| 11 | + getQuoteTimestampMs, |
| 12 | + getQuoteValueOnBook, |
| 13 | + getSignedBpsDiff, |
| 14 | + isCompetitivePrice, |
| 15 | + rawPriceToNumber, |
| 16 | +} from '../tradeMetrics'; |
| 17 | + |
| 18 | +describe('tradeMetrics', () => { |
| 19 | + describe('getFillPrice', () => { |
| 20 | + it('computes the executed unit price', () => { |
| 21 | + expect( |
| 22 | + getFillPrice({ |
| 23 | + baseAssetAmountFilled: 2, |
| 24 | + quoteAssetAmountFilled: 210, |
| 25 | + }) |
| 26 | + ).toBe(105); |
| 27 | + }); |
| 28 | + |
| 29 | + it('returns undefined for zero base size', () => { |
| 30 | + expect( |
| 31 | + getFillPrice({ |
| 32 | + baseAssetAmountFilled: 0, |
| 33 | + quoteAssetAmountFilled: 210, |
| 34 | + }) |
| 35 | + ).toBeUndefined(); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + describe('getFillTimestampMs', () => { |
| 40 | + it('normalizes seconds to milliseconds', () => { |
| 41 | + expect(getFillTimestampMs(1710000000)).toBe(1710000000000); |
| 42 | + }); |
| 43 | + |
| 44 | + it('leaves millisecond timestamps unchanged', () => { |
| 45 | + expect(getFillTimestampMs(1710000000000)).toBe(1710000000000); |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + describe('getQuoteTimestampMs', () => { |
| 50 | + it('extracts the quote timestamp when present', () => { |
| 51 | + expect(getQuoteTimestampMs({ ts: 1710000000000 })).toBe(1710000000000); |
| 52 | + }); |
| 53 | + |
| 54 | + it('returns undefined for missing or invalid timestamps', () => { |
| 55 | + expect(getQuoteTimestampMs(null)).toBeUndefined(); |
| 56 | + expect(getQuoteTimestampMs({ ts: 'bad-ts' })).toBeUndefined(); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + describe('getFillSide', () => { |
| 61 | + it('infers the maker side from taker direction first', () => { |
| 62 | + expect(getFillSide({ takerOrderDirection: 'long' })).toBe('short'); |
| 63 | + expect(getFillSide({ takerOrderDirection: 'short' })).toBe('long'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('falls back to maker direction', () => { |
| 67 | + expect(getFillSide({ makerOrderDirection: 'long' })).toBe('long'); |
| 68 | + expect(getFillSide({ makerOrderDirection: 'short' })).toBe('short'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('returns undefined when neither side is available', () => { |
| 72 | + expect(getFillSide({})).toBeUndefined(); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + describe('rawPriceToNumber', () => { |
| 77 | + it('converts oracle-offset quotes to absolute prices', () => { |
| 78 | + expect(rawPriceToNumber(2 * PRICE_PRECISION.toNumber(), 100)).toBe(102); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + describe('isCompetitivePrice', () => { |
| 83 | + it('treats bid prices at or above the fill price as competitive', () => { |
| 84 | + expect(isCompetitivePrice('long', 101, 100)).toBe(true); |
| 85 | + expect(isCompetitivePrice('long', 99, 100)).toBe(false); |
| 86 | + }); |
| 87 | + |
| 88 | + it('treats ask prices at or below the fill price as competitive', () => { |
| 89 | + expect(isCompetitivePrice('short', 99, 100)).toBe(true); |
| 90 | + expect(isCompetitivePrice('short', 101, 100)).toBe(false); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + describe('bps bucketing', () => { |
| 95 | + it('computes absolute bps distance', () => { |
| 96 | + expect(getAbsoluteBpsDiff(100.1, 100)).toBeCloseTo(10, 8); |
| 97 | + expect(getAbsoluteBpsDiff(99.9, 100)).toBeCloseTo(10, 8); |
| 98 | + }); |
| 99 | + |
| 100 | + it('computes signed bps distance', () => { |
| 101 | + expect(getSignedBpsDiff(100.1, 100)).toBeCloseTo(10, 8); |
| 102 | + expect(getSignedBpsDiff(99.9, 100)).toBeCloseTo(-10, 8); |
| 103 | + }); |
| 104 | + |
| 105 | + it('maps bps distances into the configured buckets', () => { |
| 106 | + expect(getIndicativeBpsBucket(0)).toBe('very_tight'); |
| 107 | + expect(getIndicativeBpsBucket(9.99)).toBe('very_tight'); |
| 108 | + expect(getIndicativeBpsBucket(10)).toBe('tight'); |
| 109 | + expect(getIndicativeBpsBucket(19.99)).toBe('tight'); |
| 110 | + expect(getIndicativeBpsBucket(20)).toBe('moderate'); |
| 111 | + expect(getIndicativeBpsBucket(29.99)).toBe('moderate'); |
| 112 | + expect(getIndicativeBpsBucket(30)).toBe('wide'); |
| 113 | + expect(getIndicativeBpsBucket(49.99)).toBe('wide'); |
| 114 | + expect(getIndicativeBpsBucket(50)).toBe('very_wide'); |
| 115 | + expect(getIndicativeBpsBucket(500)).toBe('very_wide'); |
| 116 | + }); |
| 117 | + |
| 118 | + it('maps signed bps distances into directional buckets', () => { |
| 119 | + expect(getIndicativeDirectionBucket(10)).toBe('better'); |
| 120 | + expect(getIndicativeDirectionBucket(0)).toBe('equal'); |
| 121 | + expect(getIndicativeDirectionBucket(-10)).toBe('worse'); |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + describe('getCompetitiveLiquidity', () => { |
| 126 | + it('aggregates only bid levels that were competitive for a long maker', () => { |
| 127 | + const liquidity = getCompetitiveLiquidity( |
| 128 | + 'mm-1', |
| 129 | + { |
| 130 | + marketIndex: 0, |
| 131 | + marketType: 'perp', |
| 132 | + oraclePrice: 100, |
| 133 | + }, |
| 134 | + 'long', |
| 135 | + 100, |
| 136 | + { |
| 137 | + ts: 1710000000000, |
| 138 | + quotes: [ |
| 139 | + { |
| 140 | + bid_price: 101 * PRICE_PRECISION.toNumber(), |
| 141 | + bid_size: BASE_PRECISION.toNumber(), |
| 142 | + }, |
| 143 | + { |
| 144 | + bid_price: 100 * PRICE_PRECISION.toNumber(), |
| 145 | + bid_size: 2 * BASE_PRECISION.toNumber(), |
| 146 | + }, |
| 147 | + { |
| 148 | + bid_price: 99 * PRICE_PRECISION.toNumber(), |
| 149 | + bid_size: 4 * BASE_PRECISION.toNumber(), |
| 150 | + }, |
| 151 | + ], |
| 152 | + } |
| 153 | + ); |
| 154 | + |
| 155 | + expect(liquidity).toEqual({ |
| 156 | + maker: 'mm-1', |
| 157 | + bestPrice: 101, |
| 158 | + size: 3, |
| 159 | + quoteValue: 301, |
| 160 | + quoteTsMs: 1710000000000, |
| 161 | + }); |
| 162 | + }); |
| 163 | + |
| 164 | + it('aggregates only ask levels that were competitive for a short maker', () => { |
| 165 | + const liquidity = getCompetitiveLiquidity( |
| 166 | + 'mm-2', |
| 167 | + { |
| 168 | + marketIndex: 0, |
| 169 | + marketType: 'perp', |
| 170 | + oraclePrice: 100, |
| 171 | + }, |
| 172 | + 'short', |
| 173 | + 100, |
| 174 | + { |
| 175 | + ts: 1710000000000, |
| 176 | + quotes: [ |
| 177 | + { |
| 178 | + ask_price: 99 * PRICE_PRECISION.toNumber(), |
| 179 | + ask_size: 1.5 * BASE_PRECISION.toNumber(), |
| 180 | + }, |
| 181 | + { |
| 182 | + ask_price: 100 * PRICE_PRECISION.toNumber(), |
| 183 | + ask_size: 0.5 * BASE_PRECISION.toNumber(), |
| 184 | + }, |
| 185 | + { |
| 186 | + ask_price: 101 * PRICE_PRECISION.toNumber(), |
| 187 | + ask_size: 10 * BASE_PRECISION.toNumber(), |
| 188 | + }, |
| 189 | + ], |
| 190 | + } |
| 191 | + ); |
| 192 | + |
| 193 | + expect(liquidity).toEqual({ |
| 194 | + maker: 'mm-2', |
| 195 | + bestPrice: 99, |
| 196 | + size: 2, |
| 197 | + quoteValue: 198.5, |
| 198 | + quoteTsMs: 1710000000000, |
| 199 | + }); |
| 200 | + }); |
| 201 | + |
| 202 | + it('supports oracle-offset quotes', () => { |
| 203 | + const liquidity = getCompetitiveLiquidity( |
| 204 | + 'mm-3', |
| 205 | + { |
| 206 | + marketIndex: 0, |
| 207 | + marketType: 'perp', |
| 208 | + oraclePrice: 100, |
| 209 | + }, |
| 210 | + 'long', |
| 211 | + 100, |
| 212 | + { |
| 213 | + ts: 1710000000000, |
| 214 | + quotes: [ |
| 215 | + { |
| 216 | + bid_price: PRICE_PRECISION.toNumber(), |
| 217 | + bid_size: BASE_PRECISION.toNumber(), |
| 218 | + is_oracle_offset: true, |
| 219 | + }, |
| 220 | + ], |
| 221 | + } |
| 222 | + ); |
| 223 | + |
| 224 | + expect(liquidity?.bestPrice).toBe(101); |
| 225 | + expect(liquidity?.size).toBe(1); |
| 226 | + }); |
| 227 | + |
| 228 | + it('uses spot market precision when provided', () => { |
| 229 | + const liquidity = getCompetitiveLiquidity( |
| 230 | + 'mm-4', |
| 231 | + { |
| 232 | + marketIndex: 1, |
| 233 | + marketType: 'spot', |
| 234 | + oraclePrice: 10, |
| 235 | + }, |
| 236 | + 'long', |
| 237 | + 10, |
| 238 | + { |
| 239 | + ts: 1710000000000, |
| 240 | + quotes: [ |
| 241 | + { |
| 242 | + bid_price: 10 * PRICE_PRECISION.toNumber(), |
| 243 | + bid_size: 2_000_000, |
| 244 | + }, |
| 245 | + ], |
| 246 | + }, |
| 247 | + 1_000_000 |
| 248 | + ); |
| 249 | + |
| 250 | + expect(liquidity?.size).toBe(2); |
| 251 | + }); |
| 252 | + |
| 253 | + it('returns undefined when no levels were competitive', () => { |
| 254 | + expect( |
| 255 | + getCompetitiveLiquidity( |
| 256 | + 'mm-5', |
| 257 | + { |
| 258 | + marketIndex: 0, |
| 259 | + marketType: 'perp', |
| 260 | + oraclePrice: 100, |
| 261 | + }, |
| 262 | + 'long', |
| 263 | + 100, |
| 264 | + { |
| 265 | + ts: 1710000000000, |
| 266 | + quotes: [ |
| 267 | + { |
| 268 | + bid_price: 99 * PRICE_PRECISION.toNumber(), |
| 269 | + bid_size: BASE_PRECISION.toNumber(), |
| 270 | + }, |
| 271 | + ], |
| 272 | + } |
| 273 | + ) |
| 274 | + ).toBeUndefined(); |
| 275 | + }); |
| 276 | + }); |
| 277 | + |
| 278 | + describe('getQuoteValueOnBook', () => { |
| 279 | + it('sums quote notional on the relevant side', () => { |
| 280 | + expect( |
| 281 | + getQuoteValueOnBook( |
| 282 | + { |
| 283 | + marketIndex: 0, |
| 284 | + marketType: 'perp', |
| 285 | + oraclePrice: 100, |
| 286 | + }, |
| 287 | + 'long', |
| 288 | + { |
| 289 | + ts: 1710000000000, |
| 290 | + quotes: [ |
| 291 | + { |
| 292 | + bid_price: 101 * PRICE_PRECISION.toNumber(), |
| 293 | + bid_size: BASE_PRECISION.toNumber(), |
| 294 | + }, |
| 295 | + { |
| 296 | + bid_price: 100 * PRICE_PRECISION.toNumber(), |
| 297 | + bid_size: 2 * BASE_PRECISION.toNumber(), |
| 298 | + }, |
| 299 | + ], |
| 300 | + } |
| 301 | + ) |
| 302 | + ).toBe(301); |
| 303 | + }); |
| 304 | + |
| 305 | + it('supports oracle offset prices', () => { |
| 306 | + expect( |
| 307 | + getQuoteValueOnBook( |
| 308 | + { |
| 309 | + marketIndex: 0, |
| 310 | + marketType: 'perp', |
| 311 | + oraclePrice: 100, |
| 312 | + }, |
| 313 | + 'long', |
| 314 | + { |
| 315 | + ts: 1710000000000, |
| 316 | + quotes: [ |
| 317 | + { |
| 318 | + bid_price: PRICE_PRECISION.toNumber(), |
| 319 | + bid_size: BASE_PRECISION.toNumber(), |
| 320 | + is_oracle_offset: true, |
| 321 | + }, |
| 322 | + ], |
| 323 | + } |
| 324 | + ) |
| 325 | + ).toBe(101); |
| 326 | + }); |
| 327 | + }); |
| 328 | +}); |
0 commit comments