-
-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathuniswap.lp.ts
258 lines (241 loc) · 7.35 KB
/
uniswap.lp.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { logger } from '../../services/logger';
import { PositionInfo, UniswapLPish } from '../../services/common-interfaces';
import { UniswapConfig } from './uniswap.config';
import { Token } from '@uniswap/sdk-core';
import * as uniV3 from '@uniswap/v3-sdk';
import {
BigNumber,
Transaction,
Wallet,
utils,
constants,
providers,
} from 'ethers';
import { UniswapLPHelper } from './uniswap.lp.helper';
import { AddPosReturn } from './uniswap.lp.interfaces';
const MaxUint128 = BigNumber.from(2).pow(128).sub(1);
export type Overrides = {
gasLimit: BigNumber;
gasPrice?: BigNumber;
value?: BigNumber;
nonce?: BigNumber;
maxFeePerGas?: BigNumber;
maxPriorityFeePerGas?: BigNumber;
};
export class UniswapLP extends UniswapLPHelper implements UniswapLPish {
private static _instances: { [name: string]: UniswapLP };
private _gasLimitEstimate: number;
private constructor(chain: string, network: string) {
super(chain, network);
this._gasLimitEstimate = UniswapConfig.config.gasLimitEstimate;
}
public static getInstance(chain: string, network: string): UniswapLP {
if (UniswapLP._instances === undefined) {
UniswapLP._instances = {};
}
if (!(chain + network in UniswapLP._instances)) {
UniswapLP._instances[chain + network] = new UniswapLP(chain, network);
}
return UniswapLP._instances[chain + network];
}
/**
* Default gas limit for swap transactions.
*/
public get gasLimitEstimate(): number {
return this._gasLimitEstimate;
}
async getPosition(tokenId: number): Promise<PositionInfo> {
const contract = this.getContract('nft', this.chain.provider);
const requests = [
contract.positions(tokenId),
this.collectFees(this.chain.provider, tokenId), // static call to calculate earned fees
];
const positionInfoReq = await Promise.allSettled(requests);
const rejected = positionInfoReq.filter(
(r) => r.status === 'rejected'
) as PromiseRejectedResult[];
if (rejected.length > 0)
throw new Error(`Unable to fetch position with id ${tokenId}`);
const positionInfo = (
positionInfoReq.filter(
(r) => r.status === 'fulfilled'
) as PromiseFulfilledResult<any>[]
).map((r) => r.value);
const position = positionInfo[0];
const feeInfo = positionInfo[1];
const token0 = this.getTokenByAddress(position.token0);
const token1 = this.getTokenByAddress(position.token1);
if (!token0 || !token1) {
throw new Error(`One of the tokens in this position isn't recognized.`);
}
const fee = position.fee;
const poolAddress = uniV3.Pool.getAddress(token0, token1, fee);
const poolData = await this.getPoolState(poolAddress, fee);
const positionInst = new uniV3.Position({
pool: new uniV3.Pool(
token0,
token1,
poolData.fee,
poolData.sqrtPriceX96.toString(),
poolData.liquidity.toString(),
poolData.tick
),
tickLower: position.tickLower,
tickUpper: position.tickUpper,
liquidity: position.liquidity,
});
return {
token0: token0.symbol,
token1: token1.symbol,
fee: uniV3.FeeAmount[position.fee],
lowerPrice: positionInst.token0PriceLower.toFixed(8),
upperPrice: positionInst.token0PriceUpper.toFixed(8),
amount0: positionInst.amount0.toFixed(),
amount1: positionInst.amount1.toFixed(),
unclaimedToken0: utils.formatUnits(
feeInfo.amount0.toString(),
token0.decimals
),
unclaimedToken1: utils.formatUnits(
feeInfo.amount1.toString(),
token1.decimals
),
};
}
async addPosition(
wallet: Wallet,
token0: Token,
token1: Token,
amount0: string,
amount1: string,
fee: string,
lowerPrice: number,
upperPrice: number,
tokenId: number = 0,
gasLimit: number,
gasPrice: number,
nonce?: number,
maxFeePerGas?: BigNumber,
maxPriorityFeePerGas?: BigNumber
): Promise<Transaction> {
const convertedFee = uniV3.FeeAmount[fee as keyof typeof uniV3.FeeAmount];
const addLiquidityResponse: AddPosReturn = await this.addPositionHelper(
wallet,
token0,
token1,
amount0,
amount1,
convertedFee,
lowerPrice,
upperPrice,
tokenId
);
if (nonce === undefined) {
nonce = await this.chain.nonceManager.getNextNonce(wallet.address);
}
const tx = await wallet.sendTransaction({
data: addLiquidityResponse.calldata,
to: addLiquidityResponse.swapRequired ? this.router : this.nftManager,
...this.generateOverrides(
gasLimit,
gasPrice,
nonce,
maxFeePerGas,
maxPriorityFeePerGas,
addLiquidityResponse.value
),
});
logger.info(`Uniswap V3 Add position Tx Hash: ${tx.hash}`);
return tx;
}
async reducePosition(
wallet: Wallet,
tokenId: number,
decreasePercent: number = 100,
gasLimit: number,
gasPrice: number,
nonce?: number,
maxFeePerGas?: BigNumber,
maxPriorityFeePerGas?: BigNumber
): Promise<Transaction> {
// Reduce position and burn
const contract = this.getContract('nft', wallet);
const { calldata, value } = await this.reducePositionHelper(
wallet,
tokenId,
decreasePercent
);
if (nonce === undefined) {
nonce = await this.chain.nonceManager.getNextNonce(wallet.address);
}
const tx = await contract.multicall(
[calldata],
this.generateOverrides(
gasLimit,
gasPrice,
nonce,
maxFeePerGas,
maxPriorityFeePerGas,
value
)
);
logger.info(`Uniswap V3 Remove position Tx Hash: ${tx.hash}`);
return tx;
}
async collectFees(
wallet: Wallet | providers.StaticJsonRpcProvider,
tokenId: number,
gasLimit: number = this.gasLimitEstimate,
gasPrice: number = 0,
nonce?: number,
maxFeePerGas?: BigNumber,
maxPriorityFeePerGas?: BigNumber
): Promise<Transaction | { amount0: BigNumber; amount1: BigNumber }> {
const contract = this.getContract('nft', wallet);
const collectData = {
tokenId: tokenId,
recipient: constants.AddressZero,
amount0Max: MaxUint128,
amount1Max: MaxUint128,
};
if (wallet instanceof providers.StaticJsonRpcProvider) {
return await contract.callStatic.collect(collectData);
} else {
collectData.recipient = wallet.address;
if (nonce === undefined) {
nonce = await this.chain.nonceManager.getNextNonce(wallet.address);
}
return await contract.collect(
collectData,
this.generateOverrides(
gasLimit,
gasPrice,
nonce,
maxFeePerGas,
maxPriorityFeePerGas
)
);
}
}
generateOverrides(
gasLimit: number,
gasPrice: number,
nonce?: number,
maxFeePerGas?: BigNumber,
maxPriorityFeePerGas?: BigNumber,
value?: string
): Overrides {
const overrides: Overrides = {
gasLimit: BigNumber.from(String(gasLimit.toFixed(0))),
};
if (maxFeePerGas && maxPriorityFeePerGas) {
overrides.maxFeePerGas = maxFeePerGas;
overrides.maxPriorityFeePerGas = maxPriorityFeePerGas;
} else {
overrides.gasPrice = BigNumber.from(String((gasPrice * 1e9).toFixed(0)));
}
if (nonce) overrides.nonce = BigNumber.from(String(nonce));
if (value) overrides.value = BigNumber.from(value);
return overrides;
}
}