Skip to content

Instantly share code, notes, and snippets.

@Falilah
Last active February 16, 2023 20:00
Show Gist options
  • Save Falilah/aae8e26821dccfa961ce43e213a8bb54 to your computer and use it in GitHub Desktop.
Save Falilah/aae8e26821dccfa961ce43e213a8bb54 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;
interface IToken {
function approve(address _spender, uint256 _value) external;
function balanceOf(address who) external view returns (uint256);
function allowance(address _owner, address _spender)
external
returns (uint256 remaining);
}
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;
interface IUniswap {
function swapExactTokensForTokens(
uint256 amountIn, //send to uniswap
uint256 amountOutMin, // receive from uniswap
address[] calldata path,
address to,
uint256 deadline
) external returns (uint256[] memory amounts);
function swapTokensForExactTokens(
uint256 amountOut,
uint256 amountInMax,
address[] calldata path,
address to,
uint256 deadline
) external returns (uint256[] memory amounts);
function swapExactETHForTokens(
uint256 amountOutMin,
address[] calldata path,
address to,
uint256 deadline
) external payable returns (uint256[] memory amounts);
function swapTokensForExactETH(
uint256 amountOut,
uint256 amountInMax,
address[] calldata path,
address to,
uint256 deadline
) external returns (uint256[] memory amounts);
function swapExactTokensForETH(
uint256 amountIn,
uint256 amountOutMin,
address[] calldata path,
address to,
uint256 deadline
) external returns (uint256[] memory amounts);
function swapETHForExactTokens(
uint256 amountOut,
address[] calldata path,
address to,
uint256 deadline
) external payable returns (uint256[] memory amounts);
}
import { ethers } from "hardhat";
import { BigNumber } from "ethers";
import { providers } from "ethers";
async function main() {
//uniswap router address
const ROUTER = "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D";
//dai token address
const DAI = "0x6B175474E89094C44Da98b954EedeAC495271d0F";
//uni token address
const UNI = "0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984";
//dai holder
const DAIHolder = "0x748dE14197922c4Ae258c7939C7739f3ff1db573";
const paths = [DAI, "0x7D1AfA7B718fb893dB30A3aBc0Cfc608AaCfeBB0", UNI];
const path2 = ["0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", DAI];
let time = 1676588399;
const amountToSwap = await ethers.utils.parseEther("100");
console.log(amountToSwap);
const amountToReceive = await ethers.utils.parseEther("100");
console.log(amountToSwap);
const Uniswap = await ethers.getContractAt("IUniswap", ROUTER);
const helpers = require("@nomicfoundation/hardhat-network-helpers");
await helpers.impersonateAccount(DAIHolder);
const impersonatedSigner = await ethers.getSigner(DAIHolder);
const DaiContract = await ethers.getContractAt("IToken", DAI);
const UniContract = await ethers.getContractAt("IToken", UNI);
const holderBalance = await DaiContract.balanceOf(DAIHolder);
console.log(`Dai balance before ${holderBalance}`);
await DaiContract.connect(impersonatedSigner).approve(ROUTER, amountToSwap);
const uniBalance = await UniContract.balanceOf(DAIHolder);
console.log(`uniBalance ${uniBalance}`);
await Uniswap.connect(impersonatedSigner).swapExactTokensForTokens(
amountToSwap,
0,
paths,
DAIHolder,
time
);
const uniBalanceAfter = await UniContract.balanceOf(DAIHolder);
console.log(`uniBalanceAfter ${uniBalanceAfter}`);
const holderBalanceAfter = await DaiContract.balanceOf(DAIHolder);
console.log(`Dai balance After ${holderBalanceAfter}`);
const sent = ethers.utils.parseEther("0.1");
const ETHbalanceBefore = await ethers.provider.getBalance(DAIHolder);
console.log(`ETHBalance before ${ETHbalanceBefore}`);
await Uniswap.connect(impersonatedSigner).swapETHForExactTokens(
amountToReceive,
path2,
DAIHolder,
time,
{
value: sent,
}
);
const diff = Number(ETHbalanceBefore) - Number(sent);
console.log(`sent ${sent}`);
console.log(`difference ${diff}`);
const ETHbalanceAfter = await ethers.provider.getBalance(DAIHolder);
console.log(`ETHBalance after ${ETHbalanceAfter}`);
const holderBalanceAfterETH = await DaiContract.balanceOf(DAIHolder);
console.log(`Dai balance After ETH${holderBalanceAfterETH}`);
}
// 150,000 000 000 000 000 000 000
//150 014 568 346 647 994 343 514
// 150 000 000 000 000 000 000 249
// 15,110,085 000 000 000 000 000 000
//15,110 185 000 000 000 000 000 000
// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment