@zofai/zo-sdk 0.2.29 → 0.2.31

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 (103) hide show
  1. package/dist/abstract/BaseAPI.cjs +3 -0
  2. package/dist/abstract/BaseAPI.cjs.map +1 -1
  3. package/dist/abstract/BaseAPI.d.cts +3 -2
  4. package/dist/abstract/BaseAPI.d.cts.map +1 -1
  5. package/dist/abstract/BaseAPI.d.mts +3 -2
  6. package/dist/abstract/BaseAPI.d.mts.map +1 -1
  7. package/dist/abstract/BaseAPI.mjs +3 -0
  8. package/dist/abstract/BaseAPI.mjs.map +1 -1
  9. package/dist/abstract/BaseDataAPI.cjs +48 -2
  10. package/dist/abstract/BaseDataAPI.cjs.map +1 -1
  11. package/dist/abstract/BaseDataAPI.d.cts +15 -2
  12. package/dist/abstract/BaseDataAPI.d.cts.map +1 -1
  13. package/dist/abstract/BaseDataAPI.d.mts +15 -2
  14. package/dist/abstract/BaseDataAPI.d.mts.map +1 -1
  15. package/dist/abstract/BaseDataAPI.mjs +48 -2
  16. package/dist/abstract/BaseDataAPI.mjs.map +1 -1
  17. package/dist/consts/deployments-zlp-mainnet.json +1 -1
  18. package/dist/consts/deployments-zo-oracle-mainnet.json +18 -1
  19. package/dist/data.cjs +19 -7
  20. package/dist/data.cjs.map +1 -1
  21. package/dist/data.d.cts.map +1 -1
  22. package/dist/data.d.mts.map +1 -1
  23. package/dist/data.mjs +19 -7
  24. package/dist/data.mjs.map +1 -1
  25. package/dist/implementations/SLPDataAPI.cjs +26 -8
  26. package/dist/implementations/SLPDataAPI.cjs.map +1 -1
  27. package/dist/implementations/SLPDataAPI.d.cts.map +1 -1
  28. package/dist/implementations/SLPDataAPI.d.mts.map +1 -1
  29. package/dist/implementations/SLPDataAPI.mjs +26 -8
  30. package/dist/implementations/SLPDataAPI.mjs.map +1 -1
  31. package/dist/implementations/USDZDataAPI.cjs +26 -8
  32. package/dist/implementations/USDZDataAPI.cjs.map +1 -1
  33. package/dist/implementations/USDZDataAPI.d.cts.map +1 -1
  34. package/dist/implementations/USDZDataAPI.d.mts.map +1 -1
  35. package/dist/implementations/USDZDataAPI.mjs +26 -8
  36. package/dist/implementations/USDZDataAPI.mjs.map +1 -1
  37. package/dist/implementations/ZBTCVCDataAPI.cjs +19 -7
  38. package/dist/implementations/ZBTCVCDataAPI.cjs.map +1 -1
  39. package/dist/implementations/ZBTCVCDataAPI.d.cts.map +1 -1
  40. package/dist/implementations/ZBTCVCDataAPI.d.mts.map +1 -1
  41. package/dist/implementations/ZBTCVCDataAPI.mjs +19 -7
  42. package/dist/implementations/ZBTCVCDataAPI.mjs.map +1 -1
  43. package/dist/implementations/ZLPAPI.cjs +456 -123
  44. package/dist/implementations/ZLPAPI.cjs.map +1 -1
  45. package/dist/implementations/ZLPAPI.d.cts +46 -1
  46. package/dist/implementations/ZLPAPI.d.cts.map +1 -1
  47. package/dist/implementations/ZLPAPI.d.mts +46 -1
  48. package/dist/implementations/ZLPAPI.d.mts.map +1 -1
  49. package/dist/implementations/ZLPAPI.mjs +457 -124
  50. package/dist/implementations/ZLPAPI.mjs.map +1 -1
  51. package/dist/implementations/ZLPDataAPI.cjs +64 -8
  52. package/dist/implementations/ZLPDataAPI.cjs.map +1 -1
  53. package/dist/implementations/ZLPDataAPI.d.cts +16 -0
  54. package/dist/implementations/ZLPDataAPI.d.cts.map +1 -1
  55. package/dist/implementations/ZLPDataAPI.d.mts +16 -0
  56. package/dist/implementations/ZLPDataAPI.d.mts.map +1 -1
  57. package/dist/implementations/ZLPDataAPI.mjs +64 -8
  58. package/dist/implementations/ZLPDataAPI.mjs.map +1 -1
  59. package/dist/index.cjs +1 -0
  60. package/dist/index.cjs.map +1 -1
  61. package/dist/index.d.cts +2 -0
  62. package/dist/index.d.cts.map +1 -1
  63. package/dist/index.d.mts +2 -0
  64. package/dist/index.d.mts.map +1 -1
  65. package/dist/index.mjs +1 -0
  66. package/dist/index.mjs.map +1 -1
  67. package/dist/interfaces/base.d.cts +14 -1
  68. package/dist/interfaces/base.d.cts.map +1 -1
  69. package/dist/interfaces/base.d.mts +14 -1
  70. package/dist/interfaces/base.d.mts.map +1 -1
  71. package/dist/interfaces/zlp.d.cts +19 -0
  72. package/dist/interfaces/zlp.d.cts.map +1 -1
  73. package/dist/interfaces/zlp.d.mts +19 -0
  74. package/dist/interfaces/zlp.d.mts.map +1 -1
  75. package/dist/traderPositions.cjs +130 -0
  76. package/dist/traderPositions.cjs.map +1 -0
  77. package/dist/traderPositions.d.cts +72 -0
  78. package/dist/traderPositions.d.cts.map +1 -0
  79. package/dist/traderPositions.d.mts +72 -0
  80. package/dist/traderPositions.d.mts.map +1 -0
  81. package/dist/traderPositions.mjs +124 -0
  82. package/dist/traderPositions.mjs.map +1 -0
  83. package/docs/api-reference.md +45 -0
  84. package/docs/architecture.md +21 -3
  85. package/docs/common-operations.md +56 -8
  86. package/docs/getting-started.md +8 -3
  87. package/docs/lp-specific-features.md +89 -12
  88. package/docs/swap-integration.md +52 -20
  89. package/package.json +1 -1
  90. package/src/abstract/BaseAPI.ts +6 -2
  91. package/src/abstract/BaseDataAPI.ts +67 -3
  92. package/src/consts/deployments-zlp-mainnet.json +1 -1
  93. package/src/consts/deployments-zo-oracle-mainnet.json +18 -1
  94. package/src/data.ts +19 -7
  95. package/src/implementations/SLPDataAPI.ts +34 -16
  96. package/src/implementations/USDZDataAPI.ts +32 -14
  97. package/src/implementations/ZBTCVCDataAPI.ts +23 -11
  98. package/src/implementations/ZLPAPI.ts +760 -151
  99. package/src/implementations/ZLPDataAPI.ts +79 -14
  100. package/src/index.ts +2 -0
  101. package/src/interfaces/base.ts +15 -1
  102. package/src/interfaces/zlp.ts +170 -0
  103. package/src/traderPositions.ts +218 -0
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@zofai/zo-sdk",
3
3
  "type": "module",
4
- "version": "0.2.29",
4
+ "version": "0.2.31",
5
5
  "author": "zo",
6
6
  "exports": {
7
7
  ".": {
@@ -6,12 +6,10 @@
6
6
 
7
7
  import type { KioskClient, KioskOwnerCap } from '@mysten/kiosk'
8
8
  import type { TransactionObjectArgument } from '@mysten/sui/transactions'
9
- import type { SuiClient } from '../suiClient'
10
9
  import { Transaction } from '@mysten/sui/transactions'
11
10
  import { SUI_CLOCK_OBJECT_ID, SUI_TYPE_ARG } from '@mysten/sui/utils'
12
11
 
13
12
  import { createSpendableCoin, getCoinBalanceBreakdown, needsCoinWithBalanceFunding } from '../coins'
14
-
15
13
  import type { IConsts, LPToken, Network } from '../consts'
16
14
  import { ALLOW_TRADE_CAN_TRADE, ALLOW_TRADE_MUST_TRADE, ALLOW_TRADE_NO_TRADE } from '../consts'
17
15
  import type {
@@ -31,7 +29,9 @@ import type {
31
29
  IBaseStakePool,
32
30
  IBaseSymbolInfo,
33
31
  IBaseVaultInfo,
32
+ IGetTraderOpenPositionsOptions,
34
33
  } from '../interfaces'
34
+ import type { SuiClient } from '../suiClient'
35
35
  import { BaseDataAPI } from './BaseDataAPI'
36
36
 
37
37
  export abstract class BaseAPI extends BaseDataAPI implements IBaseAPI {
@@ -110,6 +110,10 @@ export abstract class BaseAPI extends BaseDataAPI implements IBaseAPI {
110
110
  return this.dataAPI.getPositionInfoList(positionCapInfoList, owner, batchSize)
111
111
  }
112
112
 
113
+ public getTraderOpenPositionInfoList(owner: string, options?: IGetTraderOpenPositionsOptions): Promise<IBasePositionInfo[]> {
114
+ return this.dataAPI.getTraderOpenPositionInfoList(owner, options)
115
+ }
116
+
113
117
  public getOrderCapInfoList(owner: string): Promise<IBaseOrderCapInfo[]> {
114
118
  return this.dataAPI.getOrderCapInfoList(owner)
115
119
  }
@@ -4,10 +4,10 @@
4
4
  */
5
5
  /* eslint-disable class-methods-use-this -- base class helpers used by subclasses */
6
6
 
7
- import type { SuiClient } from '../suiClient'
8
7
  import type { Transaction } from '@mysten/sui/transactions'
9
8
 
10
9
  import type { LPToken, Network } from '../consts'
10
+ import { LPToken as LPTokenEnum } from '../consts'
11
11
  import type {
12
12
  IBaseDataAPI,
13
13
  IBaseHistoryResponse,
@@ -23,8 +23,15 @@ import type {
23
23
  IBaseStakePool,
24
24
  IBaseSymbolInfo,
25
25
  IBaseVaultInfo,
26
+ IGetTraderOpenPositionsOptions,
26
27
  } from '../interfaces'
27
28
  import { OracleAPI } from '../oracle'
29
+ import type { SuiClient } from '../suiClient'
30
+ import {
31
+ fetchAllTraderPositions,
32
+ traderPositionToCapInfo,
33
+ } from '../traderPositions'
34
+ import { joinSymbol } from '../utils'
28
35
 
29
36
  export abstract class BaseDataAPI extends OracleAPI implements IBaseDataAPI {
30
37
  public readonly provider: SuiClient
@@ -38,6 +45,8 @@ export abstract class BaseDataAPI extends OracleAPI implements IBaseDataAPI {
38
45
  protected marketInfoCache: IBaseMarketInfo | null = null
39
46
  protected positionConfigCache: Record<string, IBasePositionConfig> = {}
40
47
  protected rebaseFeeModelCache: IBaseRebaseFeeModel | null = null
48
+ /** Per index-token OI funding state; `null` means fetched and missing on-chain. */
49
+ protected oiFundingStateCache: Record<string, unknown | null> = {}
41
50
  protected lastUpdate = 0
42
51
  protected readonly CACHE_DURATION = 1000 * 60 * 3 // 3 minutes
43
52
 
@@ -75,6 +84,7 @@ export abstract class BaseDataAPI extends OracleAPI implements IBaseDataAPI {
75
84
  this.marketInfoCache = null
76
85
  this.positionConfigCache = {}
77
86
  this.rebaseFeeModelCache = null
87
+ this.oiFundingStateCache = {}
78
88
  }
79
89
 
80
90
  /**
@@ -93,7 +103,7 @@ export abstract class BaseDataAPI extends OracleAPI implements IBaseDataAPI {
93
103
  */
94
104
  protected async getCachedSymbolInfo(tokenId: string, long: boolean): Promise<IBaseSymbolInfo> {
95
105
  this.validateCache()
96
- const key = `${tokenId}_${long}`
106
+ const key = joinSymbol(long ? 'long' : 'short', tokenId)
97
107
  if (!this.symbolInfoCache[key]) {
98
108
  this.symbolInfoCache[key] = await this.getSymbolInfo(tokenId, long)
99
109
  }
@@ -116,7 +126,7 @@ export abstract class BaseDataAPI extends OracleAPI implements IBaseDataAPI {
116
126
  */
117
127
  protected async getCachedPositionConfig(indexToken: string, long: boolean): Promise<IBasePositionConfig> {
118
128
  this.validateCache()
119
- const key = `${indexToken}_${long}`
129
+ const key = joinSymbol(long ? 'long' : 'short', indexToken)
120
130
  if (!this.positionConfigCache[key]) {
121
131
  this.positionConfigCache[key] = await this.getPositionConfig(indexToken, long)
122
132
  }
@@ -134,6 +144,60 @@ export abstract class BaseDataAPI extends OracleAPI implements IBaseDataAPI {
134
144
  return this.rebaseFeeModelCache
135
145
  }
136
146
 
147
+ /** Package id used to filter `/trader-positions` rows for this LP. */
148
+ protected getMarketPackageId(): string {
149
+ return this.lpToken === LPTokenEnum.SLP
150
+ ? this.consts.sudoCore.package
151
+ : this.consts.zoCore.package
152
+ }
153
+
154
+ /**
155
+ * Load OPEN position caps from zo API `/trader-positions` for this LP package.
156
+ */
157
+ protected async fetchTraderOpenPositionCapsFromApi(
158
+ owner: string,
159
+ options?: IGetTraderOpenPositionsOptions,
160
+ ): Promise<IBasePositionCapInfo[]> {
161
+ const packageId = this.getMarketPackageId().toLowerCase()
162
+ const records = await fetchAllTraderPositions(
163
+ this.apiEndpoint,
164
+ {
165
+ owner,
166
+ status: 'OPEN',
167
+ indexToken: options?.indexToken,
168
+ collateralToken: options?.collateralToken,
169
+ },
170
+ options?.limit,
171
+ )
172
+
173
+ const caps: IBasePositionCapInfo[] = []
174
+ for (const record of records) {
175
+ if (record.address?.trim().toLowerCase() !== packageId) {
176
+ continue
177
+ }
178
+ const cap = traderPositionToCapInfo(record, this.consts)
179
+ if (cap) {
180
+ caps.push(cap)
181
+ }
182
+ }
183
+ return caps
184
+ }
185
+
186
+ /**
187
+ * Fetch OPEN positions from zo API, then hydrate live position details via RPC
188
+ * (same accrual path as getPositionInfoList).
189
+ */
190
+ public async getTraderOpenPositionInfoList(
191
+ owner: string,
192
+ options?: IGetTraderOpenPositionsOptions,
193
+ ): Promise<IBasePositionInfo[]> {
194
+ const caps = await this.fetchTraderOpenPositionCapsFromApi(owner, options)
195
+ if (caps.length === 0) {
196
+ return []
197
+ }
198
+ return this.getPositionInfoList(caps, owner, options?.batchSize)
199
+ }
200
+
137
201
  // Abstract methods that must be implemented by concrete classes
138
202
  public abstract valuateVaults(tx: Transaction): any
139
203
  public abstract valuateSymbols(tx: Transaction): any
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "zo_core": {
3
3
  "package": "0xf7fade57462e56e2eff1d7adef32e4fd285b21fd81f983f407bb7110ca766cda",
4
- "upgraded_package": "0x0c846bf17f6870e1ee87f0dbad62d58c6e33b7a9a23f227fc632fccc6183786e",
4
+ "upgraded_package": "0x79e9959dd864f2f073caf507228afaaccfb09276ced183dd15abcdf8375d4a88",
5
5
  "upgrade_cap": "0xcc96f78abe0b868afaab05954ccc6357afb5d379a21389d6eb88dda8b1f60052",
6
6
  "admin_cap": "0xbc10f6481f2b5d6185c73ee680aa90a60fe3854d9f638e00f7e00d3b6a88e2b8",
7
7
  "market": "0x35c667bd8c401036103992791a924f31df0d104256a9e2313acee5b1bcf05b7e",
@@ -33,7 +33,24 @@
33
33
  "eba0732395fae9dec4bae12e52760b35fc1c5671e2da8b449c9af4efe5d54341": 624,
34
34
  "44465e17d2e9d390e70c999d5a11fda4f092847fcd2e3e5aa089d96c98a30e67": 172,
35
35
  "925ca92ff005ae943c158e3563f59698ce7e75c5a8c8dd43303a0a154887b3e6": 657,
36
- "8cead549d0e770dea8fdf5e018a85d59585265cf8bff16ba83962fc7996dbb7f": 2998
36
+ "8cead549d0e770dea8fdf5e018a85d59585265cf8bff16ba83962fc7996dbb7f": 2998,
37
+ "2b89b9dc8fdf9f34709a5b106b472f0f39bb6ca9ce04b0fd7f2e971688e2e53b": 8,
38
+ "2f95862b045670cd22bee3114c39763a4a08beeb663b145d283c31d7d1101c4f": 15,
39
+ "2a01deaec9e51a579277b34b122399984d0bbf57e2458a7e42fecd2829867a0d": 16,
40
+ "93da3352f9f1d105fdfe4971cfa80e9dd777bfc5d0f683ebb6e1294b92137bb7": 18,
41
+ "2b9ab1e972a281585084148ba1389800799bd4be63b957507db1349314e47445": 29,
42
+ "0a0408d619e9380abad35060f9192039ed5042fa6f82301d0e48bb52be830996": 92,
43
+ "4279e31cc369bbcc2faf022b382b080e32a8e689ff20fbc530d2a603eb6cd98b": 110,
44
+ "273717b49430906f4b0c230e99aa1007f83758e3199edbc887c0d06c3e332494": 163,
45
+ "58cd29ef0e714c5affc44f269b2c1899a52da4169d7acc147b9da692e6953608": 182,
46
+ "d40472610abe56d36d065a0cf889fc8f1dd9f3b7f2a478231a5fc6df07ea5ce3": 201,
47
+ "e67d98cc1fbd94f569d5ba6c3c3c759eb3ffc5d2b28e64538a53ae13efad8fd1": 648,
48
+ "2b529621fa6e2c8429f623ba705572aa64175d7768365ef829df6a12c9f365f4": 1818,
49
+ "8414cfadf82f6bed644d2e399c11df21ec0131aa574c56030b132113dbbf3a0a": 1841,
50
+ "d41369178d64f41d51ca95465c144a2c74d2fff30be69164835911943fa64c3e": 1854,
51
+ "a903b5a82cb572397e3d47595d2889cf80513f5b4cf7a36b513ae10cc8b1e338": 2310,
52
+ "fa9e8d4591613476ad0961732475dc08969d248faca270cc6c47efe009ea3070": 2311,
53
+ "1fc9127232d059b415f7b11989d06aa11a3563619e5b7a8e44ae440f0b95e4e1": 2700
37
54
  }
38
55
  },
39
56
  "stork_feeder": {
package/src/data.ts CHANGED
@@ -483,8 +483,10 @@ export class DataAPI extends OracleAPI {
483
483
  openTimestamp: parseValue(positionFields.open_timestamp),
484
484
  }
485
485
 
486
- positionInfo.reservingFeeAmount = this.#calculatePositionReserveFee(positionInfo, await this.getVaultInfo(positionInfo.collateralToken), (await this.getVaultInfo(positionInfo.collateralToken)).reservingFeeModel, Date.now() / 1000)
487
- positionInfo.fundingFeeValue = this.#calculatePositionFundingFee(positionInfo, await this.getSymbolInfo(positionInfo.indexToken, positionInfo.long), (await this.getSymbolInfo(positionInfo.indexToken, positionInfo.long)).fundingFeeModel, (await this.getOraclePrice(positionInfo.indexToken)).getPriceUnchecked().getPriceAsNumberUnchecked(), (await this.getMarketInfo()).lpSupplyWithDecimals, Date.now() / 1000)
486
+ const vaultInfo = await this.getVaultInfo(positionInfo.collateralToken)
487
+ positionInfo.reservingFeeAmount = this.#calculatePositionReserveFee(positionInfo, vaultInfo, vaultInfo.reservingFeeModel, Date.now() / 1000)
488
+ const symbolInfo = await this.getSymbolInfo(positionInfo.indexToken, positionInfo.long)
489
+ positionInfo.fundingFeeValue = this.#calculatePositionFundingFee(positionInfo, symbolInfo, symbolInfo.fundingFeeModel, (await this.getOraclePrice(positionInfo.indexToken)).getPriceUnchecked().getPriceAsNumberUnchecked(), (await this.getMarketInfo()).lpSupplyWithDecimals, Date.now() / 1000)
488
490
 
489
491
  return positionInfo
490
492
  }
@@ -664,7 +666,9 @@ export class DataAPI extends OracleAPI {
664
666
  showContent: true,
665
667
  },
666
668
  })
667
- return this.#parseMarketInfo(rawData)
669
+ const info = this.#parseMarketInfo(rawData)
670
+ this.marketInfoCache = info
671
+ return info
668
672
  }
669
673
 
670
674
  public async getVaultInfo(vaultToken: string) {
@@ -680,7 +684,9 @@ export class DataAPI extends OracleAPI {
680
684
  value: { dummy_field: false },
681
685
  },
682
686
  })
683
- return await this.#parseVaultInfo(rawData)
687
+ const info = await this.#parseVaultInfo(rawData)
688
+ this.vaultInfoCache[vaultToken] = info
689
+ return info
684
690
  }
685
691
 
686
692
  public async getSymbolInfo(indexToken: string, long: boolean) {
@@ -696,7 +702,9 @@ export class DataAPI extends OracleAPI {
696
702
  value: { dummy_field: false },
697
703
  },
698
704
  })
699
- return await this.#parseSymbolInfo(rawData, long)
705
+ const info = await this.#parseSymbolInfo(rawData, long)
706
+ this.symbolInfoCache[symbol] = info
707
+ return info
700
708
  }
701
709
 
702
710
  public async getPositionConfig(indexToken: string, long: boolean) {
@@ -711,7 +719,9 @@ export class DataAPI extends OracleAPI {
711
719
  showContent: true,
712
720
  },
713
721
  })
714
- return this.#parsePositionConfig(rawData)
722
+ const info = this.#parsePositionConfig(rawData)
723
+ this.positionConfigCache[symbol] = info
724
+ return info
715
725
  }
716
726
 
717
727
  public async getPositionCapInfoList(owner: string): Promise<IPositionCapInfo[]> {
@@ -952,7 +962,9 @@ export class DataAPI extends OracleAPI {
952
962
  showContent: true,
953
963
  },
954
964
  })
955
- return this.#parseRebaseFeeModel(rawData)
965
+ const info = this.#parseRebaseFeeModel(rawData)
966
+ this.rebaseFeeModelCache = info
967
+ return info
956
968
  }
957
969
 
958
970
  fundingFeeRate = async (indexToken: string, long: boolean) => {
@@ -39,13 +39,13 @@ import type {
39
39
  ISwapFeeBreakdown,
40
40
  } from '../interfaces'
41
41
  import type { OracleInputs } from '../oraclePro'
42
- import type { DynamicFieldInfo, SuiClient } from '../suiClient'
43
42
  import {
44
43
  buildPythProPriceFeedMap,
45
44
  resolveTokenUsdPriceFromPythPro,
46
45
  valuateSymbolsV3,
47
46
  valuateVaultsV3,
48
47
  } from '../oraclePro'
48
+ import type { DynamicFieldInfo, SuiClient } from '../suiClient'
49
49
  import { joinSymbol, parseSymbolKey, parseValue, suiSymbolToSymbol } from '../utils'
50
50
 
51
51
  export interface GetCumulativeAprResponse {
@@ -117,7 +117,7 @@ export class SLPDataAPI extends BaseDataAPI implements ISLPDataAPI {
117
117
  public async getRebaseFeeModel(): Promise<ISLPRebaseFeeModel> {
118
118
  this.validateCache()
119
119
  if (this.rebaseFeeModelCache) {
120
- return this.rebaseFeeModelCache
120
+ return this.rebaseFeeModelCache as ISLPRebaseFeeModel
121
121
  }
122
122
  const rawData = await this.provider.getObject({
123
123
  id: this.consts.sudoCore.rebaseFeeModel,
@@ -131,7 +131,9 @@ export class SLPDataAPI extends BaseDataAPI implements ISLPDataAPI {
131
131
  this.consts.sudoCore.rebaseFeeModel,
132
132
  this.consts.sudoCore.upgradedPackage,
133
133
  )
134
- return { ...model, exponent }
134
+ const info = { ...model, exponent }
135
+ this.rebaseFeeModelCache = info
136
+ return info
135
137
  }
136
138
 
137
139
  /**
@@ -448,7 +450,7 @@ export class SLPDataAPI extends BaseDataAPI implements ISLPDataAPI {
448
450
  public async getMarketInfo(): Promise<ISLPMarketInfo> {
449
451
  this.validateCache()
450
452
  if (this.marketInfoCache) {
451
- return this.marketInfoCache
453
+ return this.marketInfoCache as ISLPMarketInfo
452
454
  }
453
455
 
454
456
  const rawData = await this.provider.getObject({
@@ -458,10 +460,12 @@ export class SLPDataAPI extends BaseDataAPI implements ISLPDataAPI {
458
460
  },
459
461
  })
460
462
  const apr = await this.getCumulativeApr()
461
- return {
463
+ const info = {
462
464
  ...SLPDataAPI.parseMarketInfo(rawData),
463
465
  apr,
464
466
  }
467
+ this.marketInfoCache = info
468
+ return info
465
469
  }
466
470
 
467
471
  /**
@@ -470,7 +474,7 @@ export class SLPDataAPI extends BaseDataAPI implements ISLPDataAPI {
470
474
  public async getVaultInfo(vaultToken: string): Promise<ISLPVaultInfo> {
471
475
  this.validateCache()
472
476
  if (this.vaultInfoCache[vaultToken]) {
473
- return this.vaultInfoCache[vaultToken]
477
+ return this.vaultInfoCache[vaultToken] as ISLPVaultInfo
474
478
  }
475
479
 
476
480
  const rawData = await this.provider.getDynamicFieldObject({
@@ -480,7 +484,9 @@ export class SLPDataAPI extends BaseDataAPI implements ISLPDataAPI {
480
484
  value: { dummy_field: false },
481
485
  },
482
486
  })
483
- return await this.parseVaultInfo(rawData)
487
+ const info = await this.parseVaultInfo(rawData)
488
+ this.vaultInfoCache[vaultToken] = info
489
+ return info
484
490
  }
485
491
 
486
492
  /**
@@ -490,7 +496,7 @@ export class SLPDataAPI extends BaseDataAPI implements ISLPDataAPI {
490
496
  this.validateCache()
491
497
  const symbol = joinSymbol(long ? 'long' : 'short', indexToken)
492
498
  if (this.symbolInfoCache[symbol]) {
493
- return this.symbolInfoCache[symbol]
499
+ return this.symbolInfoCache[symbol] as ISLPSymbolInfo
494
500
  }
495
501
 
496
502
  const rawData = await this.provider.getDynamicFieldObject({
@@ -503,7 +509,9 @@ export class SLPDataAPI extends BaseDataAPI implements ISLPDataAPI {
503
509
  },
504
510
  })
505
511
 
506
- return await this.parseSymbolInfo(rawData, long)
512
+ const info = await this.parseSymbolInfo(rawData, long)
513
+ this.symbolInfoCache[symbol] = info
514
+ return info
507
515
  }
508
516
 
509
517
  /**
@@ -556,6 +564,9 @@ export class SLPDataAPI extends BaseDataAPI implements ISLPDataAPI {
556
564
  */
557
565
  public async getSymbolOiFundingState(indexToken: string): Promise<ISLPOiFundingState | null> {
558
566
  this.validateCache()
567
+ if (Object.prototype.hasOwnProperty.call(this.oiFundingStateCache, indexToken)) {
568
+ return this.oiFundingStateCache[indexToken] as ISLPOiFundingState | null
569
+ }
559
570
  try {
560
571
  const rawData = await this.provider.getDynamicFieldObject({
561
572
  parentId: this.consts.sudoCore.market,
@@ -564,11 +575,14 @@ export class SLPDataAPI extends BaseDataAPI implements ISLPDataAPI {
564
575
  value: { dummy_field: false },
565
576
  },
566
577
  })
567
- return SLPDataAPI.parseOiFundingState(rawData)
578
+ const info = SLPDataAPI.parseOiFundingState(rawData)
579
+ this.oiFundingStateCache[indexToken] = info
580
+ return info
568
581
  }
569
582
  catch (e: any) {
570
583
  // If the dynamic field doesn't exist, return null
571
584
  console.error('Error Fetching SLP Symbol OI Funding State:', e)
585
+ this.oiFundingStateCache[indexToken] = null
572
586
  return null
573
587
  }
574
588
  }
@@ -1075,7 +1089,7 @@ export class SLPDataAPI extends BaseDataAPI implements ISLPDataAPI {
1075
1089
  this.validateCache()
1076
1090
  const symbol = joinSymbol(long ? 'long' : 'short', indexToken)
1077
1091
  if (this.positionConfigCache[symbol]) {
1078
- return this.positionConfigCache[symbol]
1092
+ return this.positionConfigCache[symbol] as ISLPPositionConfig
1079
1093
  }
1080
1094
 
1081
1095
  const rawData = await this.provider.getObject({
@@ -1084,7 +1098,9 @@ export class SLPDataAPI extends BaseDataAPI implements ISLPDataAPI {
1084
1098
  showContent: true,
1085
1099
  },
1086
1100
  })
1087
- return SLPDataAPI.parsePositionConfig(rawData)
1101
+ const info = SLPDataAPI.parsePositionConfig(rawData)
1102
+ this.positionConfigCache[symbol] = info
1103
+ return info
1088
1104
  }
1089
1105
 
1090
1106
  public async getOpenPositions(batchSize = 50, symbol = 'sui'): Promise<ISLPPositionInfo[]> {
@@ -1925,21 +1941,23 @@ export class SLPDataAPI extends BaseDataAPI implements ISLPDataAPI {
1925
1941
 
1926
1942
  if (!positionFields.closed) {
1927
1943
  try {
1944
+ const vaultInfo = await this.getVaultInfo(positionInfo.collateralToken)
1928
1945
  positionInfo.reservingFeeAmount = SLPDataAPI.calculatePositionReserveFee(
1929
1946
  positionInfo,
1930
- await this.getVaultInfo(positionInfo.collateralToken),
1931
- (await this.getVaultInfo(positionInfo.collateralToken)).reservingFeeModel,
1947
+ vaultInfo,
1948
+ vaultInfo.reservingFeeModel,
1932
1949
  Date.now() / 1000,
1933
1950
  )
1934
1951
 
1935
1952
  // OI context for funding: fetch state and paired side size when enabled
1936
1953
  const oiState = await this.getSymbolOiFundingState(positionInfo.indexToken)
1937
1954
  const pairedSymbol = await this.getSymbolInfo(positionInfo.indexToken, !positionInfo.long)
1955
+ const symbolInfo = await this.getSymbolInfo(positionInfo.indexToken, positionInfo.long)
1938
1956
 
1939
1957
  positionInfo.fundingFeeValue = SLPDataAPI.calculatePositionFundingFee(
1940
1958
  positionInfo,
1941
- await this.getSymbolInfo(positionInfo.indexToken, positionInfo.long),
1942
- (await this.getSymbolInfo(positionInfo.indexToken, positionInfo.long)).fundingFeeModel,
1959
+ symbolInfo,
1960
+ symbolInfo.fundingFeeModel,
1943
1961
  (await this.getOraclePrice(positionInfo.indexToken)).getPriceUnchecked().getPriceAsNumberUnchecked(),
1944
1962
  (await this.getMarketInfo()).lpSupplyWithDecimals,
1945
1963
  Date.now() / 1000,
@@ -308,7 +308,7 @@ export class USDZDataAPI extends BaseDataAPI implements IUSDZDataAPI {
308
308
  public async getMarketInfo(): Promise<IUSDZMarketInfo> {
309
309
  this.validateCache()
310
310
  if (this.marketInfoCache) {
311
- return this.marketInfoCache
311
+ return this.marketInfoCache as IUSDZMarketInfo
312
312
  }
313
313
  const rawData = await this.provider.getObject({
314
314
  id: this.consts.zoCore.market,
@@ -316,7 +316,9 @@ export class USDZDataAPI extends BaseDataAPI implements IUSDZDataAPI {
316
316
  showContent: true,
317
317
  },
318
318
  })
319
- return USDZDataAPI.parseMarketInfo(rawData)
319
+ const info = USDZDataAPI.parseMarketInfo(rawData)
320
+ this.marketInfoCache = info
321
+ return info
320
322
  }
321
323
 
322
324
  /**
@@ -325,7 +327,7 @@ export class USDZDataAPI extends BaseDataAPI implements IUSDZDataAPI {
325
327
  public async getVaultInfo(vaultToken: string): Promise<IUSDZVaultInfo> {
326
328
  this.validateCache()
327
329
  if (this.vaultInfoCache[vaultToken]) {
328
- return this.vaultInfoCache[vaultToken]
330
+ return this.vaultInfoCache[vaultToken] as IUSDZVaultInfo
329
331
  }
330
332
 
331
333
  const rawData = await this.provider.getDynamicFieldObject({
@@ -335,7 +337,9 @@ export class USDZDataAPI extends BaseDataAPI implements IUSDZDataAPI {
335
337
  value: { dummy_field: false },
336
338
  },
337
339
  })
338
- return await this.parseVaultInfo(rawData)
340
+ const info = await this.parseVaultInfo(rawData)
341
+ this.vaultInfoCache[vaultToken] = info
342
+ return info
339
343
  }
340
344
 
341
345
  /**
@@ -345,7 +349,7 @@ export class USDZDataAPI extends BaseDataAPI implements IUSDZDataAPI {
345
349
  this.validateCache()
346
350
  const symbol = joinSymbol(long ? 'long' : 'short', indexToken)
347
351
  if (this.symbolInfoCache[symbol]) {
348
- return this.symbolInfoCache[symbol]
352
+ return this.symbolInfoCache[symbol] as IUSDZSymbolInfo
349
353
  }
350
354
  const rawData = await this.provider.getDynamicFieldObject({
351
355
  parentId: this.consts.zoCore.symbolsParent,
@@ -354,14 +358,16 @@ export class USDZDataAPI extends BaseDataAPI implements IUSDZDataAPI {
354
358
  value: { dummy_field: false },
355
359
  },
356
360
  })
357
- return await this.parseSymbolInfo(rawData, long)
361
+ const info = await this.parseSymbolInfo(rawData, long)
362
+ this.symbolInfoCache[symbol] = info
363
+ return info
358
364
  }
359
365
 
360
366
  public async getPositionConfig(indexToken: string, long: boolean): Promise<IUSDZPositionConfig> {
361
367
  this.validateCache()
362
368
  const symbol = joinSymbol(long ? 'long' : 'short', indexToken)
363
369
  if (this.positionConfigCache[symbol]) {
364
- return this.positionConfigCache[symbol]
370
+ return this.positionConfigCache[symbol] as IUSDZPositionConfig
365
371
  }
366
372
  const rawData = await this.provider.getObject({
367
373
  id: this.consts.zoCore.symbols[symbol].positionConfig,
@@ -369,7 +375,9 @@ export class USDZDataAPI extends BaseDataAPI implements IUSDZDataAPI {
369
375
  showContent: true,
370
376
  },
371
377
  })
372
- return USDZDataAPI.parsePositionConfig(rawData)
378
+ const info = USDZDataAPI.parsePositionConfig(rawData)
379
+ this.positionConfigCache[symbol] = info
380
+ return info
373
381
  }
374
382
 
375
383
  /**
@@ -399,6 +407,9 @@ export class USDZDataAPI extends BaseDataAPI implements IUSDZDataAPI {
399
407
  */
400
408
  public async getSymbolOiFundingState(indexToken: string): Promise<IUSDZOiFundingState | null> {
401
409
  this.validateCache()
410
+ if (Object.prototype.hasOwnProperty.call(this.oiFundingStateCache, indexToken)) {
411
+ return this.oiFundingStateCache[indexToken] as IUSDZOiFundingState | null
412
+ }
402
413
  try {
403
414
  const rawData = await this.provider.getDynamicFieldObject({
404
415
  parentId: this.consts.zoCore.market,
@@ -407,10 +418,13 @@ export class USDZDataAPI extends BaseDataAPI implements IUSDZDataAPI {
407
418
  value: { dummy_field: false },
408
419
  },
409
420
  })
410
- return USDZDataAPI.parseOiFundingState(rawData)
421
+ const info = USDZDataAPI.parseOiFundingState(rawData)
422
+ this.oiFundingStateCache[indexToken] = info
423
+ return info
411
424
  }
412
425
  catch (e: any) {
413
426
  console.error('Error Fetching USDZ Symbol OI Funding State:', e)
427
+ this.oiFundingStateCache[indexToken] = null
414
428
  return null
415
429
  }
416
430
  }
@@ -816,7 +830,9 @@ export class USDZDataAPI extends BaseDataAPI implements IUSDZDataAPI {
816
830
  showContent: true,
817
831
  },
818
832
  })
819
- return USDZDataAPI.parseRebaseFeeModel(rawData)
833
+ const info = USDZDataAPI.parseRebaseFeeModel(rawData)
834
+ this.rebaseFeeModelCache = info
835
+ return info
820
836
  }
821
837
 
822
838
  public async fundingFeeRate(indexToken: string, long: boolean): Promise<number> {
@@ -1205,20 +1221,22 @@ export class USDZDataAPI extends BaseDataAPI implements IUSDZDataAPI {
1205
1221
  openTimestamp: parseValue(positionFields.open_timestamp),
1206
1222
  }
1207
1223
 
1224
+ const vaultInfo = await this.getVaultInfo(positionInfo.collateralToken)
1208
1225
  positionInfo.reservingFeeAmount = USDZDataAPI.calculatePositionReserveFee(
1209
1226
  positionInfo,
1210
- await this.getVaultInfo(positionInfo.collateralToken),
1211
- (await this.getVaultInfo(positionInfo.collateralToken)).reservingFeeModel,
1227
+ vaultInfo,
1228
+ vaultInfo.reservingFeeModel,
1212
1229
  Date.now() / 1000,
1213
1230
  )
1214
1231
 
1215
1232
  // OI context for funding: fetch state and paired side size when enabled
1216
1233
  const oiState = await this.getSymbolOiFundingState(positionInfo.indexToken)
1217
1234
  const pairedSymbol = await this.getSymbolInfo(positionInfo.indexToken, !positionInfo.long)
1235
+ const symbolInfo = await this.getSymbolInfo(positionInfo.indexToken, positionInfo.long)
1218
1236
  positionInfo.fundingFeeValue = USDZDataAPI.calculatePositionFundingFee(
1219
1237
  positionInfo,
1220
- await this.getSymbolInfo(positionInfo.indexToken, positionInfo.long),
1221
- (await this.getSymbolInfo(positionInfo.indexToken, positionInfo.long)).fundingFeeModel,
1238
+ symbolInfo,
1239
+ symbolInfo.fundingFeeModel,
1222
1240
  (await this.getOraclePrice(positionInfo.indexToken)).getPriceUnchecked().getPriceAsNumberUnchecked(),
1223
1241
  (await this.getMarketInfo()).lpSupplyWithDecimals,
1224
1242
  Date.now() / 1000,
@@ -230,7 +230,7 @@ export class ZBTCVCDataAPI extends BaseDataAPI implements IZBTCVCDataAPI {
230
230
  public async getMarketInfo(): Promise<IZBTCVCMarketInfo> {
231
231
  this.validateCache()
232
232
  if (this.marketInfoCache) {
233
- return this.marketInfoCache
233
+ return this.marketInfoCache as IZBTCVCMarketInfo
234
234
  }
235
235
  const rawData = await this.provider.getObject({
236
236
  id: this.consts.zoCore.market,
@@ -238,7 +238,9 @@ export class ZBTCVCDataAPI extends BaseDataAPI implements IZBTCVCDataAPI {
238
238
  showContent: true,
239
239
  },
240
240
  })
241
- return ZBTCVCDataAPI.parseMarketInfo(rawData)
241
+ const info = ZBTCVCDataAPI.parseMarketInfo(rawData)
242
+ this.marketInfoCache = info
243
+ return info
242
244
  }
243
245
 
244
246
  /**
@@ -247,7 +249,7 @@ export class ZBTCVCDataAPI extends BaseDataAPI implements IZBTCVCDataAPI {
247
249
  public async getVaultInfo(vaultToken: string): Promise<IZBTCVCVaultInfo> {
248
250
  this.validateCache()
249
251
  if (this.vaultInfoCache[vaultToken]) {
250
- return this.vaultInfoCache[vaultToken]
252
+ return this.vaultInfoCache[vaultToken] as IZBTCVCVaultInfo
251
253
  }
252
254
 
253
255
  const rawData = await this.provider.getDynamicFieldObject({
@@ -257,7 +259,9 @@ export class ZBTCVCDataAPI extends BaseDataAPI implements IZBTCVCDataAPI {
257
259
  value: { dummy_field: false },
258
260
  },
259
261
  })
260
- return await this.parseVaultInfo(rawData)
262
+ const info = await this.parseVaultInfo(rawData)
263
+ this.vaultInfoCache[vaultToken] = info
264
+ return info
261
265
  }
262
266
 
263
267
  /**
@@ -267,7 +271,7 @@ export class ZBTCVCDataAPI extends BaseDataAPI implements IZBTCVCDataAPI {
267
271
  this.validateCache()
268
272
  const symbol = joinSymbol(long ? 'long' : 'short', indexToken)
269
273
  if (this.symbolInfoCache[symbol]) {
270
- return this.symbolInfoCache[symbol]
274
+ return this.symbolInfoCache[symbol] as IZBTCVCSymbolInfo
271
275
  }
272
276
  const rawData = await this.provider.getDynamicFieldObject({
273
277
  parentId: this.consts.zoCore.symbolsParent,
@@ -276,14 +280,16 @@ export class ZBTCVCDataAPI extends BaseDataAPI implements IZBTCVCDataAPI {
276
280
  value: { dummy_field: false },
277
281
  },
278
282
  })
279
- return await this.parseSymbolInfo(rawData, long)
283
+ const info = await this.parseSymbolInfo(rawData, long)
284
+ this.symbolInfoCache[symbol] = info
285
+ return info
280
286
  }
281
287
 
282
288
  public async getPositionConfig(indexToken: string, long: boolean): Promise<IZBTCVCPositionConfig> {
283
289
  this.validateCache()
284
290
  const symbol = joinSymbol(long ? 'long' : 'short', indexToken)
285
291
  if (this.positionConfigCache[symbol]) {
286
- return this.positionConfigCache[symbol]
292
+ return this.positionConfigCache[symbol] as IZBTCVCPositionConfig
287
293
  }
288
294
  const rawData = await this.provider.getObject({
289
295
  id: this.consts.zoCore.symbols[symbol].positionConfig,
@@ -291,7 +297,9 @@ export class ZBTCVCDataAPI extends BaseDataAPI implements IZBTCVCDataAPI {
291
297
  showContent: true,
292
298
  },
293
299
  })
294
- return ZBTCVCDataAPI.parsePositionConfig(rawData)
300
+ const info = ZBTCVCDataAPI.parsePositionConfig(rawData)
301
+ this.positionConfigCache[symbol] = info
302
+ return info
295
303
  }
296
304
 
297
305
  /**
@@ -536,7 +544,9 @@ export class ZBTCVCDataAPI extends BaseDataAPI implements IZBTCVCDataAPI {
536
544
  showContent: true,
537
545
  },
538
546
  })
539
- return ZBTCVCDataAPI.parseRebaseFeeModel(rawData)
547
+ const info = ZBTCVCDataAPI.parseRebaseFeeModel(rawData)
548
+ this.rebaseFeeModelCache = info
549
+ return info
540
550
  }
541
551
 
542
552
  public async fundingFeeRate(indexToken: string, long: boolean): Promise<number> {
@@ -837,8 +847,10 @@ export class ZBTCVCDataAPI extends BaseDataAPI implements IZBTCVCDataAPI {
837
847
  openTimestamp: parseValue(positionFields.open_timestamp),
838
848
  }
839
849
 
840
- positionInfo.reservingFeeAmount = ZBTCVCDataAPI.calculatePositionReserveFee(positionInfo, await this.getVaultInfo(positionInfo.collateralToken), (await this.getVaultInfo(positionInfo.collateralToken)).reservingFeeModel, Date.now() / 1000)
841
- positionInfo.fundingFeeValue = ZBTCVCDataAPI.calculatePositionFundingFee(positionInfo, await this.getSymbolInfo(positionInfo.indexToken, positionInfo.long), (await this.getSymbolInfo(positionInfo.indexToken, positionInfo.long)).fundingFeeModel, (await this.getOraclePrice(positionInfo.indexToken)).getPriceUnchecked().getPriceAsNumberUnchecked(), (await this.getMarketInfo()).lpSupplyWithDecimals, Date.now() / 1000)
850
+ const vaultInfo = await this.getVaultInfo(positionInfo.collateralToken)
851
+ positionInfo.reservingFeeAmount = ZBTCVCDataAPI.calculatePositionReserveFee(positionInfo, vaultInfo, vaultInfo.reservingFeeModel, Date.now() / 1000)
852
+ const symbolInfo = await this.getSymbolInfo(positionInfo.indexToken, positionInfo.long)
853
+ positionInfo.fundingFeeValue = ZBTCVCDataAPI.calculatePositionFundingFee(positionInfo, symbolInfo, symbolInfo.fundingFeeModel, (await this.getOraclePrice(positionInfo.indexToken)).getPriceUnchecked().getPriceAsNumberUnchecked(), (await this.getMarketInfo()).lpSupplyWithDecimals, Date.now() / 1000)
842
854
 
843
855
  return positionInfo
844
856
  }