@exponent-labs/exponent-sdk 0.1.7 → 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.
- package/build/EventDecoderV2.d.ts +31 -0
- package/build/EventDecoderV2.js +76 -0
- package/build/EventDecoderV2.js.map +1 -0
- package/build/addressLookupTableUtil.d.ts +17 -1
- package/build/addressLookupTableUtil.js +35 -1
- package/build/addressLookupTableUtil.js.map +1 -1
- package/build/clmm/events.d.ts +10 -0
- package/build/clmm/events.js +10 -0
- package/build/clmm/events.js.map +1 -0
- package/build/clmm/index.d.ts +1 -0
- package/build/clmm/index.js +18 -0
- package/build/clmm/index.js.map +1 -0
- package/build/events.d.ts +200 -9
- package/build/events.js +73 -24
- package/build/events.js.map +1 -1
- package/build/eventsV2.d.ts +7 -0
- package/build/eventsV2.js +10 -0
- package/build/eventsV2.js.map +1 -0
- package/build/flavors.d.ts +2 -0
- package/build/flavors.js +81 -27
- package/build/flavors.js.map +1 -1
- package/build/index.d.ts +6 -0
- package/build/index.js +14 -4
- package/build/index.js.map +1 -1
- package/build/lpPosition.js +4 -1
- package/build/lpPosition.js.map +1 -1
- package/build/market.d.ts +14 -2
- package/build/market.js +70 -29
- package/build/market.js.map +1 -1
- package/build/marketThree.d.ts +664 -0
- package/build/marketThree.js +1415 -0
- package/build/marketThree.js.map +1 -0
- package/build/marketThree.test.d.ts +1 -0
- package/build/marketThree.test.js +166 -0
- package/build/marketThree.test.js.map +1 -0
- package/build/orderbook/events.d.ts +7 -0
- package/build/orderbook/events.js +10 -0
- package/build/orderbook/events.js.map +1 -0
- package/build/orderbook/index.d.ts +4 -0
- package/build/orderbook/index.js +41 -0
- package/build/orderbook/index.js.map +1 -0
- package/build/orderbook/math.d.ts +26 -0
- package/build/orderbook/math.js +111 -0
- package/build/orderbook/math.js.map +1 -0
- package/build/orderbook/orderbook.d.ts +175 -0
- package/build/orderbook/orderbook.js +756 -0
- package/build/orderbook/orderbook.js.map +1 -0
- package/build/orderbook/types.d.ts +49 -0
- package/build/orderbook/types.js +27 -0
- package/build/orderbook/types.js.map +1 -0
- package/build/orderbook/utils.d.ts +18 -0
- package/build/orderbook/utils.js +74 -0
- package/build/orderbook/utils.js.map +1 -0
- package/build/router.d.ts +92 -0
- package/build/router.js +214 -0
- package/build/router.js.map +1 -0
- package/build/syPosition.js +6 -0
- package/build/syPosition.js.map +1 -1
- package/build/utils/index.d.ts +3 -2
- package/build/utils/index.js +22 -1
- package/build/utils/index.js.map +1 -1
- package/build/vault.d.ts +3 -1
- package/build/vault.js +98 -62
- package/build/vault.js.map +1 -1
- package/build/ytPosition.d.ts +2 -0
- package/build/ytPosition.js +18 -5
- package/build/ytPosition.js.map +1 -1
- package/package.json +28 -23
- package/src/EventDecoderV2.ts +96 -0
- package/src/addressLookupTableUtil.ts +42 -1
- package/src/clmm/events.ts +17 -0
- package/src/clmm/index.ts +1 -0
- package/src/events.ts +280 -27
- package/src/eventsV2.ts +13 -0
- package/src/flavors.ts +97 -27
- package/src/index.ts +6 -0
- package/src/lpPosition.ts +5 -2
- package/src/market.ts +100 -31
- package/src/marketThree.test.ts +208 -0
- package/src/marketThree.ts +2430 -0
- package/src/orderbook/events.ts +13 -0
- package/src/orderbook/index.ts +12 -0
- package/src/orderbook/math.ts +122 -0
- package/src/orderbook/orderbook.ts +1153 -0
- package/src/orderbook/types.ts +45 -0
- package/src/orderbook/utils.ts +74 -0
- package/src/router.ts +360 -0
- package/src/syPosition.ts +4 -0
- package/src/utils/index.ts +27 -2
- package/src/vault.ts +100 -62
- package/src/ytPosition.ts +28 -7
- 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
|
+
})
|