-
Notifications
You must be signed in to change notification settings - Fork 0
/
UniswapV3ops.js
345 lines (314 loc) · 16.1 KB
/
UniswapV3ops.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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/**
* used to interact with Uniswap V3 smart contracts
*/
const {BlockchainConfig, getItemFromTokenList} = require("./BlockchainConfig.js");
const Util = require("./Util.js");
const ERC20ops = require("./ERC20ops.js");
class UniswapV3ops {
constructor (_GLOBAL){
this.GLOBAL = _GLOBAL;
}
/**
* get pool address for the given tokenIn, tokenOut and fee
* @param {*} _tokenIn
* @param {*} _tokenOut
* @param {*} _fee
* @returns address (Promise)
*/
getPoolAddress(_tokenIn, _tokenOut, _fee, _times = 10){
//handle response tx
Util.assertValidInputs([_tokenIn, _tokenOut, _fee], "getPoolAddress");
let txPromise = new Promise(async (resolve, reject) =>{
let totalTimes = new Array(_times).fill(1);
for (let shot in totalTimes){
try {
let feeBip = _fee * (10**4);
let swapFactoryAddress = BlockchainConfig.blockchain[this.GLOBAL.blockchain].UNISWAPV3_FACTORY_ADDRESS;
let swapFactoryContract = new this.GLOBAL.web3Instance.eth.Contract(BlockchainConfig.blockchain[this.GLOBAL.blockchain].UNISWAPV3_FACTORY_ABI, swapFactoryAddress, { from: this.GLOBAL.ownerAddress });
let address = await swapFactoryContract.methods.getPool(_tokenIn.address, _tokenOut.address, feeBip).call();
//inform if it is in the second try and so forth
if(parseInt(shot) > 0){
console.log(`##### UniswapV3 pool address got it in SHOT ${parseInt(shot)+1} #####`);
}
resolve(address);
break;
} catch (error) {
//alchemy error code for exceeding units per second capacity.
if ( Util.isAlchemyExceedingError(error)){
let waitTimeInMs = Util.getAlchemyWaitingLongTime();
if(parseInt(shot) > 2){
console.log(`##### trying to get UniswapV3 pool address again in ${Number(waitTimeInMs).toFixed(2)} ms... #####`);
}
await Util.sleep(waitTimeInMs);
} else {
console.log(`### error on query UniswapV3 pool address ${_tokenIn.address} ${_tokenOut.address} fee: ${_fee} error: ${error} ### `)
reject(0.0);
break
}
}
}
});
return txPromise;
}
/**
* Converts amountIn to wei, get amount out on Uniswap V3 according to _fee and converts amountOutWei to regular amountOut according to token out decimals
* @param {*} _amountIn
* @param {*} _tokenIn
* @param {*} _tokenOut
* @param {*} _fee
* @returns transaction (Promise)
*/
queryAmountOut(_quoterAddress, _amountIn, _tokenIn, _tokenOut, _fee, _times = 10){
Util.assertValidInputs([_quoterAddress, _amountIn, _tokenIn, _tokenOut, _fee], "queryAmountOut");
//handle response tx
let txPromise = new Promise(async (resolve, reject) =>{
let totalTimes = new Array(_times).fill(1);
for (let shot in totalTimes){
try {
let feeBip = _fee * (10**4);
let amountInWei = Util.amountToBlockchain(_amountIn, _tokenIn.decimals);
let quoterContract = new this.GLOBAL.web3Instance.eth.Contract(BlockchainConfig.blockchain[this.GLOBAL.blockchain].UNISWAPV3_QUOTER_ABI, _quoterAddress, { from: this.GLOBAL.ownerAddress });
let amountOutWei = await quoterContract.methods.quoteExactInputSingle(_tokenIn.address, _tokenOut.address, feeBip, amountInWei, 0).call();
let amountOut = Util.amountFromBlockchain(amountOutWei, _tokenOut.decimals);
//inform if it is in the second try and so forth
if(parseInt(shot) > 0){
console.log(`##### UniswapV3 amount out got it in SHOT ${parseInt(shot)+1} #####`);
}
resolve(amountOut);
break;
} catch (error) {
//alchemy error code for exceeding units per second capacity.
if ( Util.isAlchemyExceedingError(error)){
let waitTimeInMs = Util.getAlchemyWaitingTime();
if(parseInt(shot) > 2){
console.log(`##### trying to get UniswapV3 amount out again in ${Number(waitTimeInMs).toFixed(2)} ms... #####`);
}
await Util.sleep(waitTimeInMs);
} else {
console.log(`### error on query UniswapV3 amount out ${_tokenIn.address} ${_tokenOut.address} fee: ${_fee} error: ${error} ### `)
reject(0.0);
break
}
}
}
});
return txPromise;
}
/**
* query token0 of the pool contract
* @param {*} _poolAddress
* @returns address (Promise)
*/
getToken0AddressFromPool(_poolAddress){
//handle response tx
Util.assertValidInputs([_poolAddress], "getToken0AddressFromPool")
let txPromise = new Promise(async (resolve, reject) =>{
try {
let poolV3Abi = BlockchainConfig.blockchain[this.GLOBAL.blockchain].UNISWAPV3_POOL_ABI;
let poolContract = new this.GLOBAL.web3Instance.eth.Contract(poolV3Abi, _poolAddress, { from: this.GLOBAL.ownerAddress });
let token0address = await poolContract.methods.token0().call();
resolve(token0address);
} catch (error) {
reject(error);
}
});
return txPromise;
}
/**
* calculates locally, not used so far
* @param {*} _amount
* @param {*} _tokenIn
* @param {*} _tokenOut
* @param {*} _fee
* @returns amount out (Number)
*/
async getAmountOut(_amount, _tokenIn, _tokenOut, _fee){
let feeBip = _fee * (10**4);
console.log(`feeBip ${feeBip}`);
try {
let poolAddress = await this.getPoolAddress(_tokenIn, _tokenOut, feeBip);
if(poolAddress){
console.log(`poolAddress: ${poolAddress}`);
let token0address = await this.getToken0AddressFromPool(poolAddress);
console.log(`token0address: ${token0address}`);
let slot0 = await this.getSlot0FromPool(poolAddress);
let P = ( (slot0.sqrtPriceX96) / (2 ** 96) ) ** 2;
console.log(`P: ${P}`)
console.log(`tick: ${slot0.tick}`)
console.log(`1.0001**tick: ${1.0001 ** slot0.tick}`)
if(_tokenIn.address == token0address){
P = 1 / P;
}
let amountOut = ((_amount * (10 ** _tokenIn.decimals)) / (P * (10 ** _tokenOut.decimals))) ;
let amountFee = amountOut * (_fee/100);
console.log(`amountFee ${amountFee}`);
return amountOut - amountFee;
} else {
console.log("pool not found for informed tokens and feeBip ")
}
} catch (error) {
throw (error);
}
}
/**
* Show pool address for the given tokenIn, tokenOut and fee
* @param {*} _tokenIn
* @param {*} _tokenOut
* @param {*} _fee
*/
async showPoolAddress(_tokenIn, _tokenOut, _fee){
try {
let address = await this.getPoolAddress(_tokenIn, _tokenOut, _fee);
console.log(`${_tokenIn.symbol} <> ${_tokenOut.symbol} %${Number(_fee).toFixed(4)} fee | ${BlockchainConfig.blockchain[this.GLOBAL.blockchain].EXPLORER}${address}`);
} catch (error) {
throw new Error(error);
}
}
/**
* Execute query if blacklist not informed or the set {_tokenIn, _tokenOut, fee} is not contained in blacklist
* if blacklist is informed, it will be updated for each invalid / unexistent set {_tokenIn, _tokenOut, fee}
* @param {*} _amountIn
* @param {*} _tokenIn
* @param {*} _tokenOut
* @param {*} _blacklist
* @returns best fee of uniswap v3 route between tokenIn and tokenOut, and updated blacklist (Object)
*/
async queryFeeOfBestRoute(_quoterAddress, _amountIn, _tokenIn, _tokenOut, _blacklist){
Util.assertValidInputs([_quoterAddress, _amountIn, _tokenIn, _tokenOut], "queryFeeOfBestRoute")
try {
if(!_blacklist){
_blacklist = new Array();
}
let possibleFees = BlockchainConfig.blockchain[this.GLOBAL.blockchain].UNISWAPV3_FEES;
let bestFee = 0;
let bestAmountOut = 0;
for (let fee of possibleFees){
let executeQuery = true;
if(_blacklist && Util.isBlacklistedUniswapV3(_blacklist, _tokenIn.symbol, _tokenOut.symbol, fee)){
executeQuery = false;
}
if(executeQuery){
try {
let pairAddressPool = await this.getPoolAddress(_tokenIn, _tokenOut, fee);
if(pairAddressPool && pairAddressPool != "0x0000000000000000000000000000000000000000"){
let currentAmountOut = 0.0;
await this.queryAmountOut(_quoterAddress, _amountIn, _tokenIn, _tokenOut, fee).then(((response) =>{
currentAmountOut = response;
})).catch((reject) =>{
currentAmountOut = reject;
_blacklist = Util.addToBlacklistUniswapV3(_blacklist, _tokenIn.symbol, _tokenOut.symbol, fee);
}).finally(()=>{
if(currentAmountOut > bestAmountOut){
bestAmountOut = currentAmountOut;
bestFee = fee;
}
});
}else {
_blacklist = Util.addToBlacklistUniswapV3(_blacklist, _tokenIn.symbol, _tokenOut.symbol, fee);
}
} catch (error) {//in case of error getting amount out adds it in the blacklist and continues
_blacklist = Util.addToBlacklistUniswapV3(_blacklist, _tokenIn.symbol, _tokenOut.symbol, fee);
}
}
}
return {bestFee: bestFee, updatedBlacklist: _blacklist};
} catch (error) {
console.log("######## Unexpected error no queryFeeOfBestRoute: ########")
console.log(error);
return {bestFee: bestFee, updatedBlacklist: _blacklist};
}
}
/**
* used to calculate price locally
* @param {*} _poolAddress
* @returns slot0 (Promise)
*/
getSlot0FromPool(_poolAddress){
//handle response tx
let txPromise = new Promise(async (resolve, reject) =>{
try {
let poolV3Abi = BlockchainConfig.blockchain[this.GLOBAL.blockchain].UNISWAPV3_POOL_ABI;
let poolContract = new this.GLOBAL.web3Instance.eth.Contract(poolV3Abi, _poolAddress, { from: this.GLOBAL.ownerAddress });
let slot0 = await poolContract.methods.slot0().call();
resolve(slot0);
} catch (error) {
reject(error);
}
});
return txPromise;
}
/**
* Realize swap between two tokens
* @param {*} _amount
* @param {*} _tokenIn
* @param {*} _tokenOut
* @param {*} _fee 0.05%, 0.1%, 0.3%
* @returns transaction (Promise)
*/
async swap(_amount, _tokenIn, _tokenOut, _fee){
//handle response tx
let txPromise = new Promise(async (resolve, reject) =>{
try {
//vefifies if poolAddress exists
let poolAddress = await this.getPoolAddress(_tokenIn, _tokenOut, _fee);
if(!poolAddress || poolAddress == "0x0000000000000000000000000000000000000000"){
await this.showPoolAddress(_tokenIn, _tokenOut, _fee);
throw new Error("Pool address not found")
}
//instanciate router and factory contracts
let swapRouterAddress = BlockchainConfig.blockchain[this.GLOBAL.blockchain].UNISWAPV3_ROUTER_ADDRESS;
let swapRouterContract = new this.GLOBAL.web3Instance.eth.Contract(BlockchainConfig.blockchain[this.GLOBAL.blockchain].UNISWAPV3_ROUTER_ABI, swapRouterAddress, { from: this.GLOBAL.ownerAddress });
//extract params
let tokenIn = _tokenIn.address;
let tokenOut = _tokenOut.address;
let fee = _fee * (10**4);
let amountInWei = Util.amountToBlockchain(_amount);
//aprove swap
let erc20ops = new ERC20ops(this.GLOBAL);
await erc20ops.approve(_tokenIn, swapRouterAddress, _amount);
//define params
const swapParams = {
tokenIn: tokenIn,
tokenOut: tokenOut,
fee: fee,
recipient: this.GLOBAL.ownerAddress,
deadline: Math.floor(Date.now() / 1000) + (600 * 10),
amountIn: amountInWei,
amountOutMinimum: 0,
sqrtPriceLimitX96: 0,
}
//encode method
let dataSwap = swapRouterContract.methods.exactInputSingle(swapParams).encodeABI();
//sets maxFeePerGas and maxPriorityFeePerGas, lesser values were generating 'transaction underpriced' error on Polygon mainnet
let maxPriorityFeePerGas = await this.GLOBAL.web3Instance.eth.getGasPrice();
let maxFeePerGas = maxPriorityFeePerGas * 3;
//declare raw tx to withdraw
let rawSwapTx = {
from: this.GLOBAL.ownerAddress,
to: swapRouterAddress,
maxFeePerGas: String(maxFeePerGas),
maxPriorityFeePerGas: String(maxPriorityFeePerGas),
gasLimit: BlockchainConfig.blockchain[this.GLOBAL.blockchain].GAS_LIMIT_HIGH,
data: dataSwap
};
//sign tx
let signedWithdrawTx = await this.GLOBAL.web3Instance.eth.signTransaction(rawSwapTx, this.GLOBAL.ownerAddress);
//send signed transaction
let withdrawTx = this.GLOBAL.web3Instance.eth.sendSignedTransaction(signedWithdrawTx.raw || signedWithdrawTx.rawTransaction);
withdrawTx.on("receipt", (receipt) => {
console.log(`### ${_amount} ${_tokenIn.symbol} exchanged by ${_tokenOut.symbol} successfully: ###`);
console.log(`### tx: ${receipt.transactionHash} ###`);
resolve(receipt);
});
withdrawTx.on("error", (err) => {
console.log("### Exchange tx error: ###");
reject(err);
});
} catch (error) {
reject(error);
}
});
return txPromise;
}
}
module.exports = UniswapV3ops;