@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.
- package/CHANGELOG.md +16 -0
- package/build/addressLookupTableUtil.d.ts +12 -0
- package/build/addressLookupTableUtil.js +32 -0
- package/build/addressLookupTableUtil.js.map +1 -0
- package/build/environment.d.ts +10 -0
- package/build/environment.js +13 -0
- package/build/environment.js.map +1 -0
- package/build/events.d.ts +339 -0
- package/build/events.js +231 -0
- package/build/events.js.map +1 -0
- package/build/flavors.d.ts +24 -0
- package/build/flavors.js +713 -0
- package/build/flavors.js.map +1 -0
- package/build/index.d.ts +11 -0
- package/build/index.js +45 -0
- package/build/index.js.map +1 -0
- package/build/lpPosition.d.ts +35 -0
- package/build/lpPosition.js +103 -0
- package/build/lpPosition.js.map +1 -0
- package/build/market.d.ts +567 -0
- package/build/market.js +1445 -0
- package/build/market.js.map +1 -0
- package/build/syPosition.d.ts +6 -0
- package/build/syPosition.js +115 -0
- package/build/syPosition.js.map +1 -0
- package/build/tokenUtil.d.ts +3 -0
- package/build/tokenUtil.js +23 -0
- package/build/tokenUtil.js.map +1 -0
- package/build/utils/altUtil.d.ts +8 -0
- package/build/utils/altUtil.js +35 -0
- package/build/utils/altUtil.js.map +1 -0
- package/build/utils/binSolver.d.ts +1 -0
- package/build/utils/binSolver.js +45 -0
- package/build/utils/binSolver.js.map +1 -0
- package/build/utils/binSolver.test.d.ts +1 -0
- package/build/utils/binSolver.test.js +15 -0
- package/build/utils/binSolver.test.js.map +1 -0
- package/build/utils/index.d.ts +6 -0
- package/build/utils/index.js +31 -0
- package/build/utils/index.js.map +1 -0
- package/build/utils/ix.d.ts +6 -0
- package/build/utils/ix.js +3 -0
- package/build/utils/ix.js.map +1 -0
- package/build/vault.d.ts +289 -0
- package/build/vault.js +615 -0
- package/build/vault.js.map +1 -0
- package/build/ytPosition.d.ts +86 -0
- package/build/ytPosition.js +231 -0
- package/build/ytPosition.js.map +1 -0
- package/jest.config.js +5 -0
- package/package.json +42 -0
- package/src/addressLookupTableUtil.ts +34 -0
- package/src/environment.ts +19 -0
- package/src/events.ts +595 -0
- package/src/flavors.ts +773 -0
- package/src/index.ts +11 -0
- package/src/lpPosition.ts +129 -0
- package/src/market.ts +2338 -0
- package/src/syPosition.ts +151 -0
- package/src/tokenUtil.ts +20 -0
- package/src/utils/altUtil.ts +47 -0
- package/src/utils/binSolver.test.ts +15 -0
- package/src/utils/binSolver.ts +44 -0
- package/src/utils/index.ts +32 -0
- package/src/utils/ix.ts +7 -0
- package/src/vault.ts +999 -0
- package/src/ytPosition.ts +313 -0
- 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
|
+
}
|