@juiceswapxyz/v4-sdk 0.0.1-beta.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Uniswap Labs
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,169 @@
1
+ import { BigintIsh, Percent, NativeCurrency } from '@juiceswapxyz/sdk-core';
2
+ import { TypedDataDomain, TypedDataField } from '@ethersproject/abstract-signer';
3
+ import { Position } from './entities/position';
4
+ import { MethodParameters } from './utils/calldata';
5
+ import { Interface } from '@ethersproject/abi';
6
+ import { PoolKey } from './entities';
7
+ export interface CommonOptions {
8
+ /**
9
+ * How much the pool price is allowed to move from the specified action.
10
+ */
11
+ slippageTolerance: Percent;
12
+ /**
13
+ * Optional data to pass to hooks
14
+ */
15
+ hookData?: string;
16
+ /**
17
+ * When the transaction expires, in epoch seconds.
18
+ */
19
+ deadline: BigintIsh;
20
+ }
21
+ export interface ModifyPositionSpecificOptions {
22
+ /**
23
+ * Indicates the ID of the position to increase liquidity for.
24
+ */
25
+ tokenId: BigintIsh;
26
+ }
27
+ export interface MintSpecificOptions {
28
+ /**
29
+ * The account that should receive the minted NFT.
30
+ */
31
+ recipient: string;
32
+ /**
33
+ * Creates pool if not initialized before mint.
34
+ */
35
+ createPool?: boolean;
36
+ /**
37
+ * Initial price to set on the pool if creating
38
+ */
39
+ sqrtPriceX96?: BigintIsh;
40
+ /**
41
+ * Whether the mint is part of a migration from V3 to V4.
42
+ */
43
+ migrate?: boolean;
44
+ }
45
+ /**
46
+ * Options for producing the calldata to add liquidity.
47
+ */
48
+ export interface CommonAddLiquidityOptions {
49
+ /**
50
+ * Whether to spend ether. If true, one of the currencies must be the NATIVE currency.
51
+ */
52
+ useNative?: NativeCurrency;
53
+ /**
54
+ * The optional permit2 batch permit parameters for spending token0 and token1
55
+ */
56
+ batchPermit?: BatchPermitOptions;
57
+ }
58
+ /**
59
+ * Options for producing the calldata to exit a position.
60
+ */
61
+ export interface RemoveLiquiditySpecificOptions {
62
+ /**
63
+ * The percentage of position liquidity to exit.
64
+ */
65
+ liquidityPercentage: Percent;
66
+ /**
67
+ * Whether the NFT should be burned if the entire position is being exited, by default false.
68
+ */
69
+ burnToken?: boolean;
70
+ /**
71
+ * The optional permit of the token ID being exited, in case the exit transaction is being sent by an account that does not own the NFT
72
+ */
73
+ permit?: NFTPermitOptions;
74
+ }
75
+ export interface CollectSpecificOptions {
76
+ /**
77
+ * Indicates the ID of the position to collect for.
78
+ */
79
+ tokenId: BigintIsh;
80
+ /**
81
+ * The account that should receive the tokens.
82
+ */
83
+ recipient: string;
84
+ }
85
+ export interface TransferOptions {
86
+ /**
87
+ * The account sending the NFT.
88
+ */
89
+ sender: string;
90
+ /**
91
+ * The account that should receive the NFT.
92
+ */
93
+ recipient: string;
94
+ /**
95
+ * The id of the token being sent.
96
+ */
97
+ tokenId: BigintIsh;
98
+ }
99
+ export interface PermitDetails {
100
+ token: string;
101
+ amount: BigintIsh;
102
+ expiration: BigintIsh;
103
+ nonce: BigintIsh;
104
+ }
105
+ export interface AllowanceTransferPermitSingle {
106
+ details: PermitDetails;
107
+ spender: string;
108
+ sigDeadline: BigintIsh;
109
+ }
110
+ export interface AllowanceTransferPermitBatch {
111
+ details: PermitDetails[];
112
+ spender: string;
113
+ sigDeadline: BigintIsh;
114
+ }
115
+ export interface BatchPermitOptions {
116
+ owner: string;
117
+ permitBatch: AllowanceTransferPermitBatch;
118
+ signature: string;
119
+ }
120
+ export interface NFTPermitValues {
121
+ spender: string;
122
+ tokenId: BigintIsh;
123
+ deadline: BigintIsh;
124
+ nonce: BigintIsh;
125
+ }
126
+ export interface NFTPermitOptions extends NFTPermitValues {
127
+ signature: string;
128
+ }
129
+ export interface NFTPermitData {
130
+ domain: TypedDataDomain;
131
+ types: Record<string, TypedDataField[]>;
132
+ values: NFTPermitValues;
133
+ }
134
+ export declare type MintOptions = CommonOptions & CommonAddLiquidityOptions & MintSpecificOptions;
135
+ export declare type IncreaseLiquidityOptions = CommonOptions & CommonAddLiquidityOptions & ModifyPositionSpecificOptions;
136
+ export declare type AddLiquidityOptions = MintOptions | IncreaseLiquidityOptions;
137
+ export declare type RemoveLiquidityOptions = CommonOptions & RemoveLiquiditySpecificOptions & ModifyPositionSpecificOptions;
138
+ export declare type CollectOptions = CommonOptions & CollectSpecificOptions;
139
+ export declare abstract class V4PositionManager {
140
+ static INTERFACE: Interface;
141
+ /**
142
+ * Cannot be constructed.
143
+ */
144
+ private constructor();
145
+ /**
146
+ * Public methods to encode method parameters for different actions on the PositionManager contract
147
+ */
148
+ static createCallParameters(poolKey: PoolKey, sqrtPriceX96: BigintIsh): MethodParameters;
149
+ static addCallParameters(position: Position, options: AddLiquidityOptions): MethodParameters;
150
+ /**
151
+ * Produces the calldata for completely or partially exiting a position
152
+ * @param position The position to exit
153
+ * @param options Additional information necessary for generating the calldata
154
+ * @returns The call parameters
155
+ */
156
+ static removeCallParameters(position: Position, options: RemoveLiquidityOptions): MethodParameters;
157
+ /**
158
+ * Produces the calldata for collecting fees from a position
159
+ * @param position The position to collect fees from
160
+ * @param options Additional information necessary for generating the calldata
161
+ * @returns The call parameters
162
+ */
163
+ static collectCallParameters(position: Position, options: CollectOptions): MethodParameters;
164
+ private static encodeInitializePool;
165
+ static encodeModifyLiquidities(unlockData: string, deadline: BigintIsh): string;
166
+ static encodePermitBatch(owner: string, permitBatch: AllowanceTransferPermitBatch, signature: string): string;
167
+ static encodeERC721Permit(spender: string, tokenId: BigintIsh, deadline: BigintIsh, nonce: BigintIsh, signature: string): string;
168
+ static getPermitData(permit: NFTPermitValues, positionManagerAddress: string, chainId: number): NFTPermitData;
169
+ }
@@ -0,0 +1 @@
1
+ export declare const MSG_SENDER = "0x0000000000000000000000000000000000000001";
@@ -0,0 +1,4 @@
1
+ export * from './pool';
2
+ export * from './route';
3
+ export * from './trade';
4
+ export * from './position';
@@ -0,0 +1,103 @@
1
+ import { BigintIsh, Currency, CurrencyAmount, Price } from '@juiceswapxyz/sdk-core';
2
+ import { Tick, TickConstructorArgs, TickDataProvider } from '@juiceswapxyz/v3-sdk';
3
+ import JSBI from 'jsbi';
4
+ export declare const DYNAMIC_FEE_FLAG = 8388608;
5
+ export declare type PoolKey = {
6
+ currency0: string;
7
+ currency1: string;
8
+ fee: number;
9
+ tickSpacing: number;
10
+ hooks: string;
11
+ };
12
+ /**
13
+ * Represents a V4 pool
14
+ */
15
+ export declare class Pool {
16
+ readonly currency0: Currency;
17
+ readonly currency1: Currency;
18
+ readonly fee: number;
19
+ readonly tickSpacing: number;
20
+ readonly sqrtRatioX96: JSBI;
21
+ readonly hooks: string;
22
+ readonly liquidity: JSBI;
23
+ readonly tickCurrent: number;
24
+ readonly tickDataProvider: TickDataProvider;
25
+ readonly poolKey: PoolKey;
26
+ readonly poolId: string;
27
+ private _currency0Price?;
28
+ private _currency1Price?;
29
+ static getPoolKey(currencyA: Currency, currencyB: Currency, fee: number, tickSpacing: number, hooks: string): PoolKey;
30
+ static getPoolId(currencyA: Currency, currencyB: Currency, fee: number, tickSpacing: number, hooks: string): string;
31
+ /**
32
+ * Construct a pool
33
+ * @param currencyA One of the currencys in the pool
34
+ * @param currencyB The other currency in the pool
35
+ * @param fee The fee in hundredths of a bips of the input amount of every swap that is collected by the pool
36
+ * @param tickSpacing The tickSpacing of the pool
37
+ * @param hooks The address of the hook contract
38
+ * @param sqrtRatioX96 The sqrt of the current ratio of amounts of currency1 to currency0
39
+ * @param liquidity The current value of in range liquidity
40
+ * @param tickCurrent The current tick of the pool
41
+ */
42
+ constructor(currencyA: Currency, currencyB: Currency, fee: number, tickSpacing: number, hooks: string, sqrtRatioX96: BigintIsh, liquidity: BigintIsh, tickCurrent: number, ticks?: TickDataProvider | (Tick | TickConstructorArgs)[]);
43
+ /** backwards compatibility with v2/3 sdks */
44
+ get token0(): Currency;
45
+ get token1(): Currency;
46
+ /**
47
+ * Returns true if the currency is either currency0 or currency1
48
+ * @param currency The currency to check
49
+ * @returns True if currency is either currency0 or currency1
50
+ */
51
+ involvesCurrency(currency: Currency): boolean;
52
+ /** backwards compatibility with v2/3 sdks */
53
+ involvesToken(currency: Currency): boolean;
54
+ /**
55
+ * v4-only involvesToken convenience method, used for mixed route ETH <-> WETH connection only
56
+ * @param currency
57
+ */
58
+ v4InvolvesToken(currency: Currency): boolean;
59
+ /**
60
+ * Returns the current mid price of the pool in terms of currency0, i.e. the ratio of currency1 over currency0
61
+ */
62
+ get currency0Price(): Price<Currency, Currency>;
63
+ /** backwards compatibility with v2/3 sdks */
64
+ get token0Price(): Price<Currency, Currency>;
65
+ /**
66
+ * Returns the current mid price of the pool in terms of currency1, i.e. the ratio of currency0 over currency1
67
+ */
68
+ get currency1Price(): Price<Currency, Currency>;
69
+ /** backwards compatibility with v2/3 sdks */
70
+ get token1Price(): Price<Currency, Currency>;
71
+ /**
72
+ * Return the price of the given currency in terms of the other currency in the pool.
73
+ * @param currency The currency to return price of
74
+ * @returns The price of the given currency, in terms of the other.
75
+ */
76
+ priceOf(currency: Currency): Price<Currency, Currency>;
77
+ /**
78
+ * Returns the chain ID of the currencies in the pool.
79
+ */
80
+ get chainId(): number;
81
+ /** Works only for vanilla hookless v3 pools, otherwise throws an error */
82
+ getOutputAmount(inputAmount: CurrencyAmount<Currency>, sqrtPriceLimitX96?: JSBI): Promise<[CurrencyAmount<Currency>, Pool]>;
83
+ /**
84
+ * Given a desired output amount of a currency, return the computed input amount and a pool with state updated after the trade
85
+ * Works only for vanilla hookless v3 pools, otherwise throws an error
86
+ * @param outputAmount the output amount for which to quote the input amount
87
+ * @param sqrtPriceLimitX96 The Q64.96 sqrt price limit. If zero for one, the price cannot be less than this value after the swap. If one for zero, the price cannot be greater than this value after the swap
88
+ * @returns The input amount and the pool with updated state
89
+ */
90
+ getInputAmount(outputAmount: CurrencyAmount<Currency>, sqrtPriceLimitX96?: JSBI): Promise<[CurrencyAmount<Currency>, Pool]>;
91
+ /**
92
+ * Executes a swap
93
+ * @param zeroForOne Whether the amount in is token0 or token1
94
+ * @param amountSpecified The amount of the swap, which implicitly configures the swap as exact input (positive), or exact output (negative)
95
+ * @param sqrtPriceLimitX96 The Q64.96 sqrt price limit. If zero for one, the price cannot be less than this value after the swap. If one for zero, the price cannot be greater than this value after the swap
96
+ * @returns amountCalculated
97
+ * @returns sqrtRatioX96
98
+ * @returns liquidity
99
+ * @returns tickCurrent
100
+ */
101
+ private swap;
102
+ private hookImpactsSwap;
103
+ }
@@ -0,0 +1,144 @@
1
+ import { BigintIsh, Percent, Price, CurrencyAmount, Currency } from '@juiceswapxyz/sdk-core';
2
+ import JSBI from 'jsbi';
3
+ import { Pool } from './pool';
4
+ import { AllowanceTransferPermitBatch } from '../PositionManager';
5
+ interface PositionConstructorArgs {
6
+ pool: Pool;
7
+ liquidity: BigintIsh;
8
+ tickLower: number;
9
+ tickUpper: number;
10
+ }
11
+ /**
12
+ * Represents a position on a Uniswap V4 Pool
13
+ * @dev Similar to the V3 implementation
14
+ * - using Currency instead of Token
15
+ * - keep in mind that Pool and liquidity must be fetched from the pool manager
16
+ */
17
+ export declare class Position {
18
+ readonly pool: Pool;
19
+ readonly tickLower: number;
20
+ readonly tickUpper: number;
21
+ readonly liquidity: JSBI;
22
+ private _token0Amount;
23
+ private _token1Amount;
24
+ private _mintAmounts;
25
+ /**
26
+ * Constructs a position for a given pool with the given liquidity
27
+ * @param pool For which pool the liquidity is assigned
28
+ * @param liquidity The amount of liquidity that is in the position
29
+ * @param tickLower The lower tick of the position
30
+ * @param tickUpper The upper tick of the position
31
+ */
32
+ constructor({ pool, liquidity, tickLower, tickUpper }: PositionConstructorArgs);
33
+ /**
34
+ * Returns the price of token0 at the lower tick
35
+ */
36
+ get token0PriceLower(): Price<Currency, Currency>;
37
+ /**
38
+ * Returns the price of token0 at the upper tick
39
+ */
40
+ get token0PriceUpper(): Price<Currency, Currency>;
41
+ /**
42
+ * Returns the amount of token0 that this position's liquidity could be burned for at the current pool price
43
+ */
44
+ get amount0(): CurrencyAmount<Currency>;
45
+ /**
46
+ * Returns the amount of token1 that this position's liquidity could be burned for at the current pool price
47
+ */
48
+ get amount1(): CurrencyAmount<Currency>;
49
+ /**
50
+ * Returns the lower and upper sqrt ratios if the price 'slips' up to slippage tolerance percentage
51
+ * @param slippageTolerance The amount by which the price can 'slip' before the transaction will revert
52
+ * @returns The sqrt ratios after slippage
53
+ */
54
+ private ratiosAfterSlippage;
55
+ /**
56
+ * Returns the maximum amount of token0 and token1 that must be sent in order to safely mint the amount of liquidity held by the position
57
+ * with the given slippage tolerance
58
+ * @param slippageTolerance Tolerance of unfavorable slippage from the current price
59
+ * @returns The amounts, with slippage
60
+ * @dev In v4, minting and increasing is protected by maximum amounts of token0 and token1.
61
+ */
62
+ mintAmountsWithSlippage(slippageTolerance: Percent): Readonly<{
63
+ amount0: JSBI;
64
+ amount1: JSBI;
65
+ }>;
66
+ /**
67
+ * Returns the minimum amounts that should be requested in order to safely burn the amount of liquidity held by the
68
+ * position with the given slippage tolerance
69
+ * @param slippageTolerance tolerance of unfavorable slippage from the current price
70
+ * @returns The amounts, with slippage
71
+ */
72
+ burnAmountsWithSlippage(slippageTolerance: Percent): Readonly<{
73
+ amount0: JSBI;
74
+ amount1: JSBI;
75
+ }>;
76
+ /**
77
+ * Returns the minimum amounts that must be sent in order to mint the amount of liquidity held by the position at
78
+ * the current price for the pool
79
+ */
80
+ get mintAmounts(): Readonly<{
81
+ amount0: JSBI;
82
+ amount1: JSBI;
83
+ }>;
84
+ /**
85
+ * Returns the AllowanceTransferPermitBatch for adding liquidity to a position
86
+ * @param slippageTolerance The amount by which the price can 'slip' before the transaction will revert
87
+ * @param spender The spender of the permit (should usually be the PositionManager)
88
+ * @param nonce A valid permit2 nonce
89
+ * @param deadline The deadline for the permit
90
+ */
91
+ permitBatchData(slippageTolerance: Percent, spender: string, nonce: BigintIsh, deadline: BigintIsh): AllowanceTransferPermitBatch;
92
+ /**
93
+ * Computes the maximum amount of liquidity received for a given amount of token0, token1,
94
+ * and the prices at the tick boundaries.
95
+ * @param pool The pool for which the position should be created
96
+ * @param tickLower The lower tick of the position
97
+ * @param tickUpper The upper tick of the position
98
+ * @param amount0 token0 amountzw
99
+ * @param amount1 token1 amount
100
+ * @param useFullPrecision If false, liquidity will be maximized according to what the router can calculate,
101
+ * not what core can theoretically support
102
+ * @returns The amount of liquidity for the position
103
+ */
104
+ static fromAmounts({ pool, tickLower, tickUpper, amount0, amount1, useFullPrecision, }: {
105
+ pool: Pool;
106
+ tickLower: number;
107
+ tickUpper: number;
108
+ amount0: BigintIsh;
109
+ amount1: BigintIsh;
110
+ useFullPrecision: boolean;
111
+ }): Position;
112
+ /**
113
+ * Computes a position with the maximum amount of liquidity received for a given amount of token0, assuming an unlimited amount of token1
114
+ * @param pool The pool for which the position is created
115
+ * @param tickLower The lower tick
116
+ * @param tickUpper The upper tick
117
+ * @param amount0 The desired amount of token0
118
+ * @param useFullPrecision If true, liquidity will be maximized according to what the router can calculate,
119
+ * not what core can theoretically support
120
+ * @returns The position
121
+ */
122
+ static fromAmount0({ pool, tickLower, tickUpper, amount0, useFullPrecision, }: {
123
+ pool: Pool;
124
+ tickLower: number;
125
+ tickUpper: number;
126
+ amount0: BigintIsh;
127
+ useFullPrecision: boolean;
128
+ }): Position;
129
+ /**
130
+ * Computes a position with the maximum amount of liquidity received for a given amount of token1, assuming an unlimited amount of token0
131
+ * @param pool The pool for which the position is created
132
+ * @param tickLower The lower tick
133
+ * @param tickUpper The upper tick
134
+ * @param amount1 The desired amount of token1
135
+ * @returns The position
136
+ */
137
+ static fromAmount1({ pool, tickLower, tickUpper, amount1, }: {
138
+ pool: Pool;
139
+ tickLower: number;
140
+ tickUpper: number;
141
+ amount1: BigintIsh;
142
+ }): Position;
143
+ }
144
+ export {};
@@ -0,0 +1,28 @@
1
+ import { Currency, Price } from '@juiceswapxyz/sdk-core';
2
+ import { Pool } from './pool';
3
+ /**
4
+ * Represents a list of pools through which a swap can occur
5
+ * @template TInput The input currency
6
+ * @template TOutput The output currency
7
+ */
8
+ export declare class Route<TInput extends Currency, TOutput extends Currency> {
9
+ readonly pools: Pool[];
10
+ readonly currencyPath: Currency[];
11
+ readonly input: TInput;
12
+ readonly output: TOutput;
13
+ readonly pathInput: Currency;
14
+ readonly pathOutput: Currency;
15
+ private _midPrice;
16
+ /**
17
+ * Creates an instance of route.
18
+ * @param pools An array of `Pool` objects, ordered by the route the swap will take
19
+ * @param input The input currency
20
+ * @param output The output currency
21
+ */
22
+ constructor(pools: Pool[], input: TInput, output: TOutput);
23
+ get chainId(): number;
24
+ /**
25
+ * Returns the mid price of the route
26
+ */
27
+ get midPrice(): Price<TInput, TOutput>;
28
+ }