pinpet-sdk 0.1.2 → 2.0.0
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 +12 -0
- package/dist/index.d copy.ts +342 -0
- package/dist/index.d.ts +9 -10
- package/dist/pinpet-sdk.cjs.js +6009 -3432
- package/dist/pinpet-sdk.cjs.js.map +1 -1
- package/dist/pinpet-sdk.esm.js +5991 -3414
- package/dist/pinpet-sdk.esm.js.map +1 -1
- package/dist/pinpet-sdk.js +5992 -3415
- package/dist/pinpet-sdk.js.map +1 -1
- package/package.json +5 -4
- package/src/idl/pinpet.json +1688 -1173
- package/src/modules/chain.js +720 -549
- package/src/modules/fast.js +569 -266
- package/src/modules/simulator/calcLiq.js +8 -1
- package/src/modules/simulator/close_indices.js +217 -0
- package/src/modules/simulator/long_shrot_stop.js +418 -49
- package/src/modules/simulator/stop_loss_utils.js +233 -78
- package/src/modules/simulator/utils.js +20 -1
- package/src/modules/simulator.js +251 -8
- package/src/modules/token.js +234 -16
- package/src/modules/tools.js +277 -0
- package/src/modules/trading.js +301 -506
- package/src/sdk.js +11 -123
- package/src/types/index.d.ts +9 -10
- package/src/utils/constants.js +13 -13
- package/src/utils/orderUtils.js +0 -448
- package/src/modules/close.js +0 -0
- package/src/modules/simulator/buy.js_del +0 -711
- package/src/modules/simulator/sell.js_del +0 -323
package/README.md
CHANGED
|
@@ -2,6 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
A JavaScript SDK for interacting with PinPet protocol-related Solana Anchor smart contracts. The SDK supports both Node.js and browser environments, providing modular functionality for trading, token management, order management, and more.
|
|
4
4
|
|
|
5
|
+
|
|
6
|
+
## PinPet Project Repositories
|
|
7
|
+
|
|
8
|
+
- **[PinPet swap](https://devnet.pinpet.fun/#/)** - devnet swap web
|
|
9
|
+
- **[Technical Documentation](https://doc.pinpet.fun/#/)** - Multi-language technical specification docs
|
|
10
|
+
- **[pinpet-sdk](https://github.com/pinpetfun/pinpet-sdk)** - SDK for trading operations
|
|
11
|
+
- **[pinpet-anchor](https://github.com/pinpetfun/pinpet-anchor)** - Solana smart contract code
|
|
12
|
+
- **[pinpet-web](https://github.com/pinpetfun/pinpet-web)** - Website frontend code
|
|
13
|
+
- **[pinpet-server](https://github.com/pinpetfun/pinpet-server)** - Transaction acceleration server code
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
5
17
|
## Installation
|
|
6
18
|
|
|
7
19
|
```bash
|
|
@@ -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;
|
package/dist/index.d.ts
CHANGED
|
@@ -9,20 +9,19 @@ export interface NetworkConfig {
|
|
|
9
9
|
name: string;
|
|
10
10
|
defaultDataSource: DataSourceType;
|
|
11
11
|
solanaEndpoint: string;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
pinPetFastApiUrl: string;
|
|
13
|
+
feeRecipient: string;
|
|
14
|
+
baseFeeRecipient: string;
|
|
15
|
+
paramsAccount: string;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
export interface PinPetSdkOptions {
|
|
19
19
|
defaultDataSource?: DataSourceType;
|
|
20
20
|
solanaEndpoint?: string;
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
debug_log_path?: string;
|
|
21
|
+
pinPetFastApiUrl?: string;
|
|
22
|
+
feeRecipient?: string;
|
|
23
|
+
baseFeeRecipient?: string;
|
|
24
|
+
paramsAccount?: string;
|
|
26
25
|
debugLogPath?: string;
|
|
27
26
|
}
|
|
28
27
|
|
|
@@ -257,7 +256,7 @@ export declare class PinPetSdk {
|
|
|
257
256
|
feeRecipient: PublicKey;
|
|
258
257
|
baseFeeRecipient: PublicKey;
|
|
259
258
|
paramsAccount: PublicKey;
|
|
260
|
-
|
|
259
|
+
pinPetFastApiUrl: string;
|
|
261
260
|
debugLogPath: string | null;
|
|
262
261
|
|
|
263
262
|
// 常量
|