pinpet-sdk 0.1.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 +674 -0
- package/dist/index.d.ts +342 -0
- package/dist/pinpet-sdk.cjs.js +50648 -0
- package/dist/pinpet-sdk.cjs.js.map +1 -0
- package/dist/pinpet-sdk.esm.js +39265 -0
- package/dist/pinpet-sdk.esm.js.map +1 -0
- package/dist/pinpet-sdk.js +39277 -0
- package/dist/pinpet-sdk.js.map +1 -0
- package/package.json +67 -0
- package/src/idl/pinpet.json +3511 -0
- package/src/index.js +43 -0
- package/src/modules/chain.js +1102 -0
- package/src/modules/close.js +0 -0
- package/src/modules/fast.js +431 -0
- package/src/modules/param.js +171 -0
- package/src/modules/simulator/buy.js_del +711 -0
- package/src/modules/simulator/buy_sell_token.js +300 -0
- package/src/modules/simulator/calcLiq.js +701 -0
- package/src/modules/simulator/long_shrot_stop.js +602 -0
- package/src/modules/simulator/sell.js_del +323 -0
- package/src/modules/simulator/stop_loss_utils.js +223 -0
- package/src/modules/simulator/utils.js +44 -0
- package/src/modules/simulator.js +133 -0
- package/src/modules/token.js +140 -0
- package/src/modules/trading.js +1087 -0
- package/src/sdk.js +370 -0
- package/src/types/index.d.ts +342 -0
- package/src/utils/constants.js +49 -0
- package/src/utils/curve_amm.js +884 -0
- package/src/utils/orderUtils.js +465 -0
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
const CurveAMM = require('../utils/curve_amm');
|
|
2
|
+
const { simulateLongStopLoss, simulateSellStopLoss, simulateLongSolStopLoss, simulateSellSolStopLoss } = require('./simulator/long_shrot_stop');
|
|
3
|
+
const { simulateTokenBuy, simulateTokenSell } = require('./simulator/buy_sell_token');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Simulator Module Class
|
|
10
|
+
*/
|
|
11
|
+
class SimulatorModule {
|
|
12
|
+
constructor(sdk) {
|
|
13
|
+
this.sdk = sdk;
|
|
14
|
+
|
|
15
|
+
// Liquidity reservation ratio - how much liquidity to reserve relative to the last locked liquidity
|
|
16
|
+
this.LIQUIDITY_RESERVATION = 100; // 100%;
|
|
17
|
+
// Price adjustment percentage
|
|
18
|
+
this.PRICE_ADJUSTMENT_PERCENTAGE = 0.5; // 0.5%
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Simulate token buy transaction - calculate if target token amount can be purchased
|
|
23
|
+
* 模拟以 Token 数量为目标的买入交易 - 计算是否能买到指定数量的 Token
|
|
24
|
+
* @param {string} mint - Token address 代币地址
|
|
25
|
+
* @param {bigint|string|number} buyTokenAmount - Target token amount to buy 目标购买的 Token 数量
|
|
26
|
+
* @param {string} passOrder - Optional order address to skip (won't be liquidated) 可选的跳过订单地址
|
|
27
|
+
* @param {Object|null} lastPrice - Token price info, default null
|
|
28
|
+
* @param {Object|null} ordersData - Orders response object, default null
|
|
29
|
+
* @returns {Promise<Object>} Token buy simulation result with the following structure:
|
|
30
|
+
* - liqResult: {Object} Complete liquidity calculation result from calcLiqTokenBuy, containing:
|
|
31
|
+
* - free_lp_sol_amount_sum: {bigint} Total available free liquidity SOL amount
|
|
32
|
+
* - free_lp_token_amount_sum: {bigint} Total available free liquidity token amount
|
|
33
|
+
* - lock_lp_sol_amount_sum: {bigint} Total locked liquidity SOL amount
|
|
34
|
+
* - lock_lp_token_amount_sum: {bigint} Total locked liquidity token amount
|
|
35
|
+
* - has_infinite_lp: {boolean} Whether includes infinite liquidity beyond last order
|
|
36
|
+
* - pass_order_id: {number} Index of skipped order in array (-1 if none skipped)
|
|
37
|
+
* - force_close_num: {number} Number of orders that need force closure for target amount
|
|
38
|
+
* - ideal_lp_sol_amount: {bigint} Theoretical minimum SOL required at current price
|
|
39
|
+
* - real_lp_sol_amount: {bigint} Actual SOL required considering real liquidity distribution
|
|
40
|
+
* - completion: {string} Purchase completion percentage as decimal string (e.g., "85.2", "100.0")
|
|
41
|
+
* - slippage: {string} Price slippage percentage as decimal string (e.g., "2.5", "0.8")
|
|
42
|
+
* - suggestedTokenAmount: {string} Recommended token amount to buy based on available liquidity
|
|
43
|
+
* - suggestedSolAmount: {string} Required SOL amount for suggested token purchase
|
|
44
|
+
*/
|
|
45
|
+
async simulateTokenBuy(mint, buyTokenAmount, passOrder = null, lastPrice = null, ordersData = null) {
|
|
46
|
+
return simulateTokenBuy.call(this, mint, buyTokenAmount, passOrder, lastPrice, ordersData);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Simulate token sell transaction analysis
|
|
51
|
+
* @param {string} mint - Token address
|
|
52
|
+
* @param {bigint|string|number} sellTokenAmount - Token amount to sell (u64 format, precision 10^6)
|
|
53
|
+
* @param {string} passOrder - Optional order address to skip (won't be liquidated) 可选的跳过订单地址
|
|
54
|
+
* @param {Object|null} lastPrice - Token price info, default null
|
|
55
|
+
* @param {Object|null} ordersData - Orders response object, default null
|
|
56
|
+
* @returns {Promise<Object>} Token sell simulation result with the following structure:
|
|
57
|
+
* - liqResult: {Object} Complete liquidity calculation result from calcLiqTokenSell, containing:
|
|
58
|
+
* - free_lp_sol_amount_sum: {bigint} Total available free liquidity SOL obtainable from selling
|
|
59
|
+
* - free_lp_token_amount_sum: {bigint} Maximum tokens sellable without force closing orders
|
|
60
|
+
* - lock_lp_sol_amount_sum: {bigint} Total locked liquidity SOL amount (excluding skipped orders)
|
|
61
|
+
* - lock_lp_token_amount_sum: {bigint} Total locked liquidity token amount (excluding skipped orders)
|
|
62
|
+
* - has_infinite_lp: {boolean} Whether includes infinite liquidity to minimum price
|
|
63
|
+
* - pass_order_id: {number} Index of skipped order in array (-1 if none skipped)
|
|
64
|
+
* - force_close_num: {number} Number of orders that need force closure for target sell amount
|
|
65
|
+
* - ideal_lp_sol_amount: {bigint} Theoretical maximum SOL obtainable at current price
|
|
66
|
+
* - real_lp_sol_amount: {bigint} Actual SOL obtainable considering real liquidity distribution
|
|
67
|
+
* - completion: {string} Sell completion percentage as decimal string (e.g., "85.2", "100.0")
|
|
68
|
+
* - slippage: {string} Price slippage percentage as decimal string (e.g., "2.5", "0.8")
|
|
69
|
+
* - suggestedTokenAmount: {string} Recommended token amount to sell based on available liquidity
|
|
70
|
+
* - suggestedSolAmount: {string} Expected SOL amount from suggested token sale
|
|
71
|
+
*/
|
|
72
|
+
async simulateTokenSell(mint, sellTokenAmount, passOrder = null, lastPrice = null, ordersData = null) {
|
|
73
|
+
return simulateTokenSell.call(this, mint, sellTokenAmount, passOrder, lastPrice, ordersData);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Simulate long position stop loss calculation
|
|
78
|
+
* @param {string} mint - Token address
|
|
79
|
+
* @param {bigint|string|number} buyTokenAmount - Token amount to buy for long position (u64 format, precision 10^6)
|
|
80
|
+
* @param {bigint|string|number} stopLossPrice - User desired stop loss price (u128 format)
|
|
81
|
+
* @param {Object|null} lastPrice - Token info, default null
|
|
82
|
+
* @param {Object|null} ordersData - Orders data, default null
|
|
83
|
+
* @returns {Promise<Object>} Stop loss analysis result
|
|
84
|
+
*/
|
|
85
|
+
async simulateLongStopLoss(mint, buyTokenAmount, stopLossPrice, lastPrice = null, ordersData = null) {
|
|
86
|
+
return simulateLongStopLoss.call(this, mint, buyTokenAmount, stopLossPrice, lastPrice, ordersData);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Simulate short position stop loss calculation
|
|
91
|
+
* @param {string} mint - Token address
|
|
92
|
+
* @param {bigint|string|number} sellTokenAmount - Token amount to sell for short position (u64 format, precision 10^6)
|
|
93
|
+
* @param {bigint|string|number} stopLossPrice - User desired stop loss price (u128 format)
|
|
94
|
+
* @param {Object|null} lastPrice - Token info, default null
|
|
95
|
+
* @param {Object|null} ordersData - Orders data, default null
|
|
96
|
+
* @returns {Promise<Object>} Stop loss analysis result
|
|
97
|
+
*/
|
|
98
|
+
async simulateSellStopLoss(mint, sellTokenAmount, stopLossPrice, lastPrice = null, ordersData = null) {
|
|
99
|
+
return simulateSellStopLoss.call(this, mint, sellTokenAmount, stopLossPrice, lastPrice, ordersData);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Simulate long position stop loss calculation with SOL amount input
|
|
104
|
+
* @param {string} mint - Token address
|
|
105
|
+
* @param {bigint|string|number} buySolAmount - SOL amount to spend for long position (u64 format, lamports)
|
|
106
|
+
* @param {bigint|string|number} stopLossPrice - User desired stop loss price (u128 format)
|
|
107
|
+
* @param {Object|null} lastPrice - Token info, default null
|
|
108
|
+
* @param {Object|null} ordersData - Orders data, default null
|
|
109
|
+
* @param {number} borrowFee - Borrow fee rate, default 2000 (2000/100000 = 0.02%)
|
|
110
|
+
* @returns {Promise<Object>} Stop loss analysis result (same as simulateLongStopLoss)
|
|
111
|
+
*/
|
|
112
|
+
async simulateLongSolStopLoss(mint, buySolAmount, stopLossPrice, lastPrice = null, ordersData = null, borrowFee = 2000) {
|
|
113
|
+
return simulateLongSolStopLoss.call(this, mint, buySolAmount, stopLossPrice, lastPrice, ordersData, borrowFee);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Simulate short position stop loss calculation with SOL amount input
|
|
118
|
+
* @param {string} mint - Token address
|
|
119
|
+
* @param {bigint|string|number} sellSolAmount - SOL amount needed for short position stop loss (u64 format, lamports)
|
|
120
|
+
* @param {bigint|string|number} stopLossPrice - User desired stop loss price (u128 format)
|
|
121
|
+
* @param {Object|null} lastPrice - Token info, default null
|
|
122
|
+
* @param {Object|null} ordersData - Orders data, default null
|
|
123
|
+
* @param {number} borrowFee - Borrow fee rate, default 2000 (2000/100000 = 0.02%)
|
|
124
|
+
* @returns {Promise<Object>} Stop loss analysis result (same as simulateSellStopLoss)
|
|
125
|
+
*/
|
|
126
|
+
async simulateSellSolStopLoss(mint, sellSolAmount, stopLossPrice, lastPrice = null, ordersData = null, borrowFee = 2000) {
|
|
127
|
+
return simulateSellSolStopLoss.call(this, mint, sellSolAmount, stopLossPrice, lastPrice, ordersData, borrowFee);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
module.exports = SimulatorModule;
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
const { ComputeBudgetProgram, PublicKey, Transaction, Keypair, SystemProgram, SYSVAR_RENT_PUBKEY } = require('@solana/web3.js');
|
|
2
|
+
const { TOKEN_PROGRAM_ID } = require('@solana/spl-token');
|
|
3
|
+
const anchor = require('@coral-xyz/anchor');
|
|
4
|
+
// 统一使用 buffer 包,所有平台一致
|
|
5
|
+
const { Buffer } = require('buffer');
|
|
6
|
+
|
|
7
|
+
// Metaplex Token Metadata 程序ID
|
|
8
|
+
const METADATA_PROGRAM_ID = new PublicKey("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s");
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Token Module
|
|
12
|
+
* Handles token creation, queries, balance and other operations
|
|
13
|
+
*/
|
|
14
|
+
class TokenModule {
|
|
15
|
+
constructor(sdk) {
|
|
16
|
+
this.sdk = sdk;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Create 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
|
+
* @returns {Promise<Object>} Object containing transaction, signers and account info
|
|
28
|
+
*/
|
|
29
|
+
async create({
|
|
30
|
+
mint,
|
|
31
|
+
name,
|
|
32
|
+
symbol,
|
|
33
|
+
uri,
|
|
34
|
+
payer
|
|
35
|
+
}) {
|
|
36
|
+
console.log('Token Module - Create:', {
|
|
37
|
+
mint: mint.publicKey.toString(),
|
|
38
|
+
name,
|
|
39
|
+
symbol,
|
|
40
|
+
uri,
|
|
41
|
+
payer: payer.toString()
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// Calculate borrowing liquidity pool account address (borrowing_curve)
|
|
45
|
+
const [curveAccount] = PublicKey.findProgramAddressSync(
|
|
46
|
+
[
|
|
47
|
+
Buffer.from("borrowing_curve"),
|
|
48
|
+
mint.publicKey.toBuffer(),
|
|
49
|
+
],
|
|
50
|
+
this.sdk.programId
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
// Calculate liquidity pool token account address (pool_token)
|
|
54
|
+
const [poolTokenAccount] = PublicKey.findProgramAddressSync(
|
|
55
|
+
[
|
|
56
|
+
Buffer.from("pool_token"),
|
|
57
|
+
mint.publicKey.toBuffer(),
|
|
58
|
+
],
|
|
59
|
+
this.sdk.programId
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
// Calculate liquidity pool SOL account address (pool_sol)
|
|
63
|
+
const [poolSolAccount] = PublicKey.findProgramAddressSync(
|
|
64
|
+
[
|
|
65
|
+
Buffer.from("pool_sol"),
|
|
66
|
+
mint.publicKey.toBuffer(),
|
|
67
|
+
],
|
|
68
|
+
this.sdk.programId
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
// Calculate Metaplex metadata account address
|
|
72
|
+
const [metadataAccount] = PublicKey.findProgramAddressSync(
|
|
73
|
+
[
|
|
74
|
+
Buffer.from("metadata"),
|
|
75
|
+
METADATA_PROGRAM_ID.toBuffer(),
|
|
76
|
+
mint.publicKey.toBuffer(),
|
|
77
|
+
],
|
|
78
|
+
METADATA_PROGRAM_ID
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
console.log('Calculated account addresses:');
|
|
82
|
+
console.log(' Borrowing liquidity pool account:', curveAccount.toString());
|
|
83
|
+
console.log(' Liquidity pool token account:', poolTokenAccount.toString());
|
|
84
|
+
console.log(' Liquidity pool SOL account:', poolSolAccount.toString());
|
|
85
|
+
console.log(' Metadata account:', metadataAccount.toString());
|
|
86
|
+
console.log(' Params account:', this.sdk.paramsAccount?.toString() || 'Not set');
|
|
87
|
+
|
|
88
|
+
// Validate required configuration
|
|
89
|
+
if (!this.sdk.paramsAccount) {
|
|
90
|
+
throw new Error('SDK paramsAccount not configured, please provide params_account configuration during initialization');
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Create compute budget instruction
|
|
94
|
+
const modifyComputeUnits = ComputeBudgetProgram.setComputeUnitLimit({
|
|
95
|
+
units: 400000
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
// Create transaction instructions
|
|
99
|
+
const createIx = await this.sdk.program.methods
|
|
100
|
+
.create(name, symbol, uri)
|
|
101
|
+
.accounts({
|
|
102
|
+
payer: payer,
|
|
103
|
+
mintAccount: mint.publicKey,
|
|
104
|
+
curveAccount: curveAccount,
|
|
105
|
+
poolTokenAccount: poolTokenAccount,
|
|
106
|
+
poolSolAccount: poolSolAccount,
|
|
107
|
+
metadata: metadataAccount,
|
|
108
|
+
metadataProgram: METADATA_PROGRAM_ID,
|
|
109
|
+
params: this.sdk.paramsAccount,
|
|
110
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
111
|
+
systemProgram: SystemProgram.programId,
|
|
112
|
+
rent: SYSVAR_RENT_PUBKEY,
|
|
113
|
+
})
|
|
114
|
+
.instruction();
|
|
115
|
+
|
|
116
|
+
// Create transaction and add instructions
|
|
117
|
+
const transaction = new Transaction();
|
|
118
|
+
transaction.add(modifyComputeUnits);
|
|
119
|
+
transaction.add(createIx);
|
|
120
|
+
|
|
121
|
+
console.log('Token creation transaction built, signers required:', [payer.toString(), mint.publicKey.toString()]);
|
|
122
|
+
|
|
123
|
+
return {
|
|
124
|
+
transaction,
|
|
125
|
+
signers: [mint], // mint keypair needs to be a signer
|
|
126
|
+
accounts: {
|
|
127
|
+
mint: mint.publicKey,
|
|
128
|
+
curveAccount,
|
|
129
|
+
poolTokenAccount,
|
|
130
|
+
poolSolAccount,
|
|
131
|
+
metadataAccount,
|
|
132
|
+
payer
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
module.exports = TokenModule;
|