1
1
import { Interface } from '@ethersproject/abi'
2
2
import invariant from 'tiny-invariant'
3
3
import { abi } from '@uniswap/swap-router-contracts/artifacts/contracts/interfaces/IApproveAndCall.sol/IApproveAndCall.json'
4
- import { Currency , Token } from '@uniswap/sdk-core'
5
- import { NonfungiblePositionManager } from '@uniswap/v3-sdk'
4
+ import { Currency , Percent , Token } from '@uniswap/sdk-core'
5
+ import {
6
+ MintSpecificOptions ,
7
+ IncreaseSpecificOptions ,
8
+ NonfungiblePositionManager ,
9
+ Position ,
10
+ toHex ,
11
+ } from '@uniswap/v3-sdk'
12
+
13
+ // condensed version of v3-sdk AddLiquidityOptions containing only necessary swap + add attributes
14
+ export type CondensedAddLiquidityOptions = Omit < MintSpecificOptions , 'createPool' > | IncreaseSpecificOptions
6
15
7
16
export enum ApprovalTypes {
8
17
NOT_REQUIRED = 0 ,
@@ -12,6 +21,11 @@ export enum ApprovalTypes {
12
21
ZERO_THEN_MAX_MINUS_ONE = 4 ,
13
22
}
14
23
24
+ // type guard
25
+ export function isMint ( options : CondensedAddLiquidityOptions ) : options is Omit < MintSpecificOptions , 'createPool' > {
26
+ return Object . keys ( options ) . some ( ( k ) => k === 'recipient' )
27
+ }
28
+
15
29
export abstract class ApproveAndCall {
16
30
public static INTERFACE : Interface = new Interface ( abi )
17
31
@@ -47,6 +61,39 @@ export abstract class ApproveAndCall {
47
61
}
48
62
}
49
63
64
+ public static encodeAddLiquidity (
65
+ position : Position ,
66
+ addLiquidityOptions : CondensedAddLiquidityOptions ,
67
+ slippageTolerance : Percent
68
+ ) : string {
69
+ const { amount0 : amount0Min , amount1 : amount1Min } = position . mintAmountsWithSlippage ( slippageTolerance )
70
+
71
+ if ( isMint ( addLiquidityOptions ) ) {
72
+ return ApproveAndCall . INTERFACE . encodeFunctionData ( 'mint' , [
73
+ {
74
+ token0 : position . pool . token0 . address ,
75
+ token1 : position . pool . token1 . address ,
76
+ fee : position . pool . fee ,
77
+ tickLower : position . tickLower ,
78
+ tickUpper : position . tickUpper ,
79
+ amount0Min : toHex ( amount0Min ) ,
80
+ amount1Min : toHex ( amount1Min ) ,
81
+ recipient : addLiquidityOptions . recipient ,
82
+ } ,
83
+ ] )
84
+ } else {
85
+ return ApproveAndCall . INTERFACE . encodeFunctionData ( 'increaseLiquidity' , [
86
+ {
87
+ token0 : position . pool . token0 . address ,
88
+ token1 : position . pool . token1 . address ,
89
+ amount0Min : toHex ( amount0Min ) ,
90
+ amount1Min : toHex ( amount1Min ) ,
91
+ tokenId : toHex ( addLiquidityOptions . tokenId ) ,
92
+ } ,
93
+ ] )
94
+ }
95
+ }
96
+
50
97
public static encodeApprove ( token : Currency , approvalType : ApprovalTypes ) : string {
51
98
switch ( approvalType ) {
52
99
case ApprovalTypes . MAX :
0 commit comments