-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuniswap.js
60 lines (45 loc) · 2.32 KB
/
uniswap.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
const { ChainId, Token, WETH, Fetcher, Trade, Route, TokenAmount, TradeType } = require('@uniswap/v2-sdk');
const { ethers } = require('ethers');
const chainId = ChainId.MAINNET;
const elTokenAddress = process.env.EL_contract
const usdtTokenAddress = process.env.USDT_contract
const infuraURL = process.env.INFURA_URL
const elDecimals = 18; // Replace with the actual decimals of EL
const usdtDecimals = 6; // USDT typically has 6 decimals
const provider = new ethers.providers.JsonRpcProvider(infuraURL); // Replace with your Ethereum node URL
const signer = new ethers.Wallet('YOUR_PRIVATE_KEY', provider); // Replace with your wallet private key
const elToken = new Token(chainId, elTokenAddress, elDecimals);
const usdtToken = new Token(chainId, usdtTokenAddress, usdtDecimals);
// Assume you're using the WETH token address for the WETH9 token on the mainnet
const wethToken = WETH[chainId];
async function swapELForUSDT(elAmountRaw) {
const elAmount = new TokenAmount(elToken, ethers.utils.parseUnits(elAmountRaw, elDecimals).toString());
// Fetch the pair data
const pair = await Fetcher.fetchPairData(elToken, usdtToken, provider);
// Create the route
const route = new Route([pair], elToken);
// Create the trade
const trade = new Trade(route, elAmount, TradeType.EXACT_INPUT);
// Prepare the transaction
const slippageTolerance = new Percent('50', '10000'); // 0.50%
const amountOutMin = trade.minimumAmountOut(slippageTolerance).raw.toString();
const path = [elTokenAddress, wethToken.address, usdtTokenAddress];
const to = 'YOUR_ADDRESS'; // Replace with the recipient address
const deadline = Math.floor(Date.now() / 1000) + 60 * 20; // 20 minutes from the current time
// Prepare to sign the transaction
const value = trade.inputAmount.raw.toString();
const UniswapV2Router02Address = 'UNISWAP_V2_ROUTER_02_ADDRESS'; // Replace with the correct router address
const tx = await signer.sendTransaction({
to: UniswapV2Router02Address,
value: value,
data: UniswapV2Router02Interface.encodeFunctionData('swapExactTokensForTokens', [
amountOutMin,
path,
to,
deadline
]),
});
console.log(`Transaction hash: ${tx.hash}`);
}
// Example: swap 1 EL for as much USDT as possible
swapELForUSDT('1');