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/trading.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
const { ComputeBudgetProgram, PublicKey, Transaction, SystemProgram, SYSVAR_RENT_PUBKEY } = require('@solana/web3.js');
|
|
2
2
|
const { createAssociatedTokenAccountInstruction, getAssociatedTokenAddress, TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID } = require('@solana/spl-token');
|
|
3
3
|
const anchor = require('@coral-xyz/anchor');
|
|
4
|
-
//
|
|
4
|
+
// Use the buffer package uniformly, consistent across all platforms
|
|
5
5
|
const { Buffer } = require('buffer');
|
|
6
6
|
const { MAX_CANDIDATE_INDICES } = require('./simulator/utils');
|
|
7
7
|
|
|
8
|
-
//
|
|
8
|
+
// Environment detection and conditional loading
|
|
9
9
|
const IS_NODE = typeof process !== 'undefined' && process.versions && process.versions.node;
|
|
10
10
|
|
|
11
11
|
/**
|
|
@@ -293,11 +293,11 @@ class TradingModule {
|
|
|
293
293
|
* Margin Long
|
|
294
294
|
* @param {Object} params - Long parameters
|
|
295
295
|
* @param {string|PublicKey} params.mintAccount - Token mint account address
|
|
296
|
-
* @param {anchor.BN} params.buyTokenAmount - Amount of tokens to buy (
|
|
297
|
-
* @param {anchor.BN} params.maxSolAmount - Maximum SOL to spend (
|
|
298
|
-
* @param {anchor.BN} params.marginSol - Margin amount (
|
|
299
|
-
* @param {anchor.BN} params.closePrice - Close price (
|
|
300
|
-
* @param {Array<number>} params.closeInsertIndices - Close insert indices array (
|
|
296
|
+
* @param {anchor.BN} params.buyTokenAmount - Amount of tokens to buy (fixed value)
|
|
297
|
+
* @param {anchor.BN} params.maxSolAmount - Maximum SOL to spend (the actual amount may be slightly less)
|
|
298
|
+
* @param {anchor.BN} params.marginSol - Margin amount (margin in SOL)
|
|
299
|
+
* @param {anchor.BN} params.closePrice - Close price (close price / stop loss price)
|
|
300
|
+
* @param {Array<number>} params.closeInsertIndices - Close insert indices array (position indices for inserting into the orderbook at close)
|
|
301
301
|
* @param {PublicKey} params.payer - Payer public key
|
|
302
302
|
* @param {Object} options - Optional parameters
|
|
303
303
|
* @param {number} options.computeUnits - Compute units limit, default 1400000
|
|
@@ -310,14 +310,14 @@ class TradingModule {
|
|
|
310
310
|
* maxSolAmount: new anchor.BN("1100000000"),
|
|
311
311
|
* marginSol: new anchor.BN("2200000000"),
|
|
312
312
|
* closePrice: new anchor.BN("1000000000000000"),
|
|
313
|
-
* closeInsertIndices: [0, 1, 2], //
|
|
313
|
+
* closeInsertIndices: [0, 1, 2], // insert position index array
|
|
314
314
|
* payer: wallet.publicKey
|
|
315
315
|
* });
|
|
316
316
|
*/
|
|
317
317
|
async long({ mintAccount, buyTokenAmount, maxSolAmount, marginSol, closePrice, closeInsertIndices, payer }, options = {}) {
|
|
318
318
|
const { computeUnits = 1400000 } = options;
|
|
319
319
|
|
|
320
|
-
// 1.
|
|
320
|
+
// 1. Parameter validation and conversion
|
|
321
321
|
const mint = typeof mintAccount === 'string' ? new PublicKey(mintAccount) : mintAccount;
|
|
322
322
|
|
|
323
323
|
if (!anchor.BN.isBN(buyTokenAmount) || !anchor.BN.isBN(maxSolAmount) ||
|
|
@@ -402,18 +402,18 @@ class TradingModule {
|
|
|
402
402
|
}
|
|
403
403
|
|
|
404
404
|
/**
|
|
405
|
-
*
|
|
406
|
-
* @param {Object} params -
|
|
407
|
-
* @param {string|PublicKey} params.mintAccount -
|
|
408
|
-
* @param {anchor.BN} params.borrowSellTokenAmount -
|
|
409
|
-
* @param {anchor.BN} params.minSolOutput -
|
|
410
|
-
* @param {anchor.BN} params.marginSol -
|
|
411
|
-
* @param {anchor.BN} params.closePrice -
|
|
412
|
-
* @param {Array<number>} params.closeInsertIndices - Close insert indices array (
|
|
413
|
-
* @param {PublicKey} params.payer -
|
|
414
|
-
* @param {Object} options -
|
|
415
|
-
* @param {number} options.computeUnits -
|
|
416
|
-
* @returns {Promise<Object>}
|
|
405
|
+
* Margin Short
|
|
406
|
+
* @param {Object} params - Short parameters
|
|
407
|
+
* @param {string|PublicKey} params.mintAccount - Token mint account address
|
|
408
|
+
* @param {anchor.BN} params.borrowSellTokenAmount - Borrowed token amount to sell (the amount of tokens you wish to sell)
|
|
409
|
+
* @param {anchor.BN} params.minSolOutput - Minimum SOL output (the minimum SOL received after selling)
|
|
410
|
+
* @param {anchor.BN} params.marginSol - Margin amount (SOL)
|
|
411
|
+
* @param {anchor.BN} params.closePrice - Close price (stop loss price)
|
|
412
|
+
* @param {Array<number>} params.closeInsertIndices - Close insert indices array (position indices for inserting into the orderbook at close)
|
|
413
|
+
* @param {PublicKey} params.payer - Payer public key
|
|
414
|
+
* @param {Object} options - Optional parameters
|
|
415
|
+
* @param {number} options.computeUnits - Compute units limit, default 1400000
|
|
416
|
+
* @returns {Promise<Object>} Object containing transaction, signers and account info
|
|
417
417
|
*
|
|
418
418
|
* @example
|
|
419
419
|
* const result = await sdk.trading.short({
|
|
@@ -422,14 +422,14 @@ class TradingModule {
|
|
|
422
422
|
* minSolOutput: new anchor.BN("100"),
|
|
423
423
|
* marginSol: new anchor.BN("2200000000"),
|
|
424
424
|
* closePrice: new anchor.BN("1000000000000000"),
|
|
425
|
-
* closeInsertIndices: [0, 1, 2], //
|
|
425
|
+
* closeInsertIndices: [0, 1, 2], // insert position index array
|
|
426
426
|
* payer: wallet.publicKey
|
|
427
427
|
* });
|
|
428
428
|
*/
|
|
429
429
|
async short({ mintAccount, borrowSellTokenAmount, minSolOutput, marginSol, closePrice, closeInsertIndices, payer }, options = {}) {
|
|
430
430
|
const { computeUnits = 1400000 } = options;
|
|
431
431
|
|
|
432
|
-
// 1.
|
|
432
|
+
// 1. Parameter validation and conversion
|
|
433
433
|
const mint = typeof mintAccount === 'string' ? new PublicKey(mintAccount) : mintAccount;
|
|
434
434
|
|
|
435
435
|
if (!anchor.BN.isBN(borrowSellTokenAmount) || !anchor.BN.isBN(minSolOutput) ||
|
|
@@ -445,7 +445,7 @@ class TradingModule {
|
|
|
445
445
|
throw new Error('closeInsertIndices array cannot exceed 20 elements');
|
|
446
446
|
}
|
|
447
447
|
|
|
448
|
-
// 2.
|
|
448
|
+
// 2. Calculate PDA accounts
|
|
449
449
|
const accounts = this._calculatePDAAccounts(mint);
|
|
450
450
|
|
|
451
451
|
// 3. Calculate OrderBook PDA addresses
|
|
@@ -459,7 +459,7 @@ class TradingModule {
|
|
|
459
459
|
this.sdk.programId
|
|
460
460
|
);
|
|
461
461
|
|
|
462
|
-
// 4.
|
|
462
|
+
// 4. Build transaction instructions
|
|
463
463
|
const modifyComputeUnits = ComputeBudgetProgram.setComputeUnitLimit({
|
|
464
464
|
units: computeUnits
|
|
465
465
|
});
|
|
@@ -489,15 +489,15 @@ class TradingModule {
|
|
|
489
489
|
})
|
|
490
490
|
.instruction();
|
|
491
491
|
|
|
492
|
-
// 5.
|
|
492
|
+
// 5. Create transaction and add instructions
|
|
493
493
|
const transaction = new Transaction();
|
|
494
494
|
transaction.add(modifyComputeUnits);
|
|
495
495
|
transaction.add(shortIx);
|
|
496
496
|
|
|
497
|
-
// 6.
|
|
497
|
+
// 6. Return transaction object and related info
|
|
498
498
|
return {
|
|
499
499
|
transaction,
|
|
500
|
-
signers: [], //
|
|
500
|
+
signers: [], // Short transaction doesn't need additional signers, only payer signature
|
|
501
501
|
accounts: {
|
|
502
502
|
mint: mint,
|
|
503
503
|
curveAccount: accounts.curveAccount,
|
|
@@ -514,26 +514,26 @@ class TradingModule {
|
|
|
514
514
|
}
|
|
515
515
|
|
|
516
516
|
/**
|
|
517
|
-
*
|
|
518
|
-
* @param {Object} params -
|
|
519
|
-
* @param {string|PublicKey} params.mintAccount -
|
|
520
|
-
* @param {anchor.BN} params.sellTokenAmount -
|
|
521
|
-
* @param {anchor.BN} params.minSolOutput -
|
|
522
|
-
* @param {number|anchor.BN} params.closeOrderId -
|
|
523
|
-
* @param {Array<number>} params.closeOrderIndices -
|
|
524
|
-
* @param {PublicKey} params.payer -
|
|
525
|
-
* @param {PublicKey} params.userSolAccount -
|
|
526
|
-
* @param {Object} options -
|
|
527
|
-
* @param {number} options.computeUnits -
|
|
528
|
-
* @returns {Promise<Object>}
|
|
517
|
+
* Close Long Position
|
|
518
|
+
* @param {Object} params - Close position parameters
|
|
519
|
+
* @param {string|PublicKey} params.mintAccount - Token mint account address
|
|
520
|
+
* @param {anchor.BN} params.sellTokenAmount - Amount of tokens to sell
|
|
521
|
+
* @param {anchor.BN} params.minSolOutput - Minimum SOL output after selling
|
|
522
|
+
* @param {number|anchor.BN} params.closeOrderId - Order unique ID
|
|
523
|
+
* @param {Array<number>} params.closeOrderIndices - Close order position indices array
|
|
524
|
+
* @param {PublicKey} params.payer - Payer public key
|
|
525
|
+
* @param {PublicKey} params.userSolAccount - User SOL account to receive funds (must be order opener)
|
|
526
|
+
* @param {Object} options - Optional parameters
|
|
527
|
+
* @param {number} options.computeUnits - Compute units limit, default 1400000
|
|
528
|
+
* @returns {Promise<Object>} Object containing transaction, signers and account info
|
|
529
529
|
*
|
|
530
530
|
* @example
|
|
531
531
|
* const result = await sdk.trading.closeLong({
|
|
532
532
|
* mintAccount: "HZBos3RNhExDcAtzmdKXhTd4sVcQFBiT3FDBgmBBMk7",
|
|
533
533
|
* sellTokenAmount: new anchor.BN("1000000000"),
|
|
534
534
|
* minSolOutput: new anchor.BN("100000000"),
|
|
535
|
-
* closeOrderId: 12345, //
|
|
536
|
-
* closeOrderIndices: [10, 11, 12], //
|
|
535
|
+
* closeOrderId: 12345, // order unique ID
|
|
536
|
+
* closeOrderIndices: [10, 11, 12], // candidate index array
|
|
537
537
|
* payer: wallet.publicKey,
|
|
538
538
|
* userSolAccount: orderOwnerPublicKey
|
|
539
539
|
* });
|
|
@@ -541,17 +541,17 @@ class TradingModule {
|
|
|
541
541
|
async closeLong({ mintAccount, sellTokenAmount, minSolOutput, closeOrderId, closeOrderIndices, payer, userSolAccount }, options = {}) {
|
|
542
542
|
const { computeUnits = 1400000 } = options;
|
|
543
543
|
|
|
544
|
-
// 1.
|
|
544
|
+
// 1. Parameter validation and conversion
|
|
545
545
|
const mint = typeof mintAccount === 'string' ? new PublicKey(mintAccount) : mintAccount;
|
|
546
546
|
|
|
547
547
|
if (!anchor.BN.isBN(sellTokenAmount) || !anchor.BN.isBN(minSolOutput)) {
|
|
548
548
|
throw new Error('sellTokenAmount and minSolOutput must be anchor.BN type');
|
|
549
549
|
}
|
|
550
550
|
|
|
551
|
-
//
|
|
551
|
+
// Convert closeOrderId to anchor.BN (u64)
|
|
552
552
|
const closeOrderIdBN = anchor.BN.isBN(closeOrderId) ? closeOrderId : new anchor.BN(closeOrderId);
|
|
553
553
|
|
|
554
|
-
//
|
|
554
|
+
// Validate the closeOrderIndices parameter
|
|
555
555
|
if (!Array.isArray(closeOrderIndices) || closeOrderIndices.length === 0) {
|
|
556
556
|
throw new Error('closeOrderIndices must be a non-empty array');
|
|
557
557
|
}
|
|
@@ -560,7 +560,7 @@ class TradingModule {
|
|
|
560
560
|
throw new Error(`closeOrderIndices array cannot exceed ${MAX_CANDIDATE_INDICES} elements`);
|
|
561
561
|
}
|
|
562
562
|
|
|
563
|
-
//
|
|
563
|
+
// Validate and convert userSolAccount - supports string or PublicKey object
|
|
564
564
|
let userSolAccountPubkey;
|
|
565
565
|
if (!userSolAccount) {
|
|
566
566
|
throw new Error('userSolAccount is required');
|
|
@@ -575,7 +575,7 @@ class TradingModule {
|
|
|
575
575
|
} else if (userSolAccount instanceof PublicKey) {
|
|
576
576
|
userSolAccountPubkey = userSolAccount;
|
|
577
577
|
} else if (userSolAccount.constructor && userSolAccount.constructor.name === 'PublicKey') {
|
|
578
|
-
//
|
|
578
|
+
// Handle cross-package PublicKey instances - extract the string and recreate
|
|
579
579
|
try {
|
|
580
580
|
userSolAccountPubkey = new PublicKey(userSolAccount.toString());
|
|
581
581
|
} catch (error) {
|
|
@@ -585,10 +585,10 @@ class TradingModule {
|
|
|
585
585
|
throw new Error('userSolAccount must be a valid PublicKey or PublicKey string');
|
|
586
586
|
}
|
|
587
587
|
|
|
588
|
-
// 2.
|
|
588
|
+
// 2. Calculate PDA accounts
|
|
589
589
|
const accounts = this._calculatePDAAccounts(mint);
|
|
590
590
|
|
|
591
|
-
// 3.
|
|
591
|
+
// 3. Calculate OrderBook PDA addresses
|
|
592
592
|
const [upOrderbook] = PublicKey.findProgramAddressSync(
|
|
593
593
|
[Buffer.from('up_orderbook'), mint.toBuffer()],
|
|
594
594
|
this.sdk.programId
|
|
@@ -599,22 +599,22 @@ class TradingModule {
|
|
|
599
599
|
this.sdk.programId
|
|
600
600
|
);
|
|
601
601
|
|
|
602
|
-
// 4.
|
|
602
|
+
// 4. Get fee recipient accounts from curve account (skipBalances: only need addresses)
|
|
603
603
|
const curveAccountInfo = await this.sdk.chain.getCurveAccount(mint, { skipBalances: true });
|
|
604
604
|
const feeRecipientAccount = new PublicKey(curveAccountInfo.feeRecipient);
|
|
605
605
|
const baseFeeRecipientAccount = new PublicKey(curveAccountInfo.baseFeeRecipient);
|
|
606
606
|
|
|
607
|
-
// 5.
|
|
607
|
+
// 5. Build transaction instructions
|
|
608
608
|
const modifyComputeUnits = ComputeBudgetProgram.setComputeUnitLimit({
|
|
609
609
|
units: computeUnits
|
|
610
610
|
});
|
|
611
611
|
|
|
612
612
|
const closeLongIx = await this.sdk.program.methods
|
|
613
613
|
.closeLong(
|
|
614
|
-
sellTokenAmount, // sell_token_amount:
|
|
615
|
-
minSolOutput, // min_sol_output:
|
|
616
|
-
closeOrderIdBN, // close_order_id:
|
|
617
|
-
closeOrderIndices // close_order_indices:
|
|
614
|
+
sellTokenAmount, // sell_token_amount: amount of tokens to sell
|
|
615
|
+
minSolOutput, // min_sol_output: minimum SOL output
|
|
616
|
+
closeOrderIdBN, // close_order_id: order unique ID
|
|
617
|
+
closeOrderIndices // close_order_indices: close order indices array
|
|
618
618
|
)
|
|
619
619
|
.accounts({
|
|
620
620
|
payer: payer,
|
|
@@ -623,7 +623,7 @@ class TradingModule {
|
|
|
623
623
|
poolTokenAccount: accounts.poolTokenAccount,
|
|
624
624
|
poolBorrowTokenAccount: accounts.poolBorrowTokenAccount,
|
|
625
625
|
poolSolAccount: accounts.poolSolAccount,
|
|
626
|
-
userSolAccount: userSolAccountPubkey, //
|
|
626
|
+
userSolAccount: userSolAccountPubkey, // User SOL account (must be order opener)
|
|
627
627
|
tokenProgram: TOKEN_PROGRAM_ID,
|
|
628
628
|
systemProgram: SystemProgram.programId,
|
|
629
629
|
rent: SYSVAR_RENT_PUBKEY,
|
|
@@ -634,15 +634,15 @@ class TradingModule {
|
|
|
634
634
|
})
|
|
635
635
|
.instruction();
|
|
636
636
|
|
|
637
|
-
// 6.
|
|
637
|
+
// 6. Create transaction and add instructions
|
|
638
638
|
const transaction = new Transaction();
|
|
639
639
|
transaction.add(modifyComputeUnits);
|
|
640
640
|
transaction.add(closeLongIx);
|
|
641
641
|
|
|
642
|
-
// 7.
|
|
642
|
+
// 7. Return transaction object and related info
|
|
643
643
|
return {
|
|
644
644
|
transaction,
|
|
645
|
-
signers: [], //
|
|
645
|
+
signers: [], // Close long transaction doesn't need additional signers, only payer signature
|
|
646
646
|
accounts: {
|
|
647
647
|
mint: mint,
|
|
648
648
|
curveAccount: accounts.curveAccount,
|
|
@@ -664,26 +664,26 @@ class TradingModule {
|
|
|
664
664
|
|
|
665
665
|
|
|
666
666
|
/**
|
|
667
|
-
*
|
|
668
|
-
* @param {Object} params -
|
|
669
|
-
* @param {string|PublicKey} params.mintAccount -
|
|
670
|
-
* @param {anchor.BN} params.buyTokenAmount -
|
|
671
|
-
* @param {anchor.BN} params.maxSolAmount -
|
|
672
|
-
* @param {number|anchor.BN} params.closeOrderId -
|
|
673
|
-
* @param {Array<number>} params.closeOrderIndices -
|
|
674
|
-
* @param {PublicKey} params.payer -
|
|
675
|
-
* @param {PublicKey} params.userSolAccount -
|
|
676
|
-
* @param {Object} options -
|
|
677
|
-
* @param {number} options.computeUnits -
|
|
678
|
-
* @returns {Promise<Object>}
|
|
667
|
+
* Close Short Position
|
|
668
|
+
* @param {Object} params - Close position parameters
|
|
669
|
+
* @param {string|PublicKey} params.mintAccount - Token mint account address
|
|
670
|
+
* @param {anchor.BN} params.buyTokenAmount - Amount of tokens to buy
|
|
671
|
+
* @param {anchor.BN} params.maxSolAmount - Maximum SOL amount to spend
|
|
672
|
+
* @param {number|anchor.BN} params.closeOrderId - Order unique ID
|
|
673
|
+
* @param {Array<number>} params.closeOrderIndices - Close order position indices array
|
|
674
|
+
* @param {PublicKey} params.payer - Payer public key
|
|
675
|
+
* @param {PublicKey} params.userSolAccount - User SOL account to receive funds (must be order opener)
|
|
676
|
+
* @param {Object} options - Optional parameters
|
|
677
|
+
* @param {number} options.computeUnits - Compute units limit, default 1400000
|
|
678
|
+
* @returns {Promise<Object>} Object containing transaction, signers and account info
|
|
679
679
|
*
|
|
680
680
|
* @example
|
|
681
681
|
* const result = await sdk.trading.closeShort({
|
|
682
682
|
* mintAccount: "HZBos3RNhExDcAtzmdKXhTd4sVcQFBiT3FDBgmBBMk7",
|
|
683
683
|
* buyTokenAmount: new anchor.BN("1000000000"),
|
|
684
684
|
* maxSolAmount: new anchor.BN("100000000"),
|
|
685
|
-
* closeOrderId: 12345, //
|
|
686
|
-
* closeOrderIndices: [10, 11, 12], //
|
|
685
|
+
* closeOrderId: 12345, // order unique ID
|
|
686
|
+
* closeOrderIndices: [10, 11, 12], // candidate index array
|
|
687
687
|
* payer: wallet.publicKey,
|
|
688
688
|
* userSolAccount: orderOwnerPublicKey
|
|
689
689
|
* });
|
|
@@ -691,17 +691,17 @@ class TradingModule {
|
|
|
691
691
|
async closeShort({ mintAccount, buyTokenAmount, maxSolAmount, closeOrderId, closeOrderIndices, payer, userSolAccount }, options = {}) {
|
|
692
692
|
const { computeUnits = 1400000 } = options;
|
|
693
693
|
|
|
694
|
-
// 1.
|
|
694
|
+
// 1. Parameter validation and conversion
|
|
695
695
|
const mint = typeof mintAccount === 'string' ? new PublicKey(mintAccount) : mintAccount;
|
|
696
696
|
|
|
697
697
|
if (!anchor.BN.isBN(buyTokenAmount) || !anchor.BN.isBN(maxSolAmount)) {
|
|
698
698
|
throw new Error('buyTokenAmount and maxSolAmount must be anchor.BN type');
|
|
699
699
|
}
|
|
700
700
|
|
|
701
|
-
//
|
|
701
|
+
// Convert closeOrderId to anchor.BN (u64)
|
|
702
702
|
const closeOrderIdBN = anchor.BN.isBN(closeOrderId) ? closeOrderId : new anchor.BN(closeOrderId);
|
|
703
703
|
|
|
704
|
-
//
|
|
704
|
+
// Validate the closeOrderIndices parameter
|
|
705
705
|
if (!Array.isArray(closeOrderIndices) || closeOrderIndices.length === 0) {
|
|
706
706
|
throw new Error('closeOrderIndices must be a non-empty array');
|
|
707
707
|
}
|
|
@@ -710,7 +710,7 @@ class TradingModule {
|
|
|
710
710
|
throw new Error(`closeOrderIndices array cannot exceed ${MAX_CANDIDATE_INDICES} elements`);
|
|
711
711
|
}
|
|
712
712
|
|
|
713
|
-
//
|
|
713
|
+
// Validate and convert userSolAccount - supports string or PublicKey object
|
|
714
714
|
let userSolAccountPubkey;
|
|
715
715
|
if (!userSolAccount) {
|
|
716
716
|
throw new Error('userSolAccount is required');
|
|
@@ -725,7 +725,7 @@ class TradingModule {
|
|
|
725
725
|
} else if (userSolAccount instanceof PublicKey) {
|
|
726
726
|
userSolAccountPubkey = userSolAccount;
|
|
727
727
|
} else if (userSolAccount.constructor && userSolAccount.constructor.name === 'PublicKey') {
|
|
728
|
-
//
|
|
728
|
+
// Handle cross-package PublicKey instances - extract the string and recreate
|
|
729
729
|
try {
|
|
730
730
|
userSolAccountPubkey = new PublicKey(userSolAccount.toString());
|
|
731
731
|
} catch (error) {
|
|
@@ -735,10 +735,10 @@ class TradingModule {
|
|
|
735
735
|
throw new Error('userSolAccount must be a valid PublicKey or PublicKey string');
|
|
736
736
|
}
|
|
737
737
|
|
|
738
|
-
// 2.
|
|
738
|
+
// 2. Calculate PDA accounts
|
|
739
739
|
const accounts = this._calculatePDAAccounts(mint);
|
|
740
740
|
|
|
741
|
-
// 3.
|
|
741
|
+
// 3. Calculate OrderBook PDA addresses
|
|
742
742
|
const [upOrderbook] = PublicKey.findProgramAddressSync(
|
|
743
743
|
[Buffer.from('up_orderbook'), mint.toBuffer()],
|
|
744
744
|
this.sdk.programId
|
|
@@ -749,22 +749,22 @@ class TradingModule {
|
|
|
749
749
|
this.sdk.programId
|
|
750
750
|
);
|
|
751
751
|
|
|
752
|
-
// 4.
|
|
752
|
+
// 4. Get fee recipient accounts from curve account (skipBalances: only need addresses)
|
|
753
753
|
const curveAccountInfo = await this.sdk.chain.getCurveAccount(mint, { skipBalances: true });
|
|
754
754
|
const feeRecipientAccount = new PublicKey(curveAccountInfo.feeRecipient);
|
|
755
755
|
const baseFeeRecipientAccount = new PublicKey(curveAccountInfo.baseFeeRecipient);
|
|
756
756
|
|
|
757
|
-
// 5.
|
|
757
|
+
// 5. Build transaction instructions
|
|
758
758
|
const modifyComputeUnits = ComputeBudgetProgram.setComputeUnitLimit({
|
|
759
759
|
units: computeUnits
|
|
760
760
|
});
|
|
761
761
|
|
|
762
762
|
const closeShortIx = await this.sdk.program.methods
|
|
763
763
|
.closeShort(
|
|
764
|
-
buyTokenAmount, // buy_token_amount:
|
|
765
|
-
maxSolAmount, // max_sol_amount:
|
|
766
|
-
closeOrderIdBN, // close_order_id:
|
|
767
|
-
closeOrderIndices // close_order_indices:
|
|
764
|
+
buyTokenAmount, // buy_token_amount: amount of tokens to buy
|
|
765
|
+
maxSolAmount, // max_sol_amount: maximum SOL amount to spend
|
|
766
|
+
closeOrderIdBN, // close_order_id: order unique ID
|
|
767
|
+
closeOrderIndices // close_order_indices: close order indices array
|
|
768
768
|
)
|
|
769
769
|
.accounts({
|
|
770
770
|
payer: payer,
|
|
@@ -773,7 +773,7 @@ class TradingModule {
|
|
|
773
773
|
poolTokenAccount: accounts.poolTokenAccount,
|
|
774
774
|
poolBorrowTokenAccount: accounts.poolBorrowTokenAccount,
|
|
775
775
|
poolSolAccount: accounts.poolSolAccount,
|
|
776
|
-
userSolAccount: userSolAccountPubkey, //
|
|
776
|
+
userSolAccount: userSolAccountPubkey, // User SOL account (must be order opener)
|
|
777
777
|
tokenProgram: TOKEN_PROGRAM_ID,
|
|
778
778
|
systemProgram: SystemProgram.programId,
|
|
779
779
|
rent: SYSVAR_RENT_PUBKEY,
|
|
@@ -784,15 +784,15 @@ class TradingModule {
|
|
|
784
784
|
})
|
|
785
785
|
.instruction();
|
|
786
786
|
|
|
787
|
-
// 6.
|
|
787
|
+
// 6. Create transaction and add instructions
|
|
788
788
|
const transaction = new Transaction();
|
|
789
789
|
transaction.add(modifyComputeUnits);
|
|
790
790
|
transaction.add(closeShortIx);
|
|
791
791
|
|
|
792
|
-
// 7.
|
|
792
|
+
// 7. Return transaction object and related info
|
|
793
793
|
return {
|
|
794
794
|
transaction,
|
|
795
|
-
signers: [], //
|
|
795
|
+
signers: [], // Close short transaction doesn't need additional signers, only payer signature
|
|
796
796
|
accounts: {
|
|
797
797
|
mint: mint,
|
|
798
798
|
curveAccount: accounts.curveAccount,
|
|
@@ -815,31 +815,31 @@ class TradingModule {
|
|
|
815
815
|
// ========== PDA Calculation Methods ==========
|
|
816
816
|
|
|
817
817
|
/**
|
|
818
|
-
*
|
|
818
|
+
* Calculate PDA accounts
|
|
819
819
|
* @private
|
|
820
|
-
* @param {PublicKey} mintAccount -
|
|
821
|
-
* @returns {Object} PDA
|
|
820
|
+
* @param {PublicKey} mintAccount - Token mint account
|
|
821
|
+
* @returns {Object} PDA accounts object
|
|
822
822
|
*/
|
|
823
823
|
_calculatePDAAccounts(mintAccount) {
|
|
824
|
-
//
|
|
824
|
+
// Calculate the curve account PDA
|
|
825
825
|
const [curveAccount] = PublicKey.findProgramAddressSync(
|
|
826
826
|
[Buffer.from('borrowing_curve'), mintAccount.toBuffer()],
|
|
827
827
|
this.sdk.programId
|
|
828
828
|
);
|
|
829
829
|
|
|
830
|
-
//
|
|
830
|
+
// Calculate the pool token account PDA
|
|
831
831
|
const [poolTokenAccount] = PublicKey.findProgramAddressSync(
|
|
832
832
|
[Buffer.from('pool_token'), mintAccount.toBuffer()],
|
|
833
833
|
this.sdk.programId
|
|
834
834
|
);
|
|
835
835
|
|
|
836
|
-
//
|
|
836
|
+
// Calculate the borrow-pool-specific token account PDA
|
|
837
837
|
const [poolBorrowTokenAccount] = PublicKey.findProgramAddressSync(
|
|
838
838
|
[Buffer.from('pool_borrow_token'), mintAccount.toBuffer()],
|
|
839
839
|
this.sdk.programId
|
|
840
840
|
);
|
|
841
841
|
|
|
842
|
-
//
|
|
842
|
+
// Calculate the pool SOL account PDA
|
|
843
843
|
const [poolSolAccount] = PublicKey.findProgramAddressSync(
|
|
844
844
|
[Buffer.from('pool_sol'), mintAccount.toBuffer()],
|
|
845
845
|
this.sdk.programId
|
package/src/sdk.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
const anchor = require('@coral-xyz/anchor');
|
|
2
2
|
const { PublicKey } = require('@solana/web3.js');
|
|
3
|
-
//
|
|
3
|
+
// Use the buffer package consistently across all platforms
|
|
4
4
|
const { Buffer } = require('buffer');
|
|
5
5
|
|
|
6
|
-
//
|
|
6
|
+
// Environment detection and conditional loading
|
|
7
7
|
const IS_NODE = typeof process !== 'undefined' && process.versions && process.versions.node;
|
|
8
8
|
|
|
9
|
-
//
|
|
9
|
+
// Ensure global availability (compatible with existing code)
|
|
10
10
|
if (typeof global !== 'undefined' && !global.Buffer) {
|
|
11
11
|
global.Buffer = Buffer;
|
|
12
12
|
}
|
|
@@ -73,7 +73,7 @@ class Fun100xSdk {
|
|
|
73
73
|
// Maximum number of orders to fetch during queries
|
|
74
74
|
this.FIND_MAX_ORDERS_COUNT = 1000
|
|
75
75
|
|
|
76
|
-
//
|
|
76
|
+
// When liquidity is insufficient, the suggested ratio of liquidity to actually use, per-mille (1000=100%)
|
|
77
77
|
this.SUGGEST_LIQ_RATIO = 975; // 97.5% (1000=100%)
|
|
78
78
|
|
|
79
79
|
// Initialize Anchor program
|
|
@@ -92,20 +92,19 @@ class Fun100xSdk {
|
|
|
92
92
|
this.curve = CurveAMM;
|
|
93
93
|
|
|
94
94
|
/**
|
|
95
|
-
* 统一数据接口 - 根据 defaultDataSource 配置自动路由到 fast 或 chain 模块
|
|
96
95
|
* Unified data interface - automatically routes to fast or chain module based on defaultDataSource config
|
|
97
96
|
*
|
|
98
97
|
* @example
|
|
99
|
-
* //
|
|
98
|
+
* // Fetch orders using the default data source
|
|
100
99
|
* const ordersData = await sdk.data.orders(mint, { type: 'down_orders' });
|
|
101
100
|
*
|
|
102
|
-
* //
|
|
101
|
+
* // Temporarily specify a data source
|
|
103
102
|
* const ordersData = await sdk.data.orders(mint, {
|
|
104
103
|
* type: 'down_orders',
|
|
105
|
-
* dataSource: 'chain' //
|
|
104
|
+
* dataSource: 'chain' // Temporarily use the on-chain data source
|
|
106
105
|
* });
|
|
107
106
|
*
|
|
108
|
-
* //
|
|
107
|
+
* // Fetch user orders
|
|
109
108
|
* const userOrders = await sdk.data.user_orders(user, mint, {
|
|
110
109
|
* page: 1,
|
|
111
110
|
* limit: 200,
|
|
@@ -114,27 +113,27 @@ class Fun100xSdk {
|
|
|
114
113
|
*/
|
|
115
114
|
this.data = {
|
|
116
115
|
/**
|
|
117
|
-
*
|
|
118
|
-
* @param {string} mint -
|
|
119
|
-
* @param {Object} options -
|
|
120
|
-
* @returns {Promise<Object>}
|
|
116
|
+
* Get token orders data
|
|
117
|
+
* @param {string} mint - Token address
|
|
118
|
+
* @param {Object} options - Query parameters; supports the dataSource field to temporarily specify a data source
|
|
119
|
+
* @returns {Promise<Object>} Orders data
|
|
121
120
|
*/
|
|
122
121
|
orders: (mint, options = {}) => this._getDataWithSource('orders', [mint, options]),
|
|
123
122
|
|
|
124
123
|
/**
|
|
125
|
-
*
|
|
126
|
-
* @param {string} mint -
|
|
127
|
-
* @param {Object} options -
|
|
128
|
-
* @returns {Promise<string>}
|
|
124
|
+
* Get token price data
|
|
125
|
+
* @param {string} mint - Token address
|
|
126
|
+
* @param {Object} options - Query parameters; supports the dataSource field to temporarily specify a data source
|
|
127
|
+
* @returns {Promise<string>} Price string
|
|
129
128
|
*/
|
|
130
129
|
price: (mint, options = {}) => this._getDataWithSource('price', [mint, options]),
|
|
131
130
|
|
|
132
131
|
/**
|
|
133
|
-
*
|
|
134
|
-
* @param {string} user -
|
|
135
|
-
* @param {string} mint -
|
|
136
|
-
* @param {Object} options -
|
|
137
|
-
* @returns {Promise<Object>}
|
|
132
|
+
* Get user orders data
|
|
133
|
+
* @param {string} user - User address
|
|
134
|
+
* @param {string} mint - Token address
|
|
135
|
+
* @param {Object} options - Query parameters; supports the dataSource field to temporarily specify a data source
|
|
136
|
+
* @returns {Promise<Object>} User orders data
|
|
138
137
|
*/
|
|
139
138
|
user_orders: (user, mint, options = {}) => this._getDataWithSource('user_orders', [user, mint, options])
|
|
140
139
|
};
|
|
@@ -199,7 +198,7 @@ class Fun100xSdk {
|
|
|
199
198
|
|
|
200
199
|
}
|
|
201
200
|
|
|
202
|
-
//
|
|
201
|
+
// Add utility classes as static properties on the Fun100xSdk class
|
|
203
202
|
Fun100xSdk.CurveAMM = CurveAMM;
|
|
204
203
|
Fun100xSdk.OrderUtils = OrderUtils;
|
|
205
204
|
|