100x-sdk 1.0.1

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/README.md ADDED
@@ -0,0 +1,364 @@
1
+ # 100x SDK
2
+
3
+ A JavaScript/TypeScript SDK for interacting with the 100x protocol on the Solana blockchain. Supports both Node.js and browser environments, providing modular functionality for trading, token management, order management, and more.
4
+
5
+ [![npm version](https://img.shields.io/npm/v/100x-sdk.svg)](https://www.npmjs.com/package/100x-sdk)
6
+ [![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
7
+
8
+ ## Features
9
+
10
+ - =� **Spot Trading**: Direct buy and sell operations for tokens
11
+ - =� **Margin Trading**: Leverage trading with long/short positions
12
+ - = **Dual Data Sources**: Fast API and reliable on-chain data access
13
+ - =� **Token Creation**: Create and launch new tokens on the protocol
14
+ - <� **Trade Simulation**: Pre-calculate slippage and costs before execution
15
+ - =� **Comprehensive Tooling**: Order management, AMM calculations, and utilities
16
+ - < **Cross-Platform**: Works in Node.js and modern browsers
17
+ - =� **Modular Design**: Clean, intuitive API with separate functional modules
18
+
19
+ ## Installation
20
+
21
+ ```bash
22
+ npm install 100x-sdk @solana/web3.js @coral-xyz/anchor
23
+ ```
24
+
25
+ or with Yarn:
26
+
27
+ ```bash
28
+ yarn add 100x-sdk @solana/web3.js @coral-xyz/anchor
29
+ ```
30
+
31
+ ## Quick Start
32
+
33
+ ```javascript
34
+ const { Fun100xSdk, getDefaultOptions, FUN100X_PROGRAM_ID } = require('100x-sdk');
35
+ const { Connection } = require('@solana/web3.js');
36
+ const anchor = require('@coral-xyz/anchor');
37
+
38
+ // 1. Get network configuration
39
+ const options = getDefaultOptions('MAINNET'); // or 'DEVNET', 'LOCALNET'
40
+
41
+ // 2. Create connection
42
+ const connection = new Connection(options.solanaEndpoint, 'confirmed');
43
+
44
+ // 3. Initialize SDK
45
+ const sdk = new Fun100xSdk(connection, FUN100X_PROGRAM_ID, options);
46
+
47
+ // 4. Example: Buy tokens
48
+ const result = await sdk.trading.buy({
49
+ mintAccount: "TOKEN_ADDRESS",
50
+ buyTokenAmount: new anchor.BN('1000000'), // 1 token (6 decimals)
51
+ maxSolAmount: new anchor.BN('2000000000'), // 2 SOL (9 decimals)
52
+ payer: wallet.publicKey
53
+ });
54
+
55
+ // 5. Sign and send transaction
56
+ result.transaction.feePayer = wallet.publicKey;
57
+ result.transaction.recentBlockhash = (await connection.getLatestBlockhash()).blockhash;
58
+ result.transaction.sign(wallet);
59
+
60
+ const signature = await connection.sendRawTransaction(result.transaction.serialize());
61
+ await connection.confirmTransaction(signature);
62
+
63
+ console.log('Transaction successful!', signature);
64
+ ```
65
+
66
+ ## SDK Architecture
67
+
68
+ The SDK is organized into functional modules, all accessible through the main `Fun100xSdk` class:
69
+
70
+ ```javascript
71
+ const sdk = new Fun100xSdk(connection, FUN100X_PROGRAM_ID, options);
72
+
73
+ // Trading operations
74
+ await sdk.trading.buy({...});
75
+ await sdk.trading.sell({...});
76
+ await sdk.trading.long({...});
77
+ await sdk.trading.short({...});
78
+
79
+ // Data access (unified interface)
80
+ const orders = await sdk.data.orders(mint, { type: 'down_orders' });
81
+ const price = await sdk.data.price(mint);
82
+
83
+ // Token creation
84
+ await sdk.token.create({...});
85
+
86
+ // Trade simulation
87
+ const simulation = await sdk.simulator.simulateTokenBuy(mint, amount);
88
+
89
+ // Utility tools
90
+ await sdk.tools.approveTrade({...});
91
+ ```
92
+
93
+ ## Module Overview
94
+
95
+ | Module | Purpose | Key Methods |
96
+ |--------|---------|-------------|
97
+ | **TradingModule** | Execute trades | `buy`, `sell`, `long`, `short`, `closeLong`, `closeShort` |
98
+ | **FastModule** | API data access | `mints`, `mint_info`, `orders`, `price`, `user_orders` |
99
+ | **ChainModule** | On-chain data | `getCurveAccount`, `orders`, `price`, `user_orders` |
100
+ | **TokenModule** | Token creation | `create`, `createAndBuy` |
101
+ | **ParamModule** | Parameter management | `createParams`, `getParams`, `getAdmin` |
102
+ | **SimulatorModule** | Trade simulation | `simulateTokenBuy`, `simulateTokenSell`, `simulateLongStopLoss` |
103
+ | **ToolsModule** | Utility functions | `approveTrade`, `closeTradeCooldown`, `validateCooldown` |
104
+ | **CurveAMM** | AMM calculations | `u128ToDecimal`, `buyFromPriceToPrice`, `sellFromPriceToPrice` |
105
+
106
+ ## Documentation
107
+
108
+ ### Quick Reference
109
+
110
+ - =� **[Quick Start Guide](./doc/01-quick-start.md)** - Get up and running quickly
111
+ - <� **[SDK Main Class](./doc/02-100x-sdk-main-class.md)** - Core initialization and configuration
112
+ - =� **[Complete Documentation Index](./doc/README.md)** - Full documentation table of contents
113
+
114
+ ### Core Modules
115
+
116
+ - **[Trading Module](./doc/03-trading-module.md)** - Spot and margin trading operations _(Translation in progress)_
117
+ - **[Fast Module](./doc/04-fast-module.md)** - API-based data access _(Translation in progress)_
118
+ - **[Chain Module](./doc/05-chain-module.md)** - On-chain data reading _(Translation in progress)_
119
+ - **[Token Module](./doc/06-token-module.md)** - Token creation and management
120
+ - **[Param Module](./doc/07-param-module.md)** - Partner parameter management
121
+ - **[Simulator Module](./doc/08-simulator-module.md)** - Trade simulation and calculations _(Translation in progress)_
122
+
123
+ ### Utilities
124
+
125
+ - **[CurveAMM Utility](./doc/09-curve-amm-utility.md)** - AMM curve calculations _(Translation in progress)_
126
+ - **[Constants & Helpers](./doc/10-constants-and-helpers.md)** - Configuration and helper functions
127
+ - **[Tools Module](./doc/11-tools-module.md)** - Trading utilities and cooldown management
128
+ - **[Position Tab Guide](./doc/position-tab-guide.md)** - UI integration guide for positions
129
+
130
+ ### Language Options
131
+
132
+ - <�<� **English Documentation**: [./doc/](./doc/) (Current)
133
+ - <�<� **-��c**: [./doc_cn/](./doc_cn/)
134
+
135
+ ## Network Configuration
136
+
137
+ The SDK supports three network environments:
138
+
139
+ ```javascript
140
+ // Mainnet (Production)
141
+ const mainnetOptions = getDefaultOptions('MAINNET');
142
+
143
+ // Devnet (Testing)
144
+ const devnetOptions = getDefaultOptions('DEVNET');
145
+
146
+ // Localnet (Local Development)
147
+ const localnetOptions = getDefaultOptions('LOCALNET');
148
+ ```
149
+
150
+ ## Data Source Options
151
+
152
+ Choose between fast API access or reliable on-chain reading:
153
+
154
+ ```javascript
155
+ // Fast API (default) - Quick responses, slight latency
156
+ const sdk = new Fun100xSdk(connection, FUN100X_PROGRAM_ID, {
157
+ ...options,
158
+ defaultDataSource: 'fast'
159
+ });
160
+
161
+ // On-chain direct reading - More reliable, slower
162
+ const sdk = new Fun100xSdk(connection, FUN100X_PROGRAM_ID, {
163
+ ...options,
164
+ defaultDataSource: 'chain'
165
+ });
166
+
167
+ // Or switch temporarily per call
168
+ const orders = await sdk.data.orders(mint, {
169
+ type: 'down_orders',
170
+ dataSource: 'chain' // Override default
171
+ });
172
+ ```
173
+
174
+ ## Key Concepts
175
+
176
+ ### Precision Handling
177
+
178
+ - **SOL**: 9 decimal places (lamports) - `1 SOL = 1,000,000,000 lamports`
179
+ - **Tokens**: 6 decimal places - `1 Token = 1,000,000 units`
180
+ - **Price**: u128 format with 28-digit precision
181
+
182
+ ```javascript
183
+ // SOL amounts
184
+ const oneSol = new anchor.BN('1000000000'); // 1 SOL
185
+
186
+ // Token amounts
187
+ const oneToken = new anchor.BN('1000000'); // 1 Token
188
+
189
+ // Price conversion
190
+ const { CurveAMM } = require('100x-sdk');
191
+ const decimalPrice = CurveAMM.u128ToDecimal(priceU128);
192
+ const priceU128 = CurveAMM.decimalToU128(decimalPrice);
193
+ ```
194
+
195
+ ### Transaction Signing
196
+
197
+ The SDK returns unsigned transactions for security and wallet compatibility:
198
+
199
+ ```javascript
200
+ // SDK builds the transaction
201
+ const result = await sdk.trading.buy({...});
202
+
203
+ // You control the signing
204
+ result.transaction.feePayer = wallet.publicKey;
205
+ result.transaction.recentBlockhash = (await connection.getLatestBlockhash()).blockhash;
206
+
207
+ // Sign with your wallet
208
+ const signature = await wallet.sendTransaction(result.transaction, connection);
209
+ ```
210
+
211
+ ## Example Use Cases
212
+
213
+ ### Spot Trading
214
+
215
+ ```javascript
216
+ // Buy tokens
217
+ const buyResult = await sdk.trading.buy({
218
+ mintAccount: mint,
219
+ buyTokenAmount: new anchor.BN('1000000'),
220
+ maxSolAmount: new anchor.BN('2000000000'),
221
+ payer: wallet.publicKey
222
+ });
223
+
224
+ // Sell tokens
225
+ const sellResult = await sdk.trading.sell({
226
+ mintAccount: mint,
227
+ sellTokenAmount: new anchor.BN('1000000'),
228
+ minSolOutput: new anchor.BN('1800000000'),
229
+ payer: wallet.publicKey
230
+ });
231
+ ```
232
+
233
+ ### Margin Trading
234
+
235
+ ```javascript
236
+ // Open long position
237
+ const longResult = await sdk.trading.long({
238
+ mintAccount: mint,
239
+ buyTokenAmount: new anchor.BN('10000000'),
240
+ maxSolAmount: new anchor.BN('20000000000'),
241
+ marginSol: new anchor.BN('5000000000'),
242
+ closePrice: new anchor.BN('...'),
243
+ closeInsertIndices: [...],
244
+ payer: wallet.publicKey
245
+ });
246
+
247
+ // Close long position
248
+ const closeResult = await sdk.trading.closeLong({
249
+ mintAccount: mint,
250
+ sellTokenAmount: new anchor.BN('10000000'),
251
+ minSolOutput: new anchor.BN('18000000000'),
252
+ closeOrderId: orderId,
253
+ closeOrderIndices: [...],
254
+ payer: wallet.publicKey,
255
+ userSolAccount: orderOwner
256
+ });
257
+ ```
258
+
259
+ ### Data Queries
260
+
261
+ ```javascript
262
+ // Get token list
263
+ const tokens = await sdk.fast.mints({ limit: 10 });
264
+
265
+ // Get token price
266
+ const price = await sdk.data.price(mint);
267
+
268
+ // Get orders
269
+ const orders = await sdk.data.orders(mint, { type: 'down_orders' });
270
+
271
+ // Get user orders
272
+ const userOrders = await sdk.data.user_orders(userAddress, mint);
273
+ ```
274
+
275
+ ### Trade Simulation
276
+
277
+ ```javascript
278
+ // Simulate buy before execution
279
+ const simulation = await sdk.simulator.simulateTokenBuy(mint, buyTokenAmount);
280
+
281
+ console.log('Completion:', simulation.completion + '%');
282
+ console.log('Slippage:', simulation.slippage + '%');
283
+ console.log('Suggested SOL:', simulation.suggestedSolAmount);
284
+
285
+ // Use simulation results in actual trade
286
+ const result = await sdk.trading.buy({
287
+ mintAccount: mint,
288
+ buyTokenAmount: new anchor.BN(buyTokenAmount),
289
+ maxSolAmount: new anchor.BN(simulation.suggestedSolAmount),
290
+ payer: wallet.publicKey
291
+ });
292
+ ```
293
+
294
+ ## Development
295
+
296
+ ### Build
297
+
298
+ ```bash
299
+ npm run build # Build all distribution formats (CJS, ESM, UMD)
300
+ npm run build:dev # Watch mode for development
301
+ ```
302
+
303
+ ### Testing
304
+
305
+ ```bash
306
+ # Run individual test files
307
+ node tests/example-trading-buy.js
308
+ node tests/test-closeShort.js
309
+
310
+ # Standard test commands (coming soon)
311
+ npm test
312
+ ```
313
+
314
+ ### Linting
315
+
316
+ ```bash
317
+ npm run lint
318
+ ```
319
+
320
+ ## Important Notes
321
+
322
+ 1. **Data Source Selection**
323
+ - `fast` (API) - Fast responses, may have slight delays during peak times
324
+ - `chain` (Direct) - More reliable, slower, no third-party dependencies
325
+
326
+ 2. **Transaction Signing**
327
+ - SDK returns unsigned transactions
328
+ - Signing must be done externally for security
329
+ - Compatible with hardware wallets and browser extensions
330
+
331
+ 3. **Error Handling**
332
+ - All async methods can throw exceptions
333
+ - Always implement proper error handling
334
+ - Use try-catch blocks around SDK calls
335
+
336
+ 4. **Precision**
337
+ - Always use `anchor.BN` for amounts
338
+ - Remember decimal places: SOL (9), Token (6)
339
+ - Use CurveAMM utilities for price conversions
340
+
341
+ ## Contributing
342
+
343
+ Contributions are welcome! Please feel free to submit a Pull Request.
344
+
345
+ ## Support
346
+
347
+ - **Documentation**: [./doc/README.md](./doc/README.md)
348
+ - **Issues**: [GitHub Issues](https://github.com/your-org/100x-sdk/issues)
349
+ - **Discord**: [Join our community](https://discord.gg/spinpet)
350
+
351
+ ## License
352
+
353
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
354
+
355
+ ## Related Links
356
+
357
+ - [100x Protocol](https://100x.fun)
358
+ - [Solana Documentation](https://docs.solana.com)
359
+ - [Anchor Framework](https://www.anchor-lang.com)
360
+
361
+ ---
362
+
363
+ **Version**: 2.0.0
364
+ **Last Updated**: 2024-12-09