|
9 | 9 | Position,
|
10 | 10 | toHex,
|
11 | 11 | } from '@uniswap/v3-sdk'
|
| 12 | +import JSBI from 'jsbi' |
12 | 13 |
|
13 | 14 | // condensed version of v3-sdk AddLiquidityOptions containing only necessary swap + add attributes
|
14 | 15 | export type CondensedAddLiquidityOptions = Omit<MintSpecificOptions, 'createPool'> | IncreaseSpecificOptions
|
@@ -60,13 +61,30 @@ export abstract class ApproveAndCall {
|
60 | 61 | return ApproveAndCall.INTERFACE.encodeFunctionData('callPositionManager', [encodedMulticall])
|
61 | 62 | }
|
62 | 63 | }
|
63 |
| - |
| 64 | + /** |
| 65 | + * Encode adding liquidity to a position in the nft manager contract |
| 66 | + * @param position Forcasted position with expected amount out from swap |
| 67 | + * @param minimalPosition Forcasted position with custom minimal token amounts |
| 68 | + * @param addLiquidityOptions Options for adding liquidity |
| 69 | + * @param slippageTolerance Defines maximum slippage |
| 70 | + */ |
64 | 71 | public static encodeAddLiquidity(
|
65 | 72 | position: Position,
|
| 73 | + minimalPosition: Position, |
66 | 74 | addLiquidityOptions: CondensedAddLiquidityOptions,
|
67 | 75 | slippageTolerance: Percent
|
68 | 76 | ): string {
|
69 |
| - const { amount0: amount0Min, amount1: amount1Min } = position.mintAmountsWithSlippage(slippageTolerance) |
| 77 | + let { amount0: amount0Min, amount1: amount1Min } = position.mintAmountsWithSlippage(slippageTolerance) |
| 78 | + |
| 79 | + // position.mintAmountsWithSlippage() can create amounts not dependenable in scenarios |
| 80 | + // such as range orders. Allow the option to provide a position with custom minimum amounts |
| 81 | + // for these scenarios |
| 82 | + if (JSBI.lessThan(minimalPosition.amount0.quotient, amount0Min)) { |
| 83 | + amount0Min = minimalPosition.amount0.quotient |
| 84 | + } |
| 85 | + if (JSBI.lessThan(minimalPosition.amount1.quotient, amount1Min)) { |
| 86 | + amount1Min = minimalPosition.amount1.quotient |
| 87 | + } |
70 | 88 |
|
71 | 89 | if (isMint(addLiquidityOptions)) {
|
72 | 90 | return ApproveAndCall.INTERFACE.encodeFunctionData('mint', [
|
|
0 commit comments