@nradko/metric-omm-sdk-v1 0.4.2 → 0.4.4
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 +134 -19
- package/dist/abis/DepositAllowlistExtension.d.ts +43 -0
- package/dist/abis/DepositAllowlistExtension.d.ts.map +1 -1
- package/dist/abis/DepositAllowlistExtension.js +56 -0
- package/dist/abis/DepositAllowlistExtension.js.map +1 -1
- package/dist/abis/MetricOmmPoolFactory.d.ts +12 -0
- package/dist/abis/MetricOmmPoolFactory.d.ts.map +1 -1
- package/dist/abis/MetricOmmPoolFactory.js +16 -0
- package/dist/abis/MetricOmmPoolFactory.js.map +1 -1
- package/dist/abis/MetricOmmPoolLiquidityAdder.d.ts +116 -5
- package/dist/abis/MetricOmmPoolLiquidityAdder.d.ts.map +1 -1
- package/dist/abis/MetricOmmPoolLiquidityAdder.js +150 -5
- package/dist/abis/MetricOmmPoolLiquidityAdder.js.map +1 -1
- package/dist/abis/MetricOmmSimpleRouter.d.ts +32 -0
- package/dist/abis/MetricOmmSimpleRouter.d.ts.map +1 -1
- package/dist/abis/MetricOmmSimpleRouter.js +42 -0
- package/dist/abis/MetricOmmSimpleRouter.js.map +1 -1
- package/dist/abis/MetricOmmSwapQuoter.d.ts +14 -2
- package/dist/abis/MetricOmmSwapQuoter.d.ts.map +1 -1
- package/dist/abis/MetricOmmSwapQuoter.js +18 -2
- package/dist/abis/MetricOmmSwapQuoter.js.map +1 -1
- package/dist/abis/SwapAllowlistExtension.d.ts +43 -0
- package/dist/abis/SwapAllowlistExtension.d.ts.map +1 -1
- package/dist/abis/SwapAllowlistExtension.js +56 -0
- package/dist/abis/SwapAllowlistExtension.js.map +1 -1
- package/dist/abis/index.d.ts +2 -3
- package/dist/abis/index.d.ts.map +1 -1
- package/dist/abis/index.js +2 -3
- package/dist/abis/index.js.map +1 -1
- package/dist/addresses.d.ts +2 -4
- package/dist/addresses.d.ts.map +1 -1
- package/dist/addresses.js +11 -12
- package/dist/addresses.js.map +1 -1
- package/dist/dataProvider/index.d.ts +1 -1
- package/dist/dataProvider/index.d.ts.map +1 -1
- package/dist/dataProvider/index.js +1 -1
- package/dist/dataProvider/index.js.map +1 -1
- package/dist/dataProvider/read.d.ts +1 -3
- package/dist/dataProvider/read.d.ts.map +1 -1
- package/dist/dataProvider/read.js +1 -3
- package/dist/dataProvider/read.js.map +1 -1
- package/dist/index.d.ts +5 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -3
- package/dist/index.js.map +1 -1
- package/dist/peripheryPayments/index.d.ts +2 -0
- package/dist/peripheryPayments/index.d.ts.map +1 -0
- package/dist/peripheryPayments/index.js +2 -0
- package/dist/peripheryPayments/index.js.map +1 -0
- package/dist/peripheryPayments/simpleRouter.d.ts +9 -0
- package/dist/peripheryPayments/simpleRouter.d.ts.map +1 -0
- package/dist/peripheryPayments/simpleRouter.js +27 -0
- package/dist/peripheryPayments/simpleRouter.js.map +1 -0
- package/dist/pool/cursorBounds.d.ts +29 -0
- package/dist/pool/cursorBounds.d.ts.map +1 -0
- package/dist/pool/cursorBounds.js +76 -0
- package/dist/pool/cursorBounds.js.map +1 -0
- package/dist/pool/index.d.ts +3 -1
- package/dist/pool/index.d.ts.map +1 -1
- package/dist/pool/index.js +3 -1
- package/dist/pool/index.js.map +1 -1
- package/dist/pool/liquidity.d.ts +114 -7
- package/dist/pool/liquidity.d.ts.map +1 -1
- package/dist/pool/liquidity.js +96 -11
- package/dist/pool/liquidity.js.map +1 -1
- package/dist/pool/nativeLiquidity.d.ts +32 -0
- package/dist/pool/nativeLiquidity.d.ts.map +1 -0
- package/dist/pool/nativeLiquidity.js +47 -0
- package/dist/pool/nativeLiquidity.js.map +1 -0
- package/dist/router/fromRoute.d.ts +93 -0
- package/dist/router/fromRoute.d.ts.map +1 -0
- package/dist/router/fromRoute.js +187 -0
- package/dist/router/fromRoute.js.map +1 -0
- package/dist/router/index.d.ts +8 -1
- package/dist/router/index.d.ts.map +1 -1
- package/dist/router/index.js +8 -1
- package/dist/router/index.js.map +1 -1
- package/dist/router/nativeSwap.d.ts +47 -0
- package/dist/router/nativeSwap.d.ts.map +1 -0
- package/dist/router/nativeSwap.js +89 -0
- package/dist/router/nativeSwap.js.map +1 -0
- package/dist/router/swap.d.ts +82 -1
- package/dist/router/swap.d.ts.map +1 -1
- package/dist/router/swap.js +51 -1
- package/dist/router/swap.js.map +1 -1
- package/dist/router/swapPath.d.ts +46 -1
- package/dist/router/swapPath.d.ts.map +1 -1
- package/dist/router/swapPath.js +34 -0
- package/dist/router/swapPath.js.map +1 -1
- package/package.json +2 -2
- package/src/abis/DepositAllowlistExtension.ts +56 -0
- package/src/abis/MetricOmmPoolFactory.ts +16 -0
- package/src/abis/MetricOmmPoolLiquidityAdder.ts +150 -5
- package/src/abis/MetricOmmSimpleRouter.ts +42 -0
- package/src/abis/MetricOmmSwapQuoter.ts +18 -2
- package/src/abis/SwapAllowlistExtension.ts +56 -0
- package/src/abis/index.ts +2 -3
- package/src/addresses.ts +12 -15
- package/src/dataProvider/index.ts +0 -1
- package/src/dataProvider/read.ts +1 -4
- package/src/index.ts +38 -2
- package/src/peripheryPayments/index.ts +5 -0
- package/src/peripheryPayments/simpleRouter.ts +31 -0
- package/src/pool/cursorBounds.ts +112 -0
- package/src/pool/index.ts +15 -0
- package/src/pool/liquidity.ts +124 -15
- package/src/pool/nativeLiquidity.ts +71 -0
- package/src/router/fromRoute.ts +318 -0
- package/src/router/index.ts +36 -1
- package/src/router/nativeSwap.ts +123 -0
- package/src/router/swap.ts +82 -1
- package/src/router/swapPath.ts +47 -1
- package/dist/abis/MetricOmmPoolStateView.d.ts +0 -238
- package/dist/abis/MetricOmmPoolStateView.d.ts.map +0 -1
- package/dist/abis/MetricOmmPoolStateView.js +0 -315
- package/dist/abis/MetricOmmPoolStateView.js.map +0 -1
- package/src/abis/MetricOmmPoolStateView.ts +0 -314
|
@@ -73,6 +73,25 @@ export const SwapAllowlistExtensionAbi = [
|
|
|
73
73
|
name: "OnlyPoolAdmin",
|
|
74
74
|
type: "error",
|
|
75
75
|
},
|
|
76
|
+
{
|
|
77
|
+
anonymous: false,
|
|
78
|
+
inputs: [
|
|
79
|
+
{
|
|
80
|
+
indexed: true,
|
|
81
|
+
internalType: "address",
|
|
82
|
+
name: "pool",
|
|
83
|
+
type: "address",
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
indexed: false,
|
|
87
|
+
internalType: "bool",
|
|
88
|
+
name: "allowed",
|
|
89
|
+
type: "bool",
|
|
90
|
+
},
|
|
91
|
+
],
|
|
92
|
+
name: "AllowAllSwappersSet",
|
|
93
|
+
type: "event",
|
|
94
|
+
},
|
|
76
95
|
{
|
|
77
96
|
anonymous: false,
|
|
78
97
|
inputs: [
|
|
@@ -312,6 +331,25 @@ export const SwapAllowlistExtensionAbi = [
|
|
|
312
331
|
stateMutability: "nonpayable",
|
|
313
332
|
type: "function",
|
|
314
333
|
},
|
|
334
|
+
{
|
|
335
|
+
inputs: [
|
|
336
|
+
{
|
|
337
|
+
internalType: "address",
|
|
338
|
+
name: "pool",
|
|
339
|
+
type: "address",
|
|
340
|
+
},
|
|
341
|
+
],
|
|
342
|
+
name: "allowAllSwappers",
|
|
343
|
+
outputs: [
|
|
344
|
+
{
|
|
345
|
+
internalType: "bool",
|
|
346
|
+
name: "",
|
|
347
|
+
type: "bool",
|
|
348
|
+
},
|
|
349
|
+
],
|
|
350
|
+
stateMutability: "view",
|
|
351
|
+
type: "function",
|
|
352
|
+
},
|
|
315
353
|
{
|
|
316
354
|
inputs: [
|
|
317
355
|
{
|
|
@@ -545,6 +583,24 @@ export const SwapAllowlistExtensionAbi = [
|
|
|
545
583
|
stateMutability: "view",
|
|
546
584
|
type: "function",
|
|
547
585
|
},
|
|
586
|
+
{
|
|
587
|
+
inputs: [
|
|
588
|
+
{
|
|
589
|
+
internalType: "address",
|
|
590
|
+
name: "pool_",
|
|
591
|
+
type: "address",
|
|
592
|
+
},
|
|
593
|
+
{
|
|
594
|
+
internalType: "bool",
|
|
595
|
+
name: "allowed",
|
|
596
|
+
type: "bool",
|
|
597
|
+
},
|
|
598
|
+
],
|
|
599
|
+
name: "setAllowAllSwappers",
|
|
600
|
+
outputs: [],
|
|
601
|
+
stateMutability: "nonpayable",
|
|
602
|
+
type: "function",
|
|
603
|
+
},
|
|
548
604
|
{
|
|
549
605
|
inputs: [
|
|
550
606
|
{
|
package/src/abis/index.ts
CHANGED
|
@@ -4,14 +4,13 @@
|
|
|
4
4
|
|
|
5
5
|
export { MetricOmmPoolAbi } from "./MetricOmmPool.js";
|
|
6
6
|
export { MetricOmmPoolDataProviderAbi } from "./MetricOmmPoolDataProvider.js";
|
|
7
|
-
export { MetricOmmSwapQuoterAbi } from "./MetricOmmSwapQuoter.js";
|
|
8
|
-
export { MetricOmmPoolStateViewAbi } from "./MetricOmmPoolStateView.js";
|
|
9
7
|
export { MetricOmmSimpleRouterAbi } from "./MetricOmmSimpleRouter.js";
|
|
8
|
+
export { MetricOmmSwapQuoterAbi } from "./MetricOmmSwapQuoter.js";
|
|
10
9
|
export { MetricOmmPoolLiquidityAdderAbi } from "./MetricOmmPoolLiquidityAdder.js";
|
|
11
10
|
export { MetricOmmPoolFactoryAbi } from "./MetricOmmPoolFactory.js";
|
|
12
11
|
export { DepositAllowlistExtensionAbi } from "./DepositAllowlistExtension.js";
|
|
13
12
|
export { SwapAllowlistExtensionAbi } from "./SwapAllowlistExtension.js";
|
|
14
|
-
export { PriceVelocityGuardExtensionAbi } from "./PriceVelocityGuardExtension.js";
|
|
15
13
|
export { OracleValueStopLossExtensionAbi } from "./OracleValueStopLossExtension.js";
|
|
14
|
+
export { PriceVelocityGuardExtensionAbi } from "./PriceVelocityGuardExtension.js";
|
|
16
15
|
export { PriceProviderAbi } from "./PriceProvider.js";
|
|
17
16
|
export { OracleProviderAbi } from "./OracleProvider.js";
|
package/src/addresses.ts
CHANGED
|
@@ -38,9 +38,7 @@ export interface ChainAddresses {
|
|
|
38
38
|
liquidityAdder: Address;
|
|
39
39
|
/** MetricOmmPoolDataProvider (pool reads, swap depth, revert quotes). */
|
|
40
40
|
dataProvider: Address;
|
|
41
|
-
/**
|
|
42
|
-
stateView: Address;
|
|
43
|
-
/** MetricOmmSimpleRouter (single- and multi-hop ERC-20 swaps). */
|
|
41
|
+
/** MetricOmmSimpleRouter (single- and multi-hop swaps). */
|
|
44
42
|
simpleRouter: Address;
|
|
45
43
|
/** MetricOmmSwapQuoter (live pool quotes via revert callback). */
|
|
46
44
|
quoter: Address;
|
|
@@ -60,22 +58,21 @@ export interface ChainAddresses {
|
|
|
60
58
|
|
|
61
59
|
/** Shared protocol deployment (Ethereum + Base). */
|
|
62
60
|
const PROTOCOL_ADDRESSES = {
|
|
63
|
-
factory: "
|
|
64
|
-
poolDeployer: "
|
|
65
|
-
liquidityAdder: "
|
|
66
|
-
dataProvider: "
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
quoter: "0x0000000000000000000000000000000000000000",
|
|
61
|
+
factory: "0xf98aD80FE666B9a23075CdF64aF5971802844Fd8",
|
|
62
|
+
poolDeployer: "0xf93e1ea4516E99A4Ee81D4979C8D3C8Ab636e397",
|
|
63
|
+
liquidityAdder: "0xdd615506d83e2d94f30aaf17e846330164ab599e",
|
|
64
|
+
dataProvider: "0x7dc8eD16661A5003D9708B55f768484f9E963AdB",
|
|
65
|
+
simpleRouter: "0xa4d4be6a6b6d9f6532cacfe61fc666ef2f9997ed",
|
|
66
|
+
quoter: "0xfa13b57b570B43c8b9A61ba8D2C1781aB064f617",
|
|
70
67
|
extensions: {
|
|
71
|
-
depositAllowlistExtension: "
|
|
72
|
-
swapAllowlistExtension: "
|
|
73
|
-
oracleValueStopLossExtension: "
|
|
74
|
-
priceVelocityGuardExtension: "
|
|
68
|
+
depositAllowlistExtension: "0xC93BdA96021650DBfCEab6470c5329a0F391C160",
|
|
69
|
+
swapAllowlistExtension: "0x50c58A0bf9711Ae091c3aF5C3eab49544062662e",
|
|
70
|
+
oracleValueStopLossExtension: "0x34822E3D4265a8E42284Ce9807A1a804b7552838",
|
|
71
|
+
priceVelocityGuardExtension: "0x07A5c4821cd84a9A6162a6b1f756aA458B7616E3",
|
|
75
72
|
},
|
|
76
73
|
} as const satisfies Omit<ChainAddresses, "wrappedNative">;
|
|
77
74
|
|
|
78
|
-
/**
|
|
75
|
+
/** Periphery deployment — Ethereum mainnet and Base (CREATE2, same protocol addresses). */
|
|
79
76
|
export const ADDRESSES: Record<DeployedChainId, ChainAddresses> = {
|
|
80
77
|
[ChainId.ETHEREUM]: {
|
|
81
78
|
...PROTOCOL_ADDRESSES,
|
package/src/dataProvider/read.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* MetricAMM SDK - DataProvider read functions
|
|
3
|
-
* Batch reads via MetricOmmPoolDataProvider
|
|
3
|
+
* Batch reads via MetricOmmPoolDataProvider.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import type { Address, PublicClient } from "viem";
|
|
@@ -341,6 +341,3 @@ export async function getPriceProvider(
|
|
|
341
341
|
args: [poolAddress],
|
|
342
342
|
})) as Address;
|
|
343
343
|
}
|
|
344
|
-
|
|
345
|
-
/** @deprecated Use getPositionBinSharesFromDataProvider */
|
|
346
|
-
export const getPositionBinSharesFromStateView = getPositionBinSharesFromDataProvider;
|
package/src/index.ts
CHANGED
|
@@ -47,7 +47,6 @@ export {
|
|
|
47
47
|
MetricOmmPoolAbi,
|
|
48
48
|
MetricOmmPoolDataProviderAbi,
|
|
49
49
|
MetricOmmSwapQuoterAbi,
|
|
50
|
-
MetricOmmPoolStateViewAbi,
|
|
51
50
|
MetricOmmSimpleRouterAbi,
|
|
52
51
|
MetricOmmPoolLiquidityAdderAbi,
|
|
53
52
|
MetricOmmPoolFactoryAbi,
|
|
@@ -95,7 +94,6 @@ export {
|
|
|
95
94
|
getBinStatesScaled,
|
|
96
95
|
getBinTotalShares,
|
|
97
96
|
getPositionBinSharesFromDataProvider,
|
|
98
|
-
getPositionBinSharesFromStateView,
|
|
99
97
|
toPositionBinKey,
|
|
100
98
|
getPositionBinSharesRange,
|
|
101
99
|
getPriceProvider,
|
|
@@ -132,6 +130,25 @@ export {
|
|
|
132
130
|
type BinRemovalSpec,
|
|
133
131
|
} from "./pool/liquidity.js";
|
|
134
132
|
|
|
133
|
+
export {
|
|
134
|
+
prepareAddLiquidityExactSharesSelfSupportingNativePaymentCalldata,
|
|
135
|
+
prepareAddLiquidityExactSharesWithOwnerSupportingNativePaymentCalldata,
|
|
136
|
+
prepareAddLiquidityWeightedSelfSupportingNativePaymentCalldata,
|
|
137
|
+
prepareAddLiquidityWeightedWithOwnerSupportingNativePaymentCalldata,
|
|
138
|
+
UNCONSTRAINED_LIQUIDITY_CURSOR_BOUNDS,
|
|
139
|
+
type LiquidityCursorBounds,
|
|
140
|
+
prepareLiquidityCursorBounds,
|
|
141
|
+
computeLiquidityCursorBounds,
|
|
142
|
+
cursorMovementToPositionDelta,
|
|
143
|
+
} from "./pool/index.js";
|
|
144
|
+
|
|
145
|
+
// ============ PeripheryPayments ============
|
|
146
|
+
export {
|
|
147
|
+
encodeMulticallCalldata,
|
|
148
|
+
encodeRefundETHCalldata,
|
|
149
|
+
encodeUnwrapWETH9Calldata,
|
|
150
|
+
} from "./peripheryPayments/index.js";
|
|
151
|
+
|
|
135
152
|
// ============ Quoter Functions ============
|
|
136
153
|
export {
|
|
137
154
|
quoteLiveExactInSingle,
|
|
@@ -170,6 +187,14 @@ export {
|
|
|
170
187
|
prepareExactOutputSingleCalldata,
|
|
171
188
|
prepareExactInputCalldata,
|
|
172
189
|
prepareExactOutputCalldata,
|
|
190
|
+
prepareExactInputSingleAndUnwrapCalldata,
|
|
191
|
+
prepareExactInputAndUnwrapCalldata,
|
|
192
|
+
prepareExactInputSingleSupportingNativePaymentCalldata,
|
|
193
|
+
prepareExactInputSupportingNativePaymentCalldata,
|
|
194
|
+
prepareExactOutputSupportingNativePaymentCalldata,
|
|
195
|
+
prepareExactOutputSingleSupportingNativePaymentCalldata,
|
|
196
|
+
prepareExactOutputSingleAndUnwrapCalldata,
|
|
197
|
+
prepareExactOutputAndUnwrapCalldata,
|
|
173
198
|
type ExactInputSingleParams,
|
|
174
199
|
type ExactOutputSingleParams,
|
|
175
200
|
type ExactInputParams,
|
|
@@ -178,6 +203,17 @@ export {
|
|
|
178
203
|
type BuildExactOutputSingleParams,
|
|
179
204
|
type BuildExactInputParams,
|
|
180
205
|
type BuildExactOutputParams,
|
|
206
|
+
prepareExactInputFromRoute,
|
|
207
|
+
prepareExactOutputFromRoute,
|
|
208
|
+
type TradeType,
|
|
209
|
+
type RouteType,
|
|
210
|
+
type RouteLegResponse,
|
|
211
|
+
type RouteOptionResponse,
|
|
212
|
+
type RouteResponse,
|
|
213
|
+
type PrepareExactInputFromRouteParams,
|
|
214
|
+
type PrepareExactOutputFromRouteParams,
|
|
215
|
+
type PreparedExactInputFromRoute,
|
|
216
|
+
type PreparedExactOutputFromRoute,
|
|
181
217
|
} from "./router/index.js";
|
|
182
218
|
|
|
183
219
|
// ============ Factory Functions ============
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** MetricOmmSimpleRouter multicall and PeripheryPayments calldata encoders. */
|
|
2
|
+
|
|
3
|
+
import type { Address, Hex } from "viem";
|
|
4
|
+
import { encodeFunctionData } from "viem";
|
|
5
|
+
import { SIMPLE_ROUTER_ABI } from "../constants.js";
|
|
6
|
+
|
|
7
|
+
/** `multicall(bytes[])`. */
|
|
8
|
+
export function encodeMulticallCalldata(calls: readonly Hex[]): Hex {
|
|
9
|
+
return encodeFunctionData({
|
|
10
|
+
abi: SIMPLE_ROUTER_ABI,
|
|
11
|
+
functionName: "multicall",
|
|
12
|
+
args: [calls],
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** `refundETH()` — unused native balance on the router back to caller. */
|
|
17
|
+
export function encodeRefundETHCalldata(): Hex {
|
|
18
|
+
return encodeFunctionData({
|
|
19
|
+
abi: SIMPLE_ROUTER_ABI,
|
|
20
|
+
functionName: "refundETH",
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** `unwrapWETH9(amountMinimum, recipient)`. */
|
|
25
|
+
export function encodeUnwrapWETH9Calldata(amountMinimum: bigint, recipient: Address): Hex {
|
|
26
|
+
return encodeFunctionData({
|
|
27
|
+
abi: SIMPLE_ROUTER_ABI,
|
|
28
|
+
functionName: "unwrapWETH9",
|
|
29
|
+
args: [amountMinimum, recipient],
|
|
30
|
+
});
|
|
31
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helpers for weighted-add LiquidityAdder cursor bounds.
|
|
3
|
+
*
|
|
4
|
+
* Cursor position is measured in linear units: each bin spans MAX_UINT104 + 1
|
|
5
|
+
* positions (0 … MAX_UINT104). Movement can cross bin boundaries; distance is
|
|
6
|
+
* summed across bins.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { Address, PublicClient } from "viem";
|
|
10
|
+
import { MAX_UINT104 } from "../constants.js";
|
|
11
|
+
import { getSlot0 } from "../dataProvider/read.js";
|
|
12
|
+
import { getAddressesOrThrow } from "../addresses.js";
|
|
13
|
+
import { UNCONSTRAINED_LIQUIDITY_CURSOR_BOUNDS, type LiquidityCursorBounds } from "./liquidity.js";
|
|
14
|
+
|
|
15
|
+
const POSITIONS_PER_BIN = MAX_UINT104 + 1n;
|
|
16
|
+
const CURSOR_MOVEMENT_SCALE = 1_000_000_000n;
|
|
17
|
+
|
|
18
|
+
function floorDiv(a: bigint, b: bigint): bigint {
|
|
19
|
+
const q = a / b;
|
|
20
|
+
const r = a % b;
|
|
21
|
+
if (r !== 0n && a < 0n) return q - 1n;
|
|
22
|
+
return q;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function floorMod(a: bigint, b: bigint): bigint {
|
|
26
|
+
return a - floorDiv(a, b) * b;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function cursorToLinear(curBinIdx: number, curPosInBin: bigint): bigint {
|
|
30
|
+
return BigInt(curBinIdx) * POSITIONS_PER_BIN + curPosInBin;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function linearToCursor(linear: bigint): { curBinIdx: number; curPosInBin: bigint } {
|
|
34
|
+
return {
|
|
35
|
+
curBinIdx: Number(floorDiv(linear, POSITIONS_PER_BIN)),
|
|
36
|
+
curPosInBin: floorMod(linear, POSITIONS_PER_BIN),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Convert a movement factor to position units (1.0 = MAX_UINT104 positions). */
|
|
41
|
+
export function cursorMovementToPositionDelta(maximalCursorMovement: number): bigint {
|
|
42
|
+
if (!Number.isFinite(maximalCursorMovement) || maximalCursorMovement < 0) {
|
|
43
|
+
throw new Error("maximalCursorMovement must be a non-negative finite number");
|
|
44
|
+
}
|
|
45
|
+
const scaledMovement = BigInt(Math.round(maximalCursorMovement * Number(CURSOR_MOVEMENT_SCALE)));
|
|
46
|
+
return (scaledMovement * MAX_UINT104) / CURSOR_MOVEMENT_SCALE;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Build LiquidityCursorBounds around a cursor by allowing up to
|
|
51
|
+
* `maximalCursorMovement * MAX_UINT104` positions of movement left or right.
|
|
52
|
+
*/
|
|
53
|
+
export function computeLiquidityCursorBounds(
|
|
54
|
+
curBinIdx: number,
|
|
55
|
+
curPosInBin: bigint,
|
|
56
|
+
maximalCursorMovement: number,
|
|
57
|
+
): LiquidityCursorBounds {
|
|
58
|
+
const delta = cursorMovementToPositionDelta(maximalCursorMovement);
|
|
59
|
+
const currentLinear = cursorToLinear(curBinIdx, curPosInBin);
|
|
60
|
+
|
|
61
|
+
const protocolMinLinear = cursorToLinear(
|
|
62
|
+
UNCONSTRAINED_LIQUIDITY_CURSOR_BOUNDS.minimalCurBin,
|
|
63
|
+
UNCONSTRAINED_LIQUIDITY_CURSOR_BOUNDS.minimalPosition,
|
|
64
|
+
);
|
|
65
|
+
const protocolMaxLinear = cursorToLinear(
|
|
66
|
+
UNCONSTRAINED_LIQUIDITY_CURSOR_BOUNDS.maximalCurBin,
|
|
67
|
+
UNCONSTRAINED_LIQUIDITY_CURSOR_BOUNDS.maximalPosition,
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
const clampedMinLinear =
|
|
71
|
+
currentLinear - delta < protocolMinLinear ? protocolMinLinear : currentLinear - delta;
|
|
72
|
+
const clampedMaxLinear =
|
|
73
|
+
currentLinear + delta > protocolMaxLinear ? protocolMaxLinear : currentLinear + delta;
|
|
74
|
+
|
|
75
|
+
const minimal = linearToCursor(clampedMinLinear);
|
|
76
|
+
const maximal = linearToCursor(clampedMaxLinear);
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
minimalCurBin: minimal.curBinIdx,
|
|
80
|
+
minimalPosition: minimal.curPosInBin,
|
|
81
|
+
maximalCurBin: maximal.curBinIdx,
|
|
82
|
+
maximalPosition: maximal.curPosInBin,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Read the pool cursor and return LiquidityCursorBounds for weighted add encoders.
|
|
88
|
+
*
|
|
89
|
+
* @param maximalCursorMovement - 1.0 allows up to MAX_UINT104 positions of movement
|
|
90
|
+
* left or right from the current cursor (summing across bin boundaries).
|
|
91
|
+
*/
|
|
92
|
+
export async function prepareLiquidityCursorBounds(params: {
|
|
93
|
+
publicClient: PublicClient;
|
|
94
|
+
pool: Address;
|
|
95
|
+
maximalCursorMovement: number;
|
|
96
|
+
dataProviderAddress?: Address;
|
|
97
|
+
}): Promise<LiquidityCursorBounds> {
|
|
98
|
+
const chainId = params.publicClient.chain?.id;
|
|
99
|
+
if (chainId === undefined) {
|
|
100
|
+
throw new Error("publicClient.chain.id is required to resolve dataProvider address");
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const dataProviderAddress =
|
|
104
|
+
params.dataProviderAddress ?? getAddressesOrThrow(chainId).dataProvider;
|
|
105
|
+
const slot0 = await getSlot0(params.publicClient, dataProviderAddress, params.pool);
|
|
106
|
+
|
|
107
|
+
return computeLiquidityCursorBounds(
|
|
108
|
+
slot0.curBinIdx,
|
|
109
|
+
slot0.curPosInBin,
|
|
110
|
+
params.maximalCursorMovement,
|
|
111
|
+
);
|
|
112
|
+
}
|
package/src/pool/index.ts
CHANGED
|
@@ -39,6 +39,8 @@ export {
|
|
|
39
39
|
encodeAddLiquidityWeightedSelfCalldata,
|
|
40
40
|
encodeAddLiquidityWeightedWithOwnerCalldata,
|
|
41
41
|
encodeRemoveLiquidityCalldata,
|
|
42
|
+
UNCONSTRAINED_LIQUIDITY_CURSOR_BOUNDS,
|
|
43
|
+
type LiquidityCursorBounds,
|
|
42
44
|
getPositionShares,
|
|
43
45
|
getPositionSharesForBins,
|
|
44
46
|
NO_SLIPPAGE_LIMIT,
|
|
@@ -52,3 +54,16 @@ export {
|
|
|
52
54
|
type RemoveLiquidityParams,
|
|
53
55
|
type BinRemovalSpec,
|
|
54
56
|
} from "./liquidity.js";
|
|
57
|
+
|
|
58
|
+
export {
|
|
59
|
+
prepareAddLiquidityExactSharesSelfSupportingNativePaymentCalldata,
|
|
60
|
+
prepareAddLiquidityExactSharesWithOwnerSupportingNativePaymentCalldata,
|
|
61
|
+
prepareAddLiquidityWeightedSelfSupportingNativePaymentCalldata,
|
|
62
|
+
prepareAddLiquidityWeightedWithOwnerSupportingNativePaymentCalldata,
|
|
63
|
+
} from "./nativeLiquidity.js";
|
|
64
|
+
|
|
65
|
+
export {
|
|
66
|
+
prepareLiquidityCursorBounds,
|
|
67
|
+
computeLiquidityCursorBounds,
|
|
68
|
+
cursorMovementToPositionDelta,
|
|
69
|
+
} from "./cursorBounds.js";
|