@pafi-dev/trading 0.4.3 → 0.5.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 +111 -50
- package/dist/index.cjs +179 -261
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +105 -128
- package/dist/index.d.ts +105 -128
- package/dist/index.js +176 -251
- package/dist/index.js.map +1 -1
- package/package.json +13 -2
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Address, PublicClient, Hex, WalletClient, TransactionReceipt } from 'viem';
|
|
2
|
-
import { PartialUserOperation, PoolKey,
|
|
2
|
+
import { PartialUserOperation, PoolKey, V3Path, BestQuote, ExactOutputBestQuote, QuoteResult, ExactOutputQuoteResult, BROKER_HASHES } from '@pafi-dev/core';
|
|
3
3
|
export { PAFI_SUBGRAPH_URL, fetchPafiPools } from '@pafi-dev/core';
|
|
4
4
|
|
|
5
5
|
interface ApiQuoteRequest {
|
|
@@ -23,7 +23,7 @@ interface ApiQuoteResponse {
|
|
|
23
23
|
/** Echoes `request.amount` for FE bookkeeping. */
|
|
24
24
|
inputAmount: bigint;
|
|
25
25
|
/**
|
|
26
|
-
* Gross output from the
|
|
26
|
+
* Gross output from the V3 QuoterV2 — before operator fee deduction.
|
|
27
27
|
* Use for "you swap X for ~Y" UI; do not promise this number to user.
|
|
28
28
|
* 0n when `quoteError` set.
|
|
29
29
|
*/
|
|
@@ -42,7 +42,7 @@ interface ApiQuoteResponse {
|
|
|
42
42
|
* raw units. 0n on the fallback path or when handler couldn't quote.
|
|
43
43
|
*/
|
|
44
44
|
feeAmountOutput: bigint;
|
|
45
|
-
/**
|
|
45
|
+
/** V3 QuoterV2's gas estimate for the route. */
|
|
46
46
|
gasEstimate: bigint;
|
|
47
47
|
quoteError?: ApiQuoteError;
|
|
48
48
|
}
|
|
@@ -117,7 +117,7 @@ interface ApiQuoteExactOutResponse {
|
|
|
117
117
|
/** Echoes `request.amount` — the exact output amount. */
|
|
118
118
|
outputAmount: bigint;
|
|
119
119
|
/**
|
|
120
|
-
* Gross input required from the
|
|
120
|
+
* Gross input required from the V3 QuoterV2 — before operator fee. Use
|
|
121
121
|
* for "you'll pay ~X PT for exactly Y USDT" UI; do not promise this
|
|
122
122
|
* number without slippage bounding. 0n when `quoteError` set.
|
|
123
123
|
*/
|
|
@@ -132,7 +132,7 @@ interface ApiQuoteExactOutResponse {
|
|
|
132
132
|
* fallback path or when handler couldn't quote.
|
|
133
133
|
*/
|
|
134
134
|
feeAmountInput: bigint;
|
|
135
|
-
/**
|
|
135
|
+
/** V3 QuoterV2's gas estimate for the route. */
|
|
136
136
|
gasEstimate: bigint;
|
|
137
137
|
quoteError?: ApiQuoteError;
|
|
138
138
|
}
|
|
@@ -176,7 +176,7 @@ interface ApiSwapExactOutResponse {
|
|
|
176
176
|
userOpFallback?: PartialUserOperation;
|
|
177
177
|
/** Echoes `request.amount` — the exact output. */
|
|
178
178
|
outputAmount: bigint;
|
|
179
|
-
/** Raw input amount from the
|
|
179
|
+
/** Raw input amount from the V3 QuoterV2, before slippage. For display. */
|
|
180
180
|
estimatedInputAmount: bigint;
|
|
181
181
|
/** Maximum input accepted — encoded in the UserOp calldata. */
|
|
182
182
|
maxAmountIn: bigint;
|
|
@@ -322,7 +322,7 @@ declare class TradingHandlers {
|
|
|
322
322
|
private readonly chainId;
|
|
323
323
|
constructor(config: TradingHandlersConfig);
|
|
324
324
|
/**
|
|
325
|
-
* Quote exact-input PT → USDT via Uniswap
|
|
325
|
+
* Quote exact-input PT → USDT via Uniswap V3 QuoterV2.
|
|
326
326
|
*
|
|
327
327
|
* Uses multicall to batch all candidate routes into a single RPC call.
|
|
328
328
|
* Returns `quoteError: "QUOTE_UNAVAILABLE"` when no pool/path exists
|
|
@@ -349,7 +349,7 @@ declare class TradingHandlers {
|
|
|
349
349
|
handleSwap(authenticatedAddress: Address, request: ApiSwapRequest): Promise<ApiSwapResponse>;
|
|
350
350
|
/**
|
|
351
351
|
* Quote the input required to receive `request.amount` of the output
|
|
352
|
-
* token via Uniswap
|
|
352
|
+
* token via Uniswap V3 QuoterV2. Input-side operator fee is auto-quoted, so
|
|
353
353
|
* `inputGross = estimatedInputAmount + feeAmountInput` is the total
|
|
354
354
|
* the user must hold.
|
|
355
355
|
*
|
|
@@ -358,7 +358,7 @@ declare class TradingHandlers {
|
|
|
358
358
|
*/
|
|
359
359
|
handleQuoteExactOut(request: ApiQuoteExactOutRequest): Promise<ApiQuoteExactOutResponse>;
|
|
360
360
|
/**
|
|
361
|
-
* Build a
|
|
361
|
+
* Build a V3 exact-output swap UserOp.
|
|
362
362
|
*
|
|
363
363
|
* Quotes the best exact-output route, applies slippage as a CEILING on
|
|
364
364
|
* `maxAmountIn` (so the cap is never silently tightened by floor
|
|
@@ -367,7 +367,7 @@ declare class TradingHandlers {
|
|
|
367
367
|
* inputToken.transfer(feeRecipient, gasFeeAmountInput) [if > 0]
|
|
368
368
|
* → input.approve(Permit2, maxAmountIn)
|
|
369
369
|
* → Permit2.approve(router, maxAmountIn)
|
|
370
|
-
* → UniversalRouter.execute (
|
|
370
|
+
* → UniversalRouter.execute (V3_SWAP_EXACT_OUT)
|
|
371
371
|
*
|
|
372
372
|
* Operator fee is INPUT-token-side (charged before swap) so the user
|
|
373
373
|
* receives exactly `request.amount` of output.
|
|
@@ -408,60 +408,40 @@ declare function buildErc20ApprovalCalldata(spender: Address, amount: bigint): H
|
|
|
408
408
|
*/
|
|
409
409
|
declare function buildPermit2ApprovalCalldata(token: Address, spender: Address, amount: bigint, expiration: number): Hex;
|
|
410
410
|
|
|
411
|
-
|
|
412
|
-
declare const
|
|
411
|
+
/** UniversalRouter command byte for V3 exact-input swap. */
|
|
412
|
+
declare const V3_SWAP_EXACT_IN: 0;
|
|
413
|
+
/** UniversalRouter command byte for V3 exact-output swap. */
|
|
414
|
+
declare const V3_SWAP_EXACT_OUT: 1;
|
|
413
415
|
/**
|
|
414
|
-
* Build the calldata inputs[0]
|
|
415
|
-
*
|
|
416
|
-
*
|
|
417
|
-
* Actions encoded: SWAP_EXACT_IN → SETTLE_ALL → TAKE_ALL
|
|
418
|
-
*
|
|
419
|
-
* Encoding matches the Uniswap V4 SDK's V4Planner — each action's params
|
|
420
|
-
* are individually ABI-encoded, then wrapped together with the action bytes.
|
|
416
|
+
* Build the calldata `inputs[0]` payload for the `V3_SWAP_EXACT_IN`
|
|
417
|
+
* command. Path bytes are packed input→output; the router walks the
|
|
418
|
+
* same direction.
|
|
421
419
|
*/
|
|
422
|
-
declare function
|
|
420
|
+
declare function buildV3SwapInputExactIn(recipient: Address, path: V3Path, amountIn: bigint, minAmountOut: bigint): Hex;
|
|
423
421
|
/**
|
|
424
|
-
* Build the
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
*
|
|
432
|
-
* UniversalRouter.execute, using the V4 exact-output flow.
|
|
433
|
-
*
|
|
434
|
-
* Actions encoded: SWAP_EXACT_OUT → SETTLE_ALL → TAKE_ALL
|
|
435
|
-
*
|
|
436
|
-
* Settle/take semantics flip vs. the exact-input builder:
|
|
437
|
-
* - SETTLE_ALL caps the input pulled at `maxAmountIn` (router pulls
|
|
438
|
-
* only the actual amount the swap consumed; reverts if it exceeds
|
|
439
|
-
* the cap).
|
|
440
|
-
* - TAKE_ALL requires the user to receive *exactly* `amountOut` of
|
|
441
|
-
* `outputCurrency`.
|
|
442
|
-
*
|
|
443
|
-
* The `path` argument here is in V4 exact-output orientation — i.e.
|
|
444
|
-
* traversed output→input — which is what `findBestQuoteExactOut`
|
|
445
|
-
* (and `buildAllPaths(pools, tokenOut, tokenIn, ...)`) returns.
|
|
446
|
-
*
|
|
447
|
-
* @throws if `amountOut` or `maxAmountIn` exceeds 2^128 - 1.
|
|
422
|
+
* Build the calldata `inputs[0]` payload for the `V3_SWAP_EXACT_OUT`
|
|
423
|
+
* command. Path bytes are packed output→input — the router walks the
|
|
424
|
+
* desired output back to the input, taking only as much input as needed
|
|
425
|
+
* (capped by `maxAmountIn`).
|
|
426
|
+
*
|
|
427
|
+
* The `path` argument is in canonical input→output orientation; the
|
|
428
|
+
* encoder reverses internally so callers don't have to reason about
|
|
429
|
+
* direction.
|
|
448
430
|
*/
|
|
449
|
-
declare function
|
|
431
|
+
declare function buildV3SwapInputExactOut(recipient: Address, path: V3Path, amountOut: bigint, maxAmountIn: bigint): Hex;
|
|
450
432
|
/**
|
|
451
|
-
* Build the full commands + inputs args for UniversalRouter.execute
|
|
452
|
-
*
|
|
433
|
+
* Build the full `commands` + `inputs` args for `UniversalRouter.execute`
|
|
434
|
+
* for an exact-input V3 swap.
|
|
453
435
|
*/
|
|
454
|
-
declare function
|
|
436
|
+
declare function buildUniversalRouterExecuteArgs(recipient: Address, path: V3Path, amountIn: bigint, minAmountOut: bigint): {
|
|
455
437
|
commands: Hex;
|
|
456
438
|
inputs: Hex[];
|
|
457
439
|
};
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
minAmountOut: bigint;
|
|
464
|
-
}): {
|
|
440
|
+
/**
|
|
441
|
+
* Build the full `commands` + `inputs` args for `UniversalRouter.execute`
|
|
442
|
+
* for an exact-output V3 swap.
|
|
443
|
+
*/
|
|
444
|
+
declare function buildUniversalRouterExecuteArgsExactOut(recipient: Address, path: V3Path, amountOut: bigint, maxAmountIn: bigint): {
|
|
465
445
|
commands: Hex;
|
|
466
446
|
inputs: Hex[];
|
|
467
447
|
};
|
|
@@ -487,8 +467,8 @@ interface SwapSimulationResult {
|
|
|
487
467
|
declare function simulateSwap(client: PublicClient, routerAddress: Address, commands: Hex, inputs: Hex[], deadline: bigint, from: Address): Promise<SwapSimulationResult>;
|
|
488
468
|
|
|
489
469
|
/**
|
|
490
|
-
* v0.
|
|
491
|
-
* for **any** ERC-20 → ERC-20 pair routable through PAFI's
|
|
470
|
+
* v0.5 — Generalized swap UserOp builder. Direction-agnostic: works
|
|
471
|
+
* for **any** ERC-20 → ERC-20 pair routable through PAFI's V3 pools:
|
|
492
472
|
*
|
|
493
473
|
* - PT → USDT (cashout)
|
|
494
474
|
* - USDT → PT (buy PT with USDT)
|
|
@@ -499,7 +479,7 @@ declare function simulateSwap(client: PublicClient, routerAddress: Address, comm
|
|
|
499
479
|
* 1. `inputToken.approve(Permit2, amountIn)` — exactly the swap amount.
|
|
500
480
|
* 2. `Permit2.approve(inputToken, router, amountIn, deadline)` — Permit2
|
|
501
481
|
* authorization to UniversalRouter.
|
|
502
|
-
* 3. `UniversalRouter.execute(commands, inputs, deadline)` —
|
|
482
|
+
* 3. `UniversalRouter.execute(commands, inputs, deadline)` — V3 swap
|
|
503
483
|
* `inputToken → outputToken`; user receives ≥ `minAmountOut`.
|
|
504
484
|
* 4. `outputToken.transfer(feeRecipient, gasFeeAmountOutput)` —
|
|
505
485
|
* operator gas reimbursement, paid in OUTPUT token (omitted when 0).
|
|
@@ -518,16 +498,14 @@ declare function simulateSwap(client: PublicClient, routerAddress: Address, comm
|
|
|
518
498
|
* Caller MUST ensure `minAmountOut >= gasFeeAmountOutput` (fee transfer
|
|
519
499
|
* reverts otherwise → whole batch reverts).
|
|
520
500
|
*
|
|
521
|
-
* ##
|
|
522
|
-
*
|
|
523
|
-
* The V4 PAFIHook charges 10% on PT → USDT direction (one-way). The
|
|
524
|
-
* 10% is taken at pool level inside `UniversalRouter.execute`, so this
|
|
525
|
-
* builder doesn't model it explicitly — it shows up as a smaller
|
|
526
|
-
* `amountOut` from `findBestQuote`.
|
|
501
|
+
* ## Pool-level fees
|
|
527
502
|
*
|
|
528
|
-
*
|
|
529
|
-
*
|
|
530
|
-
*
|
|
503
|
+
* PAFI's PT pools are standard Uniswap V3 — no swap-time hooks. The
|
|
504
|
+
* protocol's gas-recoupment fee lives in `MintFeeWrapper` on the mint
|
|
505
|
+
* path; the only pool-level economics here are Uniswap's regular LP
|
|
506
|
+
* fee tier (per the PoolKey's `fee` field), already baked into the
|
|
507
|
+
* quote returned by `findBestQuote`. This builder doesn't apply or
|
|
508
|
+
* model any additional swap-time fee on top.
|
|
531
509
|
*/
|
|
532
510
|
interface BuildSwapUserOpParams {
|
|
533
511
|
/** User's EOA (with EIP-7702 delegation to BatchExecutor). */
|
|
@@ -549,10 +527,11 @@ interface BuildSwapUserOpParams {
|
|
|
549
527
|
*/
|
|
550
528
|
minAmountOut: bigint;
|
|
551
529
|
/**
|
|
552
|
-
*
|
|
553
|
-
*
|
|
530
|
+
* V3 swap path. `tokens.length === fees.length + 1`. Get this from
|
|
531
|
+
* `findBestQuote().bestRoute.path`. Single-hop has 2 tokens + 1 fee;
|
|
532
|
+
* multi-hop has N+1 tokens + N fees.
|
|
554
533
|
*/
|
|
555
|
-
swapPath:
|
|
534
|
+
swapPath: V3Path;
|
|
556
535
|
/** Unix seconds. After this, the router rejects the swap. */
|
|
557
536
|
deadline: bigint;
|
|
558
537
|
/**
|
|
@@ -611,10 +590,11 @@ interface BuildSwapUserOpExactOutParams {
|
|
|
611
590
|
*/
|
|
612
591
|
maxAmountIn: bigint;
|
|
613
592
|
/**
|
|
614
|
-
*
|
|
615
|
-
*
|
|
593
|
+
* V3 swap path in canonical **input→output** orientation. The encoder
|
|
594
|
+
* reverses internally for the on-chain exact-output Router call.
|
|
595
|
+
* Get this from `findBestQuoteExactOut().bestRoute.path`.
|
|
616
596
|
*/
|
|
617
|
-
swapPath:
|
|
597
|
+
swapPath: V3Path;
|
|
618
598
|
/** Unix seconds. After this, the router rejects the swap. */
|
|
619
599
|
deadline: bigint;
|
|
620
600
|
/**
|
|
@@ -640,7 +620,7 @@ interface BuildSwapUserOpExactOutParams {
|
|
|
640
620
|
};
|
|
641
621
|
}
|
|
642
622
|
/**
|
|
643
|
-
* Build an unsigned UserOp for the
|
|
623
|
+
* Build an unsigned UserOp for the V3 exact-output swap flow.
|
|
644
624
|
*
|
|
645
625
|
* UserOp shape (atomic batch via EIP-7702 BatchExecutor):
|
|
646
626
|
*
|
|
@@ -649,12 +629,12 @@ interface BuildSwapUserOpExactOutParams {
|
|
|
649
629
|
* 2. `inputToken.approve(Permit2, maxAmountIn)` — cap, not exact.
|
|
650
630
|
* 3. `Permit2.approve(inputToken, router, maxAmountIn, deadline)` —
|
|
651
631
|
* Permit2 authorization to UniversalRouter for up to `maxAmountIn`.
|
|
652
|
-
* 4. `UniversalRouter.execute(commands, inputs, deadline)` —
|
|
632
|
+
* 4. `UniversalRouter.execute(commands, inputs, deadline)` — V3
|
|
653
633
|
* exact-output swap; user receives exactly `amountOut`.
|
|
654
634
|
*
|
|
655
635
|
* @throws when `amountOut`/`maxAmountIn` are non-positive,
|
|
656
|
-
* `gasFeeAmountInput` is negative, `swapPath` is
|
|
657
|
-
* is out of Permit2's uint48 range.
|
|
636
|
+
* `gasFeeAmountInput` is negative, `swapPath` is malformed, or
|
|
637
|
+
* `deadline` is out of Permit2's uint48 range.
|
|
658
638
|
*/
|
|
659
639
|
declare function buildSwapUserOpExactOut(params: BuildSwapUserOpExactOutParams): PartialUserOperation;
|
|
660
640
|
|
|
@@ -664,8 +644,9 @@ declare function buildSwapUserOpExactOut(params: BuildSwapUserOpExactOutParams):
|
|
|
664
644
|
*/
|
|
665
645
|
declare function combineRoutes(chainId: number, pointTokenAddress: Address): PoolKey[];
|
|
666
646
|
/**
|
|
667
|
-
* Build all possible swap paths from `tokenIn` to `tokenOut` using
|
|
668
|
-
* pools. Returns an array of
|
|
647
|
+
* Build all possible V3 swap paths from `tokenIn` to `tokenOut` using
|
|
648
|
+
* the given pools. Returns an array of `V3Path` routes (each up to
|
|
649
|
+
* `maxHops` hops, in canonical input→output orientation).
|
|
669
650
|
*
|
|
670
651
|
* Supports both direct single-hop routes and multi-hop routes through
|
|
671
652
|
* intermediate tokens. Each pool is used at most once per path.
|
|
@@ -675,96 +656,89 @@ declare function combineRoutes(chainId: number, pointTokenAddress: Address): Poo
|
|
|
675
656
|
* @param tokenOut - Desired output token address
|
|
676
657
|
* @param maxHops - Maximum number of hops (default 3)
|
|
677
658
|
*/
|
|
678
|
-
declare function buildAllPaths(pools: PoolKey[], tokenIn: Address, tokenOut: Address, maxHops?: number):
|
|
659
|
+
declare function buildAllPaths(pools: PoolKey[], tokenIn: Address, tokenOut: Address, maxHops?: number): V3Path[];
|
|
679
660
|
|
|
680
661
|
/**
|
|
681
|
-
* Quote exact-input for a multi-hop path.
|
|
662
|
+
* Quote exact-input for a multi-hop V3 path.
|
|
663
|
+
*
|
|
664
|
+
* `path` is in canonical input→output orientation; the encoder produces
|
|
665
|
+
* `tokens[0] ‖ fees[0] ‖ tokens[1] ‖ ... ‖ tokens[N]` packed bytes.
|
|
682
666
|
*/
|
|
683
|
-
declare function quoteExactInput(client: PublicClient, quoterAddress: Address,
|
|
667
|
+
declare function quoteExactInput(client: PublicClient, quoterAddress: Address, path: V3Path, exactAmount: bigint): Promise<QuoteResult>;
|
|
684
668
|
/**
|
|
685
|
-
* Quote exact-input for a single-hop swap, given an explicit
|
|
669
|
+
* Quote exact-input for a single-hop V3 swap, given an explicit pool
|
|
670
|
+
* and direction. `sqrtPriceLimitX96 = 0n` disables the price-limit
|
|
671
|
+
* guard (the on-chain "no limit" sentinel).
|
|
686
672
|
*/
|
|
687
|
-
declare function quoteExactInputSingle(client: PublicClient, quoterAddress: Address,
|
|
673
|
+
declare function quoteExactInputSingle(client: PublicClient, quoterAddress: Address, tokenIn: Address, tokenOut: Address, fee: number, exactAmount: bigint, sqrtPriceLimitX96?: bigint): Promise<{
|
|
688
674
|
amountOut: bigint;
|
|
689
675
|
gasEstimate: bigint;
|
|
690
676
|
}>;
|
|
691
677
|
/**
|
|
692
|
-
* Try multiple
|
|
693
|
-
* Routes that fail (e.g. pool does not exist
|
|
678
|
+
* Try multiple V3Path routes and return the best quote plus all results.
|
|
679
|
+
* Routes that fail (e.g. pool does not exist, insufficient liquidity)
|
|
680
|
+
* are silently skipped. The first failure reason is included in the
|
|
681
|
+
* error message when EVERY route fails.
|
|
694
682
|
*
|
|
695
|
-
* Uses viem multicall to batch all quotes into a single RPC call
|
|
683
|
+
* Uses viem multicall to batch all quotes into a single RPC call.
|
|
696
684
|
*/
|
|
697
|
-
declare function quoteBestRoute(client: PublicClient, quoterAddress: Address,
|
|
685
|
+
declare function quoteBestRoute(client: PublicClient, quoterAddress: Address, routes: V3Path[], exactAmount: bigint): Promise<BestQuote>;
|
|
698
686
|
/**
|
|
699
|
-
* Find and quote the best swap route from `tokenIn` to `tokenOut`.
|
|
687
|
+
* Find and quote the best V3 swap route from `tokenIn` to `tokenOut`.
|
|
700
688
|
*
|
|
701
689
|
* Combines the caller's `pools` with `COMMON_POOLS[chainId]`, builds all
|
|
702
690
|
* possible paths (up to `maxHops`), then quotes them all via a single
|
|
703
691
|
* multicall and returns the best result.
|
|
704
692
|
*
|
|
705
693
|
* @param client - viem PublicClient
|
|
706
|
-
* @param chainId - Chain ID (used to look up COMMON_POOLS and
|
|
694
|
+
* @param chainId - Chain ID (used to look up COMMON_POOLS and QUOTER_V2_ADDRESSES)
|
|
707
695
|
* @param tokenIn - Input token address
|
|
708
696
|
* @param tokenOut - Desired output token address
|
|
709
697
|
* @param exactAmount - Exact input amount
|
|
710
698
|
* @param pools - Additional pools to consider (e.g. point-token-specific)
|
|
711
|
-
* @param quoterAddress - Override the default
|
|
699
|
+
* @param quoterAddress - Override the default V3 QuoterV2 address for this chain
|
|
712
700
|
* @param maxHops - Maximum number of hops per path (default 3)
|
|
713
701
|
*/
|
|
714
702
|
declare function findBestQuote(client: PublicClient, chainId: number, tokenIn: Address, tokenOut: Address, exactAmount: bigint, pools?: PoolKey[], quoterAddress?: Address, maxHops?: number): Promise<BestQuote>;
|
|
715
703
|
/**
|
|
716
|
-
* Quote
|
|
704
|
+
* Quote V3 exact-output for a multi-hop path.
|
|
717
705
|
*
|
|
718
|
-
* @param
|
|
719
|
-
*
|
|
720
|
-
* @param
|
|
721
|
-
*
|
|
722
|
-
* (arguments swapped vs. exact-in) to produce
|
|
723
|
-
* the right shape.
|
|
724
|
-
* @param exactAmount - Exact OUTPUT amount the user wants.
|
|
725
|
-
* @returns `{ amountIn, gasEstimate, path }` — the input amount required
|
|
726
|
-
* to receive `exactAmount` of `exactCurrency`.
|
|
706
|
+
* @param path - V3Path in canonical input→output orientation.
|
|
707
|
+
* The encoder reverses for the on-chain call.
|
|
708
|
+
* @param exactAmount - Exact OUTPUT amount the user wants to receive.
|
|
709
|
+
* @returns `{ amountIn, gasEstimate, path }` — the input amount required.
|
|
727
710
|
*/
|
|
728
|
-
declare function quoteExactOutput(client: PublicClient, quoterAddress: Address,
|
|
711
|
+
declare function quoteExactOutput(client: PublicClient, quoterAddress: Address, path: V3Path, exactAmount: bigint): Promise<ExactOutputQuoteResult>;
|
|
729
712
|
/**
|
|
730
|
-
* Quote
|
|
731
|
-
*
|
|
732
|
-
*
|
|
733
|
-
* `zeroForOne` semantics are identical to the exact-input variant —
|
|
734
|
-
* "trade direction" is invariant to the exactness parameter. Only
|
|
735
|
-
* `exactAmount` flips meaning (here: amount of *output* currency the
|
|
736
|
-
* user wants to receive).
|
|
713
|
+
* Quote V3 exact-output for a single-hop swap, given an explicit
|
|
714
|
+
* `tokenIn`/`tokenOut`/`fee` triple.
|
|
737
715
|
*/
|
|
738
|
-
declare function quoteExactOutputSingle(client: PublicClient, quoterAddress: Address,
|
|
716
|
+
declare function quoteExactOutputSingle(client: PublicClient, quoterAddress: Address, tokenIn: Address, tokenOut: Address, fee: number, exactAmount: bigint, sqrtPriceLimitX96?: bigint): Promise<{
|
|
739
717
|
amountIn: bigint;
|
|
740
718
|
gasEstimate: bigint;
|
|
741
719
|
}>;
|
|
742
720
|
/**
|
|
743
|
-
* Try multiple
|
|
721
|
+
* Try multiple V3Path routes for an exact-output swap and return the
|
|
744
722
|
* route requiring the **smallest** input amount, plus all results.
|
|
745
|
-
* Routes that fail
|
|
746
|
-
*
|
|
747
|
-
* Uses viem multicall to batch all quotes into a single RPC call.
|
|
723
|
+
* Routes that fail are silently skipped.
|
|
748
724
|
*/
|
|
749
|
-
declare function quoteBestRouteExactOut(client: PublicClient, quoterAddress: Address,
|
|
725
|
+
declare function quoteBestRouteExactOut(client: PublicClient, quoterAddress: Address, routes: V3Path[], exactAmount: bigint): Promise<ExactOutputBestQuote>;
|
|
750
726
|
/**
|
|
751
|
-
* Find and quote the best
|
|
727
|
+
* Find and quote the best V3 exact-output route from `tokenIn` to
|
|
752
728
|
* `tokenOut` for a desired output amount.
|
|
753
729
|
*
|
|
754
|
-
* Symmetric to `findBestQuote` but
|
|
755
|
-
*
|
|
756
|
-
*
|
|
757
|
-
*
|
|
758
|
-
* (arguments swapped) to enumerate paths in output→input
|
|
759
|
-
* orientation, which is what `quoteExactOutput` expects.
|
|
730
|
+
* Symmetric to `findBestQuote` but `exactAmount` is denominated in
|
|
731
|
+
* `tokenOut` (the OUTPUT token). Paths are enumerated input→output (the
|
|
732
|
+
* canonical SDK orientation) — the QuoterV2 wrapper reverses for the
|
|
733
|
+
* on-chain call.
|
|
760
734
|
*
|
|
761
735
|
* @param client - viem PublicClient.
|
|
762
|
-
* @param chainId - Chain ID (looks up COMMON_POOLS +
|
|
736
|
+
* @param chainId - Chain ID (looks up COMMON_POOLS + QUOTER_V2_ADDRESSES).
|
|
763
737
|
* @param tokenIn - Input token address (what user pays).
|
|
764
738
|
* @param tokenOut - Output token address (what user wants exactly).
|
|
765
739
|
* @param exactAmount - Exact OUTPUT amount.
|
|
766
740
|
* @param pools - Additional pools (e.g. point-token-specific).
|
|
767
|
-
* @param quoterAddress - Override the default
|
|
741
|
+
* @param quoterAddress - Override the default V3 QuoterV2 address.
|
|
768
742
|
* @param maxHops - Maximum number of hops per path (default 3).
|
|
769
743
|
*/
|
|
770
744
|
declare function findBestQuoteExactOut(client: PublicClient, chainId: number, tokenIn: Address, tokenOut: Address, exactAmount: bigint, pools?: PoolKey[], quoterAddress?: Address, maxHops?: number): Promise<ExactOutputBestQuote>;
|
|
@@ -820,8 +794,11 @@ interface SwapDirectResult {
|
|
|
820
794
|
*
|
|
821
795
|
* Flow:
|
|
822
796
|
* 1. Verify the EOA has EIP-7702 delegation to a PAFI-supported impl
|
|
823
|
-
* (
|
|
824
|
-
*
|
|
797
|
+
* (canonically Pimlico's `Simple7702Account`; the legacy Coinbase
|
|
798
|
+
* Smart Wallet v2 BatchExecutor is still accepted by
|
|
799
|
+
* `detectDelegateImpl` for EOAs delegated earlier). Throw with a
|
|
800
|
+
* clear hint pointing at `delegateDirect()` if not delegated at
|
|
801
|
+
* all.
|
|
825
802
|
* 2. `findBestQuote` → best route + gross output.
|
|
826
803
|
* 3. `buildSwapUserOp` → encoded `executeBatch(calls)` calldata.
|
|
827
804
|
* 4. `walletClient.sendTransaction({ to: userAddress, data: callData })`
|
|
@@ -900,7 +877,7 @@ interface SwapDirectExactOutResult {
|
|
|
900
877
|
receipt?: TransactionReceipt;
|
|
901
878
|
/** Echoes `params.amount` — the exact output. */
|
|
902
879
|
outputAmount: bigint;
|
|
903
|
-
/** Raw input amount from the
|
|
880
|
+
/** Raw input amount from the V3 quoter, before slippage. */
|
|
904
881
|
estimatedInputAmount: bigint;
|
|
905
882
|
/** Slippage-bumped cap on input — encoded in the swap calldata. */
|
|
906
883
|
maxAmountIn: bigint;
|
|
@@ -1030,4 +1007,4 @@ interface PerpDepositDirectResult {
|
|
|
1030
1007
|
*/
|
|
1031
1008
|
declare function perpDepositDirect(params: PerpDepositDirectParams): Promise<PerpDepositDirectResult>;
|
|
1032
1009
|
|
|
1033
|
-
export { type ApiPerpDepositRequest, type ApiPerpDepositResponse, type ApiQuoteError, type ApiQuoteExactOutRequest, type ApiQuoteExactOutResponse, type ApiQuoteRequest, type ApiQuoteResponse, type ApiSwapExactOutRequest, type ApiSwapExactOutResponse, type ApiSwapRequest, type ApiSwapResponse, type BuildSwapUserOpExactOutParams, type BuildSwapUserOpParams, type PerpDepositDirectParams, type PerpDepositDirectResult,
|
|
1010
|
+
export { type ApiPerpDepositRequest, type ApiPerpDepositResponse, type ApiQuoteError, type ApiQuoteExactOutRequest, type ApiQuoteExactOutResponse, type ApiQuoteRequest, type ApiQuoteResponse, type ApiSwapExactOutRequest, type ApiSwapExactOutResponse, type ApiSwapRequest, type ApiSwapResponse, type BuildSwapUserOpExactOutParams, type BuildSwapUserOpParams, type PerpDepositDirectParams, type PerpDepositDirectResult, type SwapDirectExactOutParams, type SwapDirectExactOutResult, type SwapDirectParams, type SwapDirectResult, type SwapSimulationResult, TradingHandlers, type TradingHandlersConfig, V3_SWAP_EXACT_IN, V3_SWAP_EXACT_OUT, buildAllPaths, buildErc20ApprovalCalldata, buildPermit2ApprovalCalldata, buildSwapUserOp, buildSwapUserOpExactOut, buildUniversalRouterExecuteArgs, buildUniversalRouterExecuteArgsExactOut, buildV3SwapInputExactIn, buildV3SwapInputExactOut, checkAllowance, combineRoutes, findBestQuote, findBestQuoteExactOut, perpDepositDirect, quoteBestRoute, quoteBestRouteExactOut, quoteExactInput, quoteExactInputSingle, quoteExactOutput, quoteExactOutputSingle, simulateSwap, swapDirect, swapDirectExactOut };
|