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
package/src/sdk.js
ADDED
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
const anchor = require('@coral-xyz/anchor');
|
|
2
|
+
const { PublicKey } = require('@solana/web3.js');
|
|
3
|
+
// 统一使用 buffer 包,所有平台一致
|
|
4
|
+
const { Buffer } = require('buffer');
|
|
5
|
+
|
|
6
|
+
// 环境检测和条件加载
|
|
7
|
+
const IS_NODE = typeof process !== 'undefined' && process.versions && process.versions.node;
|
|
8
|
+
|
|
9
|
+
// 确保全局可用(兼容现有代码)
|
|
10
|
+
if (typeof global !== 'undefined' && !global.Buffer) {
|
|
11
|
+
global.Buffer = Buffer;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
let fs, path;
|
|
15
|
+
if (IS_NODE) {
|
|
16
|
+
try {
|
|
17
|
+
fs = require('fs');
|
|
18
|
+
path = require('path');
|
|
19
|
+
} catch (e) {
|
|
20
|
+
console.warn('File system modules not available');
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
const TradingModule = require('./modules/trading');
|
|
24
|
+
const TokenModule = require('./modules/token');
|
|
25
|
+
const ParamModule = require('./modules/param');
|
|
26
|
+
const FastModule = require('./modules/fast');
|
|
27
|
+
const SimulatorModule = require('./modules/simulator');
|
|
28
|
+
const ChainModule = require('./modules/chain');
|
|
29
|
+
const OrderUtils = require('./utils/orderUtils');
|
|
30
|
+
const CurveAMM = require('./utils/curve_amm');
|
|
31
|
+
const spinpetIdl = require('./idl/pinpet.json');
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* SpinPet SDK Main Class
|
|
35
|
+
* Provides modular interfaces for interacting with SpinPet protocol
|
|
36
|
+
*/
|
|
37
|
+
class PinPetSdk {
|
|
38
|
+
/**
|
|
39
|
+
* Constructor
|
|
40
|
+
* @param {Connection} connection - Solana connection instance
|
|
41
|
+
* @param {Wallet|Keypair} wallet - Wallet instance
|
|
42
|
+
* @param {PublicKey|string} programId - Program ID
|
|
43
|
+
* @param {Object} options - Configuration options (optional)
|
|
44
|
+
*/
|
|
45
|
+
constructor(connection, programId, options = {}) {
|
|
46
|
+
//console.log("PinPetSdk options=",options)
|
|
47
|
+
// Save configuration options
|
|
48
|
+
this.options = options;
|
|
49
|
+
|
|
50
|
+
// Validate defaultDataSource configuration
|
|
51
|
+
if (options.defaultDataSource && !['fast', 'chain'].includes(options.defaultDataSource)) {
|
|
52
|
+
throw new Error('defaultDataSource must be "fast" or "chain"');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
//console.log("options.defaultDataSource",options.defaultDataSource)
|
|
56
|
+
this.defaultDataSource = options.defaultDataSource || 'fast';
|
|
57
|
+
console.log('Data source method:', this.defaultDataSource);
|
|
58
|
+
|
|
59
|
+
// Basic configuration
|
|
60
|
+
this.connection = connection;
|
|
61
|
+
//this.wallet = wallet instanceof anchor.Wallet ? wallet : new anchor.Wallet(wallet);
|
|
62
|
+
this.programId = typeof programId === 'string' ? new PublicKey(programId) : programId;
|
|
63
|
+
|
|
64
|
+
// Initialize account configuration with options
|
|
65
|
+
this.feeRecipient = this._parsePublicKey(this.options.fee_recipient);
|
|
66
|
+
this.baseFeeRecipient = this._parsePublicKey(this.options.base_fee_recipient);
|
|
67
|
+
this.paramsAccount = this._parsePublicKey(this.options.params_account);
|
|
68
|
+
this.spinFastApiUrl = this.options.spin_fast_api_url;
|
|
69
|
+
|
|
70
|
+
// Maximum number of orders that can be processed at once in the contract
|
|
71
|
+
this.MAX_ORDERS_COUNT = 9
|
|
72
|
+
// Maximum number of orders to fetch during queries
|
|
73
|
+
this.FIND_MAX_ORDERS_COUNT = 1000
|
|
74
|
+
|
|
75
|
+
// 在流动性不足时, 建议实际使用流动性的比例, 分每 (1000=100%)
|
|
76
|
+
this.SUGGEST_LIQ_RATIO = 975; // 97.5% (1000=100%)
|
|
77
|
+
|
|
78
|
+
// 只在 Node.js 环境中启用调试日志
|
|
79
|
+
this.debugLogPath = IS_NODE && fs ? (this.options.debug_log_path || this.options.debugLogPath || null) : null;
|
|
80
|
+
|
|
81
|
+
// 初始化调试文件
|
|
82
|
+
this._initDebugFiles();
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
// Initialize Anchor program
|
|
86
|
+
this.program = this._initProgram(this.options);
|
|
87
|
+
|
|
88
|
+
// Initialize functional modules
|
|
89
|
+
this.trading = new TradingModule(this);
|
|
90
|
+
this.token = new TokenModule(this);
|
|
91
|
+
this.param = new ParamModule(this);
|
|
92
|
+
this.fast = new FastModule(this);
|
|
93
|
+
this.simulator = new SimulatorModule(this);
|
|
94
|
+
this.chain = new ChainModule(this);
|
|
95
|
+
|
|
96
|
+
// Initialize curve AMM utility
|
|
97
|
+
this.curve = CurveAMM;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* 统一数据接口 - 根据 defaultDataSource 配置自动路由到 fast 或 chain 模块
|
|
101
|
+
* Unified data interface - automatically routes to fast or chain module based on defaultDataSource config
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* // 使用默认数据源获取订单
|
|
105
|
+
* const ordersData = await sdk.data.orders(mint, { type: 'down_orders' });
|
|
106
|
+
*
|
|
107
|
+
* // 临时指定数据源
|
|
108
|
+
* const ordersData = await sdk.data.orders(mint, {
|
|
109
|
+
* type: 'down_orders',
|
|
110
|
+
* dataSource: 'chain' // 临时使用链上数据源
|
|
111
|
+
* });
|
|
112
|
+
*
|
|
113
|
+
* // 获取用户订单
|
|
114
|
+
* const userOrders = await sdk.data.user_orders(user, mint, {
|
|
115
|
+
* page: 1,
|
|
116
|
+
* limit: 200,
|
|
117
|
+
* order_by: 'start_time_desc'
|
|
118
|
+
* });
|
|
119
|
+
*/
|
|
120
|
+
this.data = {
|
|
121
|
+
/**
|
|
122
|
+
* 获取代币订单数据
|
|
123
|
+
* @param {string} mint - 代币地址
|
|
124
|
+
* @param {Object} options - 查询参数,支持 dataSource 字段临时指定数据源
|
|
125
|
+
* @returns {Promise<Object>} 订单数据
|
|
126
|
+
*/
|
|
127
|
+
orders: (mint, options = {}) => this._getDataWithSource('orders', [mint, options]),
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* 获取代币价格数据
|
|
131
|
+
* @param {string} mint - 代币地址
|
|
132
|
+
* @param {Object} options - 查询参数,支持 dataSource 字段临时指定数据源
|
|
133
|
+
* @returns {Promise<string>} 价格字符串
|
|
134
|
+
*/
|
|
135
|
+
price: (mint, options = {}) => this._getDataWithSource('price', [mint, options]),
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* 获取用户订单数据
|
|
139
|
+
* @param {string} user - 用户地址
|
|
140
|
+
* @param {string} mint - 代币地址
|
|
141
|
+
* @param {Object} options - 查询参数,支持 dataSource 字段临时指定数据源
|
|
142
|
+
* @returns {Promise<Object>} 用户订单数据
|
|
143
|
+
*/
|
|
144
|
+
user_orders: (user, mint, options = {}) => this._getDataWithSource('user_orders', [user, mint, options])
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Parse PublicKey
|
|
150
|
+
* @private
|
|
151
|
+
* @param {PublicKey|string|null} key - Key to parse
|
|
152
|
+
* @returns {PublicKey|null}
|
|
153
|
+
*/
|
|
154
|
+
_parsePublicKey(key) {
|
|
155
|
+
if (!key) return null;
|
|
156
|
+
return typeof key === 'string' ? new PublicKey(key) : key;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Initialize Anchor program instance
|
|
161
|
+
* @private
|
|
162
|
+
*/
|
|
163
|
+
_initProgram(options = {}) {
|
|
164
|
+
const provider = new anchor.AnchorProvider(
|
|
165
|
+
this.connection,
|
|
166
|
+
{
|
|
167
|
+
commitment: options.commitment,
|
|
168
|
+
preflightCommitment: options.preflightCommitment,
|
|
169
|
+
skipPreflight: options.skipPreflight || false,
|
|
170
|
+
maxRetries: options.maxRetries,
|
|
171
|
+
...options
|
|
172
|
+
}
|
|
173
|
+
);
|
|
174
|
+
|
|
175
|
+
anchor.setProvider(provider);
|
|
176
|
+
|
|
177
|
+
// Create program instance using imported IDL
|
|
178
|
+
return new anchor.Program(spinpetIdl, this.programId);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// ========== Order Processing Utility Methods ==========
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Build LP Pairs Array (for trading)
|
|
185
|
+
*
|
|
186
|
+
* @param {Array} orders - Order array
|
|
187
|
+
* @param {string} direction - Direction: 'up_orders' (short orders) or 'down_orders' (long orders)
|
|
188
|
+
* @param {bigint|string|number} price - Current price (u128 format)
|
|
189
|
+
* @returns {Array} LP pairs array, format: [{ solAmount: BN, tokenAmount: BN }, ...]
|
|
190
|
+
*
|
|
191
|
+
* @example
|
|
192
|
+
* const ordersData = await sdk.fast.orders(mint, { type: 'down_orders' });
|
|
193
|
+
* const currentPrice = await sdk.fast.price(mint);
|
|
194
|
+
* const lpPairs = sdk.buildLpPairs(ordersData.data.orders, 'down_orders', currentPrice);
|
|
195
|
+
* // Returns: [
|
|
196
|
+
* // { solAmount: new anchor.BN("63947874"), tokenAmount: new anchor.BN("65982364399") },
|
|
197
|
+
* // { solAmount: new anchor.BN("1341732020"), tokenAmount: new anchor.BN("1399566720549") },
|
|
198
|
+
* // ...
|
|
199
|
+
* // ]
|
|
200
|
+
*/
|
|
201
|
+
buildLpPairs(orders, direction, price) {
|
|
202
|
+
return OrderUtils.buildLpPairs(orders, direction, price, this.MAX_ORDERS_COUNT);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Build Order Accounts Array (for trading)
|
|
207
|
+
*
|
|
208
|
+
* @param {Array} orders - Order array
|
|
209
|
+
* @returns {Array} Order account address array, format: [string, string, ..., null, null]
|
|
210
|
+
*
|
|
211
|
+
* @example
|
|
212
|
+
* const ordersData = await sdk.fast.orders(mint, { type: 'down_orders' });
|
|
213
|
+
* const orderAccounts = sdk.buildOrderAccounts(ordersData.data.orders);
|
|
214
|
+
* // Returns: [
|
|
215
|
+
* // "4fvsPDNoRRacSzE3PkEuNQeTNWMaeFqGwUxCnEbR1Dzb",
|
|
216
|
+
* // "G4nHBYX8EbrP8r35pk5TfpvJZfGNyLnd4qsfT7ru5vLd",
|
|
217
|
+
* // ...
|
|
218
|
+
* // null, null
|
|
219
|
+
* // ]
|
|
220
|
+
*/
|
|
221
|
+
buildOrderAccounts(orders) {
|
|
222
|
+
return OrderUtils.buildOrderAccounts(orders, this.MAX_ORDERS_COUNT);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Find Previous and Next Order
|
|
227
|
+
*
|
|
228
|
+
* @param {Array} orders - Order array
|
|
229
|
+
* @param {string} findOrderPda - Target order PDA address
|
|
230
|
+
* @returns {Object} Returns { prevOrder: Object|null, nextOrder: Object|null }
|
|
231
|
+
*
|
|
232
|
+
* @example
|
|
233
|
+
* const ordersData = await sdk.fast.orders(mint, { type: 'down_orders' });
|
|
234
|
+
* const result = sdk.findPrevNext(ordersData.data.orders, 'E2T72D4wZdxHRjELN5VnRdcCvS4FPcYBBT3UBEoaC5cA');
|
|
235
|
+
* // Returns:
|
|
236
|
+
* // {
|
|
237
|
+
* // prevOrder: { order_pda: "...", user: "...", ... } | null,
|
|
238
|
+
* // nextOrder: { order_pda: "...", user: "...", ... } | null
|
|
239
|
+
* // }
|
|
240
|
+
*/
|
|
241
|
+
findPrevNext(orders, findOrderPda) {
|
|
242
|
+
return OrderUtils.findPrevNext(orders, findOrderPda);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Get PDA Address Position in Orders Array
|
|
247
|
+
*
|
|
248
|
+
* 获取指定 PDA 地址在订单数组中的索引位置,主要用于 closeLong 和 closeShort 方法中
|
|
249
|
+
* Gets the index position of specified PDA address in orders array, mainly used in closeLong and closeShort methods
|
|
250
|
+
*
|
|
251
|
+
* @param {Array} orders - 订单数组 Order array
|
|
252
|
+
* @param {string|PublicKey} targetOrderPda - 目标订单PDA地址 Target order PDA address
|
|
253
|
+
* @returns {number} PDA地址在数组中的索引位置,如果没有找到返回200
|
|
254
|
+
* Index position of PDA address in array, returns 200 if not found
|
|
255
|
+
*
|
|
256
|
+
* @example
|
|
257
|
+
* // 在 closeLong 或 closeShort 中使用 Usage in closeLong or closeShort:
|
|
258
|
+
* const ordersData = await sdk.data.orders(mint.toString(), {
|
|
259
|
+
* type: 'down_orders',
|
|
260
|
+
* limit: sdk.MAX_ORDERS_COUNT + 1
|
|
261
|
+
* });
|
|
262
|
+
*
|
|
263
|
+
* const closeOrderPubkey = new PublicKey("E2T72D4wZdxHRjELN5VnRdcCvS4FPcYBBT3UBEoaC5cA");
|
|
264
|
+
* const orderIndex = sdk.findOrderIndex(ordersData.data.orders, closeOrderPubkey);
|
|
265
|
+
*
|
|
266
|
+
* if (orderIndex !== 200) {
|
|
267
|
+
* console.log(`订单在数组中的位置:${orderIndex} Order position in array: ${orderIndex}`);
|
|
268
|
+
* } else {
|
|
269
|
+
* console.log('订单未找到 Order not found');
|
|
270
|
+
* }
|
|
271
|
+
*
|
|
272
|
+
* // 也支持字符串格式的PDA地址 Also supports string format PDA address:
|
|
273
|
+
* const orderIndex2 = sdk.findOrderIndex(ordersData.data.orders, "E2T72D4wZdxHRjELN5VnRdcCvS4FPcYBBT3UBEoaC5cA");
|
|
274
|
+
*
|
|
275
|
+
* // 实际业务场景使用示例 Real business scenario usage example:
|
|
276
|
+
* async function processCloseOrder(mintAccount, closeOrderPubkey) {
|
|
277
|
+
* // 获取订单数据
|
|
278
|
+
* const ordersData = await sdk.data.orders(mintAccount.toString(), {
|
|
279
|
+
* type: 'down_orders',
|
|
280
|
+
* limit: sdk.MAX_ORDERS_COUNT + 1
|
|
281
|
+
* });
|
|
282
|
+
*
|
|
283
|
+
* // 查找订单位置用于后续处理逻辑
|
|
284
|
+
* const orderIndex = sdk.findOrderIndex(ordersData.data.orders, closeOrderPubkey);
|
|
285
|
+
*
|
|
286
|
+
* if (orderIndex !== 200) {
|
|
287
|
+
* console.log(`找到目标订单,位置: ${orderIndex}`);
|
|
288
|
+
* // 继续处理订单相关逻辑...
|
|
289
|
+
* } else {
|
|
290
|
+
* throw new Error('目标订单不存在于当前订单列表中');
|
|
291
|
+
* }
|
|
292
|
+
* }
|
|
293
|
+
*/
|
|
294
|
+
findOrderIndex(orders, targetOrderPda) {
|
|
295
|
+
return OrderUtils.findOrderIndex(orders, targetOrderPda);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// ========== Debug File Management Methods ==========
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* 初始化调试文件,删除旧文件
|
|
302
|
+
* Initialize debug files, delete old files
|
|
303
|
+
* @private
|
|
304
|
+
*/
|
|
305
|
+
_initDebugFiles() {
|
|
306
|
+
if (!this.debugLogPath || !IS_NODE || !fs || !path) {
|
|
307
|
+
return; // 浏览器环境或文件系统不可用
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
try {
|
|
311
|
+
const files = ['orderPda.txt', 'orderOpen.txt'];
|
|
312
|
+
files.forEach(file => {
|
|
313
|
+
const filePath = path.join(this.debugLogPath, file);
|
|
314
|
+
if (fs.existsSync(filePath)) {
|
|
315
|
+
fs.unlinkSync(filePath);
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
} catch (error) {
|
|
319
|
+
console.warn('Warning: Failed to initialize debug files:', error.message);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* 安全地写入调试日志
|
|
325
|
+
* Safely write debug log
|
|
326
|
+
* @private
|
|
327
|
+
* @param {string} fileName - 文件名
|
|
328
|
+
* @param {string} content - 内容
|
|
329
|
+
*/
|
|
330
|
+
_writeDebugLog(fileName, content) {
|
|
331
|
+
if (!this.debugLogPath || !IS_NODE || !fs || !path) {
|
|
332
|
+
return; // 静默失败,不报错
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
try {
|
|
336
|
+
const filePath = path.join(this.debugLogPath, fileName);
|
|
337
|
+
fs.appendFileSync(filePath, content);
|
|
338
|
+
} catch (error) {
|
|
339
|
+
console.warn(`Warning: Failed to write debug log to ${fileName}:`, error.message);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// ========== Unified Data Interface Routing Method ==========
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Route data requests based on configuration
|
|
347
|
+
*
|
|
348
|
+
* @private
|
|
349
|
+
* @param {string} method - Method name
|
|
350
|
+
* @param {Array} args - Arguments array
|
|
351
|
+
* @returns {Promise} Returns result from corresponding module method
|
|
352
|
+
*/
|
|
353
|
+
_getDataWithSource(method, args) {
|
|
354
|
+
// Extract dataSource configuration from last parameter
|
|
355
|
+
const lastArg = args[args.length - 1] || {};
|
|
356
|
+
const dataSource = lastArg.dataSource || this.defaultDataSource;
|
|
357
|
+
|
|
358
|
+
// Route to corresponding module based on data source
|
|
359
|
+
const module = dataSource === 'chain' ? this.chain : this.fast;
|
|
360
|
+
|
|
361
|
+
if (!module[method]) {
|
|
362
|
+
throw new Error(`Method ${method} does not exist in ${dataSource} module`);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
return module[method](...args);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
module.exports = PinPetSdk;
|
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
import { Connection, PublicKey, Transaction, Keypair } from '@solana/web3.js';
|
|
2
|
+
import { BN, Wallet, Program } from '@coral-xyz/anchor';
|
|
3
|
+
|
|
4
|
+
// ========================= 基础类型定义 =========================
|
|
5
|
+
|
|
6
|
+
export type DataSourceType = 'fast' | 'chain';
|
|
7
|
+
|
|
8
|
+
export interface NetworkConfig {
|
|
9
|
+
name: string;
|
|
10
|
+
defaultDataSource: DataSourceType;
|
|
11
|
+
solanaEndpoint: string;
|
|
12
|
+
spin_fast_api_url: string;
|
|
13
|
+
fee_recipient: string;
|
|
14
|
+
base_fee_recipient: string;
|
|
15
|
+
params_account: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface PinPetSdkOptions {
|
|
19
|
+
defaultDataSource?: DataSourceType;
|
|
20
|
+
solanaEndpoint?: string;
|
|
21
|
+
spin_fast_api_url?: string;
|
|
22
|
+
fee_recipient?: string;
|
|
23
|
+
base_fee_recipient?: string;
|
|
24
|
+
params_account?: string;
|
|
25
|
+
debug_log_path?: string;
|
|
26
|
+
debugLogPath?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// ========================= 订单和交易相关类型 =========================
|
|
30
|
+
|
|
31
|
+
export interface OrderData {
|
|
32
|
+
order_pda: string;
|
|
33
|
+
user: string;
|
|
34
|
+
mint: string;
|
|
35
|
+
order_type: string;
|
|
36
|
+
lock_lp_sol_amount: string;
|
|
37
|
+
lock_lp_token_amount: string;
|
|
38
|
+
lock_lp_start_price: string;
|
|
39
|
+
lock_lp_end_price: string;
|
|
40
|
+
margin_sol_amount: string;
|
|
41
|
+
borrow_amount: string;
|
|
42
|
+
position_asset_amount: string;
|
|
43
|
+
created_at?: string;
|
|
44
|
+
updated_at?: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface LpPair {
|
|
48
|
+
solAmount: BN;
|
|
49
|
+
tokenAmount: BN;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface TransactionResult {
|
|
53
|
+
transaction: Transaction;
|
|
54
|
+
signers: Keypair[];
|
|
55
|
+
accounts: Record<string, PublicKey>;
|
|
56
|
+
orderData?: {
|
|
57
|
+
ordersUsed: number;
|
|
58
|
+
lpPairsCount: number;
|
|
59
|
+
lpPairs: LpPair[];
|
|
60
|
+
orderAccounts: (string | null)[];
|
|
61
|
+
[key: string]: any;
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface OrdersResponse {
|
|
66
|
+
data: {
|
|
67
|
+
orders: OrderData[];
|
|
68
|
+
total?: number;
|
|
69
|
+
page?: number;
|
|
70
|
+
limit?: number;
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface PriceResponse {
|
|
75
|
+
price: string;
|
|
76
|
+
price_u128: string;
|
|
77
|
+
[key: string]: any;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface MintInfo {
|
|
81
|
+
mint: string;
|
|
82
|
+
name?: string;
|
|
83
|
+
symbol?: string;
|
|
84
|
+
decimals: number;
|
|
85
|
+
total_supply?: string;
|
|
86
|
+
[key: string]: any;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// ========================= 交易参数类型 =========================
|
|
90
|
+
|
|
91
|
+
export interface BuyParams {
|
|
92
|
+
mintAccount: string | PublicKey;
|
|
93
|
+
buyTokenAmount: BN;
|
|
94
|
+
maxSolAmount: BN;
|
|
95
|
+
payer: PublicKey;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export interface SellParams {
|
|
99
|
+
mintAccount: string | PublicKey;
|
|
100
|
+
sellTokenAmount: BN;
|
|
101
|
+
minSolOutput: BN;
|
|
102
|
+
payer: PublicKey;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export interface LongParams {
|
|
106
|
+
mintAccount: string | PublicKey;
|
|
107
|
+
buyTokenAmount: BN;
|
|
108
|
+
maxSolAmount: BN;
|
|
109
|
+
marginSol: BN;
|
|
110
|
+
closePrice: BN;
|
|
111
|
+
prevOrder?: PublicKey | null;
|
|
112
|
+
nextOrder?: PublicKey | null;
|
|
113
|
+
payer: PublicKey;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export interface ShortParams {
|
|
117
|
+
mintAccount: string | PublicKey;
|
|
118
|
+
borrowSellTokenAmount: BN;
|
|
119
|
+
minSolOutput: BN;
|
|
120
|
+
marginSol: BN;
|
|
121
|
+
closePrice: BN;
|
|
122
|
+
prevOrder?: PublicKey | null;
|
|
123
|
+
nextOrder?: PublicKey | null;
|
|
124
|
+
payer: PublicKey;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export interface CloseLongParams {
|
|
128
|
+
mintAccount: string | PublicKey;
|
|
129
|
+
closeOrder: string | PublicKey;
|
|
130
|
+
sellTokenAmount: BN;
|
|
131
|
+
minSolOutput: BN;
|
|
132
|
+
payer: PublicKey;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export interface CloseShortParams {
|
|
136
|
+
mintAccount: string | PublicKey;
|
|
137
|
+
closeOrder: string | PublicKey;
|
|
138
|
+
buyTokenAmount: BN;
|
|
139
|
+
maxSolAmount: BN;
|
|
140
|
+
payer: PublicKey;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export interface TransactionOptions {
|
|
144
|
+
computeUnits?: number;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// ========================= 查询参数类型 =========================
|
|
148
|
+
|
|
149
|
+
export interface OrdersQueryOptions {
|
|
150
|
+
type?: 'up_orders' | 'down_orders';
|
|
151
|
+
limit?: number;
|
|
152
|
+
page?: number;
|
|
153
|
+
dataSource?: DataSourceType;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export interface PriceQueryOptions {
|
|
157
|
+
dataSource?: DataSourceType;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export interface UserOrdersQueryOptions {
|
|
161
|
+
type?: 'up_orders' | 'down_orders';
|
|
162
|
+
limit?: number;
|
|
163
|
+
page?: number;
|
|
164
|
+
dataSource?: DataSourceType;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// ========================= 模拟器相关类型 =========================
|
|
168
|
+
|
|
169
|
+
export interface SimulationResult {
|
|
170
|
+
liqResult: {
|
|
171
|
+
free_lp_sol_amount_sum: bigint;
|
|
172
|
+
free_lp_token_amount_sum: bigint;
|
|
173
|
+
lock_lp_sol_amount_sum: bigint;
|
|
174
|
+
lock_lp_token_amount_sum: bigint;
|
|
175
|
+
has_infinite_lp: boolean;
|
|
176
|
+
pass_order_id: number;
|
|
177
|
+
force_close_num: number;
|
|
178
|
+
ideal_lp_sol_amount: bigint;
|
|
179
|
+
real_lp_sol_amount: bigint;
|
|
180
|
+
};
|
|
181
|
+
completion: string;
|
|
182
|
+
slippage: string;
|
|
183
|
+
suggestedTokenAmount: string;
|
|
184
|
+
suggestedSolAmount: string;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// ========================= 工具类相关类型 =========================
|
|
188
|
+
|
|
189
|
+
export interface FindPrevNextResult {
|
|
190
|
+
prevOrder: OrderData | null;
|
|
191
|
+
nextOrder: OrderData | null;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export interface ValidationResult {
|
|
195
|
+
valid: boolean;
|
|
196
|
+
errors: string[];
|
|
197
|
+
warnings: string[];
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// ========================= 模块接口定义 =========================
|
|
201
|
+
|
|
202
|
+
export interface TradingModule {
|
|
203
|
+
buy(params: BuyParams, options?: TransactionOptions): Promise<TransactionResult>;
|
|
204
|
+
sell(params: SellParams, options?: TransactionOptions): Promise<TransactionResult>;
|
|
205
|
+
long(params: LongParams, options?: TransactionOptions): Promise<TransactionResult>;
|
|
206
|
+
short(params: ShortParams, options?: TransactionOptions): Promise<TransactionResult>;
|
|
207
|
+
closeLong(params: CloseLongParams, options?: TransactionOptions): Promise<TransactionResult>;
|
|
208
|
+
closeShort(params: CloseShortParams, options?: TransactionOptions): Promise<TransactionResult>;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export interface FastModule {
|
|
212
|
+
mints(options?: any): Promise<any>;
|
|
213
|
+
mint_info(mint: string): Promise<MintInfo>;
|
|
214
|
+
orders(mint: string, options?: OrdersQueryOptions): Promise<OrdersResponse>;
|
|
215
|
+
price(mint: string, options?: PriceQueryOptions): Promise<PriceResponse>;
|
|
216
|
+
user_orders(user: string, mint: string, options?: UserOrdersQueryOptions): Promise<OrdersResponse>;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export interface ChainModule {
|
|
220
|
+
getCurveAccount(mint: string): Promise<any>;
|
|
221
|
+
orders(mint: string, options?: OrdersQueryOptions): Promise<OrdersResponse>;
|
|
222
|
+
price(mint: string, options?: PriceQueryOptions): Promise<PriceResponse>;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export interface TokenModule {
|
|
226
|
+
create(params: any): Promise<TransactionResult>;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export interface ParamModule {
|
|
230
|
+
createParams(params: any): Promise<TransactionResult>;
|
|
231
|
+
getParams(partner: string): Promise<any>;
|
|
232
|
+
getAdmin(): Promise<any>;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export interface SimulatorModule {
|
|
236
|
+
simulateTokenBuy(mint: string, buyTokenAmount: bigint | string | number, passOrder?: string | null): Promise<SimulationResult>;
|
|
237
|
+
simulateTokenSell(mint: string, sellTokenAmount: bigint | string | number, passOrder?: string | null): Promise<SimulationResult>;
|
|
238
|
+
simulateLongStopLoss(mint: string, buyTokenAmount: bigint | string | number, stopLossPrice: bigint | string | number, lastPrice?: any, ordersData?: any): Promise<any>;
|
|
239
|
+
simulateSellStopLoss(mint: string, sellTokenAmount: bigint | string | number, stopLossPrice: bigint | string | number, lastPrice?: any, ordersData?: any): Promise<any>;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// ========================= 数据接口类型 =========================
|
|
243
|
+
|
|
244
|
+
export interface DataInterface {
|
|
245
|
+
orders(mint: string, options?: OrdersQueryOptions): Promise<OrdersResponse>;
|
|
246
|
+
price(mint: string, options?: PriceQueryOptions): Promise<PriceResponse>;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// ========================= 主 SDK 类型定义 =========================
|
|
250
|
+
|
|
251
|
+
export declare class PinPetSdk {
|
|
252
|
+
connection: Connection;
|
|
253
|
+
programId: PublicKey;
|
|
254
|
+
program: Program;
|
|
255
|
+
options: PinPetSdkOptions;
|
|
256
|
+
defaultDataSource: DataSourceType;
|
|
257
|
+
feeRecipient: PublicKey;
|
|
258
|
+
baseFeeRecipient: PublicKey;
|
|
259
|
+
paramsAccount: PublicKey;
|
|
260
|
+
spinFastApiUrl: string;
|
|
261
|
+
debugLogPath: string | null;
|
|
262
|
+
|
|
263
|
+
// 常量
|
|
264
|
+
readonly MAX_ORDERS_COUNT: number;
|
|
265
|
+
readonly FIND_MAX_ORDERS_COUNT: number;
|
|
266
|
+
readonly SUGGEST_LIQ_RATIO: number;
|
|
267
|
+
|
|
268
|
+
// 模块
|
|
269
|
+
trading: TradingModule;
|
|
270
|
+
fast: FastModule;
|
|
271
|
+
chain: ChainModule;
|
|
272
|
+
token: TokenModule;
|
|
273
|
+
param: ParamModule;
|
|
274
|
+
simulator: SimulatorModule;
|
|
275
|
+
data: DataInterface;
|
|
276
|
+
|
|
277
|
+
constructor(
|
|
278
|
+
connection: Connection,
|
|
279
|
+
programId: string | PublicKey,
|
|
280
|
+
options?: PinPetSdkOptions
|
|
281
|
+
);
|
|
282
|
+
|
|
283
|
+
// OrderUtils 快捷方法
|
|
284
|
+
buildLpPairs(orders: OrderData[], direction: string, price: any, maxCount?: number): LpPair[];
|
|
285
|
+
buildOrderAccounts(orders: OrderData[], maxCount?: number): (string | null)[];
|
|
286
|
+
findPrevNext(orders: OrderData[], findOrderPda: string): FindPrevNextResult;
|
|
287
|
+
findOrderIndex(orders: OrderData[], targetOrderPda: string | PublicKey | null): number;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// ========================= 工具类导出 =========================
|
|
291
|
+
|
|
292
|
+
export declare class OrderUtils {
|
|
293
|
+
static buildLpPairs(orders: OrderData[], direction: string, price: any, maxCount?: number): LpPair[];
|
|
294
|
+
static buildOrderAccounts(orders: OrderData[], maxCount?: number): (string | null)[];
|
|
295
|
+
static findPrevNext(orders: OrderData[], findOrderPda: string): FindPrevNextResult;
|
|
296
|
+
static findOrderIndex(orders: OrderData[], targetOrderPda: string | PublicKey | null): number;
|
|
297
|
+
static validateOrdersFormat(orders: OrderData[], throwOnError?: boolean): boolean | ValidationResult;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
export declare class CurveAMM {
|
|
301
|
+
static readonly INITIAL_SOL_RESERVE_DECIMAL: any;
|
|
302
|
+
static readonly INITIAL_TOKEN_RESERVE_DECIMAL: any;
|
|
303
|
+
static readonly INITIAL_K_DECIMAL: any;
|
|
304
|
+
static readonly INITIAL_MIN_PRICE_DECIMAL: any;
|
|
305
|
+
static readonly PRICE_PRECISION_FACTOR_DECIMAL: any;
|
|
306
|
+
static readonly TOKEN_PRECISION_FACTOR_DECIMAL: any;
|
|
307
|
+
static readonly SOL_PRECISION_FACTOR_DECIMAL: any;
|
|
308
|
+
static readonly MAX_U128_PRICE: bigint;
|
|
309
|
+
static readonly MIN_U128_PRICE: bigint;
|
|
310
|
+
|
|
311
|
+
static u128ToDecimal(price: bigint | string | number): any;
|
|
312
|
+
static decimalToU128(price: any): bigint | null;
|
|
313
|
+
static decimalToU128Ceil(price: any): bigint | null;
|
|
314
|
+
static buyFromPriceToPrice(startLowPrice: bigint | string | number, endHighPrice: bigint | string | number): [bigint, bigint] | null;
|
|
315
|
+
static sellFromPriceToPrice(startHighPrice: bigint | string | number, endLowPrice: bigint | string | number): [bigint, bigint] | null;
|
|
316
|
+
static buyFromPriceWithSolInput(startLowPrice: bigint | string | number, solInputAmount: bigint | string | number): [bigint, bigint] | null;
|
|
317
|
+
static sellFromPriceWithTokenInput(startHighPrice: bigint | string | number, tokenInputAmount: bigint | string | number): [bigint, bigint] | null;
|
|
318
|
+
static buyFromPriceWithTokenOutput(startLowPrice: bigint | string | number, tokenOutputAmount: bigint | string | number): [bigint, bigint] | null;
|
|
319
|
+
static sellFromPriceWithSolOutput(startHighPrice: bigint | string | number, solOutputAmount: bigint | string | number): [bigint, bigint] | null;
|
|
320
|
+
static calculateAmountAfterFee(amount: bigint | string | number, fee: number): bigint | null;
|
|
321
|
+
static formatPriceForDisplay(price: bigint | string | number, decimalPlaces?: number): string;
|
|
322
|
+
static createPriceDisplayString(price: bigint | string | number, decimalPlaces?: number): string;
|
|
323
|
+
static calculatePoolPrice(lpTokenReserve: bigint | string | number | BN, lpSolReserve: bigint | string | number | BN): string | null;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// ========================= 常量和函数导出 =========================
|
|
327
|
+
|
|
328
|
+
export declare const SPINPET_PROGRAM_ID: string;
|
|
329
|
+
|
|
330
|
+
export declare function getDefaultOptions(networkName?: 'MAINNET' | 'DEVNET' | 'LOCALNET'): NetworkConfig;
|
|
331
|
+
|
|
332
|
+
// ========================= 模块类导出 =========================
|
|
333
|
+
|
|
334
|
+
export declare class TradingModule implements TradingModule {}
|
|
335
|
+
export declare class FastModule implements FastModule {}
|
|
336
|
+
export declare class ChainModule implements ChainModule {}
|
|
337
|
+
export declare class TokenModule implements TokenModule {}
|
|
338
|
+
export declare class ParamModule implements ParamModule {}
|
|
339
|
+
export declare class SimulatorModule implements SimulatorModule {}
|
|
340
|
+
|
|
341
|
+
// 默认导出
|
|
342
|
+
export default PinPetSdk;
|