@zofai/zo-sdk 0.1.92

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 (275) hide show
  1. package/.claude/settings.local.json +9 -0
  2. package/.gitattributes +4 -0
  3. package/.prettierrc.js +9 -0
  4. package/README.md +28 -0
  5. package/dist/abstract/BaseAPI.cjs +206 -0
  6. package/dist/abstract/BaseAPI.cjs.map +1 -0
  7. package/dist/abstract/BaseAPI.d.cts +172 -0
  8. package/dist/abstract/BaseAPI.d.cts.map +1 -0
  9. package/dist/abstract/BaseAPI.d.mts +172 -0
  10. package/dist/abstract/BaseAPI.d.mts.map +1 -0
  11. package/dist/abstract/BaseAPI.mjs +202 -0
  12. package/dist/abstract/BaseAPI.mjs.map +1 -0
  13. package/dist/abstract/BaseDataAPI.cjs +140 -0
  14. package/dist/abstract/BaseDataAPI.cjs.map +1 -0
  15. package/dist/abstract/BaseDataAPI.d.cts +89 -0
  16. package/dist/abstract/BaseDataAPI.d.cts.map +1 -0
  17. package/dist/abstract/BaseDataAPI.d.mts +89 -0
  18. package/dist/abstract/BaseDataAPI.d.mts.map +1 -0
  19. package/dist/abstract/BaseDataAPI.mjs +136 -0
  20. package/dist/abstract/BaseDataAPI.mjs.map +1 -0
  21. package/dist/abstract/index.cjs +12 -0
  22. package/dist/abstract/index.cjs.map +1 -0
  23. package/dist/abstract/index.d.cts +7 -0
  24. package/dist/abstract/index.d.cts.map +1 -0
  25. package/dist/abstract/index.d.mts +7 -0
  26. package/dist/abstract/index.d.mts.map +1 -0
  27. package/dist/abstract/index.mjs +7 -0
  28. package/dist/abstract/index.mjs.map +1 -0
  29. package/dist/api.cjs +779 -0
  30. package/dist/api.cjs.map +1 -0
  31. package/dist/api.d.cts +75 -0
  32. package/dist/api.d.cts.map +1 -0
  33. package/dist/api.d.mts +75 -0
  34. package/dist/api.d.mts.map +1 -0
  35. package/dist/api.mjs +775 -0
  36. package/dist/api.mjs.map +1 -0
  37. package/dist/bcs.cjs +42 -0
  38. package/dist/bcs.cjs.map +1 -0
  39. package/dist/bcs.d.cts +91 -0
  40. package/dist/bcs.d.cts.map +1 -0
  41. package/dist/bcs.d.mts +91 -0
  42. package/dist/bcs.d.mts.map +1 -0
  43. package/dist/bcs.mjs +39 -0
  44. package/dist/bcs.mjs.map +1 -0
  45. package/dist/consts/deployments-shared-mainnet.json +50 -0
  46. package/dist/consts/deployments-shared-testnet.json +45 -0
  47. package/dist/consts/deployments-slp-mainnet.json +600 -0
  48. package/dist/consts/deployments-slp-testnet.json +87 -0
  49. package/dist/consts/deployments-usdz-mainnet.json +494 -0
  50. package/dist/consts/deployments-usdz-testnet.json +98 -0
  51. package/dist/consts/deployments-zbtcvc-mainnet.json +180 -0
  52. package/dist/consts/deployments-zlp-mainnet.json +791 -0
  53. package/dist/consts/deployments-zlp-testnet.json +76 -0
  54. package/dist/consts/index.cjs +200 -0
  55. package/dist/consts/index.cjs.map +1 -0
  56. package/dist/consts/index.d.cts +157 -0
  57. package/dist/consts/index.d.cts.map +1 -0
  58. package/dist/consts/index.d.mts +157 -0
  59. package/dist/consts/index.d.mts.map +1 -0
  60. package/dist/consts/index.mjs +189 -0
  61. package/dist/consts/index.mjs.map +1 -0
  62. package/dist/consts/price_id_to_object_id.mainnet.json +56 -0
  63. package/dist/consts/price_id_to_object_id.testnet.json +17 -0
  64. package/dist/data.cjs +919 -0
  65. package/dist/data.cjs.map +1 -0
  66. package/dist/data.d.cts +235 -0
  67. package/dist/data.d.cts.map +1 -0
  68. package/dist/data.d.mts +235 -0
  69. package/dist/data.d.mts.map +1 -0
  70. package/dist/data.mjs +915 -0
  71. package/dist/data.mjs.map +1 -0
  72. package/dist/factory/SDKFactory.cjs +228 -0
  73. package/dist/factory/SDKFactory.cjs.map +1 -0
  74. package/dist/factory/SDKFactory.d.cts +84 -0
  75. package/dist/factory/SDKFactory.d.cts.map +1 -0
  76. package/dist/factory/SDKFactory.d.mts +84 -0
  77. package/dist/factory/SDKFactory.d.mts.map +1 -0
  78. package/dist/factory/SDKFactory.mjs +222 -0
  79. package/dist/factory/SDKFactory.mjs.map +1 -0
  80. package/dist/implementations/SLPAPI.cjs +1794 -0
  81. package/dist/implementations/SLPAPI.cjs.map +1 -0
  82. package/dist/implementations/SLPAPI.d.cts +183 -0
  83. package/dist/implementations/SLPAPI.d.cts.map +1 -0
  84. package/dist/implementations/SLPAPI.d.mts +183 -0
  85. package/dist/implementations/SLPAPI.d.mts.map +1 -0
  86. package/dist/implementations/SLPAPI.mjs +1790 -0
  87. package/dist/implementations/SLPAPI.mjs.map +1 -0
  88. package/dist/implementations/SLPDataAPI.cjs +1384 -0
  89. package/dist/implementations/SLPDataAPI.cjs.map +1 -0
  90. package/dist/implementations/SLPDataAPI.d.cts +158 -0
  91. package/dist/implementations/SLPDataAPI.d.cts.map +1 -0
  92. package/dist/implementations/SLPDataAPI.d.mts +158 -0
  93. package/dist/implementations/SLPDataAPI.d.mts.map +1 -0
  94. package/dist/implementations/SLPDataAPI.mjs +1380 -0
  95. package/dist/implementations/SLPDataAPI.mjs.map +1 -0
  96. package/dist/implementations/USDZAPI.cjs +1676 -0
  97. package/dist/implementations/USDZAPI.cjs.map +1 -0
  98. package/dist/implementations/USDZAPI.d.cts +180 -0
  99. package/dist/implementations/USDZAPI.d.cts.map +1 -0
  100. package/dist/implementations/USDZAPI.d.mts +180 -0
  101. package/dist/implementations/USDZAPI.d.mts.map +1 -0
  102. package/dist/implementations/USDZAPI.mjs +1672 -0
  103. package/dist/implementations/USDZAPI.mjs.map +1 -0
  104. package/dist/implementations/USDZDataAPI.cjs +1209 -0
  105. package/dist/implementations/USDZDataAPI.cjs.map +1 -0
  106. package/dist/implementations/USDZDataAPI.d.cts +191 -0
  107. package/dist/implementations/USDZDataAPI.d.cts.map +1 -0
  108. package/dist/implementations/USDZDataAPI.d.mts +191 -0
  109. package/dist/implementations/USDZDataAPI.d.mts.map +1 -0
  110. package/dist/implementations/USDZDataAPI.mjs +1205 -0
  111. package/dist/implementations/USDZDataAPI.mjs.map +1 -0
  112. package/dist/implementations/ZBTCVCAPI.cjs +906 -0
  113. package/dist/implementations/ZBTCVCAPI.cjs.map +1 -0
  114. package/dist/implementations/ZBTCVCAPI.d.cts +107 -0
  115. package/dist/implementations/ZBTCVCAPI.d.cts.map +1 -0
  116. package/dist/implementations/ZBTCVCAPI.d.mts +107 -0
  117. package/dist/implementations/ZBTCVCAPI.d.mts.map +1 -0
  118. package/dist/implementations/ZBTCVCAPI.mjs +902 -0
  119. package/dist/implementations/ZBTCVCAPI.mjs.map +1 -0
  120. package/dist/implementations/ZBTCVCDataAPI.cjs +829 -0
  121. package/dist/implementations/ZBTCVCDataAPI.cjs.map +1 -0
  122. package/dist/implementations/ZBTCVCDataAPI.d.cts +94 -0
  123. package/dist/implementations/ZBTCVCDataAPI.d.cts.map +1 -0
  124. package/dist/implementations/ZBTCVCDataAPI.d.mts +94 -0
  125. package/dist/implementations/ZBTCVCDataAPI.d.mts.map +1 -0
  126. package/dist/implementations/ZBTCVCDataAPI.mjs +825 -0
  127. package/dist/implementations/ZBTCVCDataAPI.mjs.map +1 -0
  128. package/dist/implementations/ZLPAPI.cjs +1948 -0
  129. package/dist/implementations/ZLPAPI.cjs.map +1 -0
  130. package/dist/implementations/ZLPAPI.d.cts +192 -0
  131. package/dist/implementations/ZLPAPI.d.cts.map +1 -0
  132. package/dist/implementations/ZLPAPI.d.mts +192 -0
  133. package/dist/implementations/ZLPAPI.d.mts.map +1 -0
  134. package/dist/implementations/ZLPAPI.mjs +1944 -0
  135. package/dist/implementations/ZLPAPI.mjs.map +1 -0
  136. package/dist/implementations/ZLPDataAPI.cjs +1267 -0
  137. package/dist/implementations/ZLPDataAPI.cjs.map +1 -0
  138. package/dist/implementations/ZLPDataAPI.d.cts +193 -0
  139. package/dist/implementations/ZLPDataAPI.d.cts.map +1 -0
  140. package/dist/implementations/ZLPDataAPI.d.mts +193 -0
  141. package/dist/implementations/ZLPDataAPI.d.mts.map +1 -0
  142. package/dist/implementations/ZLPDataAPI.mjs +1263 -0
  143. package/dist/implementations/ZLPDataAPI.mjs.map +1 -0
  144. package/dist/implementations/index.cjs +26 -0
  145. package/dist/implementations/index.cjs.map +1 -0
  146. package/dist/implementations/index.d.cts +13 -0
  147. package/dist/implementations/index.d.cts.map +1 -0
  148. package/dist/implementations/index.d.mts +13 -0
  149. package/dist/implementations/index.d.mts.map +1 -0
  150. package/dist/implementations/index.mjs +15 -0
  151. package/dist/implementations/index.mjs.map +1 -0
  152. package/dist/index.cjs +69 -0
  153. package/dist/index.cjs.map +1 -0
  154. package/dist/index.d.cts +51 -0
  155. package/dist/index.d.cts.map +1 -0
  156. package/dist/index.d.mts +51 -0
  157. package/dist/index.d.mts.map +1 -0
  158. package/dist/index.mjs +51 -0
  159. package/dist/index.mjs.map +1 -0
  160. package/dist/interfaces/base.cjs +7 -0
  161. package/dist/interfaces/base.cjs.map +1 -0
  162. package/dist/interfaces/base.d.cts +346 -0
  163. package/dist/interfaces/base.d.cts.map +1 -0
  164. package/dist/interfaces/base.d.mts +346 -0
  165. package/dist/interfaces/base.d.mts.map +1 -0
  166. package/dist/interfaces/base.mjs +6 -0
  167. package/dist/interfaces/base.mjs.map +1 -0
  168. package/dist/interfaces/index.cjs +31 -0
  169. package/dist/interfaces/index.cjs.map +1 -0
  170. package/dist/interfaces/index.d.cts +15 -0
  171. package/dist/interfaces/index.d.cts.map +1 -0
  172. package/dist/interfaces/index.d.mts +15 -0
  173. package/dist/interfaces/index.d.mts.map +1 -0
  174. package/dist/interfaces/index.mjs +15 -0
  175. package/dist/interfaces/index.mjs.map +1 -0
  176. package/dist/interfaces/slp.cjs +7 -0
  177. package/dist/interfaces/slp.cjs.map +1 -0
  178. package/dist/interfaces/slp.d.cts +179 -0
  179. package/dist/interfaces/slp.d.cts.map +1 -0
  180. package/dist/interfaces/slp.d.mts +179 -0
  181. package/dist/interfaces/slp.d.mts.map +1 -0
  182. package/dist/interfaces/slp.mjs +6 -0
  183. package/dist/interfaces/slp.mjs.map +1 -0
  184. package/dist/interfaces/usdz.cjs +7 -0
  185. package/dist/interfaces/usdz.cjs.map +1 -0
  186. package/dist/interfaces/usdz.d.cts +104 -0
  187. package/dist/interfaces/usdz.d.cts.map +1 -0
  188. package/dist/interfaces/usdz.d.mts +104 -0
  189. package/dist/interfaces/usdz.d.mts.map +1 -0
  190. package/dist/interfaces/usdz.mjs +6 -0
  191. package/dist/interfaces/usdz.mjs.map +1 -0
  192. package/dist/interfaces/zbtcvc.cjs +7 -0
  193. package/dist/interfaces/zbtcvc.cjs.map +1 -0
  194. package/dist/interfaces/zbtcvc.d.cts +64 -0
  195. package/dist/interfaces/zbtcvc.d.cts.map +1 -0
  196. package/dist/interfaces/zbtcvc.d.mts +64 -0
  197. package/dist/interfaces/zbtcvc.d.mts.map +1 -0
  198. package/dist/interfaces/zbtcvc.mjs +6 -0
  199. package/dist/interfaces/zbtcvc.mjs.map +1 -0
  200. package/dist/interfaces/zlp.cjs +7 -0
  201. package/dist/interfaces/zlp.cjs.map +1 -0
  202. package/dist/interfaces/zlp.d.cts +114 -0
  203. package/dist/interfaces/zlp.d.cts.map +1 -0
  204. package/dist/interfaces/zlp.d.mts +114 -0
  205. package/dist/interfaces/zlp.d.mts.map +1 -0
  206. package/dist/interfaces/zlp.mjs +6 -0
  207. package/dist/interfaces/zlp.mjs.map +1 -0
  208. package/dist/oracle.cjs +118 -0
  209. package/dist/oracle.cjs.map +1 -0
  210. package/dist/oracle.d.cts +25 -0
  211. package/dist/oracle.d.cts.map +1 -0
  212. package/dist/oracle.d.mts +25 -0
  213. package/dist/oracle.d.mts.map +1 -0
  214. package/dist/oracle.mjs +114 -0
  215. package/dist/oracle.mjs.map +1 -0
  216. package/dist/utils.cjs +129 -0
  217. package/dist/utils.cjs.map +1 -0
  218. package/dist/utils.d.cts +44 -0
  219. package/dist/utils.d.cts.map +1 -0
  220. package/dist/utils.d.mts +44 -0
  221. package/dist/utils.d.mts.map +1 -0
  222. package/dist/utils.mjs +115 -0
  223. package/dist/utils.mjs.map +1 -0
  224. package/docs/SUMMARY.md +10 -0
  225. package/docs/api-reference.md +32 -0
  226. package/docs/architecture.md +14 -0
  227. package/docs/common-operations.md +52 -0
  228. package/docs/error-handling.md +17 -0
  229. package/docs/getting-started.md +60 -0
  230. package/docs/introduction.md +15 -0
  231. package/docs/lp-specific-features.md +96 -0
  232. package/docs/type-safety.md +29 -0
  233. package/eslint.config.mjs +18 -0
  234. package/package.json +42 -0
  235. package/src/abstract/BaseAPI.ts +575 -0
  236. package/src/abstract/BaseDataAPI.ts +207 -0
  237. package/src/abstract/index.ts +7 -0
  238. package/src/api.ts +1100 -0
  239. package/src/bcs.ts +45 -0
  240. package/src/consts/deployments-shared-mainnet.json +50 -0
  241. package/src/consts/deployments-shared-testnet.json +45 -0
  242. package/src/consts/deployments-slp-mainnet.json +600 -0
  243. package/src/consts/deployments-slp-testnet.json +87 -0
  244. package/src/consts/deployments-usdz-mainnet.json +494 -0
  245. package/src/consts/deployments-usdz-testnet.json +98 -0
  246. package/src/consts/deployments-zbtcvc-mainnet.json +180 -0
  247. package/src/consts/deployments-zlp-mainnet.json +791 -0
  248. package/src/consts/deployments-zlp-testnet.json +76 -0
  249. package/src/consts/index.ts +345 -0
  250. package/src/consts/price_id_to_object_id.mainnet.json +56 -0
  251. package/src/consts/price_id_to_object_id.testnet.json +17 -0
  252. package/src/data.ts +1279 -0
  253. package/src/factory/SDKFactory.ts +340 -0
  254. package/src/implementations/SLPAPI.ts +2722 -0
  255. package/src/implementations/SLPDataAPI.ts +1839 -0
  256. package/src/implementations/USDZAPI.ts +2488 -0
  257. package/src/implementations/USDZDataAPI.ts +1548 -0
  258. package/src/implementations/ZBTCVCAPI.ts +1337 -0
  259. package/src/implementations/ZBTCVCDataAPI.ts +993 -0
  260. package/src/implementations/ZLPAPI.ts +2888 -0
  261. package/src/implementations/ZLPDataAPI.ts +1603 -0
  262. package/src/implementations/index.ts +16 -0
  263. package/src/index.ts +58 -0
  264. package/src/interfaces/base.ts +838 -0
  265. package/src/interfaces/index.ts +50 -0
  266. package/src/interfaces/slp.ts +268 -0
  267. package/src/interfaces/usdz.ts +181 -0
  268. package/src/interfaces/zbtcvc.ts +116 -0
  269. package/src/interfaces/zlp.ts +244 -0
  270. package/src/oracle.ts +153 -0
  271. package/src/utils.ts +168 -0
  272. package/tests/api.test.ts +219 -0
  273. package/tests/data.test.ts +156 -0
  274. package/tests/oracle.test.ts +33 -0
  275. package/tsconfig.json +22 -0
@@ -0,0 +1,244 @@
1
+ /**
2
+ * ZLP-specific interfaces and types
3
+ * Extends base interfaces with ZLP-specific implementations
4
+ */
5
+
6
+ import type { Transaction, TransactionObjectArgument } from '@mysten/sui/transactions'
7
+
8
+ import type {
9
+ IBaseAPI,
10
+ IBaseCredential,
11
+ IBaseDataAPI,
12
+ IBaseFundingFeeModel,
13
+ IBaseHistory,
14
+ IBaseMarketInfo,
15
+ IBaseMarketValuationInfo,
16
+ IBaseOrderCapInfo,
17
+ IBaseOrderInfo,
18
+ IBasePositionCapInfo,
19
+ IBasePositionConfig,
20
+ IBasePositionInfo,
21
+ IBaseRebaseFeeModel,
22
+ IBaseReservingFeeModel,
23
+ IBaseStakePool,
24
+ IBaseSymbolInfo,
25
+ IBaseVaultInfo,
26
+ } from './base'
27
+
28
+ // ZLP-specific interfaces
29
+ export interface IZLPMarketValuationInfo extends IBaseMarketValuationInfo { }
30
+
31
+ export interface IZLPVaultInfo extends IBaseVaultInfo { }
32
+
33
+ export interface IZLPReservingFeeModel extends IBaseReservingFeeModel { }
34
+
35
+ export interface IZLPFundingFeeModel extends IBaseFundingFeeModel { }
36
+
37
+ export interface IZLPRebaseFeeModel extends IBaseRebaseFeeModel { }
38
+
39
+ export interface IZLPSymbolInfo extends IBaseSymbolInfo { }
40
+
41
+ export interface IZLPMarketInfo extends IBaseMarketInfo {
42
+ // ZLP-specific market info fields can be added here
43
+ }
44
+
45
+ export interface IZLPPositionInfo extends IBasePositionInfo {
46
+ // ZLP-specific position fields can be added here
47
+ }
48
+
49
+ export interface IZLPPositionConfig extends IBasePositionConfig { }
50
+
51
+ export interface IZLPPositionCapInfo extends IBasePositionCapInfo { }
52
+
53
+ export interface IZLPOrderInfo extends IBaseOrderInfo { }
54
+
55
+ export interface IZLPOrderCapInfo extends IBaseOrderCapInfo { }
56
+
57
+ export interface IZLPHistory extends IBaseHistory {
58
+ // ZLP-specific history fields can be added here
59
+ }
60
+
61
+ export interface IZLPStakePool extends IBaseStakePool {
62
+ // ZLP-specific stake pool fields can be added here
63
+ rewardRate: bigint
64
+ }
65
+
66
+ export interface IZLPCredential extends IBaseCredential {
67
+ // ZLP-specific credential fields can be added here
68
+ }
69
+
70
+ // ZLP Symbol Config interfaces
71
+ export interface IZLPPositionInstantExitFeeConfig {
72
+ instant_exit_tier_1_duration_threshold: number
73
+ instant_exit_tier_1_fee_bps: number
74
+ instant_exit_tier_1_fee_enabled: boolean
75
+ instant_exit_tier_2_duration_threshold: number
76
+ instant_exit_tier_2_fee_bps: number
77
+ instant_exit_tier_2_fee_enabled: boolean
78
+ instant_exit_tier_3_duration_threshold: number
79
+ instant_exit_tier_3_fee_bps: number
80
+ instant_exit_tier_3_fee_enabled: boolean
81
+ }
82
+
83
+ export interface IZLPSymbolConfig {
84
+ id: string
85
+ max_opening_size: number
86
+ max_opening_size_enabled: boolean
87
+ max_opening_size_per_position: number
88
+ max_opening_size_per_position_enabled: boolean
89
+ instant_exit_fee_config: IZLPPositionInstantExitFeeConfig
90
+ }
91
+
92
+ export interface IZLPOiFundingModel {
93
+ id: string
94
+ multiplier: number
95
+ exponent: number
96
+ max: number
97
+ }
98
+ export interface IZLPOiFundingState {
99
+ id: string
100
+ enabled: boolean
101
+ last_update: number
102
+ model: IZLPOiFundingModel
103
+ }
104
+
105
+ /**
106
+ * Price impact configuration for a trading symbol.
107
+ * Used to compute dynamic spreads based on OI skew.
108
+ */
109
+ export interface IZLPPriceImpactConfig {
110
+ id: string
111
+ /** Whether price impact is enabled for this symbol */
112
+ enabled: boolean
113
+ /** Base spread applied to all trades (as a rate, e.g., 0.001 = 0.1%) */
114
+ baseSpreadRate: number
115
+ /** Maximum additional dynamic spread based on OI skew */
116
+ maxDynamicSpreadRate: number
117
+ /** Cap for total spread (base + dynamic) */
118
+ maxTotalSpreadRate: number
119
+ /** Exponent for spread curve (1.0=linear, 2.0=quadratic, 3.0=cubic) */
120
+ impactExponent: number
121
+ /** Maximum OI for long side used in skew calculation */
122
+ maxOiLong: number
123
+ /** Maximum OI for short side used in skew calculation */
124
+ maxOiShort: number
125
+ /** Reference position size for size scaling. If zero, size scaling is disabled. */
126
+ referenceSize: number
127
+ }
128
+
129
+ // ZLP-specific data API interface
130
+ export interface IZLPDataAPI extends IBaseDataAPI {
131
+ getSymbolConfig: (indexToken: string, long: boolean) => Promise<IZLPSymbolConfig | null>
132
+ getPriceImpactConfig: (indexToken: string) => Promise<IZLPPriceImpactConfig | null>
133
+ }
134
+
135
+ // ZLP-specific API interface
136
+ export interface IZLPAPI extends IBaseAPI {
137
+ adminIssueLootbox: (
138
+ coin: string,
139
+ tiers: number[],
140
+ beneficiaries: string[],
141
+ amounts: number[],
142
+ tx: Transaction,
143
+ ) => void
144
+
145
+ depositToPrizePool: (
146
+ coin: string,
147
+ coinObjects: string[],
148
+ amount: number,
149
+ ) => Transaction
150
+
151
+ withdrawFromPricePool: (
152
+ coin: string,
153
+ amount: number
154
+ ) => Transaction
155
+
156
+ openLootbox: (
157
+ coin: string,
158
+ lootbox: string
159
+ ) => Transaction
160
+
161
+ adminIssueLootboxV2: (
162
+ coin: string,
163
+ tiers: number[],
164
+ beneficiaries: string[],
165
+ amounts: number[],
166
+ titles: string[],
167
+ descriptions: string[],
168
+ tx: Transaction,
169
+ ) => void
170
+
171
+ openLootboxV2: (
172
+ coin: string,
173
+ lootbox: string
174
+ ) => Transaction
175
+
176
+ adminIssueVestingLootboxV2: (
177
+ coin: string,
178
+ beneficiaries: string[],
179
+ unlockedAmounts: number[],
180
+ lockedAmounts: number[],
181
+ lockDurationsMs: number[],
182
+ tx: Transaction,
183
+ ) => void
184
+
185
+ claimUnlockedV2: (
186
+ coin: string,
187
+ vestingLootbox: string,
188
+ ) => Transaction
189
+
190
+ claimVestedV2: (
191
+ coin: string,
192
+ vestingLootbox: string,
193
+ ) => Transaction
194
+
195
+ destroyVestingLootboxV2: (
196
+ coin: string,
197
+ vestingLootbox: string,
198
+ ) => Transaction
199
+
200
+ depositPtb: (
201
+ coin: string,
202
+ coinObjects: string[],
203
+ amount: number,
204
+ minAmountOut?: number,
205
+ referralAddress?: string,
206
+ sender?: string,
207
+ tx?: Transaction,
208
+ sponsoredTx?: boolean,
209
+ suiCoinObjectsForPythUpdate?: string[],
210
+ ) => Promise<TransactionObjectArgument>
211
+
212
+ stakeCoinObject: (
213
+ coinObject: TransactionObjectArgument,
214
+ pool: string,
215
+ tx?: Transaction,
216
+ ) => Transaction
217
+
218
+ withdrawLpCoinObject: (
219
+ coin: string,
220
+ lpCoinObject: TransactionObjectArgument,
221
+ minAmountOut: number,
222
+ tx?: Transaction,
223
+ ) => Promise<Transaction>
224
+
225
+ withdrawFromVaultPtb: (
226
+ pool: string,
227
+ credential: string,
228
+ withdrawAmount: bigint,
229
+ tx: Transaction,
230
+ ) => TransactionObjectArgument
231
+
232
+ claimRewardsPtb: (
233
+ pool: string,
234
+ credential: string,
235
+ tx: Transaction,
236
+ ) => TransactionObjectArgument
237
+
238
+ unstakeToCoins: (
239
+ credentials: IZLPCredential[],
240
+ amount: bigint,
241
+ pool: string,
242
+ tx: Transaction,
243
+ ) => TransactionObjectArgument[]
244
+ }
package/src/oracle.ts ADDED
@@ -0,0 +1,153 @@
1
+ import type { SuiClient } from '@mysten/sui/client'
2
+ import { Transaction } from '@mysten/sui/transactions'
3
+ import type { PriceFeed } from '@pythnetwork/pyth-sui-js'
4
+ import { SuiPriceServiceConnection, SuiPythClient } from '@pythnetwork/pyth-sui-js'
5
+
6
+ import type { IConsts, Network } from './consts'
7
+ import { getConsts, getPriceIdToPythFeeder, getPythFeederToId, getPythFeederToPriceId, getSharedConfig, LPToken } from './consts'
8
+ import { createJsonRpcProvider } from './utils'
9
+
10
+ export class OracleAPI {
11
+ network: Network
12
+ consts: IConsts
13
+ sharedConfig: IConsts
14
+ connectionURL: string
15
+ PythFeederToPriceId: Record<string, string>
16
+ PythFeederToId: Record<string, string>
17
+ provider: SuiClient
18
+ client: SuiPythClient
19
+
20
+ private priceCache: Record<string, PriceFeed> = {}
21
+
22
+ constructor(
23
+ network: Network,
24
+ provider: SuiClient | null,
25
+ // todo 需要从配置文件中获取
26
+ connectionURL: string,
27
+ lpToken: LPToken = LPToken.ZLP,
28
+ ) {
29
+ this.network = network
30
+ this.consts = getConsts(network, lpToken)
31
+ this.sharedConfig = getSharedConfig(network)
32
+ this.connectionURL = connectionURL
33
+ this.PythFeederToPriceId = getPythFeederToPriceId(network)
34
+ this.PythFeederToId = getPythFeederToId(network)
35
+ this.provider = provider || createJsonRpcProvider(network)
36
+ this.client = new SuiPythClient(this.provider, this.consts.pythFeeder.state, this.consts.pythFeeder.wormhole.state)
37
+ }
38
+
39
+ validateCache() {
40
+ const now = Date.now() / 1000
41
+ for (const key in this.priceCache) {
42
+ if (now - (this.priceCache[key].getPriceUnchecked().publishTime || 0) > 7) {
43
+ delete this.priceCache[key]
44
+ }
45
+ }
46
+ }
47
+
48
+ async getOraclePrice(tokenId: string): Promise<PriceFeed> {
49
+ this.validateCache()
50
+ if (this.priceCache[tokenId]) {
51
+ return this.priceCache[tokenId]
52
+ }
53
+ const res = await this.getOraclePrices([tokenId])
54
+ if (!res || !res[0]) {
55
+ throw new Error(`Unknown token: ${tokenId}`)
56
+ }
57
+ this.priceCache[tokenId] = res[0]
58
+ return res[0]
59
+ }
60
+
61
+ async getOraclePrices(tokens: string[]): Promise<PriceFeed[]> {
62
+ const connection = new SuiPriceServiceConnection(this.connectionURL, {
63
+ priceFeedRequestConfig: {
64
+ binary: true,
65
+ },
66
+ })
67
+ const pythObjectIds = tokens.map(token => this.consts.pythFeeder.feeder[token])
68
+ const priceFeedIds = pythObjectIds.map(pythObjectId => `0x${this.PythFeederToPriceId[pythObjectId]}`)
69
+ const price = await connection.getLatestPriceFeeds(priceFeedIds)
70
+ return price || []
71
+ }
72
+
73
+ // memory leak
74
+ async subOraclePrices(tokens: string[], callback: (price: PriceFeed) => void): Promise<() => void> {
75
+ const connection = new SuiPriceServiceConnection(this.connectionURL, {
76
+ priceFeedRequestConfig: {
77
+ binary: true,
78
+ },
79
+ })
80
+ const pythObjectIds = tokens.map(token => this.consts.pythFeeder.feeder[token])
81
+ const priceFeedIds = pythObjectIds.map(pythObjectId => `0x${this.PythFeederToPriceId[pythObjectId]}`)
82
+ await connection.subscribePriceFeedUpdates(priceFeedIds, (price: any) => {
83
+ price.id = this.PythFeederToId[getPriceIdToPythFeeder(this.network)[price.id]]
84
+ this.priceCache[price.id] = price
85
+ callback(price)
86
+ })
87
+
88
+ return () => {
89
+ connection.unsubscribePriceFeedUpdates(priceFeedIds)
90
+ }
91
+ }
92
+
93
+ async initOracleTxb(
94
+ tokens: string[],
95
+ tx?: Transaction,
96
+ sponsoredTx?: boolean,
97
+ suiCoinObject?: any,
98
+ ) {
99
+ let tx_ = tx
100
+ if (!tx_) {
101
+ tx_ = new Transaction()
102
+ }
103
+ // Remove redundant tokens first
104
+ const uniqueTokens = Array.from(new Set(tokens))
105
+
106
+ const connection = new SuiPriceServiceConnection(this.connectionURL, {
107
+ priceFeedRequestConfig: {
108
+ binary: true,
109
+ },
110
+ })
111
+ let pythObjectIds = uniqueTokens.map(token => this.consts.pythFeeder.feeder[token])
112
+ // remove dupe object ids. If any of the object ids are duplicates the call will fail
113
+ pythObjectIds = [...new Set(pythObjectIds)].filter(Boolean)
114
+ const needUpdateObjectIds = (await this.provider.multiGetObjects({
115
+ ids: pythObjectIds,
116
+ options: {
117
+ showContent: true,
118
+ },
119
+ })).map(pythObject => [
120
+ Number.parseInt((pythObject.data?.content as any).fields.price_info.fields.arrival_time || 0, 10) - Date.now() / 1000,
121
+ pythObject.data?.objectId,
122
+ ]).filter((x: any) => Math.abs(x[0]) >= 0).map(x => x[1] as string)
123
+
124
+ if (needUpdateObjectIds.length === 0) {
125
+ return tx_
126
+ }
127
+
128
+ const priceFeedIds = needUpdateObjectIds
129
+ .map(pythObjectId => this.PythFeederToPriceId[pythObjectId])
130
+ .filter(Boolean)
131
+ .map(x => `0x${x}`)
132
+
133
+ const priceUpdateData = await connection.getPriceFeedsUpdateData(priceFeedIds)
134
+
135
+ if (sponsoredTx) {
136
+ const baseUpdateFee = await this.getBaseUpdateFee()
137
+ const pythUpdateCoins = tx_.splitCoins(
138
+ suiCoinObject,
139
+ priceFeedIds.map(() => tx_.pure.u64(baseUpdateFee)),
140
+ )
141
+ await this.client.updatePriceFeedsWithCoins(tx_, priceUpdateData, priceFeedIds, pythUpdateCoins)
142
+ }
143
+ else {
144
+ await this.client.updatePriceFeeds(tx_, priceUpdateData, priceFeedIds)
145
+ }
146
+
147
+ return tx_
148
+ }
149
+
150
+ async getBaseUpdateFee(): Promise<number> {
151
+ return await this.client.getBaseUpdateFee()
152
+ }
153
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,168 @@
1
+ import { SuiClient, SuiHTTPTransport } from '@mysten/sui/client'
2
+
3
+ import type { IConsts } from './consts'
4
+ import { Network } from './consts'
5
+
6
+ export function createJsonRpcProvider(network: Network) {
7
+ let url = ''
8
+ switch (network) {
9
+ case Network.DEVNET: {
10
+ url = 'https://explorer-rpc.devnet.sui.io/'
11
+ break
12
+ }
13
+ case Network.TESTNET: {
14
+ url = 'https://sui-testnet.blockvision.org/v1/2sXCJEJNOCdIJhLaDtyhKzqwn6k'
15
+ break
16
+ }
17
+ case Network.MAINNET: {
18
+ url = 'https://rpc-mainnet.suiscan.xyz'
19
+ break
20
+ }
21
+ default: {
22
+ url = 'https://explorer-rpc.devnet.sui.io/'
23
+ }
24
+ }
25
+
26
+ return new SuiClient({
27
+ transport: new SuiHTTPTransport({ url }),
28
+ })
29
+ }
30
+
31
+ export function decimalToObject(decimal: { value: string }) {
32
+ return Number(BigInt(decimal.value)) / 1e18
33
+ }
34
+
35
+ export function rateToObject(rate: { value: string }) {
36
+ return Number(BigInt(rate.value)) / 1e18
37
+ }
38
+
39
+ interface SRate {
40
+ fields: {
41
+ is_positive: boolean
42
+ value: {
43
+ fields: {
44
+ value: string | number
45
+ }
46
+ }
47
+ }
48
+ }
49
+
50
+ export function sRateToObject(sRate: SRate) {
51
+ const sign = sRate.fields.is_positive ? 1 : -1
52
+ return Number(BigInt(sRate.fields.value.fields.value)) / 1e18 * sign
53
+ }
54
+
55
+ interface SuiDecimal {
56
+ fields: {
57
+ is_positive: boolean
58
+ value: {
59
+ fields: {
60
+ value: string
61
+ }
62
+ }
63
+ }
64
+ }
65
+
66
+ export function sDecimalToObject(sDecimal: SuiDecimal) {
67
+ const sign = sDecimal.fields.is_positive ? 1 : -1
68
+ return Number(BigInt(sDecimal.fields.value.fields.value)) / 1e18 * sign
69
+ }
70
+
71
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
72
+ export function parseValue(field: any): number {
73
+ if (field.type && field.type.endsWith('::decimal::Decimal')) {
74
+ return decimalToObject({ value: field.fields.value })
75
+ }
76
+ else if (field.type && field.type.endsWith('::rate::Rate')) {
77
+ return rateToObject({ value: field.fields.value })
78
+ }
79
+ else if (field.type && field.type.endsWith('::srate::SRate')) {
80
+ return sRateToObject(field)
81
+ }
82
+ else if (field.type && field.type.endsWith('::sdecimal::SDecimal')) {
83
+ return sDecimalToObject(field)
84
+ }
85
+ return Number.parseInt(field, 10)
86
+ }
87
+
88
+ export type ReversedKeyValue<T extends object> = {
89
+ [K in keyof T]: T[K] extends keyof T
90
+ ? keyof T
91
+ : T[K] extends keyof T | infer V
92
+ ? V
93
+ : never;
94
+ }
95
+
96
+ export function reverseKeyValue<T extends object>(obj: T): ReversedKeyValue<T> {
97
+ const reversed: any = {}
98
+
99
+ for (const key in obj) {
100
+ if (Object.hasOwn(obj, key)) {
101
+ const value = obj[key]
102
+ reversed[value] = key
103
+ }
104
+ }
105
+
106
+ return reversed
107
+ }
108
+
109
+ export function parseSymbolKey(input: string): string[] {
110
+ // This regex will match uppercase letters and numeric letters
111
+ const regex = /[A-Z0-9]/
112
+ let result: string[] = []
113
+ let wordStart = 0
114
+
115
+ for (let i = 1; i < input.length; i += 1) {
116
+ if (regex.test(input[i])) {
117
+ // Found an uppercase letter, so we split the string here
118
+ result.push(input.slice(wordStart, i))
119
+ wordStart = i
120
+ }
121
+ }
122
+
123
+ // Add the last word to the result array
124
+ result.push(input.slice(wordStart))
125
+
126
+ // Convert the words to lowercase, handle words ending with '_'
127
+ result = result.map(word => word.endsWith('_') ? word.slice(0, -1).toLowerCase() : word.toLowerCase())
128
+
129
+ return result
130
+ }
131
+
132
+ export function upperFirstCharacter(word: string): string {
133
+ return word.charAt(0).toUpperCase() + word.slice(1)
134
+ }
135
+
136
+ function isLetter(char: string): boolean {
137
+ return /^[a-z]$/i.test(char)
138
+ }
139
+
140
+ export function joinSymbol(direction: string, token: string): string {
141
+ const firstChar = token.charAt(0)
142
+ return `${direction}${isLetter(firstChar) ? upperFirstCharacter(token) : `_${token}`}`
143
+ }
144
+
145
+ export function suiSymbolToSymbol(symbol: string, consts: IConsts): string {
146
+ if (symbol === '0x2::sui::SUI') {
147
+ return 'sui'
148
+ }
149
+ const ret: Record<string, string> = {}
150
+ for (const key of Object.keys(consts.coins)) {
151
+ ret[consts.coins[key].module] = key
152
+ }
153
+ return ret[symbol]
154
+ }
155
+
156
+ export function base64ToUint8Array(base64: string) {
157
+ // Decode Base64 to binary
158
+ const binary = atob(base64)
159
+
160
+ // Create a Uint8Array from the binary data
161
+ const uint8Array = new Uint8Array(binary.length)
162
+ // Populate the Uint8Array with the binary data
163
+ for (let i = 0; i < binary.length; i += 1) {
164
+ uint8Array[i] = binary.codePointAt(i)!
165
+ }
166
+
167
+ return uint8Array
168
+ }