From 8173e0866e2511848e576c23e4c65b01ed8ad05a Mon Sep 17 00:00:00 2001 From: radia12 <91352342+radia12@users.noreply.github.com> Date: Sat, 22 Apr 2023 19:53:55 +0200 Subject: [PATCH] Migrate from ganache to hardhat --- .gitignore | 8 ++-- contracts/ExerciceSol.sol | 67 ++++++++++++++++++++++++++++++++ contracts/MyToken.sol | 11 ++++++ hardhat.config.js | 16 ++++---- scripts/deployTD.js | 81 ++++++++++++++++++++------------------- 5 files changed, 132 insertions(+), 51 deletions(-) create mode 100644 contracts/ExerciceSol.sol create mode 100644 contracts/MyToken.sol diff --git a/.gitignore b/.gitignore index 9c1a3ee..d1aa899 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ node_modules -.env -artifacts/ -*DS_Store* -cache/ \ No newline at end of file +*2_td_test.js* +secrets.json +package.json +package-lock.json \ No newline at end of file diff --git a/contracts/ExerciceSol.sol b/contracts/ExerciceSol.sol new file mode 100644 index 0000000..9c34b9c --- /dev/null +++ b/contracts/ExerciceSol.sol @@ -0,0 +1,67 @@ +pragma solidity ^0.8.0; + +import "./IExerciceSolution.sol"; +import "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol"; +import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; + +contract ExerciceSolution is IExerciceSolution { + IUniswapV2Router02 public router; + ERC20 public myToken; + ERC20 public WETH; + ERC20 public DummyToken; + ERC20 public UniswapLiquid; + + constructor(address _router, address _WETH, address _UniswapLiquid, address _DummyToken, address _myToken) { + router = IUniswapV2Router02(_router); + myToken = ERC20(_myToken); + WETH = ERC20(_WETH); + DummyToken = ERC20(_DummyToken); + UniswapLiquid = ERC20(_UniswapLiquid); + } + + function addLiquidity() override external { + // Approve the transfer of myToken and WETH to the Uniswap router + uint256 myTokenLiquidity = 3546500; + uint256 WETHLiquidity = 148300000000000000; + require(myToken.approve(address(router), myTokenLiquidity), "Failed to approve myToken transfer"); + require(WETH.approve(address(router), WETHLiquidity), "Failed to approve WETH transfer"); + + // Call the addLiquidity function of the Uniswap router + router.addLiquidity( + address(myToken), + address(WETH), + myTokenLiquidity, + WETHLiquidity, + 0, + 0, + address(this), + block.timestamp + ); + } + + function withdrawLiquidity() override external { + uint256 liquidity = 10000; + require(UniswapLiquid.approve(address(router), liquidity), "UniswapLiquid couldn't be approved"); + router.removeLiquidity(address(myToken), address(WETH), liquidity, 0, 0, address(this), block.timestamp); + } + + function swapYourTokenForDummyToken() override external { + uint256 liquidity = 100000; + require(myToken.approve(address(router), liquidity), "Your token couldn't be approved"); + require(DummyToken.approve(address(router), 100000), "Dummy token couldn't be approved"); + address[] memory path = new address[](2); + path[0] = address(myToken); + path[1] = address(DummyToken); + router.swapExactTokensForTokens(liquidity, 10000, path, address(this), block.timestamp); + } + + function swapYourTokenForEth() override external { + uint256 liquidity = 100000; + require(myToken.approve(address(router), liquidity), "Your token couldn't be approved"); + require(WETH.approve(address(router), 1000), "WETH couldn't be approved"); + address[] memory path = new address[](2); + path[0] = address(myToken); + path[1] = address(WETH); + router.swapExactTokensForETH(liquidity, 1000, path, address(this), block.timestamp); + } +} diff --git a/contracts/MyToken.sol b/contracts/MyToken.sol new file mode 100644 index 0000000..01f80f5 --- /dev/null +++ b/contracts/MyToken.sol @@ -0,0 +1,11 @@ +pragma solidity ^0.8.0; + +import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; + +contract 4heKr is ERC20 { + +constructor(string memory name, string memory symbol,uint256 initialSupply) public ERC20(name, symbol) { + _mint(msg.sender, initialSupply); + } + +} \ No newline at end of file diff --git a/hardhat.config.js b/hardhat.config.js index f1f63ed..d87311a 100644 --- a/hardhat.config.js +++ b/hardhat.config.js @@ -1,21 +1,21 @@ -require("@nomicfoundation/hardhat-toolbox"); -require('dotenv').config() +const { mnemonic, infuraApiKey, etherscanApiKey } = require('./secrets.json'); +require("@nomiclabs/hardhat-waffle"); +require("@nomiclabs/hardhat-etherscan"); -/** @type import('hardhat/config').HardhatUserConfig */ module.exports = { networks: { hardhat: { }, goerli: { - url: "https://goerli.infura.io/v3/" + process.env.infura, + url: `https://goerli.infura.io/v3/${infuraApiKey}`, accounts: { - mnemonic: process.env.mnemonic + mnemonic: mnemonic } }, sepolia: { - url: "https://sepolia.infura.io/v3/" + process.env.infura, + url: `https://sepolia.infura.io/v3/${infuraApiKey}`, accounts: { - mnemonic: process.env.mnemonic + mnemonic: mnemonic } } }, @@ -29,6 +29,6 @@ module.exports = { } }, etherscan: { - apiKey: process.env.etherscan, + apiKey: etherscanApiKey, }, }; diff --git a/scripts/deployTD.js b/scripts/deployTD.js index 9f008b8..d21d4f3 100644 --- a/scripts/deployTD.js +++ b/scripts/deployTD.js @@ -1,60 +1,63 @@ -// Deploying the TD somewhere -// To verify it on Etherscan: -// npx hardhat verify --network sepolia