@pafi-dev/trading 0.9.0 → 0.10.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 +12 -9
- package/dist/index.cjs +2063 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1330 -2
- package/dist/index.d.ts +1330 -2
- package/dist/index.js +2053 -2
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Address, PublicClient, Hex, WalletClient, TransactionReceipt } from 'viem';
|
|
2
|
-
import { PartialUserOperation, PoolKey, V3Path, BestQuote, ExactOutputBestQuote, QuoteResult, ExactOutputQuoteResult, BROKER_HASHES } from '@pafi-dev/core';
|
|
2
|
+
import { PartialUserOperation, PoolKey, V3Path, BestQuote, ExactOutputBestQuote, QuoteResult, ExactOutputQuoteResult, BROKER_HASHES, Operation } from '@pafi-dev/core';
|
|
3
3
|
export { PAFI_SUBGRAPH_URL, fetchPafiPools } from '@pafi-dev/core';
|
|
4
4
|
|
|
5
5
|
interface ApiQuoteRequest {
|
|
@@ -291,6 +291,65 @@ interface ApiPerpDepositResponse {
|
|
|
291
291
|
/** Relay address used. Same as `vaultAddress` when `path === "vault"`. */
|
|
292
292
|
relayAddress: Address;
|
|
293
293
|
}
|
|
294
|
+
interface ApiErc20TransferRequest {
|
|
295
|
+
chainId: number;
|
|
296
|
+
userAddress: Address;
|
|
297
|
+
/** Token to transfer — must be USDC, USDT, or an active PointToken. */
|
|
298
|
+
tokenAddress: Address;
|
|
299
|
+
/**
|
|
300
|
+
* Final recipient. Rejected when:
|
|
301
|
+
* - equals `getContractAddresses(chainId).pafiFeeRecipient` (self-fee abuse), or
|
|
302
|
+
* - equals `0x0000...0000` (use the burn scenario for that).
|
|
303
|
+
*/
|
|
304
|
+
recipient: Address;
|
|
305
|
+
/** Token-native units (6-dec for stables, 18-dec for PT). Must be > 0. */
|
|
306
|
+
amount: bigint;
|
|
307
|
+
/** ERC-4337 account nonce for the user's EOA (from EntryPoint). */
|
|
308
|
+
aaNonce: bigint;
|
|
309
|
+
/**
|
|
310
|
+
* Operator fee in the SAME token. Paid as a transfer to
|
|
311
|
+
* `pafiFeeRecipient` prepended to the batch.
|
|
312
|
+
*
|
|
313
|
+
* - `undefined` (default): auto-quote via `quoteOperatorFeeForTransfer`.
|
|
314
|
+
* - `0n`: strip fee transfer (forces single-call no-fee userOp).
|
|
315
|
+
* - explicit `bigint`: override (use a tighter Pimlico-derived
|
|
316
|
+
* estimate if you have one).
|
|
317
|
+
*/
|
|
318
|
+
feeAmount?: bigint;
|
|
319
|
+
/**
|
|
320
|
+
* Opt-in fallback when Chainlink / subgraph are unavailable during
|
|
321
|
+
* auto-quote. Default `false` — fails loud with the oracle's error so
|
|
322
|
+
* production knows to alert. Set `true` AND optionally configure
|
|
323
|
+
* `fallbackPtPriceUsdt` to absorb dev/staging RPC outages.
|
|
324
|
+
*
|
|
325
|
+
* Stable tokens (USDC/USDT) only use Chainlink; PT routes through both
|
|
326
|
+
* Chainlink and the V3 pool subgraph. Fallback applies to whichever
|
|
327
|
+
* fails. See `QuoteOperatorFeeForTransferConfig.allowStaleFallback`.
|
|
328
|
+
*
|
|
329
|
+
* Only consulted when `feeAmount` is undefined (auto-quote path).
|
|
330
|
+
*/
|
|
331
|
+
allowStaleFallback?: boolean;
|
|
332
|
+
/** Fallback ETH price (USD) when Chainlink fails. Default 3000. */
|
|
333
|
+
fallbackEthPriceUsd?: number;
|
|
334
|
+
/** Fallback PT price (USDT per 1 PT) when subgraph fails. Default 0.1. */
|
|
335
|
+
fallbackPtPriceUsdt?: number;
|
|
336
|
+
}
|
|
337
|
+
interface ApiErc20TransferResponse {
|
|
338
|
+
/** Sponsored variant — 2 calls (fee transfer + actual transfer). */
|
|
339
|
+
userOp: PartialUserOperation;
|
|
340
|
+
/**
|
|
341
|
+
* Fee-stripped fallback variant — 1 call (transfer only). Emitted
|
|
342
|
+
* only when `feeAmount > 0n`. Submit when the paymaster refuses;
|
|
343
|
+
* user pays ETH gas directly. Same nonce as `userOp`.
|
|
344
|
+
*/
|
|
345
|
+
userOpFallback?: PartialUserOperation;
|
|
346
|
+
/** Resolved operator fee in the same token. `0n` when no-fee mode. */
|
|
347
|
+
feeAmountUsed: bigint;
|
|
348
|
+
/** Recipient used for the fee transfer (= `pafiFeeRecipient`). */
|
|
349
|
+
feeRecipient: Address;
|
|
350
|
+
/** Echo of `tokenAddress` (checksummed). Use as a sanity tag in FE logs. */
|
|
351
|
+
tokenAddress: Address;
|
|
352
|
+
}
|
|
294
353
|
|
|
295
354
|
interface TradingHandlersConfig {
|
|
296
355
|
provider: PublicClient;
|
|
@@ -396,6 +455,30 @@ declare class TradingHandlers {
|
|
|
396
455
|
* sentinel (Relay not deployed for that chain).
|
|
397
456
|
*/
|
|
398
457
|
handlePerpDeposit(authenticatedAddress: Address, request: ApiPerpDepositRequest): Promise<ApiPerpDepositResponse>;
|
|
458
|
+
/**
|
|
459
|
+
* Build a sponsored ERC-20 transfer UserOp pair (sponsored + fallback).
|
|
460
|
+
*
|
|
461
|
+
* Sponsored variant: `[token.transfer(PAFI, fee), token.transfer(recipient, amount)]`.
|
|
462
|
+
* Fallback variant: `[token.transfer(recipient, amount)]` (user pays ETH).
|
|
463
|
+
*
|
|
464
|
+
* Both calls hit the SAME token contract — fee currency equals the
|
|
465
|
+
* token being sent, so the user needs only ONE token (≥ `amount + fee`)
|
|
466
|
+
* to complete a gasless send.
|
|
467
|
+
*
|
|
468
|
+
* Allowlist enforced HERE (client-side fail-fast) **and** server-side
|
|
469
|
+
* by sponsor-relayer's IntentValidator:
|
|
470
|
+
* - USDC (`getContractAddresses(chainId).usdc`)
|
|
471
|
+
* - USDT (`getContractAddresses(chainId).usdt`)
|
|
472
|
+
* - active PointToken (caller responsibility to check
|
|
473
|
+
* `IssuerRegistry.isActiveByPointToken` if not stable)
|
|
474
|
+
*
|
|
475
|
+
* Recipient rejected when equal to `pafiFeeRecipient` (self-fee abuse)
|
|
476
|
+
* or `0x0` (use the burn scenario for that).
|
|
477
|
+
*
|
|
478
|
+
* See `docs/SPONSORED_ERC20_TRANSFER_SPEC.md` for the full call
|
|
479
|
+
* pattern, error codes, and edge cases.
|
|
480
|
+
*/
|
|
481
|
+
handleErc20Transfer(authenticatedAddress: Address, request: ApiErc20TransferRequest): Promise<ApiErc20TransferResponse>;
|
|
399
482
|
}
|
|
400
483
|
|
|
401
484
|
declare function checkAllowance(client: PublicClient, token: Address, owner: Address, spender: Address): Promise<bigint>;
|
|
@@ -1007,4 +1090,1249 @@ interface PerpDepositDirectResult {
|
|
|
1007
1090
|
*/
|
|
1008
1091
|
declare function perpDepositDirect(params: PerpDepositDirectParams): Promise<PerpDepositDirectResult>;
|
|
1009
1092
|
|
|
1010
|
-
|
|
1093
|
+
interface AddLiquidityDirectParams {
|
|
1094
|
+
/** User EOA — must be EIP-7702 delegated. */
|
|
1095
|
+
userAddress: Address;
|
|
1096
|
+
chainId: number;
|
|
1097
|
+
publicClient: PublicClient;
|
|
1098
|
+
walletClient: WalletClient;
|
|
1099
|
+
/** Pool token0 (must be sorted < token1). */
|
|
1100
|
+
token0: Address;
|
|
1101
|
+
/** Pool token1. */
|
|
1102
|
+
token1: Address;
|
|
1103
|
+
/** V3 fee tier (e.g. 500, 3000, 10000). */
|
|
1104
|
+
fee: number;
|
|
1105
|
+
/** Position range — both ticks must be multiples of the pool's tickSpacing. */
|
|
1106
|
+
tickLower: number;
|
|
1107
|
+
tickUpper: number;
|
|
1108
|
+
/** Caller pins ONE side; the other is computed by the SDK. */
|
|
1109
|
+
amount0Desired?: bigint;
|
|
1110
|
+
amount1Desired?: bigint;
|
|
1111
|
+
/** bps, default 50. Capped at 10000. */
|
|
1112
|
+
slippageBps?: number;
|
|
1113
|
+
/** Unix seconds. Default `now + 5min`. */
|
|
1114
|
+
deadline?: bigint;
|
|
1115
|
+
/** Wait for receipt + parse tokenId. Default `true`. */
|
|
1116
|
+
waitForReceipt?: boolean;
|
|
1117
|
+
onWarning?: (msg: string) => void;
|
|
1118
|
+
}
|
|
1119
|
+
interface AddLiquidityDirectResult {
|
|
1120
|
+
txHash: Hex;
|
|
1121
|
+
receipt?: TransactionReceipt;
|
|
1122
|
+
/** Newly-minted position NFT id. Available when `waitForReceipt !== false`. */
|
|
1123
|
+
tokenId?: bigint;
|
|
1124
|
+
/** Amount0 baked into the calldata (after estimation). */
|
|
1125
|
+
amount0Desired: bigint;
|
|
1126
|
+
amount1Desired: bigint;
|
|
1127
|
+
amount0Min: bigint;
|
|
1128
|
+
amount1Min: bigint;
|
|
1129
|
+
/** L value computed off-chain from amounts + range. Informational. */
|
|
1130
|
+
liquidity: bigint;
|
|
1131
|
+
deadline: bigint;
|
|
1132
|
+
}
|
|
1133
|
+
/**
|
|
1134
|
+
* One-shot helper for the **FE-direct add-liquidity** path — no AA, no
|
|
1135
|
+
* bundler, no PAFI sponsor-relayer. The user EOA pays gas in ETH and
|
|
1136
|
+
* broadcasts a single type-2 transaction calling its own EIP-7702
|
|
1137
|
+
* delegated bytecode (`Simple7702Account.executeBatch`).
|
|
1138
|
+
*
|
|
1139
|
+
* Flow:
|
|
1140
|
+
* 1. Validate inputs (tick range, single-amount guard, slippage).
|
|
1141
|
+
* 2. Verify the EOA is EIP-7702 delegated.
|
|
1142
|
+
* 3. Read pool state (sqrtPriceX96).
|
|
1143
|
+
* 4. Estimate the other side's amount + liquidity from one pinned side.
|
|
1144
|
+
* 5. Apply slippage → (amount0Min, amount1Min).
|
|
1145
|
+
* 6. Build 3-op batch: approve token0, approve token1, NPM.mint.
|
|
1146
|
+
* 7. Send native tx.
|
|
1147
|
+
* 8. Wait receipt + parse `Transfer(0x0 → user, tokenId)` → return tokenId.
|
|
1148
|
+
*
|
|
1149
|
+
* @example
|
|
1150
|
+
* ```ts
|
|
1151
|
+
* const result = await addLiquidityDirect({
|
|
1152
|
+
* userAddress: wallet.address,
|
|
1153
|
+
* chainId: 8453,
|
|
1154
|
+
* publicClient,
|
|
1155
|
+
* walletClient,
|
|
1156
|
+
* token0: PT,
|
|
1157
|
+
* token1: USDC,
|
|
1158
|
+
* fee: 3000,
|
|
1159
|
+
* tickLower: -60000,
|
|
1160
|
+
* tickUpper: -54000,
|
|
1161
|
+
* amount0Desired: parseUnits("100", 18),
|
|
1162
|
+
* });
|
|
1163
|
+
* console.log("Minted position:", result.tokenId);
|
|
1164
|
+
* ```
|
|
1165
|
+
*/
|
|
1166
|
+
declare function addLiquidityDirect(params: AddLiquidityDirectParams): Promise<AddLiquidityDirectResult>;
|
|
1167
|
+
|
|
1168
|
+
interface IncreaseLiquidityDirectParams {
|
|
1169
|
+
userAddress: Address;
|
|
1170
|
+
chainId: number;
|
|
1171
|
+
publicClient: PublicClient;
|
|
1172
|
+
walletClient: WalletClient;
|
|
1173
|
+
/** Existing position NFT id. SDK reads its range + token pair. */
|
|
1174
|
+
tokenId: bigint;
|
|
1175
|
+
amount0Desired?: bigint;
|
|
1176
|
+
amount1Desired?: bigint;
|
|
1177
|
+
slippageBps?: number;
|
|
1178
|
+
deadline?: bigint;
|
|
1179
|
+
waitForReceipt?: boolean;
|
|
1180
|
+
onWarning?: (msg: string) => void;
|
|
1181
|
+
}
|
|
1182
|
+
interface IncreaseLiquidityDirectResult {
|
|
1183
|
+
txHash: Hex;
|
|
1184
|
+
receipt?: TransactionReceipt;
|
|
1185
|
+
amount0Desired: bigint;
|
|
1186
|
+
amount1Desired: bigint;
|
|
1187
|
+
amount0Min: bigint;
|
|
1188
|
+
amount1Min: bigint;
|
|
1189
|
+
/** L added (estimated off-chain; receipt-parsed value would be authoritative). */
|
|
1190
|
+
liquidityAdded: bigint;
|
|
1191
|
+
deadline: bigint;
|
|
1192
|
+
}
|
|
1193
|
+
/**
|
|
1194
|
+
* Top up an existing V3 LP position via NPM.increaseLiquidity.
|
|
1195
|
+
*
|
|
1196
|
+
* Range comes from the existing position; user only specifies one side's
|
|
1197
|
+
* amount and the SDK computes the rest. Same 3-op batch as add (two
|
|
1198
|
+
* approvals + the NPM call), but range is read on-chain rather than
|
|
1199
|
+
* supplied by caller.
|
|
1200
|
+
*/
|
|
1201
|
+
declare function increaseLiquidityDirect(params: IncreaseLiquidityDirectParams): Promise<IncreaseLiquidityDirectResult>;
|
|
1202
|
+
|
|
1203
|
+
interface RemoveLiquidityDirectParams {
|
|
1204
|
+
userAddress: Address;
|
|
1205
|
+
chainId: number;
|
|
1206
|
+
publicClient: PublicClient;
|
|
1207
|
+
walletClient: WalletClient;
|
|
1208
|
+
tokenId: bigint;
|
|
1209
|
+
/** How much L to withdraw. Pass `liquidity` OR `liquidityBps`. Exactly one. */
|
|
1210
|
+
liquidity?: bigint;
|
|
1211
|
+
/** 0..10000 — percentage of current liquidity to remove. */
|
|
1212
|
+
liquidityBps?: number;
|
|
1213
|
+
slippageBps?: number;
|
|
1214
|
+
deadline?: bigint;
|
|
1215
|
+
waitForReceipt?: boolean;
|
|
1216
|
+
onWarning?: (msg: string) => void;
|
|
1217
|
+
}
|
|
1218
|
+
interface RemoveLiquidityDirectResult {
|
|
1219
|
+
txHash: Hex;
|
|
1220
|
+
receipt?: TransactionReceipt;
|
|
1221
|
+
liquidityRemoved: bigint;
|
|
1222
|
+
amount0Expected: bigint;
|
|
1223
|
+
amount1Expected: bigint;
|
|
1224
|
+
amount0Min: bigint;
|
|
1225
|
+
amount1Min: bigint;
|
|
1226
|
+
deadline: bigint;
|
|
1227
|
+
}
|
|
1228
|
+
/**
|
|
1229
|
+
* Partial withdraw from an existing V3 LP position.
|
|
1230
|
+
*
|
|
1231
|
+
* Batch shape:
|
|
1232
|
+
* 1. NPM.decreaseLiquidity({ tokenId, liquidity, amount0Min, amount1Min, deadline })
|
|
1233
|
+
* 2. NPM.collect({ tokenId, recipient: userAddress, amount0Max: MAX, amount1Max: MAX })
|
|
1234
|
+
*
|
|
1235
|
+
* No approvals — NPM doesn't pull tokens on remove. The collect leg is
|
|
1236
|
+
* mandatory because decrease only credits `tokensOwed*`; tokens stay in
|
|
1237
|
+
* the NPM until the user collects them.
|
|
1238
|
+
*/
|
|
1239
|
+
declare function removeLiquidityDirect(params: RemoveLiquidityDirectParams): Promise<RemoveLiquidityDirectResult>;
|
|
1240
|
+
|
|
1241
|
+
interface ClosePositionDirectParams {
|
|
1242
|
+
userAddress: Address;
|
|
1243
|
+
chainId: number;
|
|
1244
|
+
publicClient: PublicClient;
|
|
1245
|
+
walletClient: WalletClient;
|
|
1246
|
+
tokenId: bigint;
|
|
1247
|
+
slippageBps?: number;
|
|
1248
|
+
deadline?: bigint;
|
|
1249
|
+
waitForReceipt?: boolean;
|
|
1250
|
+
onWarning?: (msg: string) => void;
|
|
1251
|
+
}
|
|
1252
|
+
interface ClosePositionDirectResult {
|
|
1253
|
+
txHash: Hex;
|
|
1254
|
+
receipt?: TransactionReceipt;
|
|
1255
|
+
liquidityRemoved: bigint;
|
|
1256
|
+
amount0Expected: bigint;
|
|
1257
|
+
amount1Expected: bigint;
|
|
1258
|
+
amount0Min: bigint;
|
|
1259
|
+
amount1Min: bigint;
|
|
1260
|
+
deadline: bigint;
|
|
1261
|
+
}
|
|
1262
|
+
/**
|
|
1263
|
+
* Full close: decrease all liquidity (if any) + collect fees + burn the
|
|
1264
|
+
* position NFT. The SDK picks the batch shape from current position state:
|
|
1265
|
+
*
|
|
1266
|
+
* - liquidity > 0: decrease(all) → collect → burn (3 ops)
|
|
1267
|
+
* - liquidity == 0 + fees > 0: collect → burn (2 ops)
|
|
1268
|
+
* - liquidity == 0 + no fees: burn (1 op)
|
|
1269
|
+
*
|
|
1270
|
+
* Useful for cleaning up an LP NFT off the user's EOA after they're done.
|
|
1271
|
+
*/
|
|
1272
|
+
declare function closePositionDirect(params: ClosePositionDirectParams): Promise<ClosePositionDirectResult>;
|
|
1273
|
+
|
|
1274
|
+
interface CollectFeesDirectParams {
|
|
1275
|
+
userAddress: Address;
|
|
1276
|
+
chainId: number;
|
|
1277
|
+
publicClient: PublicClient;
|
|
1278
|
+
walletClient: WalletClient;
|
|
1279
|
+
tokenId: bigint;
|
|
1280
|
+
/** Recipient of the collected fees. Default `userAddress`. */
|
|
1281
|
+
recipient?: Address;
|
|
1282
|
+
waitForReceipt?: boolean;
|
|
1283
|
+
onWarning?: (msg: string) => void;
|
|
1284
|
+
}
|
|
1285
|
+
interface CollectFeesDirectResult {
|
|
1286
|
+
txHash: Hex;
|
|
1287
|
+
receipt?: TransactionReceipt;
|
|
1288
|
+
/** Pre-tx estimate (tokensOwed + pending). Actual transferred amount in the receipt's Collect log. */
|
|
1289
|
+
amount0Expected: bigint;
|
|
1290
|
+
amount1Expected: bigint;
|
|
1291
|
+
recipient: Address;
|
|
1292
|
+
}
|
|
1293
|
+
/**
|
|
1294
|
+
* Collect accrued fees from a position without changing its liquidity.
|
|
1295
|
+
*
|
|
1296
|
+
* Single-op batch (`NPM.collect`). NPM internally calls
|
|
1297
|
+
* `pool.burn(tickLower, tickUpper, 0)` when the position has active
|
|
1298
|
+
* liquidity, which flushes newly-accrued swap fees from the pool into
|
|
1299
|
+
* the position's `tokensOwed*` before transferring them out.
|
|
1300
|
+
*
|
|
1301
|
+
* The estimate combines `position.tokensOwed*` (already in NPM) with the
|
|
1302
|
+
* "pending" fees still in the pool (derived off-chain from feeGrowth
|
|
1303
|
+
* deltas). The actual transferred amount comes from the on-chain
|
|
1304
|
+
* `Collect` event in the receipt and may differ slightly if another
|
|
1305
|
+
* tx accrued more fees between the read and mine.
|
|
1306
|
+
*/
|
|
1307
|
+
declare function collectFeesDirect(params: CollectFeesDirectParams): Promise<CollectFeesDirectResult>;
|
|
1308
|
+
|
|
1309
|
+
/**
|
|
1310
|
+
* Uniswap V3 NonfungiblePositionManager (NPM) ABI — subset used by the
|
|
1311
|
+
* `@pafi-dev/trading` liquidity flows. PAFI deployment lives at
|
|
1312
|
+
* `V3_NPM_ADDRESSES[chainId]`.
|
|
1313
|
+
*
|
|
1314
|
+
* Hand-authored (not generated) — same convention as `v3QuoterV2Abi` in
|
|
1315
|
+
* `@pafi-dev/core`. Only the methods + events the SDK actually uses are
|
|
1316
|
+
* declared, to keep the surface small and the type narrowing precise.
|
|
1317
|
+
*
|
|
1318
|
+
* Intentionally omitted: `multicall`, `WETH9`, `refundETH`, `sweepToken`,
|
|
1319
|
+
* `unwrapWETH9` (ETH wrap helpers — out of v1 scope), `tokenURI`, `name`,
|
|
1320
|
+
* `symbol`, `tokenOfOwnerByIndex` (ERC-721 metadata + enumerable — position
|
|
1321
|
+
* listing is the consumer backend's job), `permit`, `safeTransferFrom`.
|
|
1322
|
+
*/
|
|
1323
|
+
declare const nonfungiblePositionManagerAbi: readonly [{
|
|
1324
|
+
readonly type: "function";
|
|
1325
|
+
readonly name: "mint";
|
|
1326
|
+
readonly stateMutability: "payable";
|
|
1327
|
+
readonly inputs: readonly [{
|
|
1328
|
+
readonly name: "params";
|
|
1329
|
+
readonly type: "tuple";
|
|
1330
|
+
readonly components: readonly [{
|
|
1331
|
+
readonly name: "token0";
|
|
1332
|
+
readonly type: "address";
|
|
1333
|
+
}, {
|
|
1334
|
+
readonly name: "token1";
|
|
1335
|
+
readonly type: "address";
|
|
1336
|
+
}, {
|
|
1337
|
+
readonly name: "fee";
|
|
1338
|
+
readonly type: "uint24";
|
|
1339
|
+
}, {
|
|
1340
|
+
readonly name: "tickLower";
|
|
1341
|
+
readonly type: "int24";
|
|
1342
|
+
}, {
|
|
1343
|
+
readonly name: "tickUpper";
|
|
1344
|
+
readonly type: "int24";
|
|
1345
|
+
}, {
|
|
1346
|
+
readonly name: "amount0Desired";
|
|
1347
|
+
readonly type: "uint256";
|
|
1348
|
+
}, {
|
|
1349
|
+
readonly name: "amount1Desired";
|
|
1350
|
+
readonly type: "uint256";
|
|
1351
|
+
}, {
|
|
1352
|
+
readonly name: "amount0Min";
|
|
1353
|
+
readonly type: "uint256";
|
|
1354
|
+
}, {
|
|
1355
|
+
readonly name: "amount1Min";
|
|
1356
|
+
readonly type: "uint256";
|
|
1357
|
+
}, {
|
|
1358
|
+
readonly name: "recipient";
|
|
1359
|
+
readonly type: "address";
|
|
1360
|
+
}, {
|
|
1361
|
+
readonly name: "deadline";
|
|
1362
|
+
readonly type: "uint256";
|
|
1363
|
+
}];
|
|
1364
|
+
}];
|
|
1365
|
+
readonly outputs: readonly [{
|
|
1366
|
+
readonly name: "tokenId";
|
|
1367
|
+
readonly type: "uint256";
|
|
1368
|
+
}, {
|
|
1369
|
+
readonly name: "liquidity";
|
|
1370
|
+
readonly type: "uint128";
|
|
1371
|
+
}, {
|
|
1372
|
+
readonly name: "amount0";
|
|
1373
|
+
readonly type: "uint256";
|
|
1374
|
+
}, {
|
|
1375
|
+
readonly name: "amount1";
|
|
1376
|
+
readonly type: "uint256";
|
|
1377
|
+
}];
|
|
1378
|
+
}, {
|
|
1379
|
+
readonly type: "function";
|
|
1380
|
+
readonly name: "increaseLiquidity";
|
|
1381
|
+
readonly stateMutability: "payable";
|
|
1382
|
+
readonly inputs: readonly [{
|
|
1383
|
+
readonly name: "params";
|
|
1384
|
+
readonly type: "tuple";
|
|
1385
|
+
readonly components: readonly [{
|
|
1386
|
+
readonly name: "tokenId";
|
|
1387
|
+
readonly type: "uint256";
|
|
1388
|
+
}, {
|
|
1389
|
+
readonly name: "amount0Desired";
|
|
1390
|
+
readonly type: "uint256";
|
|
1391
|
+
}, {
|
|
1392
|
+
readonly name: "amount1Desired";
|
|
1393
|
+
readonly type: "uint256";
|
|
1394
|
+
}, {
|
|
1395
|
+
readonly name: "amount0Min";
|
|
1396
|
+
readonly type: "uint256";
|
|
1397
|
+
}, {
|
|
1398
|
+
readonly name: "amount1Min";
|
|
1399
|
+
readonly type: "uint256";
|
|
1400
|
+
}, {
|
|
1401
|
+
readonly name: "deadline";
|
|
1402
|
+
readonly type: "uint256";
|
|
1403
|
+
}];
|
|
1404
|
+
}];
|
|
1405
|
+
readonly outputs: readonly [{
|
|
1406
|
+
readonly name: "liquidity";
|
|
1407
|
+
readonly type: "uint128";
|
|
1408
|
+
}, {
|
|
1409
|
+
readonly name: "amount0";
|
|
1410
|
+
readonly type: "uint256";
|
|
1411
|
+
}, {
|
|
1412
|
+
readonly name: "amount1";
|
|
1413
|
+
readonly type: "uint256";
|
|
1414
|
+
}];
|
|
1415
|
+
}, {
|
|
1416
|
+
readonly type: "function";
|
|
1417
|
+
readonly name: "decreaseLiquidity";
|
|
1418
|
+
readonly stateMutability: "payable";
|
|
1419
|
+
readonly inputs: readonly [{
|
|
1420
|
+
readonly name: "params";
|
|
1421
|
+
readonly type: "tuple";
|
|
1422
|
+
readonly components: readonly [{
|
|
1423
|
+
readonly name: "tokenId";
|
|
1424
|
+
readonly type: "uint256";
|
|
1425
|
+
}, {
|
|
1426
|
+
readonly name: "liquidity";
|
|
1427
|
+
readonly type: "uint128";
|
|
1428
|
+
}, {
|
|
1429
|
+
readonly name: "amount0Min";
|
|
1430
|
+
readonly type: "uint256";
|
|
1431
|
+
}, {
|
|
1432
|
+
readonly name: "amount1Min";
|
|
1433
|
+
readonly type: "uint256";
|
|
1434
|
+
}, {
|
|
1435
|
+
readonly name: "deadline";
|
|
1436
|
+
readonly type: "uint256";
|
|
1437
|
+
}];
|
|
1438
|
+
}];
|
|
1439
|
+
readonly outputs: readonly [{
|
|
1440
|
+
readonly name: "amount0";
|
|
1441
|
+
readonly type: "uint256";
|
|
1442
|
+
}, {
|
|
1443
|
+
readonly name: "amount1";
|
|
1444
|
+
readonly type: "uint256";
|
|
1445
|
+
}];
|
|
1446
|
+
}, {
|
|
1447
|
+
readonly type: "function";
|
|
1448
|
+
readonly name: "collect";
|
|
1449
|
+
readonly stateMutability: "payable";
|
|
1450
|
+
readonly inputs: readonly [{
|
|
1451
|
+
readonly name: "params";
|
|
1452
|
+
readonly type: "tuple";
|
|
1453
|
+
readonly components: readonly [{
|
|
1454
|
+
readonly name: "tokenId";
|
|
1455
|
+
readonly type: "uint256";
|
|
1456
|
+
}, {
|
|
1457
|
+
readonly name: "recipient";
|
|
1458
|
+
readonly type: "address";
|
|
1459
|
+
}, {
|
|
1460
|
+
readonly name: "amount0Max";
|
|
1461
|
+
readonly type: "uint128";
|
|
1462
|
+
}, {
|
|
1463
|
+
readonly name: "amount1Max";
|
|
1464
|
+
readonly type: "uint128";
|
|
1465
|
+
}];
|
|
1466
|
+
}];
|
|
1467
|
+
readonly outputs: readonly [{
|
|
1468
|
+
readonly name: "amount0";
|
|
1469
|
+
readonly type: "uint256";
|
|
1470
|
+
}, {
|
|
1471
|
+
readonly name: "amount1";
|
|
1472
|
+
readonly type: "uint256";
|
|
1473
|
+
}];
|
|
1474
|
+
}, {
|
|
1475
|
+
readonly type: "function";
|
|
1476
|
+
readonly name: "burn";
|
|
1477
|
+
readonly stateMutability: "payable";
|
|
1478
|
+
readonly inputs: readonly [{
|
|
1479
|
+
readonly name: "tokenId";
|
|
1480
|
+
readonly type: "uint256";
|
|
1481
|
+
}];
|
|
1482
|
+
readonly outputs: readonly [];
|
|
1483
|
+
}, {
|
|
1484
|
+
readonly type: "function";
|
|
1485
|
+
readonly name: "positions";
|
|
1486
|
+
readonly stateMutability: "view";
|
|
1487
|
+
readonly inputs: readonly [{
|
|
1488
|
+
readonly name: "tokenId";
|
|
1489
|
+
readonly type: "uint256";
|
|
1490
|
+
}];
|
|
1491
|
+
readonly outputs: readonly [{
|
|
1492
|
+
readonly name: "nonce";
|
|
1493
|
+
readonly type: "uint96";
|
|
1494
|
+
}, {
|
|
1495
|
+
readonly name: "operator";
|
|
1496
|
+
readonly type: "address";
|
|
1497
|
+
}, {
|
|
1498
|
+
readonly name: "token0";
|
|
1499
|
+
readonly type: "address";
|
|
1500
|
+
}, {
|
|
1501
|
+
readonly name: "token1";
|
|
1502
|
+
readonly type: "address";
|
|
1503
|
+
}, {
|
|
1504
|
+
readonly name: "fee";
|
|
1505
|
+
readonly type: "uint24";
|
|
1506
|
+
}, {
|
|
1507
|
+
readonly name: "tickLower";
|
|
1508
|
+
readonly type: "int24";
|
|
1509
|
+
}, {
|
|
1510
|
+
readonly name: "tickUpper";
|
|
1511
|
+
readonly type: "int24";
|
|
1512
|
+
}, {
|
|
1513
|
+
readonly name: "liquidity";
|
|
1514
|
+
readonly type: "uint128";
|
|
1515
|
+
}, {
|
|
1516
|
+
readonly name: "feeGrowthInside0LastX128";
|
|
1517
|
+
readonly type: "uint256";
|
|
1518
|
+
}, {
|
|
1519
|
+
readonly name: "feeGrowthInside1LastX128";
|
|
1520
|
+
readonly type: "uint256";
|
|
1521
|
+
}, {
|
|
1522
|
+
readonly name: "tokensOwed0";
|
|
1523
|
+
readonly type: "uint128";
|
|
1524
|
+
}, {
|
|
1525
|
+
readonly name: "tokensOwed1";
|
|
1526
|
+
readonly type: "uint128";
|
|
1527
|
+
}];
|
|
1528
|
+
}, {
|
|
1529
|
+
readonly type: "function";
|
|
1530
|
+
readonly name: "factory";
|
|
1531
|
+
readonly stateMutability: "view";
|
|
1532
|
+
readonly inputs: readonly [];
|
|
1533
|
+
readonly outputs: readonly [{
|
|
1534
|
+
readonly name: "";
|
|
1535
|
+
readonly type: "address";
|
|
1536
|
+
}];
|
|
1537
|
+
}, {
|
|
1538
|
+
readonly type: "event";
|
|
1539
|
+
readonly name: "Transfer";
|
|
1540
|
+
readonly inputs: readonly [{
|
|
1541
|
+
readonly name: "from";
|
|
1542
|
+
readonly type: "address";
|
|
1543
|
+
readonly indexed: true;
|
|
1544
|
+
}, {
|
|
1545
|
+
readonly name: "to";
|
|
1546
|
+
readonly type: "address";
|
|
1547
|
+
readonly indexed: true;
|
|
1548
|
+
}, {
|
|
1549
|
+
readonly name: "tokenId";
|
|
1550
|
+
readonly type: "uint256";
|
|
1551
|
+
readonly indexed: true;
|
|
1552
|
+
}];
|
|
1553
|
+
}, {
|
|
1554
|
+
readonly type: "event";
|
|
1555
|
+
readonly name: "IncreaseLiquidity";
|
|
1556
|
+
readonly inputs: readonly [{
|
|
1557
|
+
readonly name: "tokenId";
|
|
1558
|
+
readonly type: "uint256";
|
|
1559
|
+
readonly indexed: true;
|
|
1560
|
+
}, {
|
|
1561
|
+
readonly name: "liquidity";
|
|
1562
|
+
readonly type: "uint128";
|
|
1563
|
+
readonly indexed: false;
|
|
1564
|
+
}, {
|
|
1565
|
+
readonly name: "amount0";
|
|
1566
|
+
readonly type: "uint256";
|
|
1567
|
+
readonly indexed: false;
|
|
1568
|
+
}, {
|
|
1569
|
+
readonly name: "amount1";
|
|
1570
|
+
readonly type: "uint256";
|
|
1571
|
+
readonly indexed: false;
|
|
1572
|
+
}];
|
|
1573
|
+
}, {
|
|
1574
|
+
readonly type: "event";
|
|
1575
|
+
readonly name: "DecreaseLiquidity";
|
|
1576
|
+
readonly inputs: readonly [{
|
|
1577
|
+
readonly name: "tokenId";
|
|
1578
|
+
readonly type: "uint256";
|
|
1579
|
+
readonly indexed: true;
|
|
1580
|
+
}, {
|
|
1581
|
+
readonly name: "liquidity";
|
|
1582
|
+
readonly type: "uint128";
|
|
1583
|
+
readonly indexed: false;
|
|
1584
|
+
}, {
|
|
1585
|
+
readonly name: "amount0";
|
|
1586
|
+
readonly type: "uint256";
|
|
1587
|
+
readonly indexed: false;
|
|
1588
|
+
}, {
|
|
1589
|
+
readonly name: "amount1";
|
|
1590
|
+
readonly type: "uint256";
|
|
1591
|
+
readonly indexed: false;
|
|
1592
|
+
}];
|
|
1593
|
+
}, {
|
|
1594
|
+
readonly type: "event";
|
|
1595
|
+
readonly name: "Collect";
|
|
1596
|
+
readonly inputs: readonly [{
|
|
1597
|
+
readonly name: "tokenId";
|
|
1598
|
+
readonly type: "uint256";
|
|
1599
|
+
readonly indexed: true;
|
|
1600
|
+
}, {
|
|
1601
|
+
readonly name: "recipient";
|
|
1602
|
+
readonly type: "address";
|
|
1603
|
+
readonly indexed: false;
|
|
1604
|
+
}, {
|
|
1605
|
+
readonly name: "amount0";
|
|
1606
|
+
readonly type: "uint256";
|
|
1607
|
+
readonly indexed: false;
|
|
1608
|
+
}, {
|
|
1609
|
+
readonly name: "amount1";
|
|
1610
|
+
readonly type: "uint256";
|
|
1611
|
+
readonly indexed: false;
|
|
1612
|
+
}];
|
|
1613
|
+
}];
|
|
1614
|
+
|
|
1615
|
+
/**
|
|
1616
|
+
* Uniswap V3 Pool ABI — read-only subset used by the
|
|
1617
|
+
* `@pafi-dev/trading` liquidity flows to estimate amounts off-chain.
|
|
1618
|
+
*
|
|
1619
|
+
* Pool addresses are derived from `V3_FACTORY_ADDRESSES` +
|
|
1620
|
+
* `V3_POOL_INIT_CODE_HASH` (both in `@pafi-dev/core`) via
|
|
1621
|
+
* `computeV3PoolAddress()`.
|
|
1622
|
+
*
|
|
1623
|
+
* Intentionally omitted: pool-direct write methods (`swap`, `flash`,
|
|
1624
|
+
* `mint`, `burn`, `collect`) — every write call goes through NPM, never
|
|
1625
|
+
* the pool. Also omitted: `positions(bytes32)` (NPM-side is the canonical
|
|
1626
|
+
* read), `protocolFees`, `observe`, `snapshotCumulativesInside`.
|
|
1627
|
+
*/
|
|
1628
|
+
declare const v3PoolAbi: readonly [{
|
|
1629
|
+
readonly type: "function";
|
|
1630
|
+
readonly name: "token0";
|
|
1631
|
+
readonly stateMutability: "view";
|
|
1632
|
+
readonly inputs: readonly [];
|
|
1633
|
+
readonly outputs: readonly [{
|
|
1634
|
+
readonly name: "";
|
|
1635
|
+
readonly type: "address";
|
|
1636
|
+
}];
|
|
1637
|
+
}, {
|
|
1638
|
+
readonly type: "function";
|
|
1639
|
+
readonly name: "token1";
|
|
1640
|
+
readonly stateMutability: "view";
|
|
1641
|
+
readonly inputs: readonly [];
|
|
1642
|
+
readonly outputs: readonly [{
|
|
1643
|
+
readonly name: "";
|
|
1644
|
+
readonly type: "address";
|
|
1645
|
+
}];
|
|
1646
|
+
}, {
|
|
1647
|
+
readonly type: "function";
|
|
1648
|
+
readonly name: "slot0";
|
|
1649
|
+
readonly stateMutability: "view";
|
|
1650
|
+
readonly inputs: readonly [];
|
|
1651
|
+
readonly outputs: readonly [{
|
|
1652
|
+
readonly name: "sqrtPriceX96";
|
|
1653
|
+
readonly type: "uint160";
|
|
1654
|
+
}, {
|
|
1655
|
+
readonly name: "tick";
|
|
1656
|
+
readonly type: "int24";
|
|
1657
|
+
}, {
|
|
1658
|
+
readonly name: "observationIndex";
|
|
1659
|
+
readonly type: "uint16";
|
|
1660
|
+
}, {
|
|
1661
|
+
readonly name: "observationCardinality";
|
|
1662
|
+
readonly type: "uint16";
|
|
1663
|
+
}, {
|
|
1664
|
+
readonly name: "observationCardinalityNext";
|
|
1665
|
+
readonly type: "uint16";
|
|
1666
|
+
}, {
|
|
1667
|
+
readonly name: "feeProtocol";
|
|
1668
|
+
readonly type: "uint8";
|
|
1669
|
+
}, {
|
|
1670
|
+
readonly name: "unlocked";
|
|
1671
|
+
readonly type: "bool";
|
|
1672
|
+
}];
|
|
1673
|
+
}, {
|
|
1674
|
+
readonly type: "function";
|
|
1675
|
+
readonly name: "liquidity";
|
|
1676
|
+
readonly stateMutability: "view";
|
|
1677
|
+
readonly inputs: readonly [];
|
|
1678
|
+
readonly outputs: readonly [{
|
|
1679
|
+
readonly name: "";
|
|
1680
|
+
readonly type: "uint128";
|
|
1681
|
+
}];
|
|
1682
|
+
}, {
|
|
1683
|
+
readonly type: "function";
|
|
1684
|
+
readonly name: "tickSpacing";
|
|
1685
|
+
readonly stateMutability: "view";
|
|
1686
|
+
readonly inputs: readonly [];
|
|
1687
|
+
readonly outputs: readonly [{
|
|
1688
|
+
readonly name: "";
|
|
1689
|
+
readonly type: "int24";
|
|
1690
|
+
}];
|
|
1691
|
+
}, {
|
|
1692
|
+
readonly type: "function";
|
|
1693
|
+
readonly name: "fee";
|
|
1694
|
+
readonly stateMutability: "view";
|
|
1695
|
+
readonly inputs: readonly [];
|
|
1696
|
+
readonly outputs: readonly [{
|
|
1697
|
+
readonly name: "";
|
|
1698
|
+
readonly type: "uint24";
|
|
1699
|
+
}];
|
|
1700
|
+
}, {
|
|
1701
|
+
readonly type: "function";
|
|
1702
|
+
readonly name: "feeGrowthGlobal0X128";
|
|
1703
|
+
readonly stateMutability: "view";
|
|
1704
|
+
readonly inputs: readonly [];
|
|
1705
|
+
readonly outputs: readonly [{
|
|
1706
|
+
readonly name: "";
|
|
1707
|
+
readonly type: "uint256";
|
|
1708
|
+
}];
|
|
1709
|
+
}, {
|
|
1710
|
+
readonly type: "function";
|
|
1711
|
+
readonly name: "feeGrowthGlobal1X128";
|
|
1712
|
+
readonly stateMutability: "view";
|
|
1713
|
+
readonly inputs: readonly [];
|
|
1714
|
+
readonly outputs: readonly [{
|
|
1715
|
+
readonly name: "";
|
|
1716
|
+
readonly type: "uint256";
|
|
1717
|
+
}];
|
|
1718
|
+
}, {
|
|
1719
|
+
readonly type: "function";
|
|
1720
|
+
readonly name: "ticks";
|
|
1721
|
+
readonly stateMutability: "view";
|
|
1722
|
+
readonly inputs: readonly [{
|
|
1723
|
+
readonly name: "tick";
|
|
1724
|
+
readonly type: "int24";
|
|
1725
|
+
}];
|
|
1726
|
+
readonly outputs: readonly [{
|
|
1727
|
+
readonly name: "liquidityGross";
|
|
1728
|
+
readonly type: "uint128";
|
|
1729
|
+
}, {
|
|
1730
|
+
readonly name: "liquidityNet";
|
|
1731
|
+
readonly type: "int128";
|
|
1732
|
+
}, {
|
|
1733
|
+
readonly name: "feeGrowthOutside0X128";
|
|
1734
|
+
readonly type: "uint256";
|
|
1735
|
+
}, {
|
|
1736
|
+
readonly name: "feeGrowthOutside1X128";
|
|
1737
|
+
readonly type: "uint256";
|
|
1738
|
+
}, {
|
|
1739
|
+
readonly name: "tickCumulativeOutside";
|
|
1740
|
+
readonly type: "int56";
|
|
1741
|
+
}, {
|
|
1742
|
+
readonly name: "secondsPerLiquidityOutsideX128";
|
|
1743
|
+
readonly type: "uint160";
|
|
1744
|
+
}, {
|
|
1745
|
+
readonly name: "secondsOutside";
|
|
1746
|
+
readonly type: "uint56";
|
|
1747
|
+
}, {
|
|
1748
|
+
readonly name: "initialized";
|
|
1749
|
+
readonly type: "bool";
|
|
1750
|
+
}];
|
|
1751
|
+
}, {
|
|
1752
|
+
readonly type: "function";
|
|
1753
|
+
readonly name: "tickBitmap";
|
|
1754
|
+
readonly stateMutability: "view";
|
|
1755
|
+
readonly inputs: readonly [{
|
|
1756
|
+
readonly name: "wordPosition";
|
|
1757
|
+
readonly type: "int16";
|
|
1758
|
+
}];
|
|
1759
|
+
readonly outputs: readonly [{
|
|
1760
|
+
readonly name: "";
|
|
1761
|
+
readonly type: "uint256";
|
|
1762
|
+
}];
|
|
1763
|
+
}];
|
|
1764
|
+
|
|
1765
|
+
/**
|
|
1766
|
+
* PAFI's V3 NonfungiblePositionManager (NPM) per chain. All LP flows
|
|
1767
|
+
* encode calls against this address.
|
|
1768
|
+
*
|
|
1769
|
+
* Source of truth: `contracts.md` at the repo root.
|
|
1770
|
+
*/
|
|
1771
|
+
declare const V3_NPM_ADDRESSES: Record<number, Address>;
|
|
1772
|
+
/**
|
|
1773
|
+
* uint128 ceiling. Used as `amount0Max` / `amount1Max` on
|
|
1774
|
+
* `NPM.collect(...)` to mean "transfer everything owed."
|
|
1775
|
+
*/
|
|
1776
|
+
declare const UINT128_MAX: bigint;
|
|
1777
|
+
|
|
1778
|
+
/**
|
|
1779
|
+
* Uniswap V3 liquidity math — hand-rolled bigint primitives.
|
|
1780
|
+
*
|
|
1781
|
+
* Mirrors the algorithms in `@uniswap/v3-sdk` (TickMath, SqrtPriceMath,
|
|
1782
|
+
* LiquidityAmounts) without taking the package as a runtime dependency.
|
|
1783
|
+
* Unit tests cross-check golden values against the upstream SDK; the
|
|
1784
|
+
* runtime trades a ~200 LoC implementation for zero new dependencies.
|
|
1785
|
+
*
|
|
1786
|
+
* All functions are pure and side-effect-free.
|
|
1787
|
+
*/
|
|
1788
|
+
/** Minimum tick allowed by the V3 tick range. */
|
|
1789
|
+
declare const MIN_TICK = -887272;
|
|
1790
|
+
/** Maximum tick allowed by the V3 tick range. */
|
|
1791
|
+
declare const MAX_TICK = 887272;
|
|
1792
|
+
/** sqrtPriceX96 at MIN_TICK. */
|
|
1793
|
+
declare const MIN_SQRT_RATIO: bigint;
|
|
1794
|
+
/** sqrtPriceX96 at MAX_TICK. */
|
|
1795
|
+
declare const MAX_SQRT_RATIO: bigint;
|
|
1796
|
+
/** 2^96 — the X96 scaling factor. */
|
|
1797
|
+
declare const Q96: bigint;
|
|
1798
|
+
/** 2^192 — used in sqrt² → ratio conversions. */
|
|
1799
|
+
declare const Q192: bigint;
|
|
1800
|
+
/**
|
|
1801
|
+
* Convert a tick to its sqrtPriceX96, using V3's exact lookup-table
|
|
1802
|
+
* algorithm. Equivalent to `TickMath.getSqrtRatioAtTick(tick)`.
|
|
1803
|
+
*
|
|
1804
|
+
* @throws if `tick` is outside `[MIN_TICK, MAX_TICK]`
|
|
1805
|
+
*/
|
|
1806
|
+
declare function tickToSqrtPriceX96(tick: number): bigint;
|
|
1807
|
+
/**
|
|
1808
|
+
* Convert a sqrtPriceX96 back to the largest tick whose
|
|
1809
|
+
* `tickToSqrtPriceX96(tick)` is <= the input. Inverse of
|
|
1810
|
+
* `tickToSqrtPriceX96`, equivalent to
|
|
1811
|
+
* `TickMath.getTickAtSqrtRatio(sqrtPriceX96)`.
|
|
1812
|
+
*
|
|
1813
|
+
* Uses a most-significant-bit (MSB) search + iterative refinement.
|
|
1814
|
+
*
|
|
1815
|
+
* @throws if `sqrtPriceX96` is outside `[MIN_SQRT_RATIO, MAX_SQRT_RATIO)`
|
|
1816
|
+
*/
|
|
1817
|
+
declare function sqrtPriceX96ToTick(sqrtPriceX96: bigint): number;
|
|
1818
|
+
/**
|
|
1819
|
+
* Snap a tick to the nearest multiple of `tickSpacing` (rounds toward
|
|
1820
|
+
* zero). Use to convert a user-entered tick to one the pool will accept.
|
|
1821
|
+
*/
|
|
1822
|
+
declare function nearestUsableTick(tick: number, tickSpacing: number): number;
|
|
1823
|
+
/** Minimum tick aligned to `tickSpacing`, clipped to `MIN_TICK`. */
|
|
1824
|
+
declare function minUsableTick(tickSpacing: number): number;
|
|
1825
|
+
/** Maximum tick aligned to `tickSpacing`, clipped to `MAX_TICK`. */
|
|
1826
|
+
declare function maxUsableTick(tickSpacing: number): number;
|
|
1827
|
+
/**
|
|
1828
|
+
* Standard fee-tier → tickSpacing mapping. Matches Uniswap V3 mainnet
|
|
1829
|
+
* conventions and is verified on-chain for PAFI's V3-fork deployment.
|
|
1830
|
+
* Returns `undefined` for non-standard fees; the caller should then read
|
|
1831
|
+
* `factory.feeAmountTickSpacing(fee)` to be safe.
|
|
1832
|
+
*/
|
|
1833
|
+
declare function tickSpacingForFee(fee: number): number | undefined;
|
|
1834
|
+
/**
|
|
1835
|
+
* Convert a human-readable price (`token1` per `token0`) to a tick.
|
|
1836
|
+
* Decimals are factored in so callers can pass real-world quantities.
|
|
1837
|
+
*
|
|
1838
|
+
* Returned tick may need `nearestUsableTick()` to be pool-valid.
|
|
1839
|
+
*/
|
|
1840
|
+
declare function priceToTick(params: {
|
|
1841
|
+
price: number;
|
|
1842
|
+
token0Decimals: number;
|
|
1843
|
+
token1Decimals: number;
|
|
1844
|
+
}): number;
|
|
1845
|
+
/**
|
|
1846
|
+
* Convert a tick back to a human-readable price (`token1` per `token0`).
|
|
1847
|
+
* Inverse of `priceToTick`.
|
|
1848
|
+
*/
|
|
1849
|
+
declare function tickToPrice(params: {
|
|
1850
|
+
tick: number;
|
|
1851
|
+
token0Decimals: number;
|
|
1852
|
+
token1Decimals: number;
|
|
1853
|
+
}): number;
|
|
1854
|
+
/**
|
|
1855
|
+
* Token0 amount required to provide `liquidity` over [sqrtL, sqrtU].
|
|
1856
|
+
* Matches `SqrtPriceMath.getAmount0Delta(..., roundUp=true)` from V3.
|
|
1857
|
+
*/
|
|
1858
|
+
declare function getAmount0ForLiquidity(sqrtPriceX96Lower: bigint, sqrtPriceX96Upper: bigint, liquidity: bigint): bigint;
|
|
1859
|
+
/**
|
|
1860
|
+
* Token1 amount required to provide `liquidity` over [sqrtL, sqrtU].
|
|
1861
|
+
* Matches `SqrtPriceMath.getAmount1Delta(..., roundUp=true)` from V3.
|
|
1862
|
+
*/
|
|
1863
|
+
declare function getAmount1ForLiquidity(sqrtPriceX96Lower: bigint, sqrtPriceX96Upper: bigint, liquidity: bigint): bigint;
|
|
1864
|
+
/**
|
|
1865
|
+
* Canonical "given liquidity + range + current price, compute amounts"
|
|
1866
|
+
* helper. Handles the three V3 cases (current below, in, or above range).
|
|
1867
|
+
*/
|
|
1868
|
+
declare function getAmountsForLiquidity(params: {
|
|
1869
|
+
sqrtPriceX96Current: bigint;
|
|
1870
|
+
sqrtPriceX96Lower: bigint;
|
|
1871
|
+
sqrtPriceX96Upper: bigint;
|
|
1872
|
+
liquidity: bigint;
|
|
1873
|
+
}): {
|
|
1874
|
+
amount0: bigint;
|
|
1875
|
+
amount1: bigint;
|
|
1876
|
+
};
|
|
1877
|
+
/**
|
|
1878
|
+
* Liquidity that `amount0` of token0 would buy over [sqrtL, sqrtU].
|
|
1879
|
+
* Used when the user pins `amount0Desired` and the range is at-or-below
|
|
1880
|
+
* current price.
|
|
1881
|
+
*/
|
|
1882
|
+
declare function getLiquidityForAmount0(sqrtPriceX96Lower: bigint, sqrtPriceX96Upper: bigint, amount0: bigint): bigint;
|
|
1883
|
+
/**
|
|
1884
|
+
* Liquidity that `amount1` of token1 would buy over [sqrtL, sqrtU].
|
|
1885
|
+
* Used when the user pins `amount1Desired` and the range is at-or-above
|
|
1886
|
+
* current price.
|
|
1887
|
+
*/
|
|
1888
|
+
declare function getLiquidityForAmount1(sqrtPriceX96Lower: bigint, sqrtPriceX96Upper: bigint, amount1: bigint): bigint;
|
|
1889
|
+
/**
|
|
1890
|
+
* High-level estimator. Caller has current spot + range + ONE side's
|
|
1891
|
+
* amount; returns the liquidity and the matching amounts.
|
|
1892
|
+
*
|
|
1893
|
+
* Branches:
|
|
1894
|
+
* - Current spot below range → amount0 drives, amount1 = 0.
|
|
1895
|
+
* - Current spot above range → amount1 drives, amount0 = 0.
|
|
1896
|
+
* - Current spot in range → caller's pinned side drives liquidity;
|
|
1897
|
+
* the other side is recomputed from that
|
|
1898
|
+
* liquidity at the current price.
|
|
1899
|
+
*
|
|
1900
|
+
* @throws if neither or both of amount0Desired / amount1Desired are set,
|
|
1901
|
+
* or if the pinned side is incompatible with the position
|
|
1902
|
+
* (e.g. amount1 pinned but spot < range).
|
|
1903
|
+
*/
|
|
1904
|
+
declare function estimateLiquidityFromOneSide(params: {
|
|
1905
|
+
sqrtPriceX96Current: bigint;
|
|
1906
|
+
sqrtPriceX96Lower: bigint;
|
|
1907
|
+
sqrtPriceX96Upper: bigint;
|
|
1908
|
+
amount0Desired?: bigint;
|
|
1909
|
+
amount1Desired?: bigint;
|
|
1910
|
+
}): {
|
|
1911
|
+
liquidity: bigint;
|
|
1912
|
+
amount0: bigint;
|
|
1913
|
+
amount1: bigint;
|
|
1914
|
+
};
|
|
1915
|
+
/**
|
|
1916
|
+
* Apply slippage to `(amount0Desired, amount1Desired)` for an add or
|
|
1917
|
+
* increase, returning the `(amount0Min, amount1Min)` tuple to send to
|
|
1918
|
+
* NPM. Floor div — the on-chain minimum must round DOWN for safety.
|
|
1919
|
+
*/
|
|
1920
|
+
declare function applyMintSlippage(params: {
|
|
1921
|
+
amount0Desired: bigint;
|
|
1922
|
+
amount1Desired: bigint;
|
|
1923
|
+
slippageBps: number;
|
|
1924
|
+
}): {
|
|
1925
|
+
amount0Min: bigint;
|
|
1926
|
+
amount1Min: bigint;
|
|
1927
|
+
};
|
|
1928
|
+
/**
|
|
1929
|
+
* Apply slippage to expected `(amount0, amount1)` for a decrease/close,
|
|
1930
|
+
* returning `(amount0Min, amount1Min)` to send to NPM. Floor div, same
|
|
1931
|
+
* rationale as `applyMintSlippage`.
|
|
1932
|
+
*/
|
|
1933
|
+
declare function applyRemoveSlippage(params: {
|
|
1934
|
+
amount0Expected: bigint;
|
|
1935
|
+
amount1Expected: bigint;
|
|
1936
|
+
slippageBps: number;
|
|
1937
|
+
}): {
|
|
1938
|
+
amount0Min: bigint;
|
|
1939
|
+
amount1Min: bigint;
|
|
1940
|
+
};
|
|
1941
|
+
|
|
1942
|
+
/**
|
|
1943
|
+
* Decoded `NPM.positions(tokenId)` row.
|
|
1944
|
+
*
|
|
1945
|
+
* Fields mirror the 12-tuple returned by the contract, named for
|
|
1946
|
+
* readability. `feeGrowthInside*LastX128` are Q128.128 fixed-point —
|
|
1947
|
+
* used together with `pool.feeGrowthGlobal*X128()` and `pool.ticks(...)`
|
|
1948
|
+
* to derive pending fees in `collectFeesDirect`.
|
|
1949
|
+
*/
|
|
1950
|
+
interface PositionInfo {
|
|
1951
|
+
nonce: bigint;
|
|
1952
|
+
operator: Address;
|
|
1953
|
+
token0: Address;
|
|
1954
|
+
token1: Address;
|
|
1955
|
+
fee: number;
|
|
1956
|
+
tickLower: number;
|
|
1957
|
+
tickUpper: number;
|
|
1958
|
+
liquidity: bigint;
|
|
1959
|
+
feeGrowthInside0LastX128: bigint;
|
|
1960
|
+
feeGrowthInside1LastX128: bigint;
|
|
1961
|
+
tokensOwed0: bigint;
|
|
1962
|
+
tokensOwed1: bigint;
|
|
1963
|
+
}
|
|
1964
|
+
/**
|
|
1965
|
+
* Read an NFT position's state by tokenId.
|
|
1966
|
+
*
|
|
1967
|
+
* @param client viem PublicClient
|
|
1968
|
+
* @param npm NonfungiblePositionManager address (`V3_NPM_ADDRESSES[chainId]`)
|
|
1969
|
+
* @param tokenId The NFT id
|
|
1970
|
+
*/
|
|
1971
|
+
declare function readPosition(client: PublicClient, npm: Address, tokenId: bigint): Promise<PositionInfo>;
|
|
1972
|
+
|
|
1973
|
+
/**
|
|
1974
|
+
* Distilled V3 pool state — the fields LP flows need to estimate amounts
|
|
1975
|
+
* and validate ticks.
|
|
1976
|
+
*/
|
|
1977
|
+
interface PoolState {
|
|
1978
|
+
sqrtPriceX96: bigint;
|
|
1979
|
+
/** Current tick (`int24` from slot0). */
|
|
1980
|
+
tick: number;
|
|
1981
|
+
/** In-range liquidity at the current tick. */
|
|
1982
|
+
liquidity: bigint;
|
|
1983
|
+
tickSpacing: number;
|
|
1984
|
+
/** Fee tier in hundredths of a basis point (e.g. `3000` = 0.3%). */
|
|
1985
|
+
fee: number;
|
|
1986
|
+
}
|
|
1987
|
+
/**
|
|
1988
|
+
* Per-tick state read from `pool.ticks(tick)`. Only the fields LP flows
|
|
1989
|
+
* use are kept; the rest of the tuple is dropped.
|
|
1990
|
+
*/
|
|
1991
|
+
interface TickInfo {
|
|
1992
|
+
liquidityGross: bigint;
|
|
1993
|
+
liquidityNet: bigint;
|
|
1994
|
+
feeGrowthOutside0X128: bigint;
|
|
1995
|
+
feeGrowthOutside1X128: bigint;
|
|
1996
|
+
initialized: boolean;
|
|
1997
|
+
}
|
|
1998
|
+
/**
|
|
1999
|
+
* Pool fee-growth globals — used together with `TickInfo` and the
|
|
2000
|
+
* position's `feeGrowthInside*LastX128` to compute pending fees.
|
|
2001
|
+
*/
|
|
2002
|
+
interface PoolFeeGrowthGlobals {
|
|
2003
|
+
feeGrowthGlobal0X128: bigint;
|
|
2004
|
+
feeGrowthGlobal1X128: bigint;
|
|
2005
|
+
}
|
|
2006
|
+
/**
|
|
2007
|
+
* Read a pool's current state in a single multicall.
|
|
2008
|
+
*
|
|
2009
|
+
* @param client viem PublicClient
|
|
2010
|
+
* @param pool Pool address (use `computeV3PoolAddress` to derive)
|
|
2011
|
+
*/
|
|
2012
|
+
declare function readPoolState(client: PublicClient, pool: Address,
|
|
2013
|
+
/** Pin reads to this block. Default: latest (matches prior behavior). */
|
|
2014
|
+
blockNumber?: bigint): Promise<PoolState>;
|
|
2015
|
+
/**
|
|
2016
|
+
* Read tick info for `tickLower` and `tickUpper` in a single multicall.
|
|
2017
|
+
*
|
|
2018
|
+
* Used by `collectFeesDirect` to compute `feeGrowthInside*` (and from
|
|
2019
|
+
* that, the pending-fee estimate).
|
|
2020
|
+
*/
|
|
2021
|
+
declare function readTicksAt(client: PublicClient, pool: Address, tickLower: number, tickUpper: number): Promise<{
|
|
2022
|
+
lower: TickInfo;
|
|
2023
|
+
upper: TickInfo;
|
|
2024
|
+
}>;
|
|
2025
|
+
/**
|
|
2026
|
+
* Read pool fee-growth globals.
|
|
2027
|
+
*
|
|
2028
|
+
* Could be folded into `readPoolState` but kept separate so that flows
|
|
2029
|
+
* which don't need fee tracking (mint, decrease) don't waste an RPC on
|
|
2030
|
+
* the extra two reads.
|
|
2031
|
+
*/
|
|
2032
|
+
declare function readFeeGrowthGlobals(client: PublicClient, pool: Address): Promise<PoolFeeGrowthGlobals>;
|
|
2033
|
+
|
|
2034
|
+
interface BuildMintParams {
|
|
2035
|
+
/** NonfungiblePositionManager address (`V3_NPM_ADDRESSES[chainId]`). */
|
|
2036
|
+
npm: Address;
|
|
2037
|
+
token0: Address;
|
|
2038
|
+
token1: Address;
|
|
2039
|
+
fee: number;
|
|
2040
|
+
tickLower: number;
|
|
2041
|
+
tickUpper: number;
|
|
2042
|
+
/** Exact amount0 caller is approving + sending. */
|
|
2043
|
+
amount0Desired: bigint;
|
|
2044
|
+
/** Exact amount1 caller is approving + sending. */
|
|
2045
|
+
amount1Desired: bigint;
|
|
2046
|
+
amount0Min: bigint;
|
|
2047
|
+
amount1Min: bigint;
|
|
2048
|
+
/** Position NFT recipient — typically the user's EOA. */
|
|
2049
|
+
recipient: Address;
|
|
2050
|
+
/** Unix seconds. NPM reverts if `block.timestamp > deadline`. */
|
|
2051
|
+
deadline: bigint;
|
|
2052
|
+
}
|
|
2053
|
+
/**
|
|
2054
|
+
* Build the 3-op batch for `addLiquidityDirect`:
|
|
2055
|
+
*
|
|
2056
|
+
* 1. token0.approve(NPM, amount0Desired)
|
|
2057
|
+
* 2. token1.approve(NPM, amount1Desired)
|
|
2058
|
+
* 3. NPM.mint({ ... })
|
|
2059
|
+
*
|
|
2060
|
+
* Approvals are per-call exact (not `max`) — leaves zero residual
|
|
2061
|
+
* allowance after the batch, preventing a stale approve from being
|
|
2062
|
+
* front-run on a later mint.
|
|
2063
|
+
*
|
|
2064
|
+
* Caller is responsible for ordering `token0 < token1` (V3 invariant);
|
|
2065
|
+
* this builder doesn't sort, since the desired/min amounts are
|
|
2066
|
+
* positional and re-sorting would break them.
|
|
2067
|
+
*/
|
|
2068
|
+
declare function buildMint(params: BuildMintParams): Operation[];
|
|
2069
|
+
|
|
2070
|
+
interface BuildIncreaseLiquidityParams {
|
|
2071
|
+
npm: Address;
|
|
2072
|
+
token0: Address;
|
|
2073
|
+
token1: Address;
|
|
2074
|
+
tokenId: bigint;
|
|
2075
|
+
amount0Desired: bigint;
|
|
2076
|
+
amount1Desired: bigint;
|
|
2077
|
+
amount0Min: bigint;
|
|
2078
|
+
amount1Min: bigint;
|
|
2079
|
+
deadline: bigint;
|
|
2080
|
+
}
|
|
2081
|
+
/**
|
|
2082
|
+
* Build the 3-op batch for `increaseLiquidityDirect`:
|
|
2083
|
+
*
|
|
2084
|
+
* 1. token0.approve(NPM, amount0Desired)
|
|
2085
|
+
* 2. token1.approve(NPM, amount1Desired)
|
|
2086
|
+
* 3. NPM.increaseLiquidity({ tokenId, ... })
|
|
2087
|
+
*
|
|
2088
|
+
* Range comes from the existing position — caller resolves it via
|
|
2089
|
+
* `readPosition(tokenId)` before calling this builder.
|
|
2090
|
+
*/
|
|
2091
|
+
declare function buildIncreaseLiquidity(params: BuildIncreaseLiquidityParams): Operation[];
|
|
2092
|
+
|
|
2093
|
+
interface BuildDecreaseLiquidityParams {
|
|
2094
|
+
npm: Address;
|
|
2095
|
+
tokenId: bigint;
|
|
2096
|
+
liquidity: bigint;
|
|
2097
|
+
amount0Min: bigint;
|
|
2098
|
+
amount1Min: bigint;
|
|
2099
|
+
deadline: bigint;
|
|
2100
|
+
}
|
|
2101
|
+
/**
|
|
2102
|
+
* Build a single `NPM.decreaseLiquidity({ ... })` Operation.
|
|
2103
|
+
*
|
|
2104
|
+
* No approvals needed — NPM doesn't pull tokens on decrease (it credits
|
|
2105
|
+
* the position's `tokensOwed0/1`, which a subsequent `collect()` flushes
|
|
2106
|
+
* to the recipient).
|
|
2107
|
+
*/
|
|
2108
|
+
declare function buildDecreaseLiquidity(params: BuildDecreaseLiquidityParams): Operation;
|
|
2109
|
+
|
|
2110
|
+
interface BuildCollectParams {
|
|
2111
|
+
npm: Address;
|
|
2112
|
+
tokenId: bigint;
|
|
2113
|
+
/** Where the collected fees + principal go. Typically the user's EOA. */
|
|
2114
|
+
recipient: Address;
|
|
2115
|
+
/** Caps on the transfer. Default `UINT128_MAX` → take everything owed. */
|
|
2116
|
+
amount0Max?: bigint;
|
|
2117
|
+
amount1Max?: bigint;
|
|
2118
|
+
}
|
|
2119
|
+
/**
|
|
2120
|
+
* Build a single `NPM.collect({ ... })` Operation.
|
|
2121
|
+
*
|
|
2122
|
+
* When the position still has `liquidity > 0`, NPM internally calls
|
|
2123
|
+
* `pool.burn(tickLower, tickUpper, 0)` to flush newly-accrued fees into
|
|
2124
|
+
* `tokensOwed*` before transferring. So a standalone collect on an
|
|
2125
|
+
* active position both accrues AND claims in one tx.
|
|
2126
|
+
*
|
|
2127
|
+
* For collect-everything use the default `UINT128_MAX` caps.
|
|
2128
|
+
*/
|
|
2129
|
+
declare function buildCollect(params: BuildCollectParams): Operation;
|
|
2130
|
+
|
|
2131
|
+
interface BuildBurnParams {
|
|
2132
|
+
npm: Address;
|
|
2133
|
+
tokenId: bigint;
|
|
2134
|
+
}
|
|
2135
|
+
/**
|
|
2136
|
+
* Build a single `NPM.burn(tokenId)` Operation.
|
|
2137
|
+
*
|
|
2138
|
+
* NPM enforces three preconditions on burn:
|
|
2139
|
+
* - `position.liquidity == 0`
|
|
2140
|
+
* - `position.tokensOwed0 == 0`
|
|
2141
|
+
* - `position.tokensOwed1 == 0`
|
|
2142
|
+
*
|
|
2143
|
+
* For a full close, batch `decreaseLiquidity(all) → collect → burn`. The
|
|
2144
|
+
* `buildClosePosition` orchestrator wires that for you; this builder is
|
|
2145
|
+
* exposed for callers that already know the preconditions hold (e.g.
|
|
2146
|
+
* burning an already-emptied position).
|
|
2147
|
+
*/
|
|
2148
|
+
declare function buildBurn(params: BuildBurnParams): Operation;
|
|
2149
|
+
|
|
2150
|
+
interface BuildClosePositionParams {
|
|
2151
|
+
npm: Address;
|
|
2152
|
+
tokenId: bigint;
|
|
2153
|
+
/** Current `position.liquidity`. Determines batch shape. */
|
|
2154
|
+
liquidity: bigint;
|
|
2155
|
+
/** `tokensOwed0 + tokensOwed1 > 0` → still need a collect leg. */
|
|
2156
|
+
hasOutstandingFees: boolean;
|
|
2157
|
+
/** Recipient of the collected tokens. Typically the user's EOA. */
|
|
2158
|
+
recipient: Address;
|
|
2159
|
+
/** Slippage-applied minimums for the decrease leg. */
|
|
2160
|
+
amount0Min: bigint;
|
|
2161
|
+
amount1Min: bigint;
|
|
2162
|
+
/** Unix seconds. Required for the decrease leg if liquidity > 0. */
|
|
2163
|
+
deadline: bigint;
|
|
2164
|
+
}
|
|
2165
|
+
/**
|
|
2166
|
+
* Build the full-close batch:
|
|
2167
|
+
*
|
|
2168
|
+
* - `liquidity > 0`: decreaseLiquidity(all) → collect → burn
|
|
2169
|
+
* - `liquidity == 0` + fees: collect → burn
|
|
2170
|
+
* - `liquidity == 0` + no fees: burn (only)
|
|
2171
|
+
*
|
|
2172
|
+
* The shape selection avoids unnecessary calls — burning an empty
|
|
2173
|
+
* position with no fees is a 1-op batch. Callers don't need to think
|
|
2174
|
+
* about the cases; pass the snapshot and the builder picks.
|
|
2175
|
+
*/
|
|
2176
|
+
declare function buildClosePosition(params: BuildClosePositionParams): Operation[];
|
|
2177
|
+
|
|
2178
|
+
/**
|
|
2179
|
+
* Compute the (word, bit) position of a tick in V3's bitmap layout.
|
|
2180
|
+
*
|
|
2181
|
+
* Mirrors Solidity `TickBitmap.position(int24)`:
|
|
2182
|
+
* compressed = tick / tickSpacing (floor div, not trunc)
|
|
2183
|
+
* word = compressed >> 8 (JS arithmetic shift on int32)
|
|
2184
|
+
* bit = ((compressed % 256) + 256) % 256 (JS `%` is sign-preserving — normalize)
|
|
2185
|
+
*
|
|
2186
|
+
* Throws if `tick` or `tickSpacing` is not an integer, if `tickSpacing <= 0`,
|
|
2187
|
+
* or if `tick` is not aligned to `tickSpacing`.
|
|
2188
|
+
*/
|
|
2189
|
+
declare function tickToBitmapPosition(tick: number, tickSpacing: number): {
|
|
2190
|
+
word: number;
|
|
2191
|
+
bit: number;
|
|
2192
|
+
};
|
|
2193
|
+
/**
|
|
2194
|
+
* Decode set bits of a single bitmap word into absolute tick indices.
|
|
2195
|
+
* Returns sorted ascending. O(1) when `bitmap === 0n`.
|
|
2196
|
+
*
|
|
2197
|
+
* Bitmap is uint256 → scans bit positions 0..255 inclusive. Decoded with
|
|
2198
|
+
* bigint primitives — `Number` would lose bits past position 53.
|
|
2199
|
+
*/
|
|
2200
|
+
declare function decodeBitmapWord(bitmap: bigint, wordPos: number, tickSpacing: number): number[];
|
|
2201
|
+
/**
|
|
2202
|
+
* Min/max wordPos required to scan an entire V3 pool's bitmap for the
|
|
2203
|
+
* given tickSpacing. Derived from `minUsableTick(spacing)` /
|
|
2204
|
+
* `maxUsableTick(spacing)`, not raw MIN/MAX_TICK.
|
|
2205
|
+
*/
|
|
2206
|
+
declare function bitmapWordRange(tickSpacing: number): {
|
|
2207
|
+
min: number;
|
|
2208
|
+
max: number;
|
|
2209
|
+
};
|
|
2210
|
+
|
|
2211
|
+
/**
|
|
2212
|
+
* Discriminated error codes for the liquidity-curve subsystem. Use with
|
|
2213
|
+
* `LiquidityCurveError`: callers `instanceof LiquidityCurveError` then
|
|
2214
|
+
* branch on `code` to decide retry / Sentry / user-copy behavior.
|
|
2215
|
+
*/
|
|
2216
|
+
declare enum LiquidityCurveErrorCode {
|
|
2217
|
+
/** fetchAllInitializedTicks: initTicks.length > maxTicks. Recovery: raise maxTicks. */
|
|
2218
|
+
TOO_MANY_TICKS = "TOO_MANY_TICKS",
|
|
2219
|
+
/** fetchAllInitializedTicks: bitmap-vs-ticks() mismatch. Recovery: retry against latest. */
|
|
2220
|
+
ATOMICITY_VIOLATION = "ATOMICITY_VIOLATION",
|
|
2221
|
+
/** buildLiquidityCurve: sum(liquidityNet) !== 0. Tick set incomplete or stale. */
|
|
2222
|
+
CURVE_SUM_NONZERO = "CURVE_SUM_NONZERO",
|
|
2223
|
+
/** buildLiquidityCurve: cumulative L went negative during walk. */
|
|
2224
|
+
CURVE_NEGATIVE_L = "CURVE_NEGATIVE_L",
|
|
2225
|
+
/** buildLiquidityCurve: edge L !== 0. currentLiquidity inconsistent with ticks. */
|
|
2226
|
+
CURVE_EDGE_NONZERO = "CURVE_EDGE_NONZERO"
|
|
2227
|
+
}
|
|
2228
|
+
declare class LiquidityCurveError extends Error {
|
|
2229
|
+
readonly code: LiquidityCurveErrorCode;
|
|
2230
|
+
constructor(code: LiquidityCurveErrorCode, message: string);
|
|
2231
|
+
}
|
|
2232
|
+
interface InitializedTick extends TickInfo {
|
|
2233
|
+
/** Absolute tick index — integer, divisible by `tickSpacing`. */
|
|
2234
|
+
tick: number;
|
|
2235
|
+
}
|
|
2236
|
+
interface LiquidityCurveSegment {
|
|
2237
|
+
/** Inclusive lower bound. For the leftmost segment, equals `minUsableTick(spacing)`. */
|
|
2238
|
+
tickLower: number;
|
|
2239
|
+
/** Exclusive upper bound. For the rightmost segment, equals `maxUsableTick(spacing)`. */
|
|
2240
|
+
tickUpper: number;
|
|
2241
|
+
/** `tickToSqrtPriceX96(tickLower)`. */
|
|
2242
|
+
sqrtPriceLowerX96: bigint;
|
|
2243
|
+
/** `tickToSqrtPriceX96(tickUpper)`. */
|
|
2244
|
+
sqrtPriceUpperX96: bigint;
|
|
2245
|
+
/** Active liquidity (`L`) constant across `[tickLower, tickUpper)`. */
|
|
2246
|
+
liquidityActive: bigint;
|
|
2247
|
+
}
|
|
2248
|
+
interface BuildLiquidityCurveParams {
|
|
2249
|
+
/** From `fetchAllInitializedTicks`. Must be the COMPLETE set, sorted asc. */
|
|
2250
|
+
ticks: InitializedTick[];
|
|
2251
|
+
/** From `poolState.tick`. */
|
|
2252
|
+
currentTick: number;
|
|
2253
|
+
/** From `poolState.liquidity`. */
|
|
2254
|
+
currentLiquidity: bigint;
|
|
2255
|
+
/** From `poolState.tickSpacing`. Drives edge-clamp + sqrt-price boundaries. */
|
|
2256
|
+
tickSpacing: number;
|
|
2257
|
+
}
|
|
2258
|
+
/**
|
|
2259
|
+
* Pure transformer. Produces N+1 half-open segments covering
|
|
2260
|
+
* `[minUsableTick(spacing), maxUsableTick(spacing))`, each with the
|
|
2261
|
+
* active liquidity that holds across it.
|
|
2262
|
+
*
|
|
2263
|
+
* Throws `LiquidityCurveError` on inconsistent input (see error codes).
|
|
2264
|
+
*/
|
|
2265
|
+
declare function buildLiquidityCurve(params: BuildLiquidityCurveParams): LiquidityCurveSegment[];
|
|
2266
|
+
/**
|
|
2267
|
+
* Clip a curve to a tick viewport. Returns a new sub-range of `curve`
|
|
2268
|
+
* covering `[tickLo, tickHi]`, splitting segments that straddle the
|
|
2269
|
+
* viewport edges. Pure. O(log N) via binary search on `tickLower`.
|
|
2270
|
+
*
|
|
2271
|
+
* Returns `[]` if the viewport is entirely outside the curve's coverage.
|
|
2272
|
+
*/
|
|
2273
|
+
declare function clipCurveToTickRange(curve: LiquidityCurveSegment[], tickLo: number, tickHi: number): LiquidityCurveSegment[];
|
|
2274
|
+
|
|
2275
|
+
/** Bytes of calldata per Phase 1 / Phase 2 call:
|
|
2276
|
+
* 4 (selector) + 32 (single int16 or int24 arg, ABI-padded) = 36 bytes.
|
|
2277
|
+
* Update this constant if a wider arg is ever added. */
|
|
2278
|
+
declare const CALLDATA_BYTES_PER_CALL = 36;
|
|
2279
|
+
interface FetchAllInitializedTicksParams {
|
|
2280
|
+
client: PublicClient;
|
|
2281
|
+
pool: Address;
|
|
2282
|
+
/** Pin reads to this block. Default: latest at fetch start. */
|
|
2283
|
+
blockNumber?: bigint;
|
|
2284
|
+
/** Override pool.tickSpacing read. Saves 1 RPC if caller has it. */
|
|
2285
|
+
tickSpacing?: number;
|
|
2286
|
+
/** Maximum *calls* per HTTP eth_call sub-batch. Default 250.
|
|
2287
|
+
* Translated to viem's byte-oriented batchSize via `* CALLDATA_BYTES_PER_CALL`. */
|
|
2288
|
+
callsPerBatch?: number;
|
|
2289
|
+
/** Hard cap on returned tick count. Default 50_000. */
|
|
2290
|
+
maxTicks?: number;
|
|
2291
|
+
}
|
|
2292
|
+
interface FetchAllInitializedTicksResult {
|
|
2293
|
+
blockNumber: bigint;
|
|
2294
|
+
tickSpacing: number;
|
|
2295
|
+
/** Sorted ascending by `tick`. */
|
|
2296
|
+
ticks: InitializedTick[];
|
|
2297
|
+
}
|
|
2298
|
+
/**
|
|
2299
|
+
* Fetch every initialized tick of a V3 pool atomically via two-phase
|
|
2300
|
+
* multicall (`tickBitmap` → `ticks`), pinned to a single `blockNumber`.
|
|
2301
|
+
*/
|
|
2302
|
+
declare function fetchAllInitializedTicks(params: FetchAllInitializedTicksParams): Promise<FetchAllInitializedTicksResult>;
|
|
2303
|
+
|
|
2304
|
+
interface FetchLiquidityCurveParams {
|
|
2305
|
+
client: PublicClient;
|
|
2306
|
+
pool: Address;
|
|
2307
|
+
blockNumber?: bigint;
|
|
2308
|
+
/** Pre-fetched pool state. If provided, MUST have been observed at
|
|
2309
|
+
* `blockNumber` (caller's responsibility). When omitted, fetched here. */
|
|
2310
|
+
poolState?: PoolState;
|
|
2311
|
+
/** Forwarded to fetchAllInitializedTicks. */
|
|
2312
|
+
callsPerBatch?: FetchAllInitializedTicksParams["callsPerBatch"];
|
|
2313
|
+
maxTicks?: FetchAllInitializedTicksParams["maxTicks"];
|
|
2314
|
+
}
|
|
2315
|
+
interface FetchLiquidityCurveResult {
|
|
2316
|
+
blockNumber: bigint;
|
|
2317
|
+
poolState: PoolState;
|
|
2318
|
+
ticks: InitializedTick[];
|
|
2319
|
+
curve: LiquidityCurveSegment[];
|
|
2320
|
+
/** Index into `curve` of the segment containing `poolState.tick`.
|
|
2321
|
+
* Invariant: `curve[currentSegmentIndex].tickLower <= poolState.tick < curve[currentSegmentIndex].tickUpper`. */
|
|
2322
|
+
currentSegmentIndex: number;
|
|
2323
|
+
}
|
|
2324
|
+
/**
|
|
2325
|
+
* One-call convenience for the FE add-liquidity chart. Reads pool state,
|
|
2326
|
+
* fetches all initialized ticks, builds the curve — all pinned to the
|
|
2327
|
+
* same block.
|
|
2328
|
+
*
|
|
2329
|
+
* @throws {LiquidityCurveError} with `.code`:
|
|
2330
|
+
* - `TOO_MANY_TICKS` — pool has more than `maxTicks` initialized ticks
|
|
2331
|
+
* - `ATOMICITY_VIOLATION` — bitmap-vs-ticks() mismatch (rare; retry against latest)
|
|
2332
|
+
* - `CURVE_SUM_NONZERO` — tick set incomplete (caller passed a stale `poolState`)
|
|
2333
|
+
* - `CURVE_NEGATIVE_L` — cumulative liquidity went negative during walk
|
|
2334
|
+
* - `CURVE_EDGE_NONZERO` — `poolState` inconsistent with ticks (likely stale)
|
|
2335
|
+
*/
|
|
2336
|
+
declare function fetchLiquidityCurve(params: FetchLiquidityCurveParams): Promise<FetchLiquidityCurveResult>;
|
|
2337
|
+
|
|
2338
|
+
export { type AddLiquidityDirectParams, type AddLiquidityDirectResult, type ApiPerpDepositRequest, type ApiPerpDepositResponse, type ApiQuoteError, type ApiQuoteExactOutRequest, type ApiQuoteExactOutResponse, type ApiQuoteRequest, type ApiQuoteResponse, type ApiSwapExactOutRequest, type ApiSwapExactOutResponse, type ApiSwapRequest, type ApiSwapResponse, type BuildBurnParams, type BuildClosePositionParams, type BuildCollectParams, type BuildDecreaseLiquidityParams, type BuildIncreaseLiquidityParams, type BuildLiquidityCurveParams, type BuildMintParams, type BuildSwapUserOpExactOutParams, type BuildSwapUserOpParams, CALLDATA_BYTES_PER_CALL, type ClosePositionDirectParams, type ClosePositionDirectResult, type CollectFeesDirectParams, type CollectFeesDirectResult, type FetchAllInitializedTicksParams, type FetchAllInitializedTicksResult, type FetchLiquidityCurveParams, type FetchLiquidityCurveResult, type IncreaseLiquidityDirectParams, type IncreaseLiquidityDirectResult, type InitializedTick, LiquidityCurveError, LiquidityCurveErrorCode, type LiquidityCurveSegment, MAX_SQRT_RATIO, MAX_TICK, MIN_SQRT_RATIO, MIN_TICK, type PerpDepositDirectParams, type PerpDepositDirectResult, type PoolFeeGrowthGlobals, type PoolState, type PositionInfo, Q192, Q96, type RemoveLiquidityDirectParams, type RemoveLiquidityDirectResult, type SwapDirectExactOutParams, type SwapDirectExactOutResult, type SwapDirectParams, type SwapDirectResult, type SwapSimulationResult, type TickInfo, TradingHandlers, type TradingHandlersConfig, UINT128_MAX, V3_NPM_ADDRESSES, V3_SWAP_EXACT_IN, V3_SWAP_EXACT_OUT, addLiquidityDirect, applyMintSlippage, applyRemoveSlippage, bitmapWordRange, buildAllPaths, buildBurn, buildClosePosition, buildCollect, buildDecreaseLiquidity, buildErc20ApprovalCalldata, buildIncreaseLiquidity, buildLiquidityCurve, buildMint, buildPermit2ApprovalCalldata, buildSwapUserOp, buildSwapUserOpExactOut, buildUniversalRouterExecuteArgs, buildUniversalRouterExecuteArgsExactOut, buildV3SwapInputExactIn, buildV3SwapInputExactOut, checkAllowance, clipCurveToTickRange, closePositionDirect, collectFeesDirect, combineRoutes, decodeBitmapWord, estimateLiquidityFromOneSide, fetchAllInitializedTicks, fetchLiquidityCurve, findBestQuote, findBestQuoteExactOut, getAmount0ForLiquidity, getAmount1ForLiquidity, getAmountsForLiquidity, getLiquidityForAmount0, getLiquidityForAmount1, increaseLiquidityDirect, maxUsableTick, minUsableTick, nearestUsableTick, nonfungiblePositionManagerAbi, perpDepositDirect, priceToTick, quoteBestRoute, quoteBestRouteExactOut, quoteExactInput, quoteExactInputSingle, quoteExactOutput, quoteExactOutputSingle, readFeeGrowthGlobals, readPoolState, readPosition, readTicksAt, removeLiquidityDirect, simulateSwap, sqrtPriceX96ToTick, swapDirect, swapDirectExactOut, tickSpacingForFee, tickToBitmapPosition, tickToPrice, tickToSqrtPriceX96, v3PoolAbi };
|