-
Notifications
You must be signed in to change notification settings - Fork 0
/
updated.js
166 lines (153 loc) · 7.99 KB
/
updated.js
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
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function(mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function(thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function(resolve) { resolve(value); }); }
return new (P || (P = Promise))(function(resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UniswapLP = void 0;
const logger_1 = require("../../services/logger");
const uniswap_config_1 = require("./uniswap.config");
const uniV3 = __importStar(require("@uniswap/v3-sdk"));
const ethers_1 = require("ethers");
const uniswap_lp_helper_1 = require("./uniswap.lp.helper");
const MaxUint128 = ethers_1.BigNumber.from(2).pow(128).sub(1);
class UniswapLP extends uniswap_lp_helper_1.UniswapLPHelper {
constructor(chain, network) {
super(chain, network);
this._gasLimitEstimate = uniswap_config_1.UniswapConfig.config.gasLimitEstimate;
}
static getInstance(chain, network) {
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];
}
get gasLimitEstimate() {
return this._gasLimitEstimate;
}
async getPosition(tokenId) {
const contract = this.getContract('nft', this.ethereum.provider);
const requests = [
contract.positions(tokenId),
this.collectFees(this.ethereum.provider, tokenId),
];
const positionInfoReq = await Promise.allSettled(requests);
const rejected = positionInfoReq.filter((r) => r.status === 'rejected');
if (rejected.length > 0)
throw new Error(`Unable to fetch position with id ${tokenId}`);
const positionInfo = positionInfoReq.filter((r) => r.status === 'fulfilled').map((r) => r.value);
const position = positionInfo[0];
const feeInfo = positionInfo[1];
const poolContract = this.getContract('pool', this.ethereum.provider);
const poolAddress = uniV3.Pool.getAddress(position.token0, position.token1, position.fee);
const poolTokens = await poolContract.tokens(poolAddress);
const token0 = this.getTokenByAddress(poolTokens[0]);
const token1 = this.getTokenByAddress(poolTokens[1]);
if (!token0 || !token1) {
throw new Error(`One of the tokens in this position isn't recognized.`);
}
const fee = position.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: ethers_1.utils.formatUnits(feeInfo.amount0.toString(), token0.decimals),
unclaimedToken1: ethers_1.utils.formatUnits(feeInfo.amount1.toString(), token1.decimals),
};
}
async addPosition(wallet, token0, token1, amount0, amount1, fee, lowerPrice, upperPrice, tokenId = 0, gasLimit, gasPrice, nonce, maxFeePerGas, maxPriorityFeePerGas) {
const addLiquidityResponse = await this.addPositionHelper(wallet, token0, token1, amount0, amount1, fee, lowerPrice, upperPrice, tokenId);
if (nonce === undefined) {
nonce = await this.ethereum.nonceManager.getNextNonce(wallet.address);
}
const tx = await wallet.sendTransaction(Object.assign({ data: addLiquidityResponse.calldata, to: addLiquidityResponse.swapRequired ? this.router : this.nftManager }, this.generateOverrides(gasLimit, gasPrice, nonce, maxFeePerGas, maxPriorityFeePerGas, addLiquidityResponse.value)));
logger_1.logger.info(`Uniswap V3 Add position Tx Hash: ${tx.hash}`);
return tx;
}
async reducePosition(wallet, tokenId, decreasePercent = 100, gasLimit, gasPrice, nonce, maxFeePerGas, maxPriorityFeePerGas) {
const contract = this.getContract('nft', wallet);
const { calldata, value } = await this.reducePositionHelper(wallet, tokenId, decreasePercent);
if (nonce === undefined) {
nonce = await this.ethereum.nonceManager.getNextNonce(wallet.address);
}
const tx = await contract.multicall([calldata], this.generateOverrides(gasLimit, gasPrice, nonce, maxFeePerGas, maxPriorityFeePerGas, value));
logger_1.logger.info(`Uniswap V3 Remove position Tx Hash: ${tx.hash}`);
return tx;
}
async collectFees(wallet, tokenId, gasLimit = this.gasLimitEstimate, gasPrice = 0, nonce, maxFeePerGas, maxPriorityFeePerGas) {
const contract = this.getContract('nft', wallet);
const collectData = {
tokenId: tokenId,
recipient: ethers_1.constants.AddressZero,
amount0Max: MaxUint128,
amount1Max: MaxUint128,
};
if (wallet instanceof ethers_1.providers.StaticJsonRpcProvider) {
return await contract.callStatic.collect(collectData);
} else {
collectData.recipient = wallet.address;
if (nonce === undefined) {
nonce = await this.ethereum.nonceManager.getNextNonce(wallet.address);
}
return await contract.collect(collectData, this.generateOverrides(gasLimit, gasPrice, nonce, maxFeePerGas, maxPriorityFeePerGas));
}
}
generateOverrides(gasLimit, gasPrice, nonce, maxFeePerGas, maxPriorityFeePerGas, value) {
const overrides = {
gasLimit: ethers_1.BigNumber.from(String(gasLimit.toFixed(0))),
};
if (maxFeePerGas && maxPriorityFeePerGas) {
overrides.maxFeePerGas = maxFeePerGas;
overrides.maxPriorityFeePerGas = maxPriorityFeePerGas;
} else {
overrides.gasPrice = ethers_1.BigNumber.from(String((gasPrice * 1e9).toFixed(0)));
}
if (nonce) overrides.nonce = ethers_1.BigNumber.from(String(nonce));
if (value) overrides.value = ethers_1.BigNumber.from(value);
return overrides;
}
}
exports.UniswapLP = UniswapLP;
//# sourceMappingURL=uniswap.lp.js.map