@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.
- package/.claude/settings.local.json +9 -0
- package/.gitattributes +4 -0
- package/.prettierrc.js +9 -0
- package/README.md +28 -0
- package/dist/abstract/BaseAPI.cjs +206 -0
- package/dist/abstract/BaseAPI.cjs.map +1 -0
- package/dist/abstract/BaseAPI.d.cts +172 -0
- package/dist/abstract/BaseAPI.d.cts.map +1 -0
- package/dist/abstract/BaseAPI.d.mts +172 -0
- package/dist/abstract/BaseAPI.d.mts.map +1 -0
- package/dist/abstract/BaseAPI.mjs +202 -0
- package/dist/abstract/BaseAPI.mjs.map +1 -0
- package/dist/abstract/BaseDataAPI.cjs +140 -0
- package/dist/abstract/BaseDataAPI.cjs.map +1 -0
- package/dist/abstract/BaseDataAPI.d.cts +89 -0
- package/dist/abstract/BaseDataAPI.d.cts.map +1 -0
- package/dist/abstract/BaseDataAPI.d.mts +89 -0
- package/dist/abstract/BaseDataAPI.d.mts.map +1 -0
- package/dist/abstract/BaseDataAPI.mjs +136 -0
- package/dist/abstract/BaseDataAPI.mjs.map +1 -0
- package/dist/abstract/index.cjs +12 -0
- package/dist/abstract/index.cjs.map +1 -0
- package/dist/abstract/index.d.cts +7 -0
- package/dist/abstract/index.d.cts.map +1 -0
- package/dist/abstract/index.d.mts +7 -0
- package/dist/abstract/index.d.mts.map +1 -0
- package/dist/abstract/index.mjs +7 -0
- package/dist/abstract/index.mjs.map +1 -0
- package/dist/api.cjs +779 -0
- package/dist/api.cjs.map +1 -0
- package/dist/api.d.cts +75 -0
- package/dist/api.d.cts.map +1 -0
- package/dist/api.d.mts +75 -0
- package/dist/api.d.mts.map +1 -0
- package/dist/api.mjs +775 -0
- package/dist/api.mjs.map +1 -0
- package/dist/bcs.cjs +42 -0
- package/dist/bcs.cjs.map +1 -0
- package/dist/bcs.d.cts +91 -0
- package/dist/bcs.d.cts.map +1 -0
- package/dist/bcs.d.mts +91 -0
- package/dist/bcs.d.mts.map +1 -0
- package/dist/bcs.mjs +39 -0
- package/dist/bcs.mjs.map +1 -0
- package/dist/consts/deployments-shared-mainnet.json +50 -0
- package/dist/consts/deployments-shared-testnet.json +45 -0
- package/dist/consts/deployments-slp-mainnet.json +600 -0
- package/dist/consts/deployments-slp-testnet.json +87 -0
- package/dist/consts/deployments-usdz-mainnet.json +494 -0
- package/dist/consts/deployments-usdz-testnet.json +98 -0
- package/dist/consts/deployments-zbtcvc-mainnet.json +180 -0
- package/dist/consts/deployments-zlp-mainnet.json +791 -0
- package/dist/consts/deployments-zlp-testnet.json +76 -0
- package/dist/consts/index.cjs +200 -0
- package/dist/consts/index.cjs.map +1 -0
- package/dist/consts/index.d.cts +157 -0
- package/dist/consts/index.d.cts.map +1 -0
- package/dist/consts/index.d.mts +157 -0
- package/dist/consts/index.d.mts.map +1 -0
- package/dist/consts/index.mjs +189 -0
- package/dist/consts/index.mjs.map +1 -0
- package/dist/consts/price_id_to_object_id.mainnet.json +56 -0
- package/dist/consts/price_id_to_object_id.testnet.json +17 -0
- package/dist/data.cjs +919 -0
- package/dist/data.cjs.map +1 -0
- package/dist/data.d.cts +235 -0
- package/dist/data.d.cts.map +1 -0
- package/dist/data.d.mts +235 -0
- package/dist/data.d.mts.map +1 -0
- package/dist/data.mjs +915 -0
- package/dist/data.mjs.map +1 -0
- package/dist/factory/SDKFactory.cjs +228 -0
- package/dist/factory/SDKFactory.cjs.map +1 -0
- package/dist/factory/SDKFactory.d.cts +84 -0
- package/dist/factory/SDKFactory.d.cts.map +1 -0
- package/dist/factory/SDKFactory.d.mts +84 -0
- package/dist/factory/SDKFactory.d.mts.map +1 -0
- package/dist/factory/SDKFactory.mjs +222 -0
- package/dist/factory/SDKFactory.mjs.map +1 -0
- package/dist/implementations/SLPAPI.cjs +1794 -0
- package/dist/implementations/SLPAPI.cjs.map +1 -0
- package/dist/implementations/SLPAPI.d.cts +183 -0
- package/dist/implementations/SLPAPI.d.cts.map +1 -0
- package/dist/implementations/SLPAPI.d.mts +183 -0
- package/dist/implementations/SLPAPI.d.mts.map +1 -0
- package/dist/implementations/SLPAPI.mjs +1790 -0
- package/dist/implementations/SLPAPI.mjs.map +1 -0
- package/dist/implementations/SLPDataAPI.cjs +1384 -0
- package/dist/implementations/SLPDataAPI.cjs.map +1 -0
- package/dist/implementations/SLPDataAPI.d.cts +158 -0
- package/dist/implementations/SLPDataAPI.d.cts.map +1 -0
- package/dist/implementations/SLPDataAPI.d.mts +158 -0
- package/dist/implementations/SLPDataAPI.d.mts.map +1 -0
- package/dist/implementations/SLPDataAPI.mjs +1380 -0
- package/dist/implementations/SLPDataAPI.mjs.map +1 -0
- package/dist/implementations/USDZAPI.cjs +1676 -0
- package/dist/implementations/USDZAPI.cjs.map +1 -0
- package/dist/implementations/USDZAPI.d.cts +180 -0
- package/dist/implementations/USDZAPI.d.cts.map +1 -0
- package/dist/implementations/USDZAPI.d.mts +180 -0
- package/dist/implementations/USDZAPI.d.mts.map +1 -0
- package/dist/implementations/USDZAPI.mjs +1672 -0
- package/dist/implementations/USDZAPI.mjs.map +1 -0
- package/dist/implementations/USDZDataAPI.cjs +1209 -0
- package/dist/implementations/USDZDataAPI.cjs.map +1 -0
- package/dist/implementations/USDZDataAPI.d.cts +191 -0
- package/dist/implementations/USDZDataAPI.d.cts.map +1 -0
- package/dist/implementations/USDZDataAPI.d.mts +191 -0
- package/dist/implementations/USDZDataAPI.d.mts.map +1 -0
- package/dist/implementations/USDZDataAPI.mjs +1205 -0
- package/dist/implementations/USDZDataAPI.mjs.map +1 -0
- package/dist/implementations/ZBTCVCAPI.cjs +906 -0
- package/dist/implementations/ZBTCVCAPI.cjs.map +1 -0
- package/dist/implementations/ZBTCVCAPI.d.cts +107 -0
- package/dist/implementations/ZBTCVCAPI.d.cts.map +1 -0
- package/dist/implementations/ZBTCVCAPI.d.mts +107 -0
- package/dist/implementations/ZBTCVCAPI.d.mts.map +1 -0
- package/dist/implementations/ZBTCVCAPI.mjs +902 -0
- package/dist/implementations/ZBTCVCAPI.mjs.map +1 -0
- package/dist/implementations/ZBTCVCDataAPI.cjs +829 -0
- package/dist/implementations/ZBTCVCDataAPI.cjs.map +1 -0
- package/dist/implementations/ZBTCVCDataAPI.d.cts +94 -0
- package/dist/implementations/ZBTCVCDataAPI.d.cts.map +1 -0
- package/dist/implementations/ZBTCVCDataAPI.d.mts +94 -0
- package/dist/implementations/ZBTCVCDataAPI.d.mts.map +1 -0
- package/dist/implementations/ZBTCVCDataAPI.mjs +825 -0
- package/dist/implementations/ZBTCVCDataAPI.mjs.map +1 -0
- package/dist/implementations/ZLPAPI.cjs +1948 -0
- package/dist/implementations/ZLPAPI.cjs.map +1 -0
- package/dist/implementations/ZLPAPI.d.cts +192 -0
- package/dist/implementations/ZLPAPI.d.cts.map +1 -0
- package/dist/implementations/ZLPAPI.d.mts +192 -0
- package/dist/implementations/ZLPAPI.d.mts.map +1 -0
- package/dist/implementations/ZLPAPI.mjs +1944 -0
- package/dist/implementations/ZLPAPI.mjs.map +1 -0
- package/dist/implementations/ZLPDataAPI.cjs +1267 -0
- package/dist/implementations/ZLPDataAPI.cjs.map +1 -0
- package/dist/implementations/ZLPDataAPI.d.cts +193 -0
- package/dist/implementations/ZLPDataAPI.d.cts.map +1 -0
- package/dist/implementations/ZLPDataAPI.d.mts +193 -0
- package/dist/implementations/ZLPDataAPI.d.mts.map +1 -0
- package/dist/implementations/ZLPDataAPI.mjs +1263 -0
- package/dist/implementations/ZLPDataAPI.mjs.map +1 -0
- package/dist/implementations/index.cjs +26 -0
- package/dist/implementations/index.cjs.map +1 -0
- package/dist/implementations/index.d.cts +13 -0
- package/dist/implementations/index.d.cts.map +1 -0
- package/dist/implementations/index.d.mts +13 -0
- package/dist/implementations/index.d.mts.map +1 -0
- package/dist/implementations/index.mjs +15 -0
- package/dist/implementations/index.mjs.map +1 -0
- package/dist/index.cjs +69 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +51 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +51 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +51 -0
- package/dist/index.mjs.map +1 -0
- package/dist/interfaces/base.cjs +7 -0
- package/dist/interfaces/base.cjs.map +1 -0
- package/dist/interfaces/base.d.cts +346 -0
- package/dist/interfaces/base.d.cts.map +1 -0
- package/dist/interfaces/base.d.mts +346 -0
- package/dist/interfaces/base.d.mts.map +1 -0
- package/dist/interfaces/base.mjs +6 -0
- package/dist/interfaces/base.mjs.map +1 -0
- package/dist/interfaces/index.cjs +31 -0
- package/dist/interfaces/index.cjs.map +1 -0
- package/dist/interfaces/index.d.cts +15 -0
- package/dist/interfaces/index.d.cts.map +1 -0
- package/dist/interfaces/index.d.mts +15 -0
- package/dist/interfaces/index.d.mts.map +1 -0
- package/dist/interfaces/index.mjs +15 -0
- package/dist/interfaces/index.mjs.map +1 -0
- package/dist/interfaces/slp.cjs +7 -0
- package/dist/interfaces/slp.cjs.map +1 -0
- package/dist/interfaces/slp.d.cts +179 -0
- package/dist/interfaces/slp.d.cts.map +1 -0
- package/dist/interfaces/slp.d.mts +179 -0
- package/dist/interfaces/slp.d.mts.map +1 -0
- package/dist/interfaces/slp.mjs +6 -0
- package/dist/interfaces/slp.mjs.map +1 -0
- package/dist/interfaces/usdz.cjs +7 -0
- package/dist/interfaces/usdz.cjs.map +1 -0
- package/dist/interfaces/usdz.d.cts +104 -0
- package/dist/interfaces/usdz.d.cts.map +1 -0
- package/dist/interfaces/usdz.d.mts +104 -0
- package/dist/interfaces/usdz.d.mts.map +1 -0
- package/dist/interfaces/usdz.mjs +6 -0
- package/dist/interfaces/usdz.mjs.map +1 -0
- package/dist/interfaces/zbtcvc.cjs +7 -0
- package/dist/interfaces/zbtcvc.cjs.map +1 -0
- package/dist/interfaces/zbtcvc.d.cts +64 -0
- package/dist/interfaces/zbtcvc.d.cts.map +1 -0
- package/dist/interfaces/zbtcvc.d.mts +64 -0
- package/dist/interfaces/zbtcvc.d.mts.map +1 -0
- package/dist/interfaces/zbtcvc.mjs +6 -0
- package/dist/interfaces/zbtcvc.mjs.map +1 -0
- package/dist/interfaces/zlp.cjs +7 -0
- package/dist/interfaces/zlp.cjs.map +1 -0
- package/dist/interfaces/zlp.d.cts +114 -0
- package/dist/interfaces/zlp.d.cts.map +1 -0
- package/dist/interfaces/zlp.d.mts +114 -0
- package/dist/interfaces/zlp.d.mts.map +1 -0
- package/dist/interfaces/zlp.mjs +6 -0
- package/dist/interfaces/zlp.mjs.map +1 -0
- package/dist/oracle.cjs +118 -0
- package/dist/oracle.cjs.map +1 -0
- package/dist/oracle.d.cts +25 -0
- package/dist/oracle.d.cts.map +1 -0
- package/dist/oracle.d.mts +25 -0
- package/dist/oracle.d.mts.map +1 -0
- package/dist/oracle.mjs +114 -0
- package/dist/oracle.mjs.map +1 -0
- package/dist/utils.cjs +129 -0
- package/dist/utils.cjs.map +1 -0
- package/dist/utils.d.cts +44 -0
- package/dist/utils.d.cts.map +1 -0
- package/dist/utils.d.mts +44 -0
- package/dist/utils.d.mts.map +1 -0
- package/dist/utils.mjs +115 -0
- package/dist/utils.mjs.map +1 -0
- package/docs/SUMMARY.md +10 -0
- package/docs/api-reference.md +32 -0
- package/docs/architecture.md +14 -0
- package/docs/common-operations.md +52 -0
- package/docs/error-handling.md +17 -0
- package/docs/getting-started.md +60 -0
- package/docs/introduction.md +15 -0
- package/docs/lp-specific-features.md +96 -0
- package/docs/type-safety.md +29 -0
- package/eslint.config.mjs +18 -0
- package/package.json +42 -0
- package/src/abstract/BaseAPI.ts +575 -0
- package/src/abstract/BaseDataAPI.ts +207 -0
- package/src/abstract/index.ts +7 -0
- package/src/api.ts +1100 -0
- package/src/bcs.ts +45 -0
- package/src/consts/deployments-shared-mainnet.json +50 -0
- package/src/consts/deployments-shared-testnet.json +45 -0
- package/src/consts/deployments-slp-mainnet.json +600 -0
- package/src/consts/deployments-slp-testnet.json +87 -0
- package/src/consts/deployments-usdz-mainnet.json +494 -0
- package/src/consts/deployments-usdz-testnet.json +98 -0
- package/src/consts/deployments-zbtcvc-mainnet.json +180 -0
- package/src/consts/deployments-zlp-mainnet.json +791 -0
- package/src/consts/deployments-zlp-testnet.json +76 -0
- package/src/consts/index.ts +345 -0
- package/src/consts/price_id_to_object_id.mainnet.json +56 -0
- package/src/consts/price_id_to_object_id.testnet.json +17 -0
- package/src/data.ts +1279 -0
- package/src/factory/SDKFactory.ts +340 -0
- package/src/implementations/SLPAPI.ts +2722 -0
- package/src/implementations/SLPDataAPI.ts +1839 -0
- package/src/implementations/USDZAPI.ts +2488 -0
- package/src/implementations/USDZDataAPI.ts +1548 -0
- package/src/implementations/ZBTCVCAPI.ts +1337 -0
- package/src/implementations/ZBTCVCDataAPI.ts +993 -0
- package/src/implementations/ZLPAPI.ts +2888 -0
- package/src/implementations/ZLPDataAPI.ts +1603 -0
- package/src/implementations/index.ts +16 -0
- package/src/index.ts +58 -0
- package/src/interfaces/base.ts +838 -0
- package/src/interfaces/index.ts +50 -0
- package/src/interfaces/slp.ts +268 -0
- package/src/interfaces/usdz.ts +181 -0
- package/src/interfaces/zbtcvc.ts +116 -0
- package/src/interfaces/zlp.ts +244 -0
- package/src/oracle.ts +153 -0
- package/src/utils.ts +168 -0
- package/tests/api.test.ts +219 -0
- package/tests/data.test.ts +156 -0
- package/tests/oracle.test.ts +33 -0
- 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
|
+
}
|