@exponent-labs/exponent-sdk 0.0.3

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 (68) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/build/addressLookupTableUtil.d.ts +12 -0
  3. package/build/addressLookupTableUtil.js +32 -0
  4. package/build/addressLookupTableUtil.js.map +1 -0
  5. package/build/environment.d.ts +10 -0
  6. package/build/environment.js +13 -0
  7. package/build/environment.js.map +1 -0
  8. package/build/events.d.ts +339 -0
  9. package/build/events.js +231 -0
  10. package/build/events.js.map +1 -0
  11. package/build/flavors.d.ts +24 -0
  12. package/build/flavors.js +713 -0
  13. package/build/flavors.js.map +1 -0
  14. package/build/index.d.ts +11 -0
  15. package/build/index.js +45 -0
  16. package/build/index.js.map +1 -0
  17. package/build/lpPosition.d.ts +35 -0
  18. package/build/lpPosition.js +103 -0
  19. package/build/lpPosition.js.map +1 -0
  20. package/build/market.d.ts +567 -0
  21. package/build/market.js +1445 -0
  22. package/build/market.js.map +1 -0
  23. package/build/syPosition.d.ts +6 -0
  24. package/build/syPosition.js +115 -0
  25. package/build/syPosition.js.map +1 -0
  26. package/build/tokenUtil.d.ts +3 -0
  27. package/build/tokenUtil.js +23 -0
  28. package/build/tokenUtil.js.map +1 -0
  29. package/build/utils/altUtil.d.ts +8 -0
  30. package/build/utils/altUtil.js +35 -0
  31. package/build/utils/altUtil.js.map +1 -0
  32. package/build/utils/binSolver.d.ts +1 -0
  33. package/build/utils/binSolver.js +45 -0
  34. package/build/utils/binSolver.js.map +1 -0
  35. package/build/utils/binSolver.test.d.ts +1 -0
  36. package/build/utils/binSolver.test.js +15 -0
  37. package/build/utils/binSolver.test.js.map +1 -0
  38. package/build/utils/index.d.ts +6 -0
  39. package/build/utils/index.js +31 -0
  40. package/build/utils/index.js.map +1 -0
  41. package/build/utils/ix.d.ts +6 -0
  42. package/build/utils/ix.js +3 -0
  43. package/build/utils/ix.js.map +1 -0
  44. package/build/vault.d.ts +289 -0
  45. package/build/vault.js +615 -0
  46. package/build/vault.js.map +1 -0
  47. package/build/ytPosition.d.ts +86 -0
  48. package/build/ytPosition.js +231 -0
  49. package/build/ytPosition.js.map +1 -0
  50. package/jest.config.js +5 -0
  51. package/package.json +42 -0
  52. package/src/addressLookupTableUtil.ts +34 -0
  53. package/src/environment.ts +19 -0
  54. package/src/events.ts +595 -0
  55. package/src/flavors.ts +773 -0
  56. package/src/index.ts +11 -0
  57. package/src/lpPosition.ts +129 -0
  58. package/src/market.ts +2338 -0
  59. package/src/syPosition.ts +151 -0
  60. package/src/tokenUtil.ts +20 -0
  61. package/src/utils/altUtil.ts +47 -0
  62. package/src/utils/binSolver.test.ts +15 -0
  63. package/src/utils/binSolver.ts +44 -0
  64. package/src/utils/index.ts +32 -0
  65. package/src/utils/ix.ts +7 -0
  66. package/src/vault.ts +999 -0
  67. package/src/ytPosition.ts +313 -0
  68. package/tsconfig.json +38 -0
package/src/index.ts ADDED
@@ -0,0 +1,11 @@
1
+ export { Market, LiquidityAdd, MarketAdminAction } from "./market"
2
+ export { Vault } from "./vault"
3
+ export { LpPosition, LpPositionState } from "./lpPosition"
4
+ export { YtPosition } from "./ytPosition"
5
+ export { EventDecoder } from "./events"
6
+ export type { AdminAction } from "./vault"
7
+ export type { CategorizedEvents } from "./events"
8
+ export * as events from "./events"
9
+ export * from "./environment"
10
+ export * as flavors from "./flavors"
11
+ export { makeSyPosition } from "./syPosition"
@@ -0,0 +1,129 @@
1
+ import { AnchorProvider, Program, web3 } from "@coral-xyz/anchor"
2
+ import { Market, MyWallet } from "./market"
3
+ import { Environment } from "./environment"
4
+ import { ExponentCore, IDL } from "@exponent-labs/exponent-idl"
5
+ import { ExponentFetcher } from "@exponent-labs/exponent-fetcher"
6
+ import { PreciseNumber } from "@exponent-labs/precise-number"
7
+ import { makeSyPosition } from "./syPosition"
8
+
9
+ export interface EmissionTracker {
10
+ lastSeenIndex: number
11
+ staged: bigint
12
+ }
13
+
14
+ export interface LpPositionState {
15
+ owner: web3.PublicKey
16
+ balanceLp: bigint
17
+ market: Market
18
+ emissionTrackers: EmissionTracker[]
19
+ farms: EmissionTracker[]
20
+ fetcher: ExponentFetcher
21
+ }
22
+
23
+ export class LpPosition {
24
+ public coreProgram: Program<ExponentCore>
25
+ constructor(
26
+ public env: Environment,
27
+ public connection: web3.Connection,
28
+ public selfAddress: web3.PublicKey,
29
+ public state: LpPositionState,
30
+ ) {
31
+ const mockWallet = new MyWallet(web3.Keypair.generate())
32
+ this.coreProgram = new Program(IDL as ExponentCore, new AnchorProvider(connection, mockWallet))
33
+ }
34
+
35
+ static async load(env: Environment, connection: web3.Connection, address: web3.PublicKey) {
36
+ const fetcher = new ExponentFetcher({ connection })
37
+ const x = await fetcher.fetchLpPosition(address)
38
+ const market = await Market.load(env, connection, x.market)
39
+ const state: LpPositionState = {
40
+ owner: x.owner,
41
+ balanceLp: x.lpBalance,
42
+ market,
43
+ emissionTrackers: x.emissions,
44
+ farms: x.farms,
45
+ fetcher: fetcher,
46
+ }
47
+ return new LpPosition(env, connection, address, state)
48
+ }
49
+
50
+ async reload(conn = this.connection) {
51
+ const x = await LpPosition.load(this.env, conn, this.selfAddress)
52
+ this.state = x.state
53
+ return x
54
+ }
55
+
56
+ get lpBalance() {
57
+ return this.state.balanceLp
58
+ }
59
+
60
+ get owner() {
61
+ return this.state.owner
62
+ }
63
+
64
+ get marketSdk() {
65
+ return this.state.market
66
+ }
67
+
68
+ get marketAddress() {
69
+ return this.state.market.selfAddress
70
+ }
71
+
72
+ async ixDepositLp(amount: bigint): Promise<web3.TransactionInstruction> {
73
+ const ix = this.marketSdk.ixDepositLp({
74
+ owner: this.owner,
75
+ amount,
76
+ })
77
+ return ix
78
+ }
79
+
80
+ async ixWithdrawLp(amount: bigint): Promise<web3.TransactionInstruction> {
81
+ const ix = this.marketSdk.ixWithdrawLp({
82
+ owner: this.owner,
83
+ amount,
84
+ })
85
+ return ix
86
+ }
87
+
88
+ async claimableFarmEmissions(mint: web3.PublicKey) {
89
+ const marketFarmIndex = this.state.market.state.lpFarm.farmEmissions.findIndex((e) => e.mint.equals(mint))
90
+ if (marketFarmIndex === -1) return null
91
+
92
+ const marketFarm = this.state.market.state.lpFarm.farmEmissions[marketFarmIndex]
93
+ const positionFarm = this.state.farms[marketFarmIndex]
94
+
95
+ const ts = new Date().getTime() / 1000
96
+
97
+ const deltaTimeFarm = ts - this.marketSdk.state.lpFarm.lastSeenTimestamp
98
+ const lpStakedTotal = this.marketSdk.state.lpEscrowAmount
99
+ const tokensEmitted = Number(marketFarm.tokenRate) * deltaTimeFarm
100
+ const increaseAmount = Number(tokensEmitted) / Number(lpStakedTotal)
101
+ const virtualIndex = Number(PreciseNumber.fromRaw(marketFarm.index[0]).valueString) + increaseAmount
102
+
103
+ const deltaIndex = positionFarm ? virtualIndex - positionFarm.lastSeenIndex : virtualIndex
104
+
105
+ const claimableAmount = Number(this.lpBalance) * deltaIndex
106
+
107
+ return claimableAmount + (Number(positionFarm?.staged) || 0)
108
+ }
109
+
110
+ async getClaimableMarketEmissions() {
111
+ const syPosition = this.marketSdk.state.syPosition
112
+
113
+ return this.state.market.emissions.map((emission, index) => {
114
+ const marketEmission = this.state.market.marketEmissions.trackers[index]
115
+ const positionEmission = this.state.emissionTrackers[index]
116
+ if (!positionEmission) return 0
117
+
118
+ // First calculate the market's emission index update from syEmissions
119
+ const difference = Number(syPosition.emissions[index].staged) - Number(marketEmission.lastSeenStaged)
120
+ const lpStakedTotal = this.marketSdk.state.lpEscrowAmount
121
+ const indexIncrease = difference / Number(lpStakedTotal)
122
+ const currentIndex = marketEmission.lpShareIndex + indexIncrease
123
+
124
+ // Then calculate user's claimable amount based on the updated index
125
+ const earned = Number(this.lpBalance) * (currentIndex - positionEmission.lastSeenIndex)
126
+ return Math.floor(earned) + Number(positionEmission.staged)
127
+ })
128
+ }
129
+ }