-
Notifications
You must be signed in to change notification settings - Fork 0
/
uniswap.lp.js
169 lines (169 loc) · 6.59 KB
/
uniswap.lp.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
167
168
169
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UniswapLP = void 0;
const error_handler_1 = require("../../services/error-handler");
const uniswap_config_1 = require("./uniswap.config");
const ethereum_1 = require("../../chains/ethereum/ethereum");
const uniswap_lp_helper_1 = require("./uniswap.lp.helper");
const config_manager_v2_1 = require("../../services/config-manager-v2");
const ethers_1 = require("ethers");
class UniswapLP {
constructor(chain, network) {
this._wallet = null;
this._burnToken = false;
this._burnTokenPositionIndex = 0;
this._lpHelper = new uniswap_lp_helper_1.UniswapLPHelper(chain, network);
this._chain = chain;
this._network = network;
this._token0 = null;
this._token1 = null;
this._amount0Desired = '0';
this._amount1Desired = '0';
this._fee = config_manager_v2_1.defaultFee(this._network, this._chain);
this._lowerPrice = '0';
this._upperPrice = '0';
this._decreasePercent = '0';
this._tokenId = 0;
this._deadline = this._lpHelper.ttl;
this._wallet = null;
}
init() {
return this._lpHelper.init();
}
setToken0(token0) {
this._token0 = token0;
return this;
}
setToken1(token1) {
this._token1 = token1;
return this;
}
setAmount0Desired(amount0) {
this._amount0Desired = amount0;
return this;
}
setAmount1Desired(amount1) {
this._amount1Desired = amount1;
return this;
}
setFee(fee) {
this._fee = fee;
return this;
}
setLowerPrice(lowerPrice) {
this._lowerPrice = lowerPrice;
return this;
}
setUpperPrice(upperPrice) {
this._upperPrice = upperPrice;
return this;
}
setDeadline(deadline) {
this._deadline = deadline;
return this;
}
setWallet(wallet) {
this._wallet = wallet;
return this;
}
setBurnToken(burnToken, positionIndex = 0) {
this._burnToken = burnToken;
this._burnTokenPositionIndex = positionIndex;
return this;
}
setDecreasePercent(percent) {
this._decreasePercent = percent;
return this;
}
setTokenId(tokenId) {
this._tokenId = tokenId;
return this;
}
static log(message) {
console.log(`[UniswapLP] ${message}`);
}
static error(message) {
console.error(`[UniswapLP] ERROR: ${message}`);
}
validate() {
if (!this._lpHelper.ready()) {
throw new error_handler_1.InitializationError((0, error_handler_1.SERVICE_UNITIALIZED_ERROR_MESSAGE)('UniswapLPHelper'), error_handler_1.SERVICE_UNITIALIZED_ERROR_CODE);
}
if (this._fee < 500 || this._fee > 3000) {
throw new Error('Invalid fee. Please provide fee between 500 and 3000.');
}
if (this._wallet === null) {
throw new Error('Wallet not set. Please use setWallet method to set the wallet.');
}
if (!this._wallet.provider) {
throw new Error('Invalid wallet provider.');
}
}
addLiquidity() {
var _a;
return __awaiter(this, void 0, void 0, function* () {
try {
this.validate();
if (this._token0 === null || this._token1 === null) {
throw new Error('Token0 and Token1 must be set.');
}
const walletAddress = this._wallet.address;
UniswapLP.log(`Adding liquidity to Uniswap for ${this._token0.symbol} and ${this._token1.symbol}...`);
const amount0Min = '0';
const amount1Min = '0';
const { addCallParameters, swapRequired } = yield this._lpHelper.addPositionHelper(this._wallet, this._token0, this._token1, this._amount0Desired, this._amount1Desired, this._fee, this._lowerPrice, this._upperPrice, this._tokenId);
if (swapRequired) {
UniswapLP.log(`Swapping tokens before adding liquidity...`);
const transaction = this._lpHelper.alphaRouter.swapCallParameters(addCallParameters);
yield ((_a = this._wallet) === null || _a === void 0 ? void 0 : _a.sendTransaction(transaction));
}
UniswapLP.log(`Adding liquidity to Uniswap...`);
const contract = this._lpHelper.getContract('nft', this._wallet);
const { events } = yield contract.addLiquidity(addCallParameters, {
gasLimit: uniswap_config_1.UniswapConfig.config.gasLimit,
value: uniswap_config_1.UniswapConfig.config.value,
});
UniswapLP.log(`Liquidity added successfully!`);
const nftTransferEvent = events.find((event) => event.event === 'Transfer');
if (nftTransferEvent) {
const tokenId = nftTransferEvent.args.tokenId.toNumber();
UniswapLP.log(`New NFT position created with tokenId: ${tokenId}`);
return tokenId;
}
else {
UniswapLP.error('No NFT Transfer event found after adding liquidity.');
throw new Error('Failed to add liquidity.');
}
}
catch (error) {
UniswapLP.error(error.message);
throw error;
}
});
}
reduceLiquidity() {
return __awaiter(this, void 0, void 0, function* () {
try {
this.validate();
if (this._tokenId === 0) {
throw new Error('tokenId must be set for reducing liquidity.');
}
UniswapLP.log(`Reducing liquidity from Uniswap for tokenId: ${this._tokenId}...`);
const { removeCallParameters } = yield this._lpHelper.reducePositionHelper(this._wallet, this._tokenId, this._decreasePercent);
UniswapLP.log(`Removing liquidity from Uniswap...`);
const contract = this._lpHelper.getContract('nft', this._wallet);
yield contract.decreaseLiquidity(removeCallParameters, {
gasLimit: uniswap_config_1.UniswapConfig.config.gasLimit,
value: uniswap_config_1.UniswapConfig.config.value,
});
UniswapLP.log(`Liquidity removed successfully!`);
}
catch (error) {
UniswapLP.error(error.message);
throw error;
}
});
}
}
exports.UniswapLP = UniswapLP;
//# sourceMappingURL=uniswap.lp.js.map