-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
49 lines (41 loc) · 2.03 KB
/
index.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
require("dotenv").config(); // Load environment variables
const { ethers } = require("ethers");
const { swapETHForUSDC } = require("./src/hyperswap_swap_functions");
const { swapUSDCForETH } = require("./src/hyperswap_swap_functions");
const { addLiquidityETHWithUSDC } = require("./src/hyperswap_liquidity_functions");
// Set up provider and signer
const provider = new ethers.providers.JsonRpcProvider("https://api.hyperliquid-testnet.xyz/evm"); // Corrected URL
const privateKey = process.env.PRIVATE_KEY;
const walletAddress = process.env.WALLET_ADDRESS;
try {
const signer = new ethers.Wallet(privateKey, provider);
console.log("Wallet successfully initialized");
async function main() {
try {
// Call the Swap Function
console.log("Swapping ETH for USDC...");
const amountOutMin = ethers.utils.parseUnits("100", 8); // Minimum USDC expected
const to = walletAddress; // Replace with your wallet address
const ethAmount = "0.1"; // Amount of ETH to swap
await swapETHForUSDC(provider, signer, amountOutMin, to, ethAmount);
console.log("Swap ETH → USDC completed!");
console.log("Swapping USDC for ETH...");
const usdcAmount = ethers.utils.parseUnits("100", 8); // 100 USDC
const slippageTolerance = 2; // 2% slippage
await swapUSDCForETH(provider, signer, usdcAmount, slippageTolerance, to);
console.log("Swap USDC → ETH completed!");
// Call the Add Liquidity Function
console.log("Adding liquidity...");
// Calculate slippage-adjusted minimums
const ethMin = ethers.utils.parseEther(ethAmount).mul(100 - slippageTolerance).div(100);
const usdcMin = ethers.utils.parseUnits("100", 8).mul(100 - slippageTolerance).div(100); // Adjust based on expected USDC
await addLiquidityETHWithUSDC(provider, signer, ethAmount, usdcMin, ethMin, to);
console.log("Liquidity added!");
} catch (error) {
console.error("An error occurred:", error);
}
}
main();
} catch (error) {
console.error("Error initializing wallet:", error);
}