Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate from ganache to hardhat #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
node_modules
.env
artifacts/
*DS_Store*
cache/
*2_td_test.js*
secrets.json
package.json
package-lock.json
67 changes: 67 additions & 0 deletions contracts/ExerciceSol.sol
Original file line number Diff line number Diff line change
@@ -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);
}
}
11 changes: 11 additions & 0 deletions contracts/MyToken.sol
Original file line number Diff line number Diff line change
@@ -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);
}

}
16 changes: 8 additions & 8 deletions hardhat.config.js
Original file line number Diff line number Diff line change
@@ -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
}
}
},
Expand All @@ -29,6 +29,6 @@ module.exports = {
}
},
etherscan: {
apiKey: process.env.etherscan,
apiKey: etherscanApiKey,
},
};
81 changes: 42 additions & 39 deletions scripts/deployTD.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,63 @@
// Deploying the TD somewhere
// To verify it on Etherscan:
// npx hardhat verify --network sepolia <address> <constructor arg 1> <constructor arg 2>

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;
Expand Down