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
- const hre = require("hardhat"); -const Str = require('@supercharge/strings') +const Str = require("@supercharge/strings"); async function main() { // Deploying contracts const ERC20TD = await hre.ethers.getContractFactory("ERC20TD"); const Evaluator = await hre.ethers.getContractFactory("Evaluator"); const DummyToken = await hre.ethers.getContractFactory("DummyToken"); - const erc20 = await ERC20TD.deploy("TD-AMM-101","TD-AMM-101",0); - const dummytoken = await DummyToken.deploy("dummyToken", "DTK",ethers.BigNumber.from("2000000000000000000000000000000")); + const ExerciceSol = await hre.ethers.getContractFactory("ExerciceSol"); + const MyToken = await hre.ethers.getContractFactory("MyToken"); + const erc20 = await ERC20TD.deploy("TD-AMM-101", "TD-AMM-101", 0); await erc20.deployed(); - - console.log( - `ERC20TD deployed at ${erc20.address}` + console.log(`ERC20TD deployed at ${erc20.address}`); + + const dummytoken = await DummyToken.deploy( + "dummyToken", + "DTK", + ethers.BigNumber.from("2000000000000000000000000000000") ); await dummytoken.deployed(); - console.log( - `DummyToken deployed at ${dummytoken.address}` + console.log(`DummyToken deployed at ${dummytoken.address}`); + + // Deploying ExerciceSol + const exerciceSol = await ExerciceSol.deploy(); + await exerciceSol.deployed(); + console.log("ExerciceSol deployed at:", exerciceSol.address); + + // Deploying MyToken + const myToken = await MyToken.deploy(); + await myToken.deployed(); + console.log("MyToken deployed at:", myToken.address); + + uniswapV2FactoryAddress = "0x5c69bee701ef814a2b6a3edd4b1652cb9cc5aa6f"; + wethAddress = "0xb4fbf271143f4fbf7b91a5ded31805e42b2208d6"; + const evaluator = await Evaluator.deploy( + erc20.address, + dummytoken.address, + uniswapV2FactoryAddress, + wethAddress ); - uniswapV2FactoryAddress = "0x5c69bee701ef814a2b6a3edd4b1652cb9cc5aa6f" - wethAddress = "0xb4fbf271143f4fbf7b91a5ded31805e42b2208d6" - const evaluator = await Evaluator.deploy(erc20.address, dummytoken.address, uniswapV2FactoryAddress, wethAddress) await evaluator.deployed(); - console.log( - `Evaluator deployed at ${evaluator.address}` - ); + console.log(`Evaluator deployed at ${evaluator.address}`); + // Setting the teacher + await erc20.setTeacher(evaluator.address, true); - // Setting the teacher - await erc20.setTeacher(evaluator.address, true) - - // Setting random values - randomSupplies = [] - randomTickers = [] - for (i = 0; i < 20; i++) - { - randomSupplies.push(Math.floor(Math.random()*1000000000)) - randomTickers.push(Str.random(5)) - // randomTickers.push(web3.utils.utf8ToBytes(Str.random(5))) - // randomTickers.push(Str.random(5)) - } - - console.log(randomTickers) - console.log(randomSupplies) - // console.log(web3.utils) - // console.log(type(Str.random(5)0) - await evaluator.setRandomTickersAndSupply(randomSupplies, randomTickers); + // Setting random values + randomSupplies = []; + randomTickers = []; + for (i = 0; i < 20; i++) { + randomSupplies.push(Math.floor(Math.random() * 1000000000)); + randomTickers.push(Str.random(5)); + } + console.log(randomTickers); + console.log(randomSupplies); + await evaluator.setRandomTickersAndSupply(randomSupplies, randomTickers); } -// 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;