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.
@@ -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
- // 统一使用 buffer 包,所有平台一致
4
+ // Uniformly use the buffer package, consistent across all platforms
5
5
  const { Buffer } = require('buffer');
6
6
 
7
- // Metaplex Token Metadata 程序ID
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 - 代币mint keypair
23
- * @param {string} params.name - 代币名称
24
- * @param {string} params.symbol - 代币符号
25
- * @param {string} params.uri - 元数据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
- * === 第二阶段新增:高级版池子可选参数(5个参数必须全部提供或全部不提供)===
29
- * @param {anchor.BN} [params.customLpSol] - 自定义流动池SOL数量(lamports,9位精度)
30
- * - 不传或传null:创建普通版池子,使用默认值30 SOL
31
- * - 传入值:创建高级版池子,需在 10 SOL ~ 1000 SOL 范围内
32
- * - ⚠️ 必须与其他4个 custom 参数同时提供
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] - 自定义流动池Token数量(最小单位,9位精度)
35
- * - 不传或传null:使用默认值10.73亿 Token
36
- * - 传入值:需在 1亿 ~ 100亿 Token 范围内
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] - 自定义借贷池代币占比(5-30表示5%-30%)
39
- * - 不传或传null:使用默认值20%
40
- * - 传入值:需在 5 ~ 30 范围内
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
- * - 不传或传null:使用Params账户配置的默认值
44
- * - 传入值:需在 3天(259200秒) ~ 6个月(15552000秒) 范围内
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] - 自定义手续费率(单位:基点 basis points)
47
- * - 不传或传null:使用Params账户配置的默认手续费
48
- * - 传入值:需在 1000 ~ 5000 范围内(表示 1% ~ 5%)
49
- * - 示例:2000 表示 2%
50
- * - 借贷手续费会自动上浮20%(如设置2000,则 swap_fee=2000,borrow_fee=2400)
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>} 包含transaction、signers和账户信息的对象
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
- * // 创建高级版代币(自定义流动池参数,必须提供全部5个参数)
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'), // 10.73亿 Token
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
- // 高级版池子可选参数(5个参数必须全部提供或全部不提供)
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
- // 构建创建代币指令(传入5个自定义参数)
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
- * 将 create 和 buy 两个指令合并到一个交易中,一次签名提交
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
- * === 第二阶段新增:高级版池子可选参数(5个参数必须全部提供或全部不提供)===
263
- * @param {anchor.BN} [params.customLpSol] - 自定义流动池SOL数量(lamports,9位精度)
264
- * @param {anchor.BN} [params.customLpToken] - 自定义流动池Token数量(最小单位,9位精度)
265
- * @param {number} [params.customBorrowRatio] - 自定义借贷池代币占比(5-30表示5%-30%)
266
- * @param {number} [params.customBorrowDuration] - 自定义借贷时长(秒)
267
- * @param {number} [params.customFee] - 自定义手续费率(1000-5000 basis points,表示1%-5%)
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
- // 高级版池子可选参数(5个参数必须全部提供或全部不提供)
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. 调用 create 方法获取 create 交易(传递所有自定义参数)
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
- // 传递自定义流动池参数(5个参数)
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. 从 params 账户读取手续费接收地址
322
- // 因为此时 curve_account 还未创建,无法从链上读取
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
- // 直接从 SDK 配置中获取手续费接收账户(这些在 SDK 初始化时已经设置)
326
- // 避免使用 program.account.params.fetch() 因为可能有 provider 配置问题
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. 准备 buy 所需的额外账户
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
- // 计算 cooldown PDA
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
- // 计算 orderbook PDAs (复用 create 中计算的值)
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. 检查用户代币账户是否存在,创建 ATA 指令
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. 构建 buy 指令
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. 合并交易:create + buy
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
- // 添加 create 交易的所有指令(跳过 create 中的计算单元指令)
428
+ // Add all instructions from the create transaction (skip the compute unit instruction in create)
429
429
  createResult.transaction.instructions.forEach(ix => {
430
- // 跳过 create 交易中的计算单元指令(我们已经添加了)
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
- // 添加 ATA 创建指令(如果需要)
437
+ // Add ATA creation instruction (if needed)
438
438
  if (createAtaIx) {
439
439
  transaction.add(createAtaIx);
440
440
  }
441
441
 
442
- // 添加 buy 指令
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,
@@ -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
- // 统一使用 buffer 包,所有平台一致
4
+ // Uniformly use the buffer package, consistent across all platforms
5
5
  const { Buffer } = require('buffer');
6
6
 
7
7
  /**