Skip to content

Instantly share code, notes, and snippets.

@AndreiD
Created July 7, 2024 18:36
Show Gist options
  • Save AndreiD/37390bd42a0c58494bcc4b41dd8f9e65 to your computer and use it in GitHub Desktop.
Save AndreiD/37390bd42a0c58494bcc4b41dd8f9e65 to your computer and use it in GitHub Desktop.
simple_uniswap_v3_swap
//1. make sure enough funds
//2. make sure the pair tokens have allowance to uniswap router v2
//3. find out the best pool and it's fee
import {
SWAP_ROUTER_02_ADDRESSES
} from "@uniswap/sdk-core";
import { swapRouter02ABI } from "../../data/swapRouter02ABI";
const swapRouter = new ethers.Contract(
SWAP_ROUTER_02_ADDRESSES(Number(chainId)),
swapRouter02ABI,
signer
);
let txParams = {
tokenIn: tokenIn,
tokenOut: tokenOut,
fee: poolFee,
recipient: signer.address,
deadline: Math.floor(Date.now() / 1000) + 60 * 10,
amountIn: amountIn,
amountOutMinimum: BigInt(0),
sqrtPriceLimitX96: 0,
};
console.log("txParams", txParams);
const tx = await swapRouter.exactInputSingle(txParams);
console.log("Transaction hash:", tx.hash);
await tx.wait();
const receipt = await ethersProvider.getTransactionReceipt(tx.hash);
if (receipt && receipt.status === 1) {
console.log("======> Transaction successful! <======");
}else{
console.log("you fucked up somewhere...");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment