100x-sdk 1.0.2 → 1.0.4
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/dist/100x-sdk.cjs.js +7037 -3901
- package/dist/100x-sdk.esm.js +6240 -3341
- package/dist/100x-sdk.js +6240 -3341
- package/dist/100x-sdk.js.map +1 -1
- package/dist/index.d.ts +19 -19
- package/package.json +1 -1
- package/src/modules/chain.js +26 -26
- package/src/modules/fast.js +88 -88
- package/src/modules/param.js +6 -6
- package/src/modules/simulator/buy_sell_token.js +38 -39
- package/src/modules/simulator/calcLiq.js +176 -198
- package/src/modules/simulator/calc_sol_liq.js +40 -40
- package/src/modules/simulator/close_indices.js +51 -54
- package/src/modules/simulator/long_shrot_stop.js +332 -332
- package/src/modules/simulator/stop_loss_utils.js +125 -125
- package/src/modules/simulator/utils.js +1 -2
- package/src/modules/simulator.js +14 -19
- package/src/modules/token.js +69 -69
- package/src/modules/tools.js +1 -1
- package/src/modules/trading.js +97 -97
- package/src/sdk.js +22 -23
- package/src/types/index.d.ts +19 -19
- package/src/utils/constants.js +2 -2
- package/src/utils/curve_amm.js +54 -54
- package/src/utils/orderUtils.js +1 -3
package/src/modules/token.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
const { ComputeBudgetProgram, PublicKey, Transaction, Keypair, SystemProgram, SYSVAR_RENT_PUBKEY } = require('@solana/web3.js');
|
|
2
2
|
const { TOKEN_PROGRAM_ID, getAssociatedTokenAddress, createAssociatedTokenAccountInstruction, ASSOCIATED_TOKEN_PROGRAM_ID } = require('@solana/spl-token');
|
|
3
3
|
const anchor = require('@coral-xyz/anchor');
|
|
4
|
-
//
|
|
4
|
+
// Uniformly use the buffer package, consistent across all platforms
|
|
5
5
|
const { Buffer } = require('buffer');
|
|
6
6
|
|
|
7
|
-
// Metaplex Token Metadata
|
|
7
|
+
// Metaplex Token Metadata program ID
|
|
8
8
|
const METADATA_PROGRAM_ID = new PublicKey("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s");
|
|
9
9
|
|
|
10
10
|
/**
|
|
@@ -17,42 +17,42 @@ class TokenModule {
|
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
|
-
*
|
|
21
|
-
* @param {Object} params -
|
|
22
|
-
* @param {Keypair} params.mint -
|
|
23
|
-
* @param {string} params.name -
|
|
24
|
-
* @param {string} params.symbol -
|
|
25
|
-
* @param {string} params.uri -
|
|
26
|
-
* @param {PublicKey} params.payer -
|
|
20
|
+
* Create a new token
|
|
21
|
+
* @param {Object} params - Creation parameters
|
|
22
|
+
* @param {Keypair} params.mint - Token mint keypair
|
|
23
|
+
* @param {string} params.name - Token name
|
|
24
|
+
* @param {string} params.symbol - Token symbol
|
|
25
|
+
* @param {string} params.uri - Metadata URI
|
|
26
|
+
* @param {PublicKey} params.payer - Creator public key (payer)
|
|
27
27
|
*
|
|
28
|
-
* ===
|
|
29
|
-
* @param {anchor.BN} [params.customLpSol] -
|
|
30
|
-
* -
|
|
31
|
-
* -
|
|
32
|
-
* - ⚠️
|
|
28
|
+
* === Phase 2 addition: advanced pool optional parameters (all 5 must be provided together or none at all) ===
|
|
29
|
+
* @param {anchor.BN} [params.customLpSol] - Custom liquidity pool SOL amount (lamports, 9-digit precision)
|
|
30
|
+
* - Not provided or null: creates a standard pool using the default value of 30 SOL
|
|
31
|
+
* - Provided: creates an advanced pool, must be within the range 10 SOL ~ 1000 SOL
|
|
32
|
+
* - ⚠️ Must be provided together with the other 4 custom parameters
|
|
33
33
|
*
|
|
34
|
-
* @param {anchor.BN} [params.customLpToken] -
|
|
35
|
-
* -
|
|
36
|
-
* -
|
|
34
|
+
* @param {anchor.BN} [params.customLpToken] - Custom liquidity pool Token amount (smallest unit, 9-digit precision)
|
|
35
|
+
* - Not provided or null: uses the default value of 1.073 billion Token
|
|
36
|
+
* - Provided: must be within the range 100 million ~ 10 billion Token
|
|
37
37
|
*
|
|
38
|
-
* @param {number} [params.customBorrowRatio] -
|
|
39
|
-
* -
|
|
40
|
-
* -
|
|
38
|
+
* @param {number} [params.customBorrowRatio] - Custom borrow pool token ratio (5-30 means 5%-30%)
|
|
39
|
+
* - Not provided or null: uses the default value of 20%
|
|
40
|
+
* - Provided: must be within the range 5 ~ 30
|
|
41
41
|
*
|
|
42
|
-
* @param {number} [params.customBorrowDuration] -
|
|
43
|
-
* -
|
|
44
|
-
* -
|
|
42
|
+
* @param {number} [params.customBorrowDuration] - Custom borrow duration (seconds)
|
|
43
|
+
* - Not provided or null: uses the default value configured in the Params account
|
|
44
|
+
* - Provided: must be within the range 3 days (259200 seconds) ~ 6 months (15552000 seconds)
|
|
45
45
|
*
|
|
46
|
-
* @param {number} [params.customFee] -
|
|
47
|
-
* -
|
|
48
|
-
* -
|
|
49
|
-
* -
|
|
50
|
-
* -
|
|
46
|
+
* @param {number} [params.customFee] - Custom fee rate (unit: basis points)
|
|
47
|
+
* - Not provided or null: uses the default fee configured in the Params account
|
|
48
|
+
* - Provided: must be within the range 1000 ~ 5000 (representing 1% ~ 5%)
|
|
49
|
+
* - Example: 2000 means 2%
|
|
50
|
+
* - Borrow fee is automatically increased by 20% (e.g. setting 2000 results in swap_fee=2000, borrow_fee=2400)
|
|
51
51
|
*
|
|
52
|
-
* @returns {Promise<Object>}
|
|
52
|
+
* @returns {Promise<Object>} Object containing transaction, signers and account info
|
|
53
53
|
*
|
|
54
54
|
* @example
|
|
55
|
-
* //
|
|
55
|
+
* // Create a standard token (using default parameters)
|
|
56
56
|
* const result = await sdk.token.create({
|
|
57
57
|
* mint: mintKeypair,
|
|
58
58
|
* name: "My Token",
|
|
@@ -62,7 +62,7 @@ class TokenModule {
|
|
|
62
62
|
* });
|
|
63
63
|
*
|
|
64
64
|
* @example
|
|
65
|
-
* //
|
|
65
|
+
* // Create an advanced token (custom liquidity pool parameters, all 5 parameters must be provided)
|
|
66
66
|
* const anchor = require('@coral-xyz/anchor');
|
|
67
67
|
* const result = await sdk.token.create({
|
|
68
68
|
* mint: mintKeypair,
|
|
@@ -71,9 +71,9 @@ class TokenModule {
|
|
|
71
71
|
* uri: "https://example.com/metadata.json",
|
|
72
72
|
* payer: wallet.publicKey,
|
|
73
73
|
* customLpSol: new anchor.BN('60000000000'), // 60 SOL
|
|
74
|
-
* customLpToken: new anchor.BN('1073000000000000000'), //
|
|
74
|
+
* customLpToken: new anchor.BN('1073000000000000000'), // 1.073 billion Token
|
|
75
75
|
* customBorrowRatio: 15, // 15%
|
|
76
|
-
* customBorrowDuration: 7 * 24 * 3600, // 7
|
|
76
|
+
* customBorrowDuration: 7 * 24 * 3600, // 7 days
|
|
77
77
|
* customFee: 2000 // 2% (2000 basis points)
|
|
78
78
|
* });
|
|
79
79
|
*/
|
|
@@ -83,7 +83,7 @@ class TokenModule {
|
|
|
83
83
|
symbol,
|
|
84
84
|
uri,
|
|
85
85
|
payer,
|
|
86
|
-
//
|
|
86
|
+
// Advanced pool optional parameters (all 5 must be provided together or none at all)
|
|
87
87
|
customLpSol = null,
|
|
88
88
|
customLpToken = null,
|
|
89
89
|
customBorrowRatio = null,
|
|
@@ -96,7 +96,7 @@ class TokenModule {
|
|
|
96
96
|
symbol,
|
|
97
97
|
uri,
|
|
98
98
|
payer: payer.toString(),
|
|
99
|
-
//
|
|
99
|
+
// Advanced pool parameters log
|
|
100
100
|
poolType: (customLpSol === null && customLpToken === null &&
|
|
101
101
|
customBorrowRatio === null && customBorrowDuration === null &&
|
|
102
102
|
customFee === null)
|
|
@@ -192,7 +192,7 @@ class TokenModule {
|
|
|
192
192
|
units: 400000
|
|
193
193
|
});
|
|
194
194
|
|
|
195
|
-
//
|
|
195
|
+
// Build the create token instruction (passing in 5 custom parameters)
|
|
196
196
|
const createIx = await this.sdk.program.methods
|
|
197
197
|
.createToken(
|
|
198
198
|
name,
|
|
@@ -202,7 +202,7 @@ class TokenModule {
|
|
|
202
202
|
customLpToken, // Option<u64>
|
|
203
203
|
customBorrowRatio, // Option<u8>
|
|
204
204
|
customBorrowDuration, // Option<u32>
|
|
205
|
-
customFee // Option<u16> -
|
|
205
|
+
customFee // Option<u16> - new parameter
|
|
206
206
|
)
|
|
207
207
|
.accounts({
|
|
208
208
|
payer: payer,
|
|
@@ -248,7 +248,7 @@ class TokenModule {
|
|
|
248
248
|
|
|
249
249
|
/**
|
|
250
250
|
* Create token and buy in one transaction
|
|
251
|
-
*
|
|
251
|
+
* Merges the create and buy instructions into a single transaction, submitted with one signature
|
|
252
252
|
*
|
|
253
253
|
* @param {Object} params - Creation and buy parameters
|
|
254
254
|
* @param {Keypair} params.mint - Token mint keypair
|
|
@@ -259,12 +259,12 @@ class TokenModule {
|
|
|
259
259
|
* @param {anchor.BN} params.buyTokenAmount - Amount of tokens to buy
|
|
260
260
|
* @param {anchor.BN} params.maxSolAmount - Maximum SOL to spend
|
|
261
261
|
*
|
|
262
|
-
* ===
|
|
263
|
-
* @param {anchor.BN} [params.customLpSol] -
|
|
264
|
-
* @param {anchor.BN} [params.customLpToken] -
|
|
265
|
-
* @param {number} [params.customBorrowRatio] -
|
|
266
|
-
* @param {number} [params.customBorrowDuration] -
|
|
267
|
-
* @param {number} [params.customFee] -
|
|
262
|
+
* === Phase 2 addition: advanced pool optional parameters (all 5 must be provided together or none at all) ===
|
|
263
|
+
* @param {anchor.BN} [params.customLpSol] - Custom liquidity pool SOL amount (lamports, 9-digit precision)
|
|
264
|
+
* @param {anchor.BN} [params.customLpToken] - Custom liquidity pool Token amount (smallest unit, 9-digit precision)
|
|
265
|
+
* @param {number} [params.customBorrowRatio] - Custom borrow pool token ratio (5-30 means 5%-30%)
|
|
266
|
+
* @param {number} [params.customBorrowDuration] - Custom borrow duration (seconds)
|
|
267
|
+
* @param {number} [params.customFee] - Custom fee rate (1000-5000 basis points, representing 1%-5%)
|
|
268
268
|
*
|
|
269
269
|
* @param {Object} options - Optional parameters
|
|
270
270
|
* @param {number} options.computeUnits - Compute units limit, default 1800000
|
|
@@ -278,7 +278,7 @@ class TokenModule {
|
|
|
278
278
|
payer,
|
|
279
279
|
buyTokenAmount,
|
|
280
280
|
maxSolAmount,
|
|
281
|
-
//
|
|
281
|
+
// Advanced pool optional parameters (all 5 must be provided together or none at all)
|
|
282
282
|
customLpSol = null,
|
|
283
283
|
customLpToken = null,
|
|
284
284
|
customBorrowRatio = null,
|
|
@@ -297,12 +297,12 @@ class TokenModule {
|
|
|
297
297
|
maxSolAmount: maxSolAmount.toString()
|
|
298
298
|
});
|
|
299
299
|
|
|
300
|
-
// 1.
|
|
300
|
+
// 1. Parameter validation
|
|
301
301
|
if (!anchor.BN.isBN(buyTokenAmount) || !anchor.BN.isBN(maxSolAmount)) {
|
|
302
302
|
throw new Error('buyTokenAmount and maxSolAmount must be anchor.BN type');
|
|
303
303
|
}
|
|
304
304
|
|
|
305
|
-
// 2.
|
|
305
|
+
// 2. Call the create method to get the create transaction (passing all custom parameters)
|
|
306
306
|
console.log('Step 1: Building create transaction...');
|
|
307
307
|
const createResult = await this.create({
|
|
308
308
|
mint,
|
|
@@ -310,7 +310,7 @@ class TokenModule {
|
|
|
310
310
|
symbol,
|
|
311
311
|
uri,
|
|
312
312
|
payer,
|
|
313
|
-
//
|
|
313
|
+
// Pass custom liquidity pool parameters (5 parameters)
|
|
314
314
|
customLpSol,
|
|
315
315
|
customLpToken,
|
|
316
316
|
customBorrowRatio,
|
|
@@ -318,16 +318,16 @@ class TokenModule {
|
|
|
318
318
|
customFee
|
|
319
319
|
});
|
|
320
320
|
|
|
321
|
-
// 3.
|
|
322
|
-
//
|
|
321
|
+
// 3. Read the fee recipient address from the params account
|
|
322
|
+
// Because curve_account has not been created yet, it cannot be read from chain
|
|
323
323
|
console.log('Step 2: Fetching fee recipient accounts from params...');
|
|
324
324
|
|
|
325
|
-
//
|
|
326
|
-
//
|
|
325
|
+
// Get the fee recipient accounts directly from the SDK configuration (these are set during SDK initialization)
|
|
326
|
+
// Avoid using program.account.params.fetch() because there may be provider configuration issues
|
|
327
327
|
const feeRecipientAccount = this.sdk.feeRecipient;
|
|
328
328
|
const baseFeeRecipientAccount = this.sdk.baseFeeRecipient;
|
|
329
329
|
|
|
330
|
-
//
|
|
330
|
+
// Validate that these accounts are configured
|
|
331
331
|
if (!feeRecipientAccount || !baseFeeRecipientAccount) {
|
|
332
332
|
throw new Error('Fee recipient accounts not configured in SDK options');
|
|
333
333
|
}
|
|
@@ -336,17 +336,17 @@ class TokenModule {
|
|
|
336
336
|
console.log(' Partner fee recipient:', feeRecipientAccount.toString());
|
|
337
337
|
console.log(' Base fee recipient:', baseFeeRecipientAccount.toString());
|
|
338
338
|
|
|
339
|
-
// 4.
|
|
339
|
+
// 4. Prepare the extra accounts required for buy
|
|
340
340
|
console.log('Step 3: Calculating buy-related accounts...');
|
|
341
341
|
const mintPubkey = mint.publicKey;
|
|
342
342
|
|
|
343
|
-
//
|
|
343
|
+
// Calculate user token account
|
|
344
344
|
const userTokenAccount = await getAssociatedTokenAddress(
|
|
345
345
|
mintPubkey,
|
|
346
346
|
payer
|
|
347
347
|
);
|
|
348
348
|
|
|
349
|
-
//
|
|
349
|
+
// Calculate cooldown PDA
|
|
350
350
|
const [cooldownPDA] = PublicKey.findProgramAddressSync(
|
|
351
351
|
[
|
|
352
352
|
Buffer.from('trade_cooldown'),
|
|
@@ -356,7 +356,7 @@ class TokenModule {
|
|
|
356
356
|
this.sdk.programId
|
|
357
357
|
);
|
|
358
358
|
|
|
359
|
-
//
|
|
359
|
+
// Calculate orderbook PDAs (reuse the values calculated in create)
|
|
360
360
|
const [upOrderbook] = PublicKey.findProgramAddressSync(
|
|
361
361
|
[Buffer.from('up_orderbook'), mintPubkey.toBuffer()],
|
|
362
362
|
this.sdk.programId
|
|
@@ -371,7 +371,7 @@ class TokenModule {
|
|
|
371
371
|
console.log(' User token account:', userTokenAccount.toString());
|
|
372
372
|
console.log(' Cooldown PDA:', cooldownPDA.toString());
|
|
373
373
|
|
|
374
|
-
// 5.
|
|
374
|
+
// 5. Check if the user token account exists, create ATA instruction
|
|
375
375
|
console.log('Step 4: Checking if user token account exists...');
|
|
376
376
|
const userTokenAccountInfo = await this.sdk.connection.getAccountInfo(userTokenAccount);
|
|
377
377
|
const createAtaIx = userTokenAccountInfo === null
|
|
@@ -391,7 +391,7 @@ class TokenModule {
|
|
|
391
391
|
console.log(' User token account already exists');
|
|
392
392
|
}
|
|
393
393
|
|
|
394
|
-
// 6.
|
|
394
|
+
// 6. Build the buy instruction
|
|
395
395
|
console.log('Step 5: Building buy instruction...');
|
|
396
396
|
const buyIx = await this.sdk.program.methods
|
|
397
397
|
.buy(buyTokenAmount, maxSolAmount)
|
|
@@ -415,31 +415,31 @@ class TokenModule {
|
|
|
415
415
|
})
|
|
416
416
|
.instruction();
|
|
417
417
|
|
|
418
|
-
// 7.
|
|
418
|
+
// 7. Merge transactions: create + buy
|
|
419
419
|
console.log('Step 6: Merging create and buy transactions...');
|
|
420
420
|
const transaction = new Transaction();
|
|
421
421
|
|
|
422
|
-
//
|
|
422
|
+
// Set compute unit limit
|
|
423
423
|
const modifyComputeUnits = ComputeBudgetProgram.setComputeUnitLimit({
|
|
424
424
|
units: computeUnits
|
|
425
425
|
});
|
|
426
426
|
transaction.add(modifyComputeUnits);
|
|
427
427
|
|
|
428
|
-
//
|
|
428
|
+
// Add all instructions from the create transaction (skip the compute unit instruction in create)
|
|
429
429
|
createResult.transaction.instructions.forEach(ix => {
|
|
430
|
-
//
|
|
430
|
+
// Skip the compute unit instruction in the create transaction (we already added it)
|
|
431
431
|
if (ix.programId.equals(ComputeBudgetProgram.programId)) {
|
|
432
432
|
return;
|
|
433
433
|
}
|
|
434
434
|
transaction.add(ix);
|
|
435
435
|
});
|
|
436
436
|
|
|
437
|
-
//
|
|
437
|
+
// Add ATA creation instruction (if needed)
|
|
438
438
|
if (createAtaIx) {
|
|
439
439
|
transaction.add(createAtaIx);
|
|
440
440
|
}
|
|
441
441
|
|
|
442
|
-
//
|
|
442
|
+
// Add buy instruction
|
|
443
443
|
transaction.add(buyIx);
|
|
444
444
|
|
|
445
445
|
console.log('CreateAndBuy transaction built successfully:');
|
|
@@ -447,14 +447,14 @@ class TokenModule {
|
|
|
447
447
|
console.log(' Compute units:', computeUnits);
|
|
448
448
|
console.log(' Signers required:', [payer.toString(), mint.publicKey.toString()]);
|
|
449
449
|
|
|
450
|
-
// 8.
|
|
450
|
+
// 8. Return the merged transaction
|
|
451
451
|
return {
|
|
452
452
|
transaction,
|
|
453
|
-
signers: [mint], // mint keypair
|
|
453
|
+
signers: [mint], // mint keypair needs to sign
|
|
454
454
|
accounts: {
|
|
455
|
-
// create
|
|
455
|
+
// create accounts
|
|
456
456
|
...createResult.accounts,
|
|
457
|
-
// buy
|
|
457
|
+
// buy accounts
|
|
458
458
|
userTokenAccount,
|
|
459
459
|
cooldown: cooldownPDA,
|
|
460
460
|
feeRecipientAccount,
|
package/src/modules/tools.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const anchor = require('@coral-xyz/anchor');
|
|
2
2
|
const { PublicKey, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction } = require('@solana/web3.js');
|
|
3
3
|
const { getAssociatedTokenAddress, TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID } = require('@solana/spl-token');
|
|
4
|
-
//
|
|
4
|
+
// Uniformly use the buffer package, consistent across all platforms
|
|
5
5
|
const { Buffer } = require('buffer');
|
|
6
6
|
|
|
7
7
|
/**
|