@megatao/sdk 1.1.0

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 (228) hide show
  1. package/.env.example +37 -0
  2. package/CHANGELOG.md +19 -0
  3. package/README.md +199 -0
  4. package/bin/alf +4 -0
  5. package/cli/README.md +198 -0
  6. package/cli/TEST_MANUAL.md +577 -0
  7. package/cli/commands/account.ts +545 -0
  8. package/cli/commands/funding.ts +481 -0
  9. package/cli/commands/liquidation.ts +523 -0
  10. package/cli/commands/market.ts +590 -0
  11. package/cli/commands/orders.ts +395 -0
  12. package/cli/commands/position.ts +1085 -0
  13. package/cli/commands/shared/positionUtils.ts +239 -0
  14. package/cli/commands/trading.ts +483 -0
  15. package/cli/commands/utils.ts +281 -0
  16. package/cli/commands/vault.ts +522 -0
  17. package/cli/index.ts +169 -0
  18. package/cli/interactive.ts +530 -0
  19. package/cli/utils/client.ts +457 -0
  20. package/cli/utils/config.ts +226 -0
  21. package/cli/utils/display.ts +258 -0
  22. package/cli/utils/index.ts +10 -0
  23. package/cli/utils/prompts.ts +364 -0
  24. package/config.example.json +23 -0
  25. package/dist/AlphaFuturesClient.d.ts +36 -0
  26. package/dist/AlphaFuturesClient.d.ts.map +1 -0
  27. package/dist/AlphaFuturesClient.js +116 -0
  28. package/dist/AlphaFuturesClient.js.map +1 -0
  29. package/dist/abi/Alpha.json +5987 -0
  30. package/dist/abi/abis.d.ts +319 -0
  31. package/dist/abi/abis.d.ts.map +1 -0
  32. package/dist/abi/abis.js +128 -0
  33. package/dist/abi/abis.js.map +1 -0
  34. package/dist/abi/index.d.ts +11 -0
  35. package/dist/abi/index.d.ts.map +1 -0
  36. package/dist/abi/index.js +15 -0
  37. package/dist/abi/index.js.map +1 -0
  38. package/dist/config/contracts.config.d.ts +70 -0
  39. package/dist/config/contracts.config.d.ts.map +1 -0
  40. package/dist/config/contracts.config.js +137 -0
  41. package/dist/config/contracts.config.js.map +1 -0
  42. package/dist/config/environments/alpha.config.d.ts +17 -0
  43. package/dist/config/environments/alpha.config.d.ts.map +1 -0
  44. package/dist/config/environments/alpha.config.js +140 -0
  45. package/dist/config/environments/alpha.config.js.map +1 -0
  46. package/dist/config/environments/beta.config.d.ts +16 -0
  47. package/dist/config/environments/beta.config.d.ts.map +1 -0
  48. package/dist/config/environments/beta.config.js +131 -0
  49. package/dist/config/environments/beta.config.js.map +1 -0
  50. package/dist/config/environments/dev.config.d.ts +13 -0
  51. package/dist/config/environments/dev.config.d.ts.map +1 -0
  52. package/dist/config/environments/dev.config.js +123 -0
  53. package/dist/config/environments/dev.config.js.map +1 -0
  54. package/dist/config/environments/index.d.ts +48 -0
  55. package/dist/config/environments/index.d.ts.map +1 -0
  56. package/dist/config/environments/index.js +81 -0
  57. package/dist/config/environments/index.js.map +1 -0
  58. package/dist/config/environments/localhost.config.d.ts +16 -0
  59. package/dist/config/environments/localhost.config.d.ts.map +1 -0
  60. package/dist/config/environments/localhost.config.js +152 -0
  61. package/dist/config/environments/localhost.config.js.map +1 -0
  62. package/dist/config/environments/prod.config.d.ts +20 -0
  63. package/dist/config/environments/prod.config.d.ts.map +1 -0
  64. package/dist/config/environments/prod.config.js +143 -0
  65. package/dist/config/environments/prod.config.js.map +1 -0
  66. package/dist/config/index.d.ts +7 -0
  67. package/dist/config/index.d.ts.map +1 -0
  68. package/dist/config/index.js +41 -0
  69. package/dist/config/index.js.map +1 -0
  70. package/dist/constants/assets.d.ts +76 -0
  71. package/dist/constants/assets.d.ts.map +1 -0
  72. package/dist/constants/assets.js +277 -0
  73. package/dist/constants/assets.js.map +1 -0
  74. package/dist/constants/contracts.d.ts +41 -0
  75. package/dist/constants/contracts.d.ts.map +1 -0
  76. package/dist/constants/contracts.js +57 -0
  77. package/dist/constants/contracts.js.map +1 -0
  78. package/dist/constants/index.d.ts +36 -0
  79. package/dist/constants/index.d.ts.map +1 -0
  80. package/dist/constants/index.js +75 -0
  81. package/dist/constants/index.js.map +1 -0
  82. package/dist/constants/networks.d.ts +32 -0
  83. package/dist/constants/networks.d.ts.map +1 -0
  84. package/dist/constants/networks.js +174 -0
  85. package/dist/constants/networks.js.map +1 -0
  86. package/dist/contracts/index.d.ts +5 -0
  87. package/dist/contracts/index.d.ts.map +1 -0
  88. package/dist/contracts/index.js +21 -0
  89. package/dist/contracts/index.js.map +1 -0
  90. package/dist/contracts/viem/AlphaViem.d.ts +518 -0
  91. package/dist/contracts/viem/AlphaViem.d.ts.map +1 -0
  92. package/dist/contracts/viem/AlphaViem.js +1287 -0
  93. package/dist/contracts/viem/AlphaViem.js.map +1 -0
  94. package/dist/contracts/viem/PriceOracleViem.d.ts +71 -0
  95. package/dist/contracts/viem/PriceOracleViem.d.ts.map +1 -0
  96. package/dist/contracts/viem/PriceOracleViem.js +212 -0
  97. package/dist/contracts/viem/PriceOracleViem.js.map +1 -0
  98. package/dist/contracts/viem/index.d.ts +9 -0
  99. package/dist/contracts/viem/index.d.ts.map +1 -0
  100. package/dist/contracts/viem/index.js +17 -0
  101. package/dist/contracts/viem/index.js.map +1 -0
  102. package/dist/errors/index.d.ts +44 -0
  103. package/dist/errors/index.d.ts.map +1 -0
  104. package/dist/errors/index.js +83 -0
  105. package/dist/errors/index.js.map +1 -0
  106. package/dist/index.d.ts +19 -0
  107. package/dist/index.d.ts.map +1 -0
  108. package/dist/index.js +60 -0
  109. package/dist/index.js.map +1 -0
  110. package/dist/types/alpha.d.ts +299 -0
  111. package/dist/types/alpha.d.ts.map +1 -0
  112. package/dist/types/alpha.js +6 -0
  113. package/dist/types/alpha.js.map +1 -0
  114. package/dist/types/client.d.ts +24 -0
  115. package/dist/types/client.d.ts.map +1 -0
  116. package/dist/types/client.js +13 -0
  117. package/dist/types/client.js.map +1 -0
  118. package/dist/types/contracts.d.ts +48 -0
  119. package/dist/types/contracts.d.ts.map +1 -0
  120. package/dist/types/contracts.js +6 -0
  121. package/dist/types/contracts.js.map +1 -0
  122. package/dist/types/funding.d.ts +27 -0
  123. package/dist/types/funding.d.ts.map +1 -0
  124. package/dist/types/funding.js +6 -0
  125. package/dist/types/funding.js.map +1 -0
  126. package/dist/types/index.d.ts +92 -0
  127. package/dist/types/index.d.ts.map +1 -0
  128. package/dist/types/index.js +47 -0
  129. package/dist/types/index.js.map +1 -0
  130. package/dist/types/liquidation.d.ts +20 -0
  131. package/dist/types/liquidation.d.ts.map +1 -0
  132. package/dist/types/liquidation.js +6 -0
  133. package/dist/types/liquidation.js.map +1 -0
  134. package/dist/types/margin.d.ts +29 -0
  135. package/dist/types/margin.d.ts.map +1 -0
  136. package/dist/types/margin.js +6 -0
  137. package/dist/types/margin.js.map +1 -0
  138. package/dist/types/oracle.d.ts +21 -0
  139. package/dist/types/oracle.d.ts.map +1 -0
  140. package/dist/types/oracle.js +6 -0
  141. package/dist/types/oracle.js.map +1 -0
  142. package/dist/types/positions.d.ts +43 -0
  143. package/dist/types/positions.d.ts.map +1 -0
  144. package/dist/types/positions.js +13 -0
  145. package/dist/types/positions.js.map +1 -0
  146. package/dist/utils/calculations.d.ts +84 -0
  147. package/dist/utils/calculations.d.ts.map +1 -0
  148. package/dist/utils/calculations.js +155 -0
  149. package/dist/utils/calculations.js.map +1 -0
  150. package/dist/utils/errors.d.ts +24 -0
  151. package/dist/utils/errors.d.ts.map +1 -0
  152. package/dist/utils/errors.js +129 -0
  153. package/dist/utils/errors.js.map +1 -0
  154. package/dist/utils/events.d.ts +40 -0
  155. package/dist/utils/events.d.ts.map +1 -0
  156. package/dist/utils/events.js +73 -0
  157. package/dist/utils/events.js.map +1 -0
  158. package/dist/utils/format.d.ts +40 -0
  159. package/dist/utils/format.d.ts.map +1 -0
  160. package/dist/utils/format.js +86 -0
  161. package/dist/utils/format.js.map +1 -0
  162. package/dist/utils/index.d.ts +10 -0
  163. package/dist/utils/index.d.ts.map +1 -0
  164. package/dist/utils/index.js +26 -0
  165. package/dist/utils/index.js.map +1 -0
  166. package/dist/utils/network.d.ts +52 -0
  167. package/dist/utils/network.d.ts.map +1 -0
  168. package/dist/utils/network.js +192 -0
  169. package/dist/utils/network.js.map +1 -0
  170. package/dist/utils/positionCalculations.d.ts +145 -0
  171. package/dist/utils/positionCalculations.d.ts.map +1 -0
  172. package/dist/utils/positionCalculations.js +278 -0
  173. package/dist/utils/positionCalculations.js.map +1 -0
  174. package/dist/utils/validation.d.ts +28 -0
  175. package/dist/utils/validation.d.ts.map +1 -0
  176. package/dist/utils/validation.js +68 -0
  177. package/dist/utils/validation.js.map +1 -0
  178. package/docs/README.md +40 -0
  179. package/docs/api/API.md +831 -0
  180. package/docs/guides/GETTING_STARTED.md +316 -0
  181. package/docs/guides/TRADING_GUIDE.md +677 -0
  182. package/docs/integration/INTEGRATION_GUIDE.md +1679 -0
  183. package/docs/integration/VIEM_INTEGRATION.md +294 -0
  184. package/docs/reference/CLI_QUICK_REFERENCE.md +197 -0
  185. package/docs/reference/TROUBLESHOOTING.md +922 -0
  186. package/package.json +113 -0
  187. package/src/AlphaFuturesClient.ts +158 -0
  188. package/src/abi/.gitkeep +1 -0
  189. package/src/abi/Alpha.json +5987 -0
  190. package/src/abi/README.md +99 -0
  191. package/src/abi/abis.ts +131 -0
  192. package/src/abi/index.ts +13 -0
  193. package/src/config/contracts.config.ts +186 -0
  194. package/src/config/environments/alpha.config.ts +139 -0
  195. package/src/config/environments/beta.config.ts +130 -0
  196. package/src/config/environments/dev.config.ts +122 -0
  197. package/src/config/environments/index.ts +87 -0
  198. package/src/config/environments/localhost.config.ts +153 -0
  199. package/src/config/environments/prod.config.ts +142 -0
  200. package/src/config/index.ts +29 -0
  201. package/src/constants/assets.ts +299 -0
  202. package/src/constants/contracts.ts +64 -0
  203. package/src/constants/index.ts +69 -0
  204. package/src/constants/networks.ts +182 -0
  205. package/src/contracts/index.ts +5 -0
  206. package/src/contracts/viem/AlphaViem.ts +1615 -0
  207. package/src/contracts/viem/PriceOracleViem.ts +272 -0
  208. package/src/contracts/viem/index.ts +11 -0
  209. package/src/errors/index.ts +87 -0
  210. package/src/index.ts +59 -0
  211. package/src/types/VIEM_TYPES_README.md +70 -0
  212. package/src/types/alpha.ts +358 -0
  213. package/src/types/client.ts +27 -0
  214. package/src/types/contracts.ts +74 -0
  215. package/src/types/funding.ts +31 -0
  216. package/src/types/index.ts +108 -0
  217. package/src/types/liquidation.ts +23 -0
  218. package/src/types/margin.ts +34 -0
  219. package/src/types/oracle.ts +24 -0
  220. package/src/types/positions.ts +48 -0
  221. package/src/utils/calculations.ts +175 -0
  222. package/src/utils/errors.ts +147 -0
  223. package/src/utils/events.ts +98 -0
  224. package/src/utils/format.ts +84 -0
  225. package/src/utils/index.ts +10 -0
  226. package/src/utils/network.ts +212 -0
  227. package/src/utils/positionCalculations.ts +317 -0
  228. package/src/utils/validation.ts +76 -0
@@ -0,0 +1,299 @@
1
+ /**
2
+ * Asset mappings and constants for Alpha Futures
3
+ * Includes both legacy assets and new subnet markets
4
+ */
5
+
6
+ import { Address } from 'viem';
7
+
8
+ /**
9
+ * Subnet market configuration for trading
10
+ */
11
+ export interface SubnetMarketAsset {
12
+ /** Subnet ID on the Bittensor network */
13
+ subnetId: number;
14
+ /** Human-readable name of the subnet */
15
+ name: string;
16
+ /** Market contract address for this subnet */
17
+ marketAddress: Address;
18
+ /** Symbol used for trading */
19
+ symbol: string;
20
+ /** Full display name */
21
+ displayName: string;
22
+ /** Asset decimals (typically 18 for subnet tokens) */
23
+ decimals: number;
24
+ /** Whether oracle pricing is currently available */
25
+ hasOraclePrice: boolean;
26
+ }
27
+
28
+ /**
29
+ * Bittensor subnet markets available for Alpha Futures trading
30
+ * Top 12 subnets by emission (as of 2025)
31
+ */
32
+ export const SUBNET_MARKETS: Record<string, SubnetMarketAsset> = {
33
+ CHUTES: {
34
+ subnetId: 64,
35
+ name: 'Chutes',
36
+ marketAddress: '0x0000000000000000000000000000000000000040',
37
+ symbol: 'CHUTES',
38
+ displayName: 'Chutes (SN64)',
39
+ decimals: 18,
40
+ hasOraclePrice: true,
41
+ },
42
+ LIUM: {
43
+ subnetId: 51,
44
+ name: 'lium.io',
45
+ marketAddress: '0x0000000000000000000000000000000000000033',
46
+ symbol: 'LIUM',
47
+ displayName: 'lium.io (SN51)',
48
+ decimals: 18,
49
+ hasOraclePrice: true,
50
+ },
51
+ RIDGES: {
52
+ subnetId: 62,
53
+ name: 'Ridges',
54
+ marketAddress: '0x000000000000000000000000000000000000003e',
55
+ symbol: 'RIDGES',
56
+ displayName: 'Ridges (SN62)',
57
+ decimals: 18,
58
+ hasOraclePrice: true,
59
+ },
60
+ TARGON: {
61
+ subnetId: 4,
62
+ name: 'Targon',
63
+ marketAddress: '0x0000000000000000000000000000000000000004',
64
+ symbol: 'TARGON',
65
+ displayName: 'Targon (SN4)',
66
+ decimals: 18,
67
+ hasOraclePrice: true,
68
+ },
69
+ VANTA: {
70
+ subnetId: 8,
71
+ name: 'Vanta',
72
+ marketAddress: '0x0000000000000000000000000000000000000008',
73
+ symbol: 'VANTA',
74
+ displayName: 'Vanta (SN8)',
75
+ decimals: 18,
76
+ hasOraclePrice: true,
77
+ },
78
+ AFFINE: {
79
+ subnetId: 120,
80
+ name: 'Affine',
81
+ marketAddress: '0x0000000000000000000000000000000000000078',
82
+ symbol: 'AFFINE',
83
+ displayName: 'Affine (SN120)',
84
+ decimals: 18,
85
+ hasOraclePrice: true,
86
+ },
87
+ TEMPLAR: {
88
+ subnetId: 3,
89
+ name: 'templar',
90
+ marketAddress: '0x0000000000000000000000000000000000000003',
91
+ symbol: 'TEMPLAR',
92
+ displayName: 'templar (SN3)',
93
+ decimals: 18,
94
+ hasOraclePrice: true,
95
+ },
96
+ SCORE: {
97
+ subnetId: 44,
98
+ name: 'Score',
99
+ marketAddress: '0x000000000000000000000000000000000000002c',
100
+ symbol: 'SCORE',
101
+ displayName: 'Score (SN44)',
102
+ decimals: 18,
103
+ hasOraclePrice: true,
104
+ },
105
+ IOTA: {
106
+ subnetId: 9,
107
+ name: 'iota',
108
+ marketAddress: '0x0000000000000000000000000000000000000009',
109
+ symbol: 'IOTA',
110
+ displayName: 'iota (SN9)',
111
+ decimals: 18,
112
+ hasOraclePrice: true,
113
+ },
114
+ GRADIENTS: {
115
+ subnetId: 56,
116
+ name: 'Gradients',
117
+ marketAddress: '0x0000000000000000000000000000000000000038',
118
+ symbol: 'GRADIENTS',
119
+ displayName: 'Gradients (SN56)',
120
+ decimals: 18,
121
+ hasOraclePrice: true,
122
+ },
123
+ HIPPIUS: {
124
+ subnetId: 75,
125
+ name: 'Hippius',
126
+ marketAddress: '0x000000000000000000000000000000000000004b',
127
+ symbol: 'HIPPIUS',
128
+ displayName: 'Hippius (SN75)',
129
+ decimals: 18,
130
+ hasOraclePrice: true,
131
+ },
132
+ BITMIND: {
133
+ subnetId: 34,
134
+ name: 'BitMind',
135
+ marketAddress: '0x0000000000000000000000000000000000000022',
136
+ symbol: 'BITMIND',
137
+ displayName: 'BitMind (SN34)',
138
+ decimals: 18,
139
+ hasOraclePrice: true,
140
+ },
141
+ MTSOS: {
142
+ subnetId: 65534,
143
+ name: 'MTSOS',
144
+ marketAddress: '0x000000000000000000000000000000000000FFFE',
145
+ symbol: 'MTSOS',
146
+ displayName: 'MegaTAO Sum-of-Subnets Index',
147
+ decimals: 18,
148
+ hasOraclePrice: true,
149
+ },
150
+ } as const;
151
+
152
+ /**
153
+ * Legacy asset addresses (maintained for backward compatibility)
154
+ */
155
+ export const ASSET_ADDRESSES: Record<string, Address> = {
156
+ // Legacy assets
157
+ ALPHA: '0x0000000000000000000000000000000000000001',
158
+ TAO: '0x0000000000000000000000000000000000000002',
159
+ // Subnet market addresses
160
+ CHUTES: SUBNET_MARKETS.CHUTES.marketAddress,
161
+ LIUM: SUBNET_MARKETS.LIUM.marketAddress,
162
+ RIDGES: SUBNET_MARKETS.RIDGES.marketAddress,
163
+ TARGON: SUBNET_MARKETS.TARGON.marketAddress,
164
+ VANTA: SUBNET_MARKETS.VANTA.marketAddress,
165
+ AFFINE: SUBNET_MARKETS.AFFINE.marketAddress,
166
+ TEMPLAR: SUBNET_MARKETS.TEMPLAR.marketAddress,
167
+ SCORE: SUBNET_MARKETS.SCORE.marketAddress,
168
+ IOTA: SUBNET_MARKETS.IOTA.marketAddress,
169
+ GRADIENTS: SUBNET_MARKETS.GRADIENTS.marketAddress,
170
+ HIPPIUS: SUBNET_MARKETS.HIPPIUS.marketAddress,
171
+ BITMIND: SUBNET_MARKETS.BITMIND.marketAddress,
172
+ MTSOS: SUBNET_MARKETS.MTSOS.marketAddress,
173
+ } as const;
174
+
175
+ export const ASSET_NAMES: Record<Address, string> = {
176
+ '0x0000000000000000000000000000000000000001': 'ALPHA',
177
+ '0x0000000000000000000000000000000000000002': 'TAO',
178
+ // Subnet market names
179
+ [SUBNET_MARKETS.CHUTES.marketAddress]: 'CHUTES',
180
+ [SUBNET_MARKETS.LIUM.marketAddress]: 'LIUM',
181
+ [SUBNET_MARKETS.RIDGES.marketAddress]: 'RIDGES',
182
+ [SUBNET_MARKETS.TARGON.marketAddress]: 'TARGON',
183
+ [SUBNET_MARKETS.VANTA.marketAddress]: 'VANTA',
184
+ [SUBNET_MARKETS.AFFINE.marketAddress]: 'AFFINE',
185
+ [SUBNET_MARKETS.TEMPLAR.marketAddress]: 'TEMPLAR',
186
+ [SUBNET_MARKETS.SCORE.marketAddress]: 'SCORE',
187
+ [SUBNET_MARKETS.IOTA.marketAddress]: 'IOTA',
188
+ [SUBNET_MARKETS.GRADIENTS.marketAddress]: 'GRADIENTS',
189
+ [SUBNET_MARKETS.HIPPIUS.marketAddress]: 'HIPPIUS',
190
+ [SUBNET_MARKETS.BITMIND.marketAddress]: 'BITMIND',
191
+ [SUBNET_MARKETS.MTSOS.marketAddress]: 'MTSOS',
192
+ } as const;
193
+
194
+ export const ASSET_DECIMALS: Record<string, number> = {
195
+ // Legacy assets
196
+ ALPHA: 18,
197
+ TAO: 18,
198
+ // Subnet markets (all use 18 decimals)
199
+ CHUTES: 18,
200
+ LIUM: 18,
201
+ RIDGES: 18,
202
+ TARGON: 18,
203
+ VANTA: 18,
204
+ AFFINE: 18,
205
+ TEMPLAR: 18,
206
+ SCORE: 18,
207
+ IOTA: 18,
208
+ GRADIENTS: 18,
209
+ HIPPIUS: 18,
210
+ BITMIND: 18,
211
+ MTSOS: 18,
212
+ } as const;
213
+
214
+ /**
215
+ * Convert asset symbol to address
216
+ */
217
+ export function getAssetAddress(symbol: string): Address {
218
+ const address = ASSET_ADDRESSES[symbol.toUpperCase()];
219
+ if (!address) {
220
+ throw new Error(`Unknown asset symbol: ${symbol}`);
221
+ }
222
+ return address;
223
+ }
224
+
225
+ /**
226
+ * Convert asset address to symbol
227
+ */
228
+ export function getAssetSymbol(address: Address): string {
229
+ const symbol = ASSET_NAMES[address];
230
+ if (!symbol) {
231
+ throw new Error(`Unknown asset address: ${address}`);
232
+ }
233
+ return symbol;
234
+ }
235
+
236
+ /**
237
+ * Check if asset is supported
238
+ */
239
+ export function isAssetSupported(symbolOrAddress: string): boolean {
240
+ const symbol = symbolOrAddress.toUpperCase();
241
+ return symbol in ASSET_ADDRESSES || symbolOrAddress in ASSET_NAMES;
242
+ }
243
+
244
+ /**
245
+ * Get asset decimals
246
+ */
247
+ export function getAssetDecimals(symbol: string): number {
248
+ const decimals = ASSET_DECIMALS[symbol.toUpperCase()];
249
+ if (decimals === undefined) {
250
+ throw new Error(`Unknown asset symbol: ${symbol}`);
251
+ }
252
+ return decimals;
253
+ }
254
+
255
+ /**
256
+ * Get subnet market by symbol
257
+ */
258
+ export function getSubnetMarket(symbol: string): SubnetMarketAsset {
259
+ const market = SUBNET_MARKETS[symbol.toUpperCase()];
260
+ if (!market) {
261
+ throw new Error(`Unknown subnet market symbol: ${symbol}`);
262
+ }
263
+ return market;
264
+ }
265
+
266
+ /**
267
+ * Get all available subnet markets
268
+ */
269
+ export function getAvailableSubnetMarkets(): SubnetMarketAsset[] {
270
+ return Object.values(SUBNET_MARKETS);
271
+ }
272
+
273
+ /**
274
+ * Check if symbol is a subnet market
275
+ */
276
+ export function isSubnetMarket(symbol: string): boolean {
277
+ return symbol.toUpperCase() in SUBNET_MARKETS;
278
+ }
279
+
280
+ /**
281
+ * Get subnet market by subnet ID
282
+ */
283
+ export function getSubnetMarketById(subnetId: number): SubnetMarketAsset | undefined {
284
+ return Object.values(SUBNET_MARKETS).find((market) => market.subnetId === subnetId);
285
+ }
286
+
287
+ /**
288
+ * List all supported assets (legacy + subnet markets)
289
+ */
290
+ export function getSupportedAssets(): string[] {
291
+ return Object.keys(ASSET_ADDRESSES);
292
+ }
293
+
294
+ /**
295
+ * List only subnet market symbols
296
+ */
297
+ export function getSupportedSubnetMarkets(): string[] {
298
+ return Object.keys(SUBNET_MARKETS);
299
+ }
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Contract-related constants
3
+ */
4
+
5
+ // Contract method signatures
6
+ export const CONTRACT_METHODS = {
7
+ // MarginAccount
8
+ DEPOSIT: 'deposit',
9
+ WITHDRAW: 'withdraw',
10
+ GET_BALANCE: 'getBalance',
11
+ GET_AVAILABLE_BALANCE: 'getAvailableBalance',
12
+
13
+ // Position Management
14
+ OPEN_MARKET_POSITION: 'openMarketPosition',
15
+ CLOSE_POSITION: 'closePosition',
16
+ GET_POSITION: 'getPosition',
17
+ GET_USER_POSITIONS: 'getUserPositions',
18
+
19
+ // FundingRate
20
+ UPDATE_FUNDING_RATE: 'updateFundingRate',
21
+ GET_CURRENT_FUNDING_RATE: 'getCurrentFundingRate',
22
+ GET_FUNDING_INDEX: 'getFundingIndex',
23
+
24
+ // LiquidationEngine
25
+ LIQUIDATE_POSITION: 'liquidatePosition',
26
+ IS_LIQUIDATABLE: 'isLiquidatable',
27
+ GET_LIQUIDATION_PRICE: 'getLiquidationPrice',
28
+
29
+ // PriceOracle
30
+ GET_PRICE: 'getPrice',
31
+ UPDATE_PRICE: 'updatePrice',
32
+ } as const;
33
+
34
+ // Contract events
35
+ export const CONTRACT_EVENTS = {
36
+ // MarginAccount events
37
+ DEPOSIT_EVENT: 'Deposited',
38
+ WITHDRAW_EVENT: 'Withdrawn',
39
+
40
+ // PositionManager events
41
+ POSITION_OPENED: 'PositionOpened',
42
+ POSITION_CLOSED: 'PositionClosed',
43
+ POSITION_UPDATED: 'PositionUpdated',
44
+
45
+ // FundingRate events
46
+ FUNDING_RATE_UPDATED: 'FundingRateUpdated',
47
+ FUNDING_PAID: 'FundingPaid',
48
+
49
+ // LiquidationEngine events
50
+ POSITION_LIQUIDATED: 'PositionLiquidated',
51
+
52
+ // PriceOracle events
53
+ PRICE_UPDATED: 'PriceUpdated',
54
+ } as const;
55
+
56
+ // Gas limits
57
+ export const GAS_LIMITS = {
58
+ DEPOSIT: 100000n,
59
+ WITHDRAW: 150000n,
60
+ OPEN_POSITION: 300000n,
61
+ CLOSE_POSITION: 250000n,
62
+ LIQUIDATE: 300000n,
63
+ UPDATE_FUNDING: 200000n,
64
+ } as const;
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Constants used throughout the Alpha Futures SDK
3
+ */
4
+
5
+ export * from './networks';
6
+ export * from './contracts';
7
+
8
+ // Export assets with explicit re-exports to avoid naming conflicts
9
+ export {
10
+ SUBNET_MARKETS,
11
+ ASSET_ADDRESSES,
12
+ ASSET_NAMES,
13
+ ASSET_DECIMALS,
14
+ getAssetAddress,
15
+ getAssetSymbol,
16
+ isAssetSupported,
17
+ getAssetDecimals,
18
+ getSubnetMarket,
19
+ getAvailableSubnetMarkets,
20
+ isSubnetMarket,
21
+ getSupportedAssets,
22
+ getSupportedSubnetMarkets,
23
+ type SubnetMarketAsset,
24
+ } from './assets';
25
+
26
+ // Common constants
27
+ export const PRECISION = 10n ** 18n; // 18 decimals precision
28
+ export const BASIS_POINTS = 10000n; // 100% = 10000 basis points
29
+ export const PERCENTAGE_PRECISION = 100n; // 100%
30
+
31
+ // Margin constants
32
+ export const INITIAL_MARGIN_RATIO = 3333n; // 33.33% in basis points (3x leverage)
33
+ export const MAINTENANCE_MARGIN_RATIO = 2000n; // 20% in basis points
34
+ export const LIQUIDATION_BONUS = 500n; // 5% in basis points
35
+
36
+ // Funding rate constants
37
+ export const FUNDING_INTERVAL = 8n * 60n * 60n; // 8 hours in seconds
38
+ export const MAX_FUNDING_RATE = 10n; // 0.1% in basis points
39
+ export const SKEW_SCALE = 1000000n * PRECISION; // $1M skew scale
40
+
41
+ // Fee constants
42
+ export const TRADING_FEE = 10n; // 0.1% in basis points
43
+ export const INSURANCE_FUND_RATIO = 2000n; // 20% of fees go to insurance fund
44
+
45
+ // Position constants
46
+ export const MIN_POSITION_SIZE = 10n * PRECISION; // Minimum $10 position
47
+ export const MAX_LEVERAGE = 3n; // Maximum 3x leverage
48
+
49
+ // Oracle constants
50
+ export const PRICE_STALENESS_THRESHOLD = 3600n; // 1 hour in seconds
51
+ export const PRICE_DEVIATION_THRESHOLD = 2000n; // 20% max price deviation
52
+
53
+ // Circuit breaker constants
54
+ export const PRICE_MOVEMENT_THRESHOLD = 2000n; // 20% price movement triggers circuit breaker
55
+ export const CIRCUIT_BREAKER_COOLDOWN = 3600n; // 1 hour cooldown
56
+
57
+ // Error messages
58
+ export const ERRORS = {
59
+ INSUFFICIENT_MARGIN: 'Insufficient margin for position',
60
+ POSITION_NOT_FOUND: 'Position not found',
61
+ INVALID_LEVERAGE: 'Invalid leverage',
62
+ PRICE_STALE: 'Price data is stale',
63
+ CIRCUIT_BREAKER_ACTIVE: 'Circuit breaker is active',
64
+ UNAUTHORIZED: 'Unauthorized',
65
+ INVALID_ASSET: 'Invalid asset',
66
+ POSITION_NOT_LIQUIDATABLE: 'Position is not liquidatable',
67
+ INSUFFICIENT_BALANCE: 'Insufficient balance',
68
+ BELOW_MIN_POSITION_SIZE: 'Position size below minimum',
69
+ } as const;
@@ -0,0 +1,182 @@
1
+ /**
2
+ * Network configurations for Alpha Futures
3
+ */
4
+
5
+ import { NetworkConfig, SubnetMarket } from '../types';
6
+ import { SUBNET_MARKETS } from './assets';
7
+
8
+ export const NETWORK_CONFIGS: Record<string, NetworkConfig> = {
9
+ // Bittensor Mainnet (Finney)
10
+ bittensor: {
11
+ chainId: 964,
12
+ name: 'Bittensor Mainnet',
13
+ rpcUrl: 'https://bittensor-finney.api.onfinality.io/public',
14
+ contracts: {
15
+ alpha: '0xdA50070976Da21A0E7afaB1BB0c81CD51926AE64', // Alpha Futures Proxy (Production)
16
+ priceOracle: '0x446A342221ed83b56fc17eA3e046dd19153beA85', // Alpha Price Oracle V5
17
+ collateralToken: '0x0000000000000000000000000000000000000000',
18
+ },
19
+ blockExplorer: 'https://evm.taostats.io/',
20
+ /** Deployed subnet markets available for trading - converted from SUBNET_MARKETS */
21
+ markets: Object.values(SUBNET_MARKETS).map((market) => ({
22
+ id: market.subnetId,
23
+ name: market.name,
24
+ marketAddress: market.marketAddress,
25
+ hasOraclePrice: market.hasOraclePrice,
26
+ symbol: market.symbol,
27
+ displayName: market.displayName,
28
+ })),
29
+ },
30
+
31
+ // Bittensor Testnet
32
+ bittensorTestnet: {
33
+ chainId: 965,
34
+ name: 'Bittensor Testnet',
35
+ rpcUrl: 'https://testnet.bittensor.network/rpc',
36
+ contracts: {
37
+ alpha: '0x0000000000000000000000000000000000000000',
38
+ priceOracle: '0x0000000000000000000000000000000000000000',
39
+ collateralToken: '0x0000000000000000000000000000000000000000',
40
+ },
41
+ blockExplorer: 'https://explorer.testnet.opentensor.ai',
42
+ },
43
+
44
+ // Ethereum Mainnet (for potential future deployment)
45
+ mainnet: {
46
+ chainId: 1,
47
+ name: 'Ethereum Mainnet',
48
+ rpcUrl: 'https://ethereum.publicnode.com',
49
+ contracts: {
50
+ alpha: '0x0000000000000000000000000000000000000000',
51
+ priceOracle: '0x0000000000000000000000000000000000000000',
52
+ collateralToken: '0x0000000000000000000000000000000000000000',
53
+ },
54
+ blockExplorer: 'https://etherscan.io',
55
+ },
56
+
57
+ // Sepolia Testnet
58
+ sepolia: {
59
+ chainId: 11155111,
60
+ name: 'Sepolia Testnet',
61
+ rpcUrl: 'https://ethereum-sepolia.publicnode.com',
62
+ contracts: {
63
+ alpha: '0x0000000000000000000000000000000000000000',
64
+ priceOracle: '0x0000000000000000000000000000000000000000',
65
+ collateralToken: '0x0000000000000000000000000000000000000000',
66
+ },
67
+ blockExplorer: 'https://sepolia.etherscan.io',
68
+ },
69
+
70
+ // Local Development - uses getters to read env vars at access time (not import time)
71
+ localhost: {
72
+ chainId: 31337,
73
+ name: 'Localhost',
74
+ rpcUrl: 'http://127.0.0.1:8545',
75
+ get contracts() {
76
+ return {
77
+ alpha: (process.env.ALPHA as `0x${string}`) || '0x0000000000000000000000000000000000000000',
78
+ priceOracle:
79
+ (process.env.PRICE_ORACLE as `0x${string}`) ||
80
+ '0x0000000000000000000000000000000000000000',
81
+ collateralToken:
82
+ (process.env.TAO_TOKEN as `0x${string}`) || '0x0000000000000000000000000000000000000000',
83
+ };
84
+ },
85
+ },
86
+
87
+ // Anvil Network (for deployment script)
88
+ anvil: {
89
+ chainId: 31337,
90
+ name: 'Anvil',
91
+ rpcUrl: 'http://127.0.0.1:8545',
92
+ contracts: {
93
+ alpha: '0x0000000000000000000000000000000000000000', // Will be updated by deployment script
94
+ priceOracle: '0x0000000000000000000000000000000000000000', // Will be updated by deployment script
95
+ collateralToken: '0x0000000000000000000000000000000000000000', // Will be updated by deployment script
96
+ },
97
+ blockExplorer: 'http://localhost:8545', // No block explorer for local Anvil
98
+ },
99
+
100
+ // Hardhat Network
101
+ hardhat: {
102
+ chainId: 31337,
103
+ name: 'Hardhat',
104
+ rpcUrl: 'http://127.0.0.1:8545',
105
+ contracts: {
106
+ alpha: '0x2279B7A0a67DB372996a5FaB50D91eAA73d2eBe6',
107
+ priceOracle: '0x5FC8d32690cc91D4c39d9d3abcBD16989F875707',
108
+ collateralToken: '0x0165878A594ca255338adfa4d48449f69242Eb8F',
109
+ },
110
+ },
111
+ };
112
+
113
+ // Default network
114
+ export const DEFAULT_NETWORK = 'localhost';
115
+
116
+ // Helper function to get network by chain ID
117
+ export function getNetworkByChainId(chainId: number): NetworkConfig | undefined {
118
+ return Object.values(NETWORK_CONFIGS).find((network) => network.chainId === chainId);
119
+ }
120
+
121
+ // Helper function to validate network
122
+ export function isValidNetwork(network: string): boolean {
123
+ return network in NETWORK_CONFIGS;
124
+ }
125
+
126
+ // Helper function to get current network config
127
+ export function getCurrentNetwork(network?: string): NetworkConfig {
128
+ const selectedNetwork = network || DEFAULT_NETWORK;
129
+ if (!isValidNetwork(selectedNetwork)) {
130
+ throw new Error(`Invalid network: ${selectedNetwork}`);
131
+ }
132
+ return NETWORK_CONFIGS[selectedNetwork];
133
+ }
134
+
135
+ // Helper function to get all supported networks
136
+ export function getSupportedNetworks(): string[] {
137
+ return Object.keys(NETWORK_CONFIGS);
138
+ }
139
+
140
+ // Helper function to get network name by chain ID
141
+ export function getNetworkName(chainId: number): string | undefined {
142
+ const network = getNetworkByChainId(chainId);
143
+ return network?.name;
144
+ }
145
+
146
+ /**
147
+ * Get subnet markets for a specific network
148
+ */
149
+ export function getNetworkSubnetMarkets(network: string): SubnetMarket[] {
150
+ const networkConfig = NETWORK_CONFIGS[network];
151
+ return networkConfig?.markets || [];
152
+ }
153
+
154
+ /**
155
+ * Get subnet market by symbol from a specific network
156
+ */
157
+ export function getSubnetMarketBySymbol(network: string, symbol: string): SubnetMarket | undefined {
158
+ const markets = getNetworkSubnetMarkets(network);
159
+ return markets.find((market) => market.symbol.toLowerCase() === symbol.toLowerCase());
160
+ }
161
+
162
+ /**
163
+ * Get subnet market by ID from a specific network
164
+ */
165
+ export function getSubnetMarketById(network: string, subnetId: number): SubnetMarket | undefined {
166
+ const markets = getNetworkSubnetMarkets(network);
167
+ return markets.find((market) => market.id === subnetId);
168
+ }
169
+
170
+ /**
171
+ * Check if a network supports subnet markets
172
+ */
173
+ export function networkHasSubnetMarkets(network: string): boolean {
174
+ return getNetworkSubnetMarkets(network).length > 0;
175
+ }
176
+
177
+ /**
178
+ * Get all networks that support subnet markets
179
+ */
180
+ export function getSubnetMarketNetworks(): string[] {
181
+ return Object.keys(NETWORK_CONFIGS).filter((network) => networkHasSubnetMarkets(network));
182
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Viem-based contract wrappers for Alpha Futures
3
+ */
4
+
5
+ export * from './viem';