@exponent-labs/exponent-sdk 0.1.6 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. package/build/EventDecoderV2.d.ts +31 -0
  2. package/build/EventDecoderV2.js +76 -0
  3. package/build/EventDecoderV2.js.map +1 -0
  4. package/build/addressLookupTableUtil.d.ts +17 -1
  5. package/build/addressLookupTableUtil.js +35 -1
  6. package/build/addressLookupTableUtil.js.map +1 -1
  7. package/build/clmm/events.d.ts +10 -0
  8. package/build/clmm/events.js +10 -0
  9. package/build/clmm/events.js.map +1 -0
  10. package/build/clmm/index.d.ts +1 -0
  11. package/build/clmm/index.js +18 -0
  12. package/build/clmm/index.js.map +1 -0
  13. package/build/events.d.ts +200 -9
  14. package/build/events.js +73 -24
  15. package/build/events.js.map +1 -1
  16. package/build/eventsV2.d.ts +7 -0
  17. package/build/eventsV2.js +10 -0
  18. package/build/eventsV2.js.map +1 -0
  19. package/build/flavors.d.ts +2 -0
  20. package/build/flavors.js +81 -27
  21. package/build/flavors.js.map +1 -1
  22. package/build/index.d.ts +6 -0
  23. package/build/index.js +14 -4
  24. package/build/index.js.map +1 -1
  25. package/build/lpPosition.js +4 -1
  26. package/build/lpPosition.js.map +1 -1
  27. package/build/market.d.ts +14 -2
  28. package/build/market.js +70 -29
  29. package/build/market.js.map +1 -1
  30. package/build/marketThree.d.ts +664 -0
  31. package/build/marketThree.js +1415 -0
  32. package/build/marketThree.js.map +1 -0
  33. package/build/marketThree.test.d.ts +1 -0
  34. package/build/marketThree.test.js +166 -0
  35. package/build/marketThree.test.js.map +1 -0
  36. package/build/orderbook/events.d.ts +7 -0
  37. package/build/orderbook/events.js +10 -0
  38. package/build/orderbook/events.js.map +1 -0
  39. package/build/orderbook/index.d.ts +4 -0
  40. package/build/orderbook/index.js +41 -0
  41. package/build/orderbook/index.js.map +1 -0
  42. package/build/orderbook/math.d.ts +26 -0
  43. package/build/orderbook/math.js +111 -0
  44. package/build/orderbook/math.js.map +1 -0
  45. package/build/orderbook/orderbook.d.ts +175 -0
  46. package/build/orderbook/orderbook.js +756 -0
  47. package/build/orderbook/orderbook.js.map +1 -0
  48. package/build/orderbook/types.d.ts +49 -0
  49. package/build/orderbook/types.js +27 -0
  50. package/build/orderbook/types.js.map +1 -0
  51. package/build/orderbook/utils.d.ts +18 -0
  52. package/build/orderbook/utils.js +74 -0
  53. package/build/orderbook/utils.js.map +1 -0
  54. package/build/router.d.ts +92 -0
  55. package/build/router.js +214 -0
  56. package/build/router.js.map +1 -0
  57. package/build/syPosition.js +6 -0
  58. package/build/syPosition.js.map +1 -1
  59. package/build/utils/index.d.ts +3 -2
  60. package/build/utils/index.js +22 -1
  61. package/build/utils/index.js.map +1 -1
  62. package/build/vault.d.ts +3 -1
  63. package/build/vault.js +98 -62
  64. package/build/vault.js.map +1 -1
  65. package/build/ytPosition.d.ts +2 -0
  66. package/build/ytPosition.js +18 -5
  67. package/build/ytPosition.js.map +1 -1
  68. package/package.json +28 -23
  69. package/src/EventDecoderV2.ts +96 -0
  70. package/src/addressLookupTableUtil.ts +42 -1
  71. package/src/clmm/events.ts +17 -0
  72. package/src/clmm/index.ts +1 -0
  73. package/src/events.ts +280 -27
  74. package/src/eventsV2.ts +13 -0
  75. package/src/flavors.ts +97 -27
  76. package/src/index.ts +6 -0
  77. package/src/lpPosition.ts +5 -2
  78. package/src/market.ts +100 -31
  79. package/src/marketThree.test.ts +208 -0
  80. package/src/marketThree.ts +2430 -0
  81. package/src/orderbook/events.ts +13 -0
  82. package/src/orderbook/index.ts +12 -0
  83. package/src/orderbook/math.ts +122 -0
  84. package/src/orderbook/orderbook.ts +1153 -0
  85. package/src/orderbook/types.ts +45 -0
  86. package/src/orderbook/utils.ts +74 -0
  87. package/src/router.ts +360 -0
  88. package/src/syPosition.ts +4 -0
  89. package/src/utils/index.ts +27 -2
  90. package/src/vault.ts +100 -62
  91. package/src/ytPosition.ts +28 -7
  92. package/tsconfig.json +4 -1
@@ -0,0 +1,208 @@
1
+ import { web3 } from "@coral-xyz/anchor"
2
+ import { afterAll, beforeAll, describe, expect, it } from "@jest/globals"
3
+ import Decimal from "decimal.js"
4
+
5
+ import { MarketThree } from "./marketThree"
6
+
7
+ type TickNode = {
8
+ apy: number
9
+ feeGrowthOutsidePt: bigint
10
+ feeGrowthOutsideSy: bigint
11
+ liquidityNet: bigint
12
+ liquidityGross: bigint
13
+ lnImpliedRate: number
14
+ principalPt: bigint
15
+ principalSy: bigint
16
+ principalShareSupply: bigint
17
+ }
18
+
19
+ type TicksFixture = {
20
+ ticksTree: TickNode[]
21
+ market: web3.PublicKey
22
+ feeGrowthIndexGlobalPt: bigint
23
+ feeGrowthIndexGlobalSy: bigint
24
+ currentPrefixSum: bigint
25
+ currentSpotPrice: number
26
+ currentTick: number
27
+ }
28
+
29
+ const rawTicks = {
30
+ ticksTree: [
31
+ {
32
+ apy: 10_000,
33
+ feeGrowthOutsidePt: "0",
34
+ feeGrowthOutsideSy: "0",
35
+ liquidityNet: "212216886",
36
+ liquidityGross: "212216886",
37
+ lnImpliedRate: 1.01,
38
+ principalPt: "8489056",
39
+ principalSy: "10000563",
40
+ principalShareSupply: "9213872",
41
+ },
42
+ {
43
+ apy: 100_000,
44
+ feeGrowthOutsidePt: "0",
45
+ feeGrowthOutsideSy: "0",
46
+ liquidityNet: "-212216886",
47
+ liquidityGross: "212216886",
48
+ lnImpliedRate: 1.1,
49
+ principalPt: "0",
50
+ principalSy: "3935",
51
+ principalShareSupply: "0",
52
+ },
53
+ {
54
+ apy: 65_536,
55
+ feeGrowthOutsidePt: "276675485368327",
56
+ feeGrowthOutsideSy: "0",
57
+ liquidityNet: "637259347272925184",
58
+ liquidityGross: "637259347272925184",
59
+ lnImpliedRate: 0,
60
+ principalPt: "70334384439296",
61
+ principalSy: "32088147345014784",
62
+ principalShareSupply: "0",
63
+ },
64
+ {
65
+ apy: 65_536,
66
+ feeGrowthOutsidePt: "276675485368327",
67
+ feeGrowthOutsideSy: "0",
68
+ liquidityNet: "6510516211317473280",
69
+ liquidityGross: "6510516211317473280",
70
+ lnImpliedRate: 9.085484053695086e135,
71
+ principalPt: "70335071634063",
72
+ principalSy: "4226346775310499840",
73
+ principalShareSupply: "0",
74
+ },
75
+ ],
76
+ market: "11111111111111111111111111111111",
77
+ feeGrowthIndexGlobalPt: "0",
78
+ feeGrowthIndexGlobalSy: "0",
79
+ currentPrefixSum: "0",
80
+ currentSpotPrice: 0,
81
+ currentTick: 0,
82
+ }
83
+
84
+ const ticksFixture: TicksFixture = {
85
+ ticksTree: rawTicks.ticksTree.map((tick) => ({
86
+ apy: tick.apy,
87
+ feeGrowthOutsidePt: BigInt(tick.feeGrowthOutsidePt),
88
+ feeGrowthOutsideSy: BigInt(tick.feeGrowthOutsideSy),
89
+ liquidityNet: BigInt(tick.liquidityNet),
90
+ liquidityGross: BigInt(tick.liquidityGross),
91
+ lnImpliedRate: tick.lnImpliedRate,
92
+ principalPt: BigInt(tick.principalPt),
93
+ principalSy: BigInt(tick.principalSy),
94
+ principalShareSupply: BigInt(tick.principalShareSupply),
95
+ })),
96
+ market: new web3.PublicKey(rawTicks.market),
97
+ feeGrowthIndexGlobalPt: BigInt(rawTicks.feeGrowthIndexGlobalPt),
98
+ feeGrowthIndexGlobalSy: BigInt(rawTicks.feeGrowthIndexGlobalSy),
99
+ currentPrefixSum: BigInt(rawTicks.currentPrefixSum),
100
+ currentSpotPrice: rawTicks.currentSpotPrice,
101
+ currentTick: rawTicks.currentTick,
102
+ }
103
+
104
+ const SECONDS_PER_YEAR = 365 * 24 * 60 * 60
105
+
106
+ function buildTestMarket(ticks: TicksFixture, expirationTs: number) {
107
+ const market = Object.create(MarketThree.prototype) as MarketThree
108
+
109
+ ;(market as any).state = {
110
+ ticks,
111
+ financials: {
112
+ expirationTs,
113
+ ptBalance: 0n,
114
+ syBalance: 0n,
115
+ liquidityBalance: 0n,
116
+ },
117
+ configurationOptions: {
118
+ treasuryFeeBps: 0,
119
+ lnFeeRateRoot: 0,
120
+ minLpTickAmount: 0n,
121
+ epsilonClamp: 0,
122
+ maxLpSupply: 0n,
123
+ tickSpace: 1,
124
+ },
125
+ } as any
126
+ ;(market as any).flavor = {
127
+ flavor: "generic",
128
+ currentSyExchangeRate: 1,
129
+ }
130
+
131
+ return market
132
+ }
133
+
134
+ function ticksWithSpotPrice(spotPrice: number): TicksFixture {
135
+ return {
136
+ ...ticksFixture,
137
+ currentSpotPrice: spotPrice,
138
+ }
139
+ }
140
+
141
+ describe("MarketThree.liquidityUnitPriceInAsset", () => {
142
+ let dateNowSpy: jest.SpyInstance<number, []>
143
+
144
+ beforeAll(() => {
145
+ dateNowSpy = jest.spyOn(Date, "now").mockReturnValue(0)
146
+ })
147
+
148
+ afterAll(() => {
149
+ dateNowSpy.mockRestore()
150
+ })
151
+
152
+ it("computes principal-only LP price for a populated tick range", () => {
153
+ const syExchangeRate = 1.2
154
+ const spotPrice = 0.2
155
+ const ticks = ticksWithSpotPrice(spotPrice)
156
+ const market = buildTestMarket(ticks, SECONDS_PER_YEAR)
157
+
158
+ const lowerTickKey = 10_000
159
+ const upperTickKey = 200_000
160
+
161
+ const result = market.liquidityUnitPriceInAsset({
162
+ lowerTickKey,
163
+ upperTickKey,
164
+ syExchangeRateOverride: syExchangeRate,
165
+ })
166
+
167
+ const relevantIntervals = ticks.ticksTree.filter((tick) => tick.apy >= lowerTickKey && tick.apy < upperTickKey)
168
+
169
+ const totalPrincipalPt = relevantIntervals.reduce((acc, tick) => acc + tick.principalPt, 0n)
170
+ const totalPrincipalSy = relevantIntervals.reduce((acc, tick) => acc + tick.principalSy, 0n)
171
+ const totalShareSupply = relevantIntervals.reduce((acc, tick) => acc + tick.principalShareSupply, 0n)
172
+
173
+ const timeFactor = SECONDS_PER_YEAR / SECONDS_PER_YEAR
174
+ const ptAssetValuePerToken = Math.exp(-timeFactor * spotPrice)
175
+
176
+ const principalPtValue = new Decimal(totalPrincipalPt.toString()).mul(ptAssetValuePerToken)
177
+ const principalSyValue = new Decimal(totalPrincipalSy.toString()).mul(syExchangeRate)
178
+ const expected = principalPtValue.plus(principalSyValue).div(new Decimal(totalShareSupply.toString()))
179
+
180
+ expect(result).toBeCloseTo(expected.toNumber(), 9)
181
+ })
182
+
183
+ it("returns zero when the selected range has no share supply", () => {
184
+ const ticks = ticksWithSpotPrice(0.5)
185
+ const market = buildTestMarket(ticks, SECONDS_PER_YEAR)
186
+
187
+ const price = market.liquidityUnitPriceInAsset({
188
+ lowerTickKey: 65_536,
189
+ upperTickKey: 65_537,
190
+ syExchangeRateOverride: 1.05,
191
+ })
192
+
193
+ expect(price).toBe(0)
194
+ })
195
+
196
+ it("throws when lower tick boundary is not less than upper", () => {
197
+ const ticks = ticksWithSpotPrice(0.5)
198
+ const market = buildTestMarket(ticks, SECONDS_PER_YEAR)
199
+
200
+ expect(() =>
201
+ market.liquidityUnitPriceInAsset({
202
+ lowerTickKey: 42,
203
+ upperTickKey: 42,
204
+ syExchangeRateOverride: 1,
205
+ }),
206
+ ).toThrow("lowerTickKey must be less than upperTickKey")
207
+ })
208
+ })