@exponent-labs/exponent-sdk 0.0.10 → 0.1.1
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/addressLookupTableUtil.js.map +1 -1
- package/build/events.d.ts +2 -2
- package/build/events.js +43 -49
- package/build/events.js.map +1 -1
- package/build/flavors.d.ts +3 -3
- package/build/flavors.js +19 -24
- package/build/flavors.js.map +1 -1
- package/build/lpPosition.d.ts +4 -4
- package/build/lpPosition.js +19 -19
- package/build/lpPosition.js.map +1 -1
- package/build/market.d.ts +31 -35
- package/build/market.js +202 -205
- package/build/market.js.map +1 -1
- package/build/syPosition.d.ts +2 -2
- package/build/syPosition.js +19 -16
- package/build/syPosition.js.map +1 -1
- package/build/tokenUtil.d.ts +2 -2
- package/build/tokenUtil.js +4 -4
- package/build/tokenUtil.js.map +1 -1
- package/build/utils/altUtil.d.ts +3 -3
- package/build/utils/altUtil.js +15 -9
- package/build/utils/altUtil.js.map +1 -1
- package/build/utils/binSolver.test.js +6 -5
- package/build/utils/binSolver.test.js.map +1 -1
- package/build/utils/index.js.map +1 -1
- package/build/vault.d.ts +11 -11
- package/build/vault.js +69 -68
- package/build/vault.js.map +1 -1
- package/build/ytPosition.d.ts +7 -7
- package/build/ytPosition.js +33 -33
- package/build/ytPosition.js.map +1 -1
- package/package.json +22 -22
- package/src/addressLookupTableUtil.ts +2 -1
- package/src/events.ts +44 -44
- package/src/flavors.ts +33 -39
- package/src/lpPosition.ts +22 -21
- package/src/market.ts +296 -229
- package/src/syPosition.ts +38 -26
- package/src/tokenUtil.ts +7 -5
- package/src/utils/altUtil.ts +17 -12
- package/src/utils/binSolver.test.ts +2 -0
- package/src/utils/index.ts +2 -0
- package/src/vault.ts +108 -91
- package/src/ytPosition.ts +40 -36
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@exponent-labs/exponent-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"main": "build/index.js",
|
|
5
5
|
"types": "build/index.d.ts",
|
|
6
6
|
"license": "AGPL-3.0",
|
|
@@ -10,28 +10,28 @@
|
|
|
10
10
|
},
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"@coral-xyz/anchor": "0.30.1",
|
|
13
|
-
"@exponent-labs/exponent-fetcher": "0.
|
|
14
|
-
"@exponent-labs/exponent-idl": "0.
|
|
15
|
-
"@exponent-labs/exponent-pda": "0.
|
|
16
|
-
"@exponent-labs/generic-sy-idl": "0.
|
|
17
|
-
"@exponent-labs/generic-sy-pda": "0.
|
|
18
|
-
"@exponent-labs/generic-sy-sdk": "0.
|
|
19
|
-
"@exponent-labs/jito-restaking-sy-idl": "0.
|
|
20
|
-
"@exponent-labs/jito-restaking-sy-sdk": "0.
|
|
21
|
-
"@exponent-labs/kamino-sy-sdk": "0.
|
|
22
|
-
"@exponent-labs/marginfi-sy-pda": "0.
|
|
23
|
-
"@exponent-labs/marginfi-sy-sdk": "0.
|
|
24
|
-
"@exponent-labs/market-math": "0.
|
|
25
|
-
"@exponent-labs/perena-sy-idl": "0.
|
|
26
|
-
"@exponent-labs/perena-sy-pda": "0.
|
|
27
|
-
"@exponent-labs/perena-sy-sdk": "0.
|
|
13
|
+
"@exponent-labs/exponent-fetcher": "0.1.1",
|
|
14
|
+
"@exponent-labs/exponent-idl": "0.1.1",
|
|
15
|
+
"@exponent-labs/exponent-pda": "0.1.1",
|
|
16
|
+
"@exponent-labs/generic-sy-idl": "0.1.1",
|
|
17
|
+
"@exponent-labs/generic-sy-pda": "0.1.1",
|
|
18
|
+
"@exponent-labs/generic-sy-sdk": "0.1.1",
|
|
19
|
+
"@exponent-labs/jito-restaking-sy-idl": "0.1.1",
|
|
20
|
+
"@exponent-labs/jito-restaking-sy-sdk": "0.1.1",
|
|
21
|
+
"@exponent-labs/kamino-sy-sdk": "0.1.1",
|
|
22
|
+
"@exponent-labs/marginfi-sy-pda": "0.1.1",
|
|
23
|
+
"@exponent-labs/marginfi-sy-sdk": "0.1.1",
|
|
24
|
+
"@exponent-labs/market-math": "0.1.1",
|
|
25
|
+
"@exponent-labs/perena-sy-idl": "0.1.1",
|
|
26
|
+
"@exponent-labs/perena-sy-pda": "0.1.1",
|
|
27
|
+
"@exponent-labs/perena-sy-sdk": "0.1.1",
|
|
28
28
|
"@solana/spl-token": "0.4.8",
|
|
29
|
-
"@exponent-labs/precise-number": "0.
|
|
30
|
-
"@exponent-labs/exponent-admin-pda": "0.
|
|
31
|
-
"@exponent-labs/jito-restaking-sy-pda": "0.
|
|
32
|
-
"@exponent-labs/kamino-sy-idl": "0.
|
|
33
|
-
"@exponent-labs/exponent-types": "0.
|
|
34
|
-
"@exponent-labs/kamino-sy-pda": "0.
|
|
29
|
+
"@exponent-labs/precise-number": "0.1.1",
|
|
30
|
+
"@exponent-labs/exponent-admin-pda": "0.1.1",
|
|
31
|
+
"@exponent-labs/jito-restaking-sy-pda": "0.1.1",
|
|
32
|
+
"@exponent-labs/kamino-sy-idl": "0.1.1",
|
|
33
|
+
"@exponent-labs/exponent-types": "0.1.1",
|
|
34
|
+
"@exponent-labs/kamino-sy-pda": "0.1.1",
|
|
35
35
|
"bs58": "5.0.0"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { web3 } from "@coral-xyz/anchor"
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
import { CpiAccountIndex, CpiAccountIndexes } from "@exponent-labs/exponent-types"
|
|
3
4
|
|
|
4
5
|
/** Fetch and deserialize the ALT */
|
|
5
6
|
export async function fetchAddressLookupTable(
|
package/src/events.ts
CHANGED
|
@@ -1,33 +1,35 @@
|
|
|
1
|
-
import { BorshCoder, web3
|
|
1
|
+
import { BN, BorshCoder, web3 } from "@coral-xyz/anchor"
|
|
2
|
+
import bs58 from "bs58"
|
|
3
|
+
|
|
2
4
|
import { ExponentCore, IDL } from "@exponent-labs/exponent-idl"
|
|
3
5
|
import { AnchorizedPNum } from "@exponent-labs/exponent-types"
|
|
4
|
-
|
|
6
|
+
|
|
7
|
+
const EVENT_IX_TAG = new BN("1d9acb512ea545e4", 16)
|
|
5
8
|
|
|
6
9
|
export class EventDecoder {
|
|
7
10
|
private coder: BorshCoder
|
|
8
11
|
programId: web3.PublicKey
|
|
9
|
-
private static eventIxTag =
|
|
10
|
-
private discriminators: Map<string, string>
|
|
12
|
+
private static eventIxTag = EVENT_IX_TAG
|
|
11
13
|
|
|
12
14
|
constructor(idl: ExponentCore = IDL as ExponentCore) {
|
|
13
15
|
this.programId = new web3.PublicKey(idl.address)
|
|
14
16
|
this.coder = new BorshCoder(idl)
|
|
15
|
-
this.discriminators = new Map<string, string>(
|
|
16
|
-
(idl.events ?? []).map((ev): [string, string] => [Buffer.from(ev.discriminator).toString("hex"), ev.name]),
|
|
17
|
-
)
|
|
18
17
|
}
|
|
19
18
|
|
|
20
19
|
parseAsTransactionCpiData(log: string): string | null {
|
|
21
|
-
let encodedLog: Buffer
|
|
22
20
|
try {
|
|
23
|
-
encodedLog = Buffer.from(bs58.decode(log))
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
21
|
+
const encodedLog = Buffer.from(bs58.decode(log))
|
|
22
|
+
const discriminator = encodedLog.subarray(0, 8)
|
|
23
|
+
|
|
24
|
+
const eventIxTagBuffer = Uint8Array.from(EventDecoder.eventIxTag.toBuffer("le", 8))
|
|
25
|
+
|
|
26
|
+
if (discriminator.equals(eventIxTagBuffer)) {
|
|
27
|
+
return Buffer.from(Uint8Array.from(encodedLog.subarray(8))).toString("base64")
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
throw new Error("Discriminator not found")
|
|
31
|
+
} catch (error) {
|
|
32
|
+
console.error("Unable tp parse transaction CPI data:", error)
|
|
31
33
|
return null
|
|
32
34
|
}
|
|
33
35
|
}
|
|
@@ -42,10 +44,10 @@ export class EventDecoder {
|
|
|
42
44
|
const event = this.coder.events.decode(cpiData)
|
|
43
45
|
return {
|
|
44
46
|
name: event.name,
|
|
45
|
-
data: event,
|
|
47
|
+
data: event.data,
|
|
46
48
|
}
|
|
47
|
-
} catch (
|
|
48
|
-
console.error(`Failed to decode event:`,
|
|
49
|
+
} catch (error) {
|
|
50
|
+
console.error(`Failed to decode event:`, error)
|
|
49
51
|
return null
|
|
50
52
|
}
|
|
51
53
|
}
|
|
@@ -54,8 +56,7 @@ export class EventDecoder {
|
|
|
54
56
|
transactionResponse: web3.VersionedTransactionResponse | web3.TransactionResponse,
|
|
55
57
|
connection: web3.Connection,
|
|
56
58
|
): Promise<CategorizedEvents> {
|
|
57
|
-
const
|
|
58
|
-
const inner: web3.CompiledInnerInstruction[] = transactionResponse?.meta?.innerInstructions ?? []
|
|
59
|
+
const innerInstructions: web3.CompiledInnerInstruction[] = transactionResponse?.meta?.innerInstructions ?? []
|
|
59
60
|
|
|
60
61
|
const addressTableLookups = transactionResponse?.transaction.message.addressTableLookups
|
|
61
62
|
|
|
@@ -66,25 +67,23 @@ export class EventDecoder {
|
|
|
66
67
|
}),
|
|
67
68
|
)
|
|
68
69
|
|
|
69
|
-
|
|
70
|
-
for (let j = 0; j < inner[i].instructions.length; j++) {
|
|
71
|
-
const ix = inner[i].instructions[j]
|
|
70
|
+
const instructions: web3.CompiledInstruction[] = innerInstructions.map(({ instructions }) => instructions).flat()
|
|
72
71
|
|
|
72
|
+
const events: GenericEvent[] = instructions
|
|
73
|
+
.map((ix) => {
|
|
73
74
|
const accountKeys = transactionResponse?.transaction.message.getAccountKeys({
|
|
74
75
|
addressLookupTableAccounts: lookupTables.map((lookup) => lookup.value),
|
|
75
76
|
})
|
|
76
77
|
const programPubkey = accountKeys.get(ix.programIdIndex)
|
|
77
|
-
|
|
78
78
|
if (programPubkey === undefined || !programPubkey.equals(this.programId)) {
|
|
79
|
-
|
|
79
|
+
return null
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
const event = this.decode(ix.data)
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
}
|
|
83
|
+
|
|
84
|
+
return event || null
|
|
85
|
+
})
|
|
86
|
+
.filter((ev) => !!ev)
|
|
88
87
|
|
|
89
88
|
return categorizeEvents(events)
|
|
90
89
|
}
|
|
@@ -92,26 +91,26 @@ export class EventDecoder {
|
|
|
92
91
|
parseHeliusWebsocketCpiEvents(
|
|
93
92
|
transactionResponse: web3.VersionedTransactionResponse | web3.TransactionResponse,
|
|
94
93
|
): CategorizedEvents {
|
|
95
|
-
const events: GenericEvent[] = []
|
|
96
|
-
const
|
|
94
|
+
// const events: GenericEvent[] = []
|
|
95
|
+
const innerInstructions: web3.CompiledInnerInstruction[] = transactionResponse?.meta?.innerInstructions ?? []
|
|
97
96
|
|
|
98
|
-
|
|
99
|
-
for (let j = 0; j < inner[i].instructions.length; j++) {
|
|
100
|
-
const ix = inner[i].instructions[j]
|
|
97
|
+
const instructions: web3.CompiledInstruction[] = innerInstructions.map(({ instructions }) => instructions).flat()
|
|
101
98
|
|
|
102
|
-
|
|
103
|
-
|
|
99
|
+
const events: GenericEvent[] = instructions
|
|
100
|
+
.map((ix) => {
|
|
101
|
+
//! Is it safe? If helius adds a programId field, we need to use a modified CompiledInstruction interface
|
|
102
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
103
|
+
const programPubkey = new web3.PublicKey((ix as any).programId)
|
|
104
104
|
|
|
105
105
|
if (programPubkey === undefined || !programPubkey.equals(this.programId)) {
|
|
106
|
-
|
|
106
|
+
return null
|
|
107
107
|
}
|
|
108
108
|
|
|
109
109
|
const event = this.decode(ix.data)
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
}
|
|
110
|
+
|
|
111
|
+
return event || null
|
|
112
|
+
})
|
|
113
|
+
.filter((ev) => !!ev)
|
|
115
114
|
|
|
116
115
|
return categorizeEvents(events)
|
|
117
116
|
}
|
|
@@ -119,6 +118,7 @@ export class EventDecoder {
|
|
|
119
118
|
|
|
120
119
|
interface GenericEvent {
|
|
121
120
|
name: string
|
|
121
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
122
122
|
data: any
|
|
123
123
|
}
|
|
124
124
|
|
package/src/flavors.ts
CHANGED
|
@@ -1,39 +1,40 @@
|
|
|
1
1
|
import { BN, web3 } from "@coral-xyz/anchor"
|
|
2
|
+
|
|
2
3
|
import { ExponentFetcher, fetchKaminoReserve } from "@exponent-labs/exponent-fetcher"
|
|
3
|
-
import { Environment } from "./environment"
|
|
4
|
-
import { MfiSySdk } from "@exponent-labs/marginfi-sy-sdk"
|
|
5
|
-
import { KaminoSyPda } from "@exponent-labs/kamino-sy-pda"
|
|
6
|
-
import { PROGRAM_ID as KAMINO_STANDARD_PROGRAM_ID } from "@exponent-labs/kamino-sy-idl"
|
|
7
|
-
import { KlendSySdk } from "@exponent-labs/kamino-sy-sdk"
|
|
8
4
|
import {
|
|
9
|
-
FlavorKaminoStateJson,
|
|
10
|
-
FlavorKaminoState,
|
|
11
|
-
FlavorMarginfiState,
|
|
12
|
-
FlavorMarginfiStateJson,
|
|
13
|
-
FlavorKamino,
|
|
14
|
-
FlavorMarginfi,
|
|
15
|
-
FlavorDiscriminator,
|
|
16
|
-
MintSyArgs,
|
|
17
|
-
RedeemSyArgs,
|
|
18
|
-
Flavor,
|
|
19
5
|
CpiAccountsRaw,
|
|
6
|
+
Flavor,
|
|
7
|
+
FlavorDiscriminator,
|
|
8
|
+
FlavorGeneric,
|
|
9
|
+
FlavorGenericState,
|
|
10
|
+
FlavorGenericStateJson,
|
|
20
11
|
FlavorJitoRestaking,
|
|
21
12
|
FlavorJitoRestakingState,
|
|
22
13
|
FlavorJitoRestakingStateJson,
|
|
14
|
+
FlavorKamino,
|
|
15
|
+
FlavorKaminoState,
|
|
16
|
+
FlavorKaminoStateJson,
|
|
17
|
+
FlavorMarginfi,
|
|
18
|
+
FlavorMarginfiState,
|
|
19
|
+
FlavorMarginfiStateJson,
|
|
23
20
|
FlavorPerena,
|
|
24
21
|
FlavorPerenaState,
|
|
25
22
|
FlavorPerenaStateJson,
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
InterfaceInstructionArgs,
|
|
29
|
-
FlavorGenericStateJson,
|
|
30
|
-
toInterfaceTypeGenericJson,
|
|
23
|
+
MintSyArgs,
|
|
24
|
+
RedeemSyArgs,
|
|
31
25
|
fromInterfaceTypeGenericJson,
|
|
26
|
+
toInterfaceTypeGenericJson,
|
|
32
27
|
} from "@exponent-labs/exponent-types"
|
|
28
|
+
import { GenericSySdk } from "@exponent-labs/generic-sy-sdk"
|
|
33
29
|
import { JitoRestakingSySdk } from "@exponent-labs/jito-restaking-sy-sdk"
|
|
34
|
-
import {
|
|
30
|
+
import { PROGRAM_ID as KAMINO_STANDARD_PROGRAM_ID } from "@exponent-labs/kamino-sy-idl"
|
|
31
|
+
import { KaminoSyPda } from "@exponent-labs/kamino-sy-pda"
|
|
32
|
+
import { KlendSySdk } from "@exponent-labs/kamino-sy-sdk"
|
|
33
|
+
import { MfiSySdk } from "@exponent-labs/marginfi-sy-sdk"
|
|
35
34
|
import { PerenaSySdk } from "@exponent-labs/perena-sy-sdk"
|
|
36
|
-
import {
|
|
35
|
+
import { PreciseNumber } from "@exponent-labs/precise-number"
|
|
36
|
+
|
|
37
|
+
import { Environment } from "./environment"
|
|
37
38
|
|
|
38
39
|
export function serializeFlavorMarginfi(flavor: FlavorMarginfiState): FlavorMarginfiStateJson {
|
|
39
40
|
return {
|
|
@@ -303,7 +304,7 @@ export function deserializeFlavorJitoRestaking(flavor: FlavorJitoRestakingStateJ
|
|
|
303
304
|
}
|
|
304
305
|
}
|
|
305
306
|
|
|
306
|
-
export function
|
|
307
|
+
export function serializeFlavorPerena(flavor: FlavorPerenaState): FlavorPerenaStateJson {
|
|
307
308
|
return {
|
|
308
309
|
flavor: "perena",
|
|
309
310
|
currentSyExchangeRate: flavor.currentSyExchangeRate,
|
|
@@ -388,7 +389,7 @@ export function serializeFlavorGeneric(flavor: FlavorGenericState): FlavorGeneri
|
|
|
388
389
|
}
|
|
389
390
|
}
|
|
390
391
|
|
|
391
|
-
export function
|
|
392
|
+
export function deserializeFlavorPerena(flavor: FlavorPerenaStateJson): FlavorPerenaState {
|
|
392
393
|
return {
|
|
393
394
|
flavor: "perena",
|
|
394
395
|
currentSyExchangeRate: flavor.currentSyExchangeRate,
|
|
@@ -482,8 +483,8 @@ export function makeFlavorMarginfiSync(data: FlavorMarginfiState): FlavorMarginf
|
|
|
482
483
|
flavor: "marginfi",
|
|
483
484
|
ixMintSy: (args) => mfiSySdk.ixMintSy(args),
|
|
484
485
|
ixRedeemSy: (args) => mfiSySdk.ixRedeemSy(args),
|
|
485
|
-
preIxs: async (
|
|
486
|
-
postIxs: async (
|
|
486
|
+
preIxs: async () => [],
|
|
487
|
+
postIxs: async () => [],
|
|
487
488
|
}
|
|
488
489
|
}
|
|
489
490
|
|
|
@@ -508,8 +509,8 @@ export function makeFlavorJitoRestakingSync(data: FlavorJitoRestakingState): Fla
|
|
|
508
509
|
flavor: "jitoRestaking",
|
|
509
510
|
ixMintSy: (args) => jitoSdk.ixMintSy(args),
|
|
510
511
|
ixRedeemSy: (args) => jitoSdk.ixRedeemSy(args),
|
|
511
|
-
preIxs: async (
|
|
512
|
-
postIxs: async (
|
|
512
|
+
preIxs: async () => [],
|
|
513
|
+
postIxs: async () => [],
|
|
513
514
|
}
|
|
514
515
|
}
|
|
515
516
|
|
|
@@ -622,8 +623,8 @@ async function makeMarginfiFlavor(
|
|
|
622
623
|
mfiSyState: mfiSySdk.state,
|
|
623
624
|
emissionMint: mapOptAccount(emissionMint),
|
|
624
625
|
emissionTokenProgram: mapOptAccount(emissionTokenProgram),
|
|
625
|
-
preIxs: async (
|
|
626
|
-
postIxs: async (
|
|
626
|
+
preIxs: async () => [],
|
|
627
|
+
postIxs: async () => [],
|
|
627
628
|
}
|
|
628
629
|
}
|
|
629
630
|
|
|
@@ -632,9 +633,6 @@ async function makeKaminoFlavor(
|
|
|
632
633
|
kaminoSyProgramId: web3.PublicKey,
|
|
633
634
|
cpiAccounts: CpiAccountsRaw,
|
|
634
635
|
): Promise<FlavorKamino> {
|
|
635
|
-
const isNone = (x: web3.PublicKey) => x.equals(kaminoSyProgramId)
|
|
636
|
-
const mapOptAccount = (x: web3.PublicKey) => (isNone(x) ? undefined : x)
|
|
637
|
-
|
|
638
636
|
// TODO - I do not like hard-coding these magic numbers
|
|
639
637
|
const reserve = cpiAccounts.getSyState[0].pubkey
|
|
640
638
|
const reserveAcct = await fetchKaminoReserve(reserve, fetcher.connection)
|
|
@@ -685,7 +683,6 @@ async function makeJitoRestakingFlavor(
|
|
|
685
683
|
): Promise<FlavorJitoRestaking> {
|
|
686
684
|
// TODO - I do not like hard-coding these magic numbers
|
|
687
685
|
const jitoVault = cpiAccounts.getSyState[1].pubkey
|
|
688
|
-
// const recentBlockhash = await fetcher.connection.getLatestBlockhash().then((b) => b.blockhash)
|
|
689
686
|
|
|
690
687
|
const jitoSdk = await JitoRestakingSySdk.load({
|
|
691
688
|
cnx: fetcher.connection,
|
|
@@ -757,9 +754,6 @@ async function makeGenericFlavor(
|
|
|
757
754
|
config: syConfig,
|
|
758
755
|
})
|
|
759
756
|
|
|
760
|
-
let preIxs = []
|
|
761
|
-
let postIxs = []
|
|
762
|
-
|
|
763
757
|
return {
|
|
764
758
|
flavor: "generic",
|
|
765
759
|
currentSyExchangeRate: sySdk.state.syRate,
|
|
@@ -767,8 +761,8 @@ async function makeGenericFlavor(
|
|
|
767
761
|
baseTokenProgram: sySdk.tokenProgramBase,
|
|
768
762
|
ixMintSy: (args) => sySdk.ixMintSy(args),
|
|
769
763
|
ixRedeemSy: (args) => sySdk.ixRedeemSy(args),
|
|
770
|
-
preIxs: async () =>
|
|
771
|
-
postIxs: async () =>
|
|
764
|
+
preIxs: async () => [],
|
|
765
|
+
postIxs: async () => [],
|
|
772
766
|
genericSyState: sySdk.state,
|
|
773
767
|
emissions: sySdk.emissions.map((e) => ({
|
|
774
768
|
exchangeRate: e.index,
|
package/src/lpPosition.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { AnchorProvider, Program, web3 } from "@coral-xyz/anchor"
|
|
2
|
-
|
|
3
|
-
import { Environment } from "./environment"
|
|
4
|
-
import { ExponentCore, IDL } from "@exponent-labs/exponent-idl"
|
|
2
|
+
|
|
5
3
|
import { ExponentFetcher } from "@exponent-labs/exponent-fetcher"
|
|
6
|
-
import {
|
|
7
|
-
import { makeSyPosition } from "./syPosition"
|
|
4
|
+
import { ExponentCore, IDL } from "@exponent-labs/exponent-idl"
|
|
8
5
|
import { ExponentPDA } from "@exponent-labs/exponent-pda"
|
|
6
|
+
import { PreciseNumber } from "@exponent-labs/precise-number"
|
|
7
|
+
|
|
8
|
+
import { Environment } from "./environment"
|
|
9
|
+
import { Market, MyWallet } from "./market"
|
|
9
10
|
|
|
10
11
|
export interface EmissionTracker {
|
|
11
12
|
lastSeenIndex: number
|
|
@@ -35,17 +36,17 @@ export class LpPosition {
|
|
|
35
36
|
|
|
36
37
|
static async load(env: Environment, connection: web3.Connection, address: web3.PublicKey, market?: Market) {
|
|
37
38
|
const fetcher = new ExponentFetcher({ connection })
|
|
38
|
-
const
|
|
39
|
-
const loadedMarket = market ?? (await Market.load(env, connection,
|
|
40
|
-
const
|
|
41
|
-
owner:
|
|
42
|
-
balanceLp:
|
|
39
|
+
const fetchedLpPosition = await fetcher.fetchLpPosition(address)
|
|
40
|
+
const loadedMarket = market ?? (await Market.load(env, connection, fetchedLpPosition.market))
|
|
41
|
+
const lpPositionState: LpPositionState = {
|
|
42
|
+
owner: fetchedLpPosition.owner,
|
|
43
|
+
balanceLp: fetchedLpPosition.lpBalance,
|
|
43
44
|
market: loadedMarket,
|
|
44
|
-
emissionTrackers:
|
|
45
|
-
farms:
|
|
45
|
+
emissionTrackers: fetchedLpPosition.emissions,
|
|
46
|
+
farms: fetchedLpPosition.farms,
|
|
46
47
|
fetcher: fetcher,
|
|
47
48
|
}
|
|
48
|
-
return new LpPosition(env, connection, address,
|
|
49
|
+
return new LpPosition(env, connection, address, lpPositionState)
|
|
49
50
|
}
|
|
50
51
|
|
|
51
52
|
static async loadAllByOwner(
|
|
@@ -65,7 +66,7 @@ export class LpPosition {
|
|
|
65
66
|
const positionPromises = addresses.map((addr) => {
|
|
66
67
|
try {
|
|
67
68
|
return LpPosition.load(env, connection, addr)
|
|
68
|
-
} catch
|
|
69
|
+
} catch {
|
|
69
70
|
return Promise.resolve(null)
|
|
70
71
|
}
|
|
71
72
|
})
|
|
@@ -74,10 +75,10 @@ export class LpPosition {
|
|
|
74
75
|
return positions.filter((p): p is LpPosition => p !== null)
|
|
75
76
|
}
|
|
76
77
|
|
|
77
|
-
async reload(
|
|
78
|
-
const
|
|
79
|
-
this.state =
|
|
80
|
-
return
|
|
78
|
+
async reload(connection = this.connection) {
|
|
79
|
+
const lpPosition = await LpPosition.load(this.env, connection, this.selfAddress)
|
|
80
|
+
this.state = lpPosition.state
|
|
81
|
+
return lpPosition
|
|
81
82
|
}
|
|
82
83
|
|
|
83
84
|
get lpBalance() {
|
|
@@ -119,9 +120,9 @@ export class LpPosition {
|
|
|
119
120
|
const marketFarm = this.state.market.state.lpFarm.farmEmissions[marketFarmIndex]
|
|
120
121
|
const positionFarm = this.state.farms[marketFarmIndex]
|
|
121
122
|
|
|
122
|
-
const
|
|
123
|
+
const timeNow = new Date().getTime() / 1000
|
|
123
124
|
|
|
124
|
-
const deltaTimeFarm =
|
|
125
|
+
const deltaTimeFarm = timeNow - this.marketSdk.state.lpFarm.lastSeenTimestamp
|
|
125
126
|
const lpStakedTotal = this.marketSdk.state.lpEscrowAmount
|
|
126
127
|
const tokensEmitted = Number(marketFarm.tokenRate) * deltaTimeFarm
|
|
127
128
|
const increaseAmount = Number(tokensEmitted) / Number(lpStakedTotal)
|
|
@@ -137,7 +138,7 @@ export class LpPosition {
|
|
|
137
138
|
async getClaimableMarketEmissions() {
|
|
138
139
|
const syPosition = this.marketSdk.state.syPosition
|
|
139
140
|
|
|
140
|
-
return this.state.market.emissions.map((
|
|
141
|
+
return this.state.market.emissions.map((_, index) => {
|
|
141
142
|
const marketEmission = this.state.market.marketEmissions.trackers[index]
|
|
142
143
|
const positionEmission = this.state.emissionTrackers[index]
|
|
143
144
|
if (!positionEmission) return 0
|