@peachprojects/aggregator-sdk 0.0.0-experimental-20260324105259
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 +275 -0
- package/dist/index.cjs +4 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +681 -0
- package/dist/index.mjs +1098 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +70 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,681 @@
|
|
|
1
|
+
import { ethers } from 'ethers';
|
|
2
|
+
|
|
3
|
+
export declare interface AdapterConfig {
|
|
4
|
+
protocol: ProtocolType;
|
|
5
|
+
address: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Default values for API parameters
|
|
10
|
+
*/
|
|
11
|
+
export declare const API_DEFAULTS: {
|
|
12
|
+
/** Default route search depth */
|
|
13
|
+
readonly depth: 3;
|
|
14
|
+
/** Default trade split count */
|
|
15
|
+
readonly splitCount: 20;
|
|
16
|
+
/** Default DEX providers */
|
|
17
|
+
readonly providers: Provider[];
|
|
18
|
+
/** Default client version for V3 API */
|
|
19
|
+
readonly clientVersion: 1001500;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export declare class ApiClient {
|
|
23
|
+
private baseUrl;
|
|
24
|
+
private timeout;
|
|
25
|
+
constructor(config: ApiClientConfig);
|
|
26
|
+
/**
|
|
27
|
+
* Find optimal routes via API
|
|
28
|
+
*/
|
|
29
|
+
findRoutes(params: {
|
|
30
|
+
from: string;
|
|
31
|
+
target: string;
|
|
32
|
+
amount: bigint;
|
|
33
|
+
byAmountIn?: boolean;
|
|
34
|
+
depth?: number;
|
|
35
|
+
splitCount?: number;
|
|
36
|
+
providers?: Provider[];
|
|
37
|
+
}): Promise<ApiFindRouteData>;
|
|
38
|
+
/**
|
|
39
|
+
* Get service status including available providers
|
|
40
|
+
*/
|
|
41
|
+
getStatus(): Promise<ApiStatusData>;
|
|
42
|
+
/**
|
|
43
|
+
* Get list of available providers
|
|
44
|
+
*/
|
|
45
|
+
getAvailableProviders(): Promise<string[]>;
|
|
46
|
+
/**
|
|
47
|
+
* Update API base URL
|
|
48
|
+
*/
|
|
49
|
+
setBaseUrl(url: string): void;
|
|
50
|
+
/**
|
|
51
|
+
* Get current API base URL
|
|
52
|
+
*/
|
|
53
|
+
getBaseUrl(): string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export declare interface ApiClientConfig {
|
|
57
|
+
/** API base URL (required) */
|
|
58
|
+
baseUrl: string;
|
|
59
|
+
/** Request timeout in ms (default: 10000) */
|
|
60
|
+
timeout?: number;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Contract addresses for EVM
|
|
65
|
+
*/
|
|
66
|
+
export declare interface ApiContractAddresses {
|
|
67
|
+
/** PeachAggregator router address */
|
|
68
|
+
router: string;
|
|
69
|
+
/** Adapter addresses by provider name */
|
|
70
|
+
adapters: Record<string, string>;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* API error class
|
|
75
|
+
*/
|
|
76
|
+
export declare class ApiError extends Error {
|
|
77
|
+
readonly code: number;
|
|
78
|
+
constructor(message: string, code: number);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Find route response data
|
|
83
|
+
*/
|
|
84
|
+
export declare interface ApiFindRouteData {
|
|
85
|
+
request_id: string;
|
|
86
|
+
/** Total input amount (string to support u128) */
|
|
87
|
+
amount_in: string;
|
|
88
|
+
/** Total output amount (string to support u128) */
|
|
89
|
+
amount_out: string;
|
|
90
|
+
deviation_ratio: string;
|
|
91
|
+
paths: ApiRoutePath[];
|
|
92
|
+
/** Contract addresses for building transactions */
|
|
93
|
+
contracts: ApiContractAddresses;
|
|
94
|
+
/** Estimated gas */
|
|
95
|
+
gas: number;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* API request parameters for find_routes
|
|
100
|
+
* Note: The following parameters are ignored by SDK:
|
|
101
|
+
* - liquidity_change
|
|
102
|
+
* - apikey
|
|
103
|
+
* - gas
|
|
104
|
+
* - with_sign
|
|
105
|
+
* - cal_path_limit
|
|
106
|
+
*/
|
|
107
|
+
export declare interface ApiFindRouteRequest {
|
|
108
|
+
/** Input token address (required) */
|
|
109
|
+
from: string;
|
|
110
|
+
/** Output token address (required) */
|
|
111
|
+
target: string;
|
|
112
|
+
/** Trade amount, cannot be 0 (required) */
|
|
113
|
+
amount: string;
|
|
114
|
+
/** true: calculate output from input; false: calculate input from output (default: true) */
|
|
115
|
+
by_amount_in?: boolean;
|
|
116
|
+
/** Route search depth / max hops (default: 3) */
|
|
117
|
+
depth?: number;
|
|
118
|
+
/** Trade split count for large trades optimization (default: 20) */
|
|
119
|
+
split_count?: number;
|
|
120
|
+
/** DEX providers, comma-separated (default: "PANCAKEV2,PANCAKEV3") */
|
|
121
|
+
providers?: string;
|
|
122
|
+
/** Client version (required >= 1001500 for V3) */
|
|
123
|
+
v?: number;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Full find route response
|
|
128
|
+
*/
|
|
129
|
+
export declare type ApiFindRouteResponse = ApiResponse<ApiFindRouteData>;
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Aggregator API response wrapper
|
|
133
|
+
*/
|
|
134
|
+
export declare interface ApiResponse<T> {
|
|
135
|
+
code: number;
|
|
136
|
+
msg: string;
|
|
137
|
+
data: T;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Route path from aggregator API (EVM format)
|
|
142
|
+
*/
|
|
143
|
+
export declare interface ApiRoutePath {
|
|
144
|
+
/** Pool contract address */
|
|
145
|
+
pool: string;
|
|
146
|
+
/** Provider name (e.g., "PANCAKEV3") */
|
|
147
|
+
provider: Provider;
|
|
148
|
+
/** Adapter contract address */
|
|
149
|
+
adapter: string;
|
|
150
|
+
/** Input token address */
|
|
151
|
+
token_in: string;
|
|
152
|
+
/** Output token address */
|
|
153
|
+
token_out: string;
|
|
154
|
+
/** Swap direction (true = token0 -> token1) */
|
|
155
|
+
direction: boolean;
|
|
156
|
+
/** Fee rate (e.g., "0.0005" for 0.05%) */
|
|
157
|
+
fee_rate: string;
|
|
158
|
+
/** Input amount (string to support u128) */
|
|
159
|
+
amount_in: string;
|
|
160
|
+
/** Output amount (string to support u128) */
|
|
161
|
+
amount_out: string;
|
|
162
|
+
/** Extra data for adapter (hex encoded) */
|
|
163
|
+
extra_data?: string;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Status API response data
|
|
168
|
+
*/
|
|
169
|
+
export declare interface ApiStatusData {
|
|
170
|
+
/** Available liquidity providers */
|
|
171
|
+
providers: string[];
|
|
172
|
+
/** Chain sync status for each provider */
|
|
173
|
+
chainflows: ChainflowStatus[];
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Full status response
|
|
178
|
+
*/
|
|
179
|
+
export declare type ApiStatusResponse = ApiResponse<ApiStatusData>;
|
|
180
|
+
|
|
181
|
+
export declare const BPS_DENOMINATOR = 10000n;
|
|
182
|
+
|
|
183
|
+
export declare const BSC_MAINNET_CONFIG: PeachConfig;
|
|
184
|
+
|
|
185
|
+
export declare const BSC_TESTNET_CONFIG: PeachConfig;
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Chainflow sync status for a provider
|
|
189
|
+
*/
|
|
190
|
+
export declare interface ChainflowStatus {
|
|
191
|
+
/** Provider name (e.g., "PANCAKEV3") */
|
|
192
|
+
provider: string;
|
|
193
|
+
/** Current sync transaction cursor (tx hash) */
|
|
194
|
+
tx_cursor: string | null;
|
|
195
|
+
/** Sync version info */
|
|
196
|
+
version: {
|
|
197
|
+
/** Latest synced block number */
|
|
198
|
+
latest_block_number: number;
|
|
199
|
+
/** Latest synced transaction index in that block */
|
|
200
|
+
latest_transaction_index: number;
|
|
201
|
+
};
|
|
202
|
+
/** Last update timestamp in milliseconds */
|
|
203
|
+
update_at: number;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export declare const DEFAULT_DEADLINE_SECONDS = 1200;
|
|
207
|
+
|
|
208
|
+
export declare const DEFAULT_EXECUTE_TIMEOUT_MS = 60000;
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Peach Aggregator SDK Types
|
|
212
|
+
*
|
|
213
|
+
* Simplified design: only supports linear execution + topological sort + pool merging
|
|
214
|
+
*/
|
|
215
|
+
export declare const DEFAULT_SLIPPAGE_BPS = 50;
|
|
216
|
+
|
|
217
|
+
export declare const DEFAULT_TRANSACTION_RESPONSE_POLL_INTERVALS_MS: readonly [50, 100, 200, 400, 800, 1200];
|
|
218
|
+
|
|
219
|
+
/** API returned route paths where all paths have zero amounts */
|
|
220
|
+
export declare const ERR_ZERO_AMOUNT_PATHS = 4001;
|
|
221
|
+
|
|
222
|
+
export declare interface ExecuteOptions extends SwapOptions {
|
|
223
|
+
/** Skip the preflight eth_call simulation before sending (default: false).
|
|
224
|
+
* Set to true only if you've already called simulate() and confirmed the route is valid. */
|
|
225
|
+
skipPreflight?: boolean;
|
|
226
|
+
/** Timeout in milliseconds for the wallet to sign & broadcast the transaction.
|
|
227
|
+
* If the wallet does not settle the Promise within this duration, execute() rejects
|
|
228
|
+
* with a timeout error so the caller is not stuck on "Pending Wallet Signature" forever.
|
|
229
|
+
* Default: 60_000 (60 seconds). Set to 0 to disable. */
|
|
230
|
+
timeoutMs?: number;
|
|
231
|
+
/** Polling intervals for getTransaction(hash) after the wallet returns a tx hash.
|
|
232
|
+
* Defaults to [50, 100, 200, 400, 800, 1200]ms and then repeats the last value. */
|
|
233
|
+
transactionResponsePollingIntervalsMs?: readonly number[];
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export declare class ExecuteTimeoutError extends Error {
|
|
237
|
+
readonly txHash?: string;
|
|
238
|
+
readonly stage: ExecuteTimeoutStage;
|
|
239
|
+
constructor(message: string, stage: ExecuteTimeoutStage, txHash?: string);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export declare type ExecuteTimeoutStage = "wallet_send" | "provider_index";
|
|
243
|
+
|
|
244
|
+
/** Result of findFailingStep: which step index and step caused the revert */
|
|
245
|
+
export declare interface FindFailingStepResult {
|
|
246
|
+
stepIndex: number;
|
|
247
|
+
step: SwapStep;
|
|
248
|
+
error: unknown;
|
|
249
|
+
/** Revert reason when simulating only up to this step (may differ from full route) */
|
|
250
|
+
revertMessage?: string;
|
|
251
|
+
/** Revert reason from the full-route simulation, if fullRouteError was passed */
|
|
252
|
+
fullRouteRevertMessage?: string;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Check if an address is the native token sentinel address.
|
|
257
|
+
*/
|
|
258
|
+
export declare function isNativeTokenAddress(address: string): boolean;
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Known DEX providers supported by the SDK.
|
|
262
|
+
*/
|
|
263
|
+
export declare type KnownProvider = "PANCAKEV2" | "PANCAKEV3" | "UNISWAPV3" | "DODO" | "THENA";
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Sentinel address indicating native token (e.g. BNB on BSC).
|
|
267
|
+
* Pass this as srcToken/dstToken to distinguish native BNB from WBNB ERC20.
|
|
268
|
+
*/
|
|
269
|
+
export declare const NATIVE_TOKEN_ADDRESS = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
|
|
270
|
+
|
|
271
|
+
export declare class PeachClient {
|
|
272
|
+
private provider;
|
|
273
|
+
private config;
|
|
274
|
+
private routerContract;
|
|
275
|
+
private apiClient;
|
|
276
|
+
constructor(config: PeachConfig, provider?: ethers.Provider, options?: PeachClientOptions);
|
|
277
|
+
/**
|
|
278
|
+
* Get the effective router address for a quote.
|
|
279
|
+
*/
|
|
280
|
+
private getRouterAddress;
|
|
281
|
+
/**
|
|
282
|
+
* Apply slippage to swap params, returning a new SwapParams with adjusted amountOutMin
|
|
283
|
+
*/
|
|
284
|
+
private applySlippage;
|
|
285
|
+
/**
|
|
286
|
+
* Build transaction requests for an approval (if needed) and the swap itself.
|
|
287
|
+
*/
|
|
288
|
+
swap(quote: Quote, ownerAddress: string, options: SwapOptions): Promise<SwapRequest>;
|
|
289
|
+
/**
|
|
290
|
+
* Execute swap using the legacy signer-managed flow.
|
|
291
|
+
*
|
|
292
|
+
* @deprecated Prefer swap(), then send the returned tx request with your wallet/client.
|
|
293
|
+
*/
|
|
294
|
+
execute(quote: Quote, signer: ethers.Signer, options: ExecuteOptions): Promise<ethers.TransactionResponse>;
|
|
295
|
+
/**
|
|
296
|
+
* Encode parameters to contract format
|
|
297
|
+
*/
|
|
298
|
+
private encodeParams;
|
|
299
|
+
private getProtocolForProvider;
|
|
300
|
+
/**
|
|
301
|
+
* Encode swap calldata for the Peach Aggregator contract
|
|
302
|
+
* Useful for simulation or building custom transactions
|
|
303
|
+
*
|
|
304
|
+
* @param quote - Quote object from getQuote
|
|
305
|
+
* @param slippageBps - Slippage tolerance in basis points (e.g. 50 = 0.5%). Required.
|
|
306
|
+
* @returns Encoded calldata and transaction info (to address, value)
|
|
307
|
+
*/
|
|
308
|
+
encodeSwapCalldata(quote: Quote, slippageBps: number): {
|
|
309
|
+
to: string;
|
|
310
|
+
data: string;
|
|
311
|
+
value: bigint;
|
|
312
|
+
method: 'swap' | 'swapETH';
|
|
313
|
+
};
|
|
314
|
+
private buildSwapTransactionRequest;
|
|
315
|
+
private buildApprovalRequest;
|
|
316
|
+
private buildApprovalTransactionRequest;
|
|
317
|
+
private getAllowance;
|
|
318
|
+
private applyTxOverrides;
|
|
319
|
+
private sendTransactionWithTimeout;
|
|
320
|
+
private waitForTransactionResponse;
|
|
321
|
+
private delay;
|
|
322
|
+
private getNextPollingDelay;
|
|
323
|
+
/**
|
|
324
|
+
* Get token metadata and, optionally, the balance for a specific owner.
|
|
325
|
+
*/
|
|
326
|
+
getTokenInfo(tokenAddress: string, ownerAddress?: string): Promise<{
|
|
327
|
+
symbol: string;
|
|
328
|
+
decimals: number;
|
|
329
|
+
balance?: bigint;
|
|
330
|
+
}>;
|
|
331
|
+
/**
|
|
332
|
+
* Get user token balance
|
|
333
|
+
*/
|
|
334
|
+
getBalance(tokenAddress: string, userAddress: string): Promise<bigint>;
|
|
335
|
+
/**
|
|
336
|
+
* Get quote via API
|
|
337
|
+
* @throws Error if API client is not configured
|
|
338
|
+
*/
|
|
339
|
+
getQuote(params: {
|
|
340
|
+
srcToken: string;
|
|
341
|
+
dstToken: string;
|
|
342
|
+
amountIn: bigint;
|
|
343
|
+
options?: QuoteOptions;
|
|
344
|
+
}): Promise<Quote>;
|
|
345
|
+
/**
|
|
346
|
+
* Filter paths by allowed providers, removing paths with disallowed providers
|
|
347
|
+
* and cascade-removing orphaned paths that depend on removed paths.
|
|
348
|
+
*/
|
|
349
|
+
private filterPathsByProviders;
|
|
350
|
+
/**
|
|
351
|
+
* Build Quote from route data (e.g. from JSON file or API response).
|
|
352
|
+
* Use this to simulate a swap from a pre-computed route without calling the API.
|
|
353
|
+
*
|
|
354
|
+
* @param data - Route data with paths, amount_in, amount_out, contracts, gas
|
|
355
|
+
* @param srcToken - Source token address (first path token_in)
|
|
356
|
+
* @param dstToken - Destination token address (last path token_out)
|
|
357
|
+
* @param deadlineSeconds - Optional deadline in seconds from now (default: 20 min)
|
|
358
|
+
*/
|
|
359
|
+
buildQuoteFromRouteData(data: ApiFindRouteData, srcToken: string, dstToken: string, deadlineSeconds?: number, options?: {
|
|
360
|
+
srcNative?: boolean;
|
|
361
|
+
dstNative?: boolean;
|
|
362
|
+
}): Quote;
|
|
363
|
+
/**
|
|
364
|
+
* Build Quote from API response
|
|
365
|
+
*/
|
|
366
|
+
private buildQuoteFromApi;
|
|
367
|
+
private buildQuoteFromRouteDataInternal;
|
|
368
|
+
/**
|
|
369
|
+
* Get available providers from the API
|
|
370
|
+
* @throws Error if API client is not configured
|
|
371
|
+
*/
|
|
372
|
+
getAvailableProviders(): Promise<string[]>;
|
|
373
|
+
/**
|
|
374
|
+
* Simulate swap via eth_call (no gas, no state change)
|
|
375
|
+
* Useful for testing and verifying quote accuracy
|
|
376
|
+
*
|
|
377
|
+
* @param quote - Quote object from getQuote
|
|
378
|
+
* @param slippageBps - Slippage tolerance in basis points (e.g. 50 = 0.5%). Required.
|
|
379
|
+
* @param fromAddress - Optional caller address for simulation (default: zero address)
|
|
380
|
+
* @param stateOverrides - Optional state overrides for ERC20 balance/allowance
|
|
381
|
+
* @returns Simulated amountOut and method used
|
|
382
|
+
*/
|
|
383
|
+
simulate(quote: Quote, slippageBps: number, fromAddress?: string, stateOverrides?: Record<string, {
|
|
384
|
+
stateDiff: Record<string, string>;
|
|
385
|
+
}>): Promise<{
|
|
386
|
+
amountOut: bigint;
|
|
387
|
+
method: 'swap' | 'swapETH';
|
|
388
|
+
}>;
|
|
389
|
+
private getJsonRpcProviderForStateOverrides;
|
|
390
|
+
/**
|
|
391
|
+
* Format simulate error with human-readable details
|
|
392
|
+
*/
|
|
393
|
+
private formatSimulateError;
|
|
394
|
+
/**
|
|
395
|
+
* Find which step in the route causes the same revert as the full route (e.g. MUL_ERROR).
|
|
396
|
+
* Simulates with steps [0..1], [0..2], ... and returns the first step whose revert
|
|
397
|
+
* matches fullRouteError. Ignores steps that revert with a different reason (e.g. unknown custom error).
|
|
398
|
+
*
|
|
399
|
+
* @param quote - Full quote from getQuote (the one that fails when simulated)
|
|
400
|
+
* @param slippageBps - Same as for simulate
|
|
401
|
+
* @param fromAddress - Same as for simulate
|
|
402
|
+
* @param stateOverrides - Same as for simulate (use when simulating ERC20 sell with arbitrary address)
|
|
403
|
+
* @param fullRouteError - The error from simulating the full route. Required so we match by revert reason (e.g. "MUL_ERROR"); only the step that produces the same reason is returned.
|
|
404
|
+
* @returns The step index and step details whose revert matches fullRouteError, or null if none match or full route succeeds
|
|
405
|
+
*/
|
|
406
|
+
findFailingStep(quote: Quote, slippageBps: number, fromAddress?: string, stateOverrides?: Record<string, {
|
|
407
|
+
stateDiff: Record<string, string>;
|
|
408
|
+
}>, fullRouteError?: unknown): Promise<FindFailingStepResult | null>;
|
|
409
|
+
/** Extract a comparable revert reason (e.g. "MUL_ERROR") from an error for findFailingStep matching. */
|
|
410
|
+
private normalizeRevertReason;
|
|
411
|
+
/** Build a quote that only includes the first stepCount steps (for findFailingStep). */
|
|
412
|
+
private quoteWithFirstNSteps;
|
|
413
|
+
/**
|
|
414
|
+
* Build state overrides for ERC20 token balance and allowance.
|
|
415
|
+
* Useful for simulating swaps without actual on-chain token balance/approval.
|
|
416
|
+
*
|
|
417
|
+
* Automatically covers multiple storage slot layouts (slots 0-2 for balance,
|
|
418
|
+
* slots 0-7 for allowance) to handle OZ ERC20 (slot 0/1), Ownable+ERC20 (slot 1/2),
|
|
419
|
+
* and other common BSC token implementations.
|
|
420
|
+
*
|
|
421
|
+
* WBNB (native wrap) is skipped automatically — swapETH wraps msg.value
|
|
422
|
+
* internally so no ERC20 approval from the sender is needed.
|
|
423
|
+
*
|
|
424
|
+
* @param tokenAddress - ERC20 token address (WBNB returns empty overrides)
|
|
425
|
+
* @param owner - Address that needs the balance and allowance
|
|
426
|
+
* @param routerAddress - Router address (used as fallback spender)
|
|
427
|
+
* @param balance - Balance to inject (default: 1M tokens with 18 decimals)
|
|
428
|
+
* @param spenderAddress - Spender to approve (default: routerAddress). Pass quote.routerAddress when simulating API quotes.
|
|
429
|
+
*/
|
|
430
|
+
buildStateOverrides(tokenAddress: string, owner: string, routerAddress: string, balance?: bigint, spenderAddress?: string, options?: {
|
|
431
|
+
isNative?: boolean;
|
|
432
|
+
}): Record<string, {
|
|
433
|
+
stateDiff: Record<string, string>;
|
|
434
|
+
}>;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
export declare interface PeachClientOptions {
|
|
438
|
+
/** API client configuration. Required for getQuote(). */
|
|
439
|
+
api?: ApiClientConfig;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
export declare interface PeachConfig {
|
|
443
|
+
chainId: number;
|
|
444
|
+
rpcUrl: string;
|
|
445
|
+
/** Router address override. If not provided, uses the address from API response. */
|
|
446
|
+
routerAddress?: string;
|
|
447
|
+
weth: string;
|
|
448
|
+
adapters: AdapterConfig[];
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
export declare interface PoolInfo {
|
|
452
|
+
address: string;
|
|
453
|
+
token0: string;
|
|
454
|
+
token1: string;
|
|
455
|
+
protocol: ProtocolType;
|
|
456
|
+
reserve0?: bigint;
|
|
457
|
+
reserve1?: bigint;
|
|
458
|
+
fee?: number;
|
|
459
|
+
liquidity?: bigint;
|
|
460
|
+
sqrtPriceX96?: bigint;
|
|
461
|
+
tick?: number;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
export declare enum ProtocolType {
|
|
465
|
+
PancakeV2 = "PancakeV2",
|
|
466
|
+
PancakeV3 = "PancakeV3",
|
|
467
|
+
UniswapV3 = "UniswapV3",
|
|
468
|
+
Dodo = "Dodo",
|
|
469
|
+
Thena = "Thena"
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* Supported DEX providers.
|
|
474
|
+
* Use getAvailableProviders() to get the list dynamically.
|
|
475
|
+
*/
|
|
476
|
+
export declare type Provider = KnownProvider | (string & {});
|
|
477
|
+
|
|
478
|
+
export declare interface Quote {
|
|
479
|
+
srcToken: string;
|
|
480
|
+
dstToken: string;
|
|
481
|
+
amountIn: bigint;
|
|
482
|
+
amountOut: bigint;
|
|
483
|
+
priceImpact: number;
|
|
484
|
+
route: SplitRoute;
|
|
485
|
+
params: SwapParams;
|
|
486
|
+
gasEstimate: bigint;
|
|
487
|
+
/** Router contract address from quote API (contracts.router). When set, simulate/execute use this instead of config.routerAddress. */
|
|
488
|
+
routerAddress?: string;
|
|
489
|
+
/** True when the original srcToken was the native token sentinel address */
|
|
490
|
+
srcNative?: boolean;
|
|
491
|
+
/** True when the original dstToken was the native token sentinel address */
|
|
492
|
+
dstNative?: boolean;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/**
|
|
496
|
+
* SDK-level quote request options
|
|
497
|
+
*/
|
|
498
|
+
export declare interface QuoteOptions {
|
|
499
|
+
/** true: calculate output from input; false: calculate input from output (default: true) */
|
|
500
|
+
byAmountIn?: boolean;
|
|
501
|
+
/** Route search depth / max hops (default: 3) */
|
|
502
|
+
depth?: number;
|
|
503
|
+
/** Trade split count for large trades optimization (default: 20) */
|
|
504
|
+
splitCount?: number;
|
|
505
|
+
/** DEX providers to use (default: ["PANCAKEV2", "PANCAKEV3"]) */
|
|
506
|
+
providers?: Provider[];
|
|
507
|
+
/** Transaction deadline in seconds from now (default: 1200 = 20 min) */
|
|
508
|
+
deadlineSeconds?: number;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
export declare interface Route {
|
|
512
|
+
steps: RouteStep[];
|
|
513
|
+
amountIn: bigint;
|
|
514
|
+
amountOut: bigint;
|
|
515
|
+
gasEstimate: bigint;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
export declare class RouteDiscovery {
|
|
519
|
+
private provider;
|
|
520
|
+
private config;
|
|
521
|
+
private v2Factory;
|
|
522
|
+
private v3Factory;
|
|
523
|
+
private poolCache;
|
|
524
|
+
constructor(provider: ethers.Provider, config: PeachConfig);
|
|
525
|
+
/**
|
|
526
|
+
* Discover optimal route
|
|
527
|
+
*/
|
|
528
|
+
findBestRoute(srcToken: string, dstToken: string, amountIn: bigint): Promise<SplitRoute>;
|
|
529
|
+
/**
|
|
530
|
+
* Discover available pools (direct path)
|
|
531
|
+
*/
|
|
532
|
+
private discoverPools;
|
|
533
|
+
/**
|
|
534
|
+
* Find V2 pool
|
|
535
|
+
*/
|
|
536
|
+
private findV2Pool;
|
|
537
|
+
/**
|
|
538
|
+
* Find V3 pool
|
|
539
|
+
*/
|
|
540
|
+
private findV3Pool;
|
|
541
|
+
/**
|
|
542
|
+
* Calculate route quotes
|
|
543
|
+
*/
|
|
544
|
+
private calculateRoutes;
|
|
545
|
+
/**
|
|
546
|
+
* Get output for a single pool
|
|
547
|
+
*/
|
|
548
|
+
private getAmountOut;
|
|
549
|
+
/**
|
|
550
|
+
* V2 output calculation
|
|
551
|
+
*/
|
|
552
|
+
private getV2AmountOut;
|
|
553
|
+
/**
|
|
554
|
+
* V3 output estimation (simplified)
|
|
555
|
+
*/
|
|
556
|
+
private estimateV3AmountOut;
|
|
557
|
+
/**
|
|
558
|
+
* Clear cache
|
|
559
|
+
*/
|
|
560
|
+
clearCache(): void;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
export declare interface RouteStep {
|
|
564
|
+
pool: PoolInfo;
|
|
565
|
+
tokenIn: string;
|
|
566
|
+
tokenOut: string;
|
|
567
|
+
amountIn: bigint;
|
|
568
|
+
amountOut: bigint;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
export declare interface SplitRoute {
|
|
572
|
+
routes: Route[];
|
|
573
|
+
percentages: number[];
|
|
574
|
+
totalAmountIn: bigint;
|
|
575
|
+
totalAmountOut: bigint;
|
|
576
|
+
totalGasEstimate: bigint;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
export declare interface SwapApprovalRequest {
|
|
580
|
+
token: string;
|
|
581
|
+
owner: string;
|
|
582
|
+
spender: string;
|
|
583
|
+
currentAllowance: bigint;
|
|
584
|
+
requiredAmount: bigint;
|
|
585
|
+
approveAmount: bigint;
|
|
586
|
+
tx: SwapTxRequest;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
export declare class SwapBuilder {
|
|
590
|
+
private adapters;
|
|
591
|
+
constructor(adapters: Map<ProtocolType, string>);
|
|
592
|
+
/**
|
|
593
|
+
* Build SwapParams from split route
|
|
594
|
+
*/
|
|
595
|
+
build(splitRoute: SplitRoute, srcToken: string, dstToken: string, amountOutMin: bigint, deadlineSeconds?: number): SwapParams;
|
|
596
|
+
/**
|
|
597
|
+
* Flatten split route
|
|
598
|
+
*/
|
|
599
|
+
private flattenRoutes;
|
|
600
|
+
/**
|
|
601
|
+
* Merge identical pools
|
|
602
|
+
* Key: pool + tokenIn + tokenOut
|
|
603
|
+
*/
|
|
604
|
+
private mergeIdenticalPools;
|
|
605
|
+
/**
|
|
606
|
+
* Topological sort (Kahn's Algorithm)
|
|
607
|
+
*/
|
|
608
|
+
private topologicalSort;
|
|
609
|
+
/**
|
|
610
|
+
* Convert to contract SwapStep format
|
|
611
|
+
*/
|
|
612
|
+
private convertToSwapSteps;
|
|
613
|
+
/**
|
|
614
|
+
* Encode protocol-specific parameters
|
|
615
|
+
*/
|
|
616
|
+
private encodeExtraData;
|
|
617
|
+
/**
|
|
618
|
+
* Extract intermediate tokens
|
|
619
|
+
*/
|
|
620
|
+
private extractIntermediates;
|
|
621
|
+
/**
|
|
622
|
+
* Generate unique step key
|
|
623
|
+
*/
|
|
624
|
+
private stepKey;
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
export declare interface SwapOptions {
|
|
628
|
+
/** Slippage tolerance in basis points (e.g. 50 = 0.5%). Required. */
|
|
629
|
+
slippageBps: number;
|
|
630
|
+
/** Gas price in wei. Applied to the swap tx and, if present, the approval tx. */
|
|
631
|
+
gasPrice?: bigint;
|
|
632
|
+
/** Gas limit for the swap tx. */
|
|
633
|
+
gasLimit?: bigint;
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
export declare interface SwapParams {
|
|
637
|
+
srcToken: string;
|
|
638
|
+
dstToken: string;
|
|
639
|
+
amountIn: bigint;
|
|
640
|
+
amountOutMin: bigint;
|
|
641
|
+
steps: SwapStep[];
|
|
642
|
+
intermediateTokens: string[];
|
|
643
|
+
deadline: bigint;
|
|
644
|
+
quoteId: string;
|
|
645
|
+
expectAmountOut: bigint;
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
export declare interface SwapRequest {
|
|
649
|
+
routerAddress: string;
|
|
650
|
+
method: "swap" | "swapETH";
|
|
651
|
+
tx: SwapTxRequest;
|
|
652
|
+
approval?: SwapApprovalRequest;
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
export declare interface SwapResult {
|
|
656
|
+
txHash: string;
|
|
657
|
+
amountIn: bigint;
|
|
658
|
+
amountOut: bigint;
|
|
659
|
+
gasUsed: bigint;
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
export declare interface SwapStep {
|
|
663
|
+
adapter: string;
|
|
664
|
+
pool: string;
|
|
665
|
+
tokenIn: string;
|
|
666
|
+
tokenOut: string;
|
|
667
|
+
amountIn: bigint;
|
|
668
|
+
extraData: string;
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
export declare interface SwapTxRequest {
|
|
672
|
+
to: string;
|
|
673
|
+
data: string;
|
|
674
|
+
value: bigint;
|
|
675
|
+
gasPrice?: bigint;
|
|
676
|
+
gasLimit?: bigint;
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
export declare function withWalletSendTimeout<T>(promise: Promise<T>, timeoutMs?: number): Promise<T>;
|
|
680
|
+
|
|
681
|
+
export { }
|