@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.
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
package/src/vault.ts CHANGED
@@ -15,6 +15,7 @@ import {
15
15
  VaultEmission,
16
16
  VaultJson,
17
17
  VaultState,
18
+ VaultStateJson,
18
19
  deserializeCpiAccountsRaw,
19
20
  serializeCpiAccountsRaw,
20
21
  } from "@exponent-labs/exponent-types"
@@ -99,14 +100,14 @@ export class Vault {
99
100
  public env: Environment,
100
101
  public connection: web3.Connection,
101
102
  ) {
102
- this.coreProgram = new Program<ExponentCore>(
103
+ this.coreProgram = new Program(
103
104
  IDL as ExponentCore,
104
105
  new AnchorProvider(connection, new MyWallet(web3.Keypair.generate())),
105
106
  )
106
107
  }
107
108
 
108
109
  static loadFromJson(vaultJson: VaultJson, address: web3.PublicKey, env: Environment, connection: web3.Connection) {
109
- const state = toVaultState(vaultJson)
110
+ const state = deserializeVaultState(vaultJson.state)
110
111
  return new Vault(state, address, env, connection)
111
112
  }
112
113
 
@@ -169,6 +170,7 @@ export class Vault {
169
170
  return {
170
171
  startTs: this.state.startTs,
171
172
  duration: this.state.durationSeconds,
173
+ state: serializeVaultState(this.state),
172
174
  authority: this.state.authority.toString(),
173
175
  syBalance: this.syBalance.toString(),
174
176
  addressLookupTable: this.state.addressLookupTable.toString(),
@@ -197,22 +199,7 @@ export class Vault {
197
199
  // Misspelled word
198
200
  yieldPositonAddress: this.state.yieldPositonAddress.toString(),
199
201
  interestFeeBps: this.state.interestBpsFee,
200
- flavor: (() => {
201
- switch (this.state.flavor.flavor) {
202
- case "marginfi":
203
- return serializeFlavorMarginfi(this.state.flavor)
204
- case "kamino":
205
- return serializeFlavorKamino(this.state.flavor)
206
- case "jitoRestaking":
207
- return serializeFlavorJitoRestaking(this.state.flavor)
208
- case "perena":
209
- return serializeFlavorPerena(this.state.flavor)
210
- case "generic":
211
- return serializeFlavorGeneric(this.state.flavor)
212
- default:
213
- throw new Error(`Unknown flavor: ${this.state.flavor}`)
214
- }
215
- })(),
202
+ flavor: serializeFlavorState(this.state.flavor),
216
203
  emissions: this.state.emissions.map((e) => serializeEmission(e)),
217
204
  maxPySupply: this.state.maxPySupply.toString(),
218
205
  }
@@ -477,7 +464,7 @@ export class Vault {
477
464
  const redeemSyIx = await this.state.flavor.ixRedeemSy({
478
465
  amountSy: "0",
479
466
  redeemer: owner,
480
- redeemerBaseTokenAccount: baseDst,
467
+ redeemerBaseTokenAccount: baseDst || web3.PublicKey.default,
481
468
  redeemerSyTokenAccount: syDst,
482
469
  })
483
470
 
@@ -872,50 +859,101 @@ export class Vault {
872
859
  }
873
860
  }
874
861
 
875
- function toVaultState(vault: VaultJson): VaultState {
862
+ function serializeFlavorState(flavor: VaultState["flavor"]): VaultStateJson["flavor"] {
863
+ switch (flavor.flavor) {
864
+ case "marginfi":
865
+ return serializeFlavorMarginfi(flavor)
866
+ case "kamino":
867
+ return serializeFlavorKamino(flavor)
868
+ case "jitoRestaking":
869
+ return serializeFlavorJitoRestaking(flavor)
870
+ case "perena":
871
+ return serializeFlavorPerena(flavor)
872
+ case "generic":
873
+ return serializeFlavorGeneric(flavor)
874
+ default:
875
+ throw new Error(`Unknown flavor: ${flavor}`)
876
+ }
877
+ }
878
+
879
+ function deserializeFlavorState(flavor: VaultStateJson["flavor"]): VaultState["flavor"] {
880
+ switch (flavor.flavor) {
881
+ case "marginfi":
882
+ return makeFlavorMarginfiSync(deserializeFlavorMarginfi(flavor))
883
+ case "kamino":
884
+ return makeFlavorKaminoSync(deserializeFlavorKamino(flavor))
885
+ case "jitoRestaking":
886
+ return makeFlavorJitoRestakingSync(deserializeFlavorJitoRestaking(flavor))
887
+ case "perena":
888
+ return makeFlavorPerenaSync(deserializeFlavorPerena(flavor))
889
+ case "generic":
890
+ return makeFlavorGenericSync(deserializeFlavorGeneric(flavor))
891
+ default:
892
+ throw new Error(`Unknown flavor: ${flavor}`)
893
+ }
894
+ }
895
+
896
+ export function serializeVaultState(state: VaultState): VaultStateJson {
876
897
  return {
877
- ptSupply: BigInt(vault.ptSupply),
878
- syForPt: BigInt(vault.syForPt),
879
- startTs: vault.startTs,
880
- durationSeconds: vault.duration,
881
- authority: new web3.PublicKey(vault.authority),
882
- addressLookupTable: new web3.PublicKey(vault.addressLookupTable),
883
- lastSeenSyExchangeRate: vault.lastSeenSyExchangeRate,
884
- allTimeHighSyExchangeRate: vault.allTimeHighSyExchangeRate,
885
- escrowSy: new web3.PublicKey(vault.escrowSy),
886
- escrowYt: new web3.PublicKey(vault.escrowYt),
887
- syProgram: new web3.PublicKey(vault.syProgram),
888
- syPosition: deserializeSyPosition(vault.syPosition),
889
- mintPt: new web3.PublicKey(vault.mintPt),
890
- status: vault.status,
891
- minOperationSizeStrip: BigInt(vault.minOperationSizeStrip),
892
- minOperationSizeMerge: BigInt(vault.minOperationSizeMerge),
893
- mintYt: new web3.PublicKey(vault.mintYt),
894
- mintSy: new web3.PublicKey(vault.mintSy),
895
- yieldPositonAddress: new web3.PublicKey(vault.yieldPositonAddress),
896
- treasurySyTokenAccount: new web3.PublicKey(vault.treasurySyTokenAccount),
897
- interestBpsFee: vault.interestFeeBps,
898
- totalSyInEscrow: BigInt(vault.totalSyInEscrow),
899
- cpiAccounts: deserializeCpiAccountsRaw(vault.cpiAccounts),
900
- finalSyExchangeRate: vault.finalSyExchangeRate,
901
- flavor: (() => {
902
- switch (vault.flavor.flavor) {
903
- case "marginfi":
904
- return makeFlavorMarginfiSync(deserializeFlavorMarginfi(vault.flavor))
905
- case "kamino":
906
- return makeFlavorKaminoSync(deserializeFlavorKamino(vault.flavor))
907
- case "jitoRestaking":
908
- return makeFlavorJitoRestakingSync(deserializeFlavorJitoRestaking(vault.flavor))
909
- case "perena":
910
- return makeFlavorPerenaSync(deserializeFlavorPerena(vault.flavor))
911
- case "generic":
912
- return makeFlavorGenericSync(deserializeFlavorGeneric(vault.flavor))
913
- default:
914
- throw new Error(`Unknown flavor: ${vault.flavor}`)
915
- }
916
- })(),
917
- emissions: vault.emissions.map((e) => deserializeEmission(e)),
918
- maxPySupply: BigInt(vault.maxPySupply),
898
+ ptSupply: state.ptSupply.toString(),
899
+ syForPt: state.syForPt.toString(),
900
+ startTs: state.startTs,
901
+ durationSeconds: state.durationSeconds,
902
+ authority: state.authority.toString(),
903
+ addressLookupTable: state.addressLookupTable.toString(),
904
+ lastSeenSyExchangeRate: state.lastSeenSyExchangeRate,
905
+ allTimeHighSyExchangeRate: state.allTimeHighSyExchangeRate,
906
+ escrowSy: state.escrowSy.toString(),
907
+ escrowYt: state.escrowYt.toString(),
908
+ syProgram: state.syProgram.toString(),
909
+ syPosition: serializeSyPosition(state.syPosition),
910
+ cpiAccounts: serializeCpiAccountsRaw(state.cpiAccounts),
911
+ mintPt: state.mintPt.toString(),
912
+ status: state.status,
913
+ minOperationSizeStrip: state.minOperationSizeStrip.toString(),
914
+ minOperationSizeMerge: state.minOperationSizeMerge.toString(),
915
+ mintYt: state.mintYt.toString(),
916
+ mintSy: state.mintSy.toString(),
917
+ yieldPositonAddress: state.yieldPositonAddress.toString(),
918
+ treasurySyTokenAccount: state.treasurySyTokenAccount.toString(),
919
+ interestBpsFee: state.interestBpsFee,
920
+ totalSyInEscrow: state.totalSyInEscrow.toString(),
921
+ flavor: serializeFlavorState(state.flavor),
922
+ finalSyExchangeRate: state.finalSyExchangeRate,
923
+ emissions: state.emissions.map((emission) => serializeEmission(emission)),
924
+ maxPySupply: state.maxPySupply.toString(),
925
+ }
926
+ }
927
+
928
+ export function deserializeVaultState(state: VaultStateJson): VaultState {
929
+ return {
930
+ ptSupply: BigInt(state.ptSupply),
931
+ syForPt: BigInt(state.syForPt),
932
+ startTs: state.startTs,
933
+ durationSeconds: state.durationSeconds,
934
+ authority: new web3.PublicKey(state.authority),
935
+ addressLookupTable: new web3.PublicKey(state.addressLookupTable),
936
+ lastSeenSyExchangeRate: state.lastSeenSyExchangeRate,
937
+ allTimeHighSyExchangeRate: state.allTimeHighSyExchangeRate,
938
+ escrowSy: new web3.PublicKey(state.escrowSy),
939
+ escrowYt: new web3.PublicKey(state.escrowYt),
940
+ syProgram: new web3.PublicKey(state.syProgram),
941
+ syPosition: deserializeSyPosition(state.syPosition),
942
+ cpiAccounts: deserializeCpiAccountsRaw(state.cpiAccounts),
943
+ mintPt: new web3.PublicKey(state.mintPt),
944
+ status: state.status,
945
+ minOperationSizeStrip: BigInt(state.minOperationSizeStrip),
946
+ minOperationSizeMerge: BigInt(state.minOperationSizeMerge),
947
+ mintYt: new web3.PublicKey(state.mintYt),
948
+ mintSy: new web3.PublicKey(state.mintSy),
949
+ yieldPositonAddress: new web3.PublicKey(state.yieldPositonAddress),
950
+ treasurySyTokenAccount: new web3.PublicKey(state.treasurySyTokenAccount),
951
+ interestBpsFee: state.interestBpsFee,
952
+ totalSyInEscrow: BigInt(state.totalSyInEscrow),
953
+ flavor: deserializeFlavorState(state.flavor),
954
+ finalSyExchangeRate: state.finalSyExchangeRate,
955
+ emissions: state.emissions.map((emission) => deserializeEmission(emission)),
956
+ maxPySupply: BigInt(state.maxPySupply),
919
957
  }
920
958
  }
921
959
 
package/src/ytPosition.ts CHANGED
@@ -23,7 +23,7 @@ export class YtPosition {
23
23
  public vault: Vault,
24
24
  ) {
25
25
  const mockWallet = new MyWallet(web3.Keypair.generate())
26
- this.coreProgram = new Program<ExponentCore>(IDL as ExponentCore, new AnchorProvider(connection, mockWallet))
26
+ this.coreProgram = new Program(IDL as ExponentCore, new AnchorProvider(connection, mockWallet))
27
27
  }
28
28
 
29
29
  /** Loads the YT position by address, and loads Vault, too */
@@ -59,6 +59,32 @@ export class YtPosition {
59
59
  return Promise.all(addresses.map((addr) => YtPosition.load(env, connection, addr)))
60
60
  }
61
61
 
62
+ /** Batch load multiple YT positions in a single RPC call */
63
+ static async loadBatch(
64
+ env: Environment,
65
+ connection: web3.Connection,
66
+ owner: web3.PublicKey,
67
+ vaults: Vault[],
68
+ ): Promise<(YtPosition | null)[]> {
69
+ if (vaults.length === 0) return []
70
+
71
+ const pda = new ExponentPDA(env.coreProgramId)
72
+ const addresses = vaults.map((vault) =>
73
+ pda.yieldPosition({
74
+ vault: vault.selfAddress,
75
+ owner,
76
+ }),
77
+ )
78
+
79
+ const fetcher = new ExponentFetcher({ connection })
80
+ const positions = await fetcher.fetchYtPositions(addresses)
81
+
82
+ return positions.map((state, index) => {
83
+ if (!state) return null
84
+ return new YtPosition(env, connection, addresses[index], state, vaults[index])
85
+ })
86
+ }
87
+
62
88
  async reload(connection = this.connection) {
63
89
  const x = await YtPosition.load(this.env, connection, this.selfAddress)
64
90
  this.state = x.state
@@ -112,19 +138,14 @@ export class YtPosition {
112
138
  return this.vault.syEmissions.map((e, index) => {
113
139
  // Use initial index if user doesn't have a position yet (lastSeenIndex is 0)
114
140
  const lastSeenIndex = this.state.emissions[index]?.lastSeenIndex ?? 0
115
- console.log("lastSeenIndex: ", lastSeenIndex)
141
+ // console.log("lastSeenIndex: ", lastSeenIndex)
116
142
 
117
143
  // use finalIndex from the vault's emissions
118
144
  const finalIndex = Number(
119
145
  PreciseNumber.fromRaw(this.vault.state.emissions[index]?.finalIndex?.[0] || e.index[0]).valueString,
120
146
  )
121
147
 
122
- console.log("finalIndex: ", finalIndex)
123
- console.log("totalSyBalance: ", totalSyBalance)
124
- console.log("finalIndex - lastSeenIndex: ", finalIndex - Number(lastSeenIndex))
125
-
126
148
  const earnedEmission = totalSyBalance * (finalIndex - Number(lastSeenIndex))
127
- console.log("earnedEmission: ", earnedEmission)
128
149
 
129
150
  return {
130
151
  mint: e.mint,
package/tsconfig.json CHANGED
@@ -23,6 +23,7 @@
23
23
  { "path": "../market-math" },
24
24
  { "path": "../marginfi-util" },
25
25
  { "path": "../exponent-pda" },
26
+ { "path": "../exponent-clmm-pda" },
26
27
  { "path": "../marginfi-sy-sdk" },
27
28
  { "path": "../kamino-sy-sdk" },
28
29
  { "path": "../exponent-types" },
@@ -33,6 +34,8 @@
33
34
  { "path": "../perena-sy-pda" },
34
35
  { "path": "../generic-sy-sdk" },
35
36
  { "path": "../generic-sy-idl" },
36
- { "path": "../generic-sy-pda" }
37
+ { "path": "../generic-sy-pda" },
38
+ { "path": "../exponent-orderbook-idl" },
39
+ { "path": "../market-three-math" }
37
40
  ]
38
41
  }