Skip to content

Commit c8e42b7

Browse files
committed
feat: swap add (WIP)
1 parent fc8d357 commit c8e42b7

File tree

5 files changed

+123
-72
lines changed

5 files changed

+123
-72
lines changed

package-lock.json

+53-66
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
"license": "ISC",
1010
"description": "",
1111
"dependencies": {
12-
"@thanpolas/univ3prices": "^3.0.2",
1312
"@uniswap/v3-core": "^1.0.1",
13+
"@uniswap/v3-periphery": "^1.4.4",
14+
"dotenv": "^16.4.5",
1415
"ethers": "^6.13.2",
1516
"ts-node": "^10.9.2"
1617
}

src/address.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const address = {
2+
// Arbiturm mainnet
3+
poolAddress: "0xC6962004f452bE9203591991D15f6b388e09E8D0",
4+
factoryAddress: "0x1F98431c8aD98523631AE4a59f267346ea31F984",
5+
swapRouterAddress: "0xE592427A0AEce92De3Edee1F18E0157C05861564",
6+
};

src/index.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
import { ethers } from "ethers";
22
import UniswapV3PoolArtifact from "@uniswap/v3-core/artifacts/contracts/interfaces/IUniswapV3Pool.sol/IUniswapV3Pool.json";
3-
import UniswapV3FactoryArtifact from "@uniswap/v3-core/artifacts/contracts/interfaces/IUniswapV3Factory.sol/IUniswapV3Factory.json";
43
import ERC20ABI from "../abis/ERC20.json";
4+
import { address } from "./address";
55

66
// Arbitrum RPC 노드 연결
77
const provider = new ethers.JsonRpcProvider("https://arb1.arbitrum.io/rpc");
88

99
// 유니스왑 V3 풀 컨트랙트 주소 ETH / USDC
10-
const poolAddress = "0xC6962004f452bE9203591991D15f6b388e09E8D0";
11-
const factoryAddress = "0x1F98431c8aD98523631AE4a59f267346ea31F984";
10+
const poolAddress = address.poolAddress;
1211

1312
const { abi: UniswapV3PoolABI } = UniswapV3PoolArtifact;
14-
const { abi: UniswapV3FactoryABI } = UniswapV3FactoryArtifact;
1513

1614
// 유니스왑 풀 컨트랙트 객체 생성
1715
const poolContract = new ethers.Contract(poolAddress, UniswapV3PoolABI, provider);
18-
const factoryContract = new ethers.Contract(factoryAddress, UniswapV3FactoryABI, provider);
1916

2017
const getPoolInfo = async () => {
2118
const slot0 = await poolContract.slot0();

src/swap.ts

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import "dotenv/config";
2+
import { ethers } from "ethers";
3+
import UniswapV3SwapRouterArtifact from "@uniswap/v3-periphery/artifacts/contracts/SwapRouter.sol/SwapRouter.json";
4+
import ERC20ABI from "../abis/ERC20.json";
5+
import { address } from "./address";
6+
// 유니스왑 V3 Router 주소 (Arbitrum)
7+
const swapRouterAddress = address.swapRouterAddress;
8+
9+
// Uniswap V3 Router의 ABI (swapExactInputSingle 함수)
10+
const { abi: uniswapV3SwapRouterABI } = UniswapV3SwapRouterArtifact;
11+
12+
async function swapTokens() {
13+
// RPC 노드 연결
14+
const provider = new ethers.JsonRpcProvider("https://arb1.arbitrum.io/rpc");
15+
16+
// 사용자의 지갑
17+
const privateKey = "YOUR_PRIVATE_KEY"; // 사용자 개인키
18+
const wallet = new ethers.Wallet(privateKey, provider);
19+
20+
// 유니스왑 V3 Router 컨트랙트 객체 생성
21+
const routerContract = new ethers.Contract(swapRouterAddress, uniswapV3SwapRouterABI, wallet);
22+
23+
// 스왑할 토큰 정보 (예: WETH -> USDC)
24+
const tokenIn = ""; // WETH 주소
25+
const tokenOut = ""; // USDC 주소
26+
const fee = 3000; // 0.3% 풀 사용
27+
const recipient = wallet.address; // 스왑 결과를 받을 주소 (보통 사용자 본인의 주소)
28+
const deadline = Math.floor(Date.now() / 1000) + 60 * 10; // 10분 후 트랜잭션 마감
29+
const amountIn = ethers.parseUnits("0.1", 18); // 스왑할 0.1 WETH (단위: 18 decimals)
30+
const amountOutMinimum = ethers.parseUnits("1", 6); // 최소 1 USDC 수령 (단위: 6 decimals)
31+
32+
try {
33+
// 토큰 승인: 먼저 WETH를 유니스왑 V3 Router에 사용하도록 승인
34+
const tokenInContract = new ethers.Contract(tokenIn, ERC20ABI, wallet);
35+
36+
const approvalTx = await tokenInContract.approve(swapRouterAddress, amountIn);
37+
await approvalTx.wait();
38+
console.log("Token approved");
39+
40+
// 스왑 트랜잭션 생성
41+
const swapTx = await routerContract.exactInputSingle({
42+
tokenIn,
43+
tokenOut,
44+
fee,
45+
recipient,
46+
deadline,
47+
amountIn,
48+
amountOutMinimum,
49+
sqrtPriceLimitX96: 0, // 가격 제한 없음
50+
});
51+
52+
// 트랜잭션 대기
53+
const receipt = await swapTx.wait();
54+
console.log("Swap completed: ", receipt);
55+
} catch (error) {
56+
console.error("An error occured during swap:", error);
57+
}
58+
}
59+
60+
swapTokens();

0 commit comments

Comments
 (0)