Skip to content

Commit 23d4081

Browse files
author
v.semkin
committed
initialized
0 parents  commit 23d4081

18 files changed

+52396
-0
lines changed

.env.example

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ETHERSCAN_API_KEY=ABC123ABC123ABC123ABC123ABC123ABC1
2+
ROPSTEN_URL=https://eth-ropsten.alchemyapi.io/v2/<YOUR ALCHEMY KEY>
3+
PRIVATE_KEY=0xabc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc1

.eslintignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
artifacts
3+
cache
4+
coverage

.eslintrc.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module.exports = {
2+
env: {
3+
browser: false,
4+
es2021: true,
5+
mocha: true,
6+
node: true,
7+
},
8+
plugins: ["@typescript-eslint"],
9+
extends: [
10+
"standard",
11+
"plugin:prettier/recommended",
12+
"plugin:node/recommended",
13+
],
14+
parser: "@typescript-eslint/parser",
15+
parserOptions: {
16+
ecmaVersion: 12,
17+
},
18+
rules: {
19+
"node/no-unsupported-features/es-syntax": [
20+
"error",
21+
{ ignores: ["modules"] },
22+
],
23+
},
24+
};

.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
node_modules
2+
.env
3+
coverage
4+
coverage.json
5+
typechain
6+
7+
#Hardhat files
8+
cache
9+
artifacts

.npmignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
hardhat.config.ts
2+
scripts
3+
test

.prettierignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
artifacts
3+
cache
4+
coverage*
5+
gasReporterOutput.json

.prettierrc

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"overrides": [
3+
{
4+
"files": "*.sol",
5+
"options": {
6+
"printWidth": 120
7+
}
8+
},
9+
{
10+
"files": "*.ts",
11+
"options": {
12+
"tabWidth": 4,
13+
"printWidth": 120
14+
}
15+
}
16+
]
17+
}
18+

.solhint.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "solhint:recommended",
3+
"rules": {
4+
"func-visibility": ["warn", { "ignoreConstructors": true }]
5+
}
6+
}

.solhintignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

README.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Advanced Sample Hardhat Project
2+
3+
This project demonstrates an advanced Hardhat use case, integrating other tools commonly used alongside Hardhat in the ecosystem.
4+
5+
The project comes with a sample contract, a test for that contract, a sample script that deploys that contract, and an example of a task implementation, which simply lists the available accounts. It also comes with a variety of other tools, preconfigured to work with the project code.
6+
7+
Try running some of the following tasks:
8+
9+
```shell
10+
npx hardhat accounts
11+
npx hardhat compile
12+
npx hardhat clean
13+
npx hardhat test
14+
npx hardhat node
15+
npx hardhat help
16+
REPORT_GAS=true npx hardhat test
17+
npx hardhat coverage
18+
npx hardhat run scripts/deploy.ts
19+
TS_NODE_FILES=true npx ts-node scripts/deploy.ts
20+
npx eslint '**/*.{js,ts}'
21+
npx eslint '**/*.{js,ts}' --fix
22+
npx prettier '**/*.{json,sol,md}' --check
23+
npx prettier '**/*.{json,sol,md}' --write
24+
npx solhint 'contracts/**/*.sol'
25+
npx solhint 'contracts/**/*.sol' --fix
26+
```
27+
28+
# Etherscan verification
29+
30+
To try out Etherscan verification, you first need to deploy a contract to an Ethereum network that's supported by Etherscan, such as Ropsten.
31+
32+
In this project, copy the .env.example file to a file named .env, and then edit it to fill in the details. Enter your Etherscan API key, your Ropsten node URL (eg from Alchemy), and the private key of the account which will send the deployment transaction. With a valid .env file in place, first deploy your contract:
33+
34+
```shell
35+
hardhat run --network ropsten scripts/deploy.ts
36+
```
37+
38+
Then, copy the deployment address and paste it in to replace `DEPLOYED_CONTRACT_ADDRESS` in this command:
39+
40+
```shell
41+
npx hardhat verify --network ropsten DEPLOYED_CONTRACT_ADDRESS "Hello, Hardhat!"
42+
```
43+
44+
# Performance optimizations
45+
46+
For faster runs of your tests and scripts, consider skipping ts-node's type checking by setting the environment variable `TS_NODE_TRANSPILE_ONLY` to `1` in hardhat's environment. For more details see [the documentation](https://hardhat.org/guides/typescript.html#performance-optimizations).

contracts/Dex.sol

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
pragma solidity ^0.8.4;
2+
// SPDX-License-Identifier: MIT
3+
// import "hardhat/console.sol";
4+
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
5+
6+
contract DEX {
7+
IERC20 private token;
8+
uint256 public lockedLiquidity;
9+
mapping(address => uint256) public liquidity;
10+
11+
constructor(address _token) {
12+
token = IERC20(_token);
13+
}
14+
15+
function initialize(uint256 _tokenAmount) public payable returns (uint256) {
16+
require(lockedLiquidity == 0, "DEX already initialized.");
17+
require(token.balanceOf(msg.sender) >= _tokenAmount, "Token balance not sufficient.");
18+
require(token.allowance(msg.sender, address(this)) >= _tokenAmount, "Token allowance not sufficient.");
19+
lockedLiquidity = address(this).balance;
20+
liquidity[msg.sender] = lockedLiquidity;
21+
token.transferFrom(msg.sender, address(this), _tokenAmount);
22+
return lockedLiquidity;
23+
}
24+
25+
function price(
26+
uint256 _inputAmount,
27+
uint256 _inputReserve,
28+
uint256 _outputReserve
29+
) public pure returns (uint256) {
30+
uint256 amountWithFeee = _inputAmount * 997;
31+
uint256 numerator = amountWithFeee * _outputReserve;
32+
uint256 denominator = _inputReserve * 1000 + amountWithFeee;
33+
return numerator / denominator;
34+
}
35+
36+
function ethToToken() public payable returns (uint256) {
37+
uint256 tokenReserve = token.balanceOf(address(this));
38+
uint256 tokensBought = price(msg.value, address(this).balance - msg.value, tokenReserve);
39+
require(token.transfer(msg.sender, tokensBought));
40+
return tokensBought;
41+
}
42+
43+
function tokenToEth(uint256 tokens) public returns (uint256) {
44+
uint256 tokenReserve = token.balanceOf(address(this));
45+
uint256 ethBought = price(tokens, tokenReserve, address(this).balance);
46+
(bool sent, ) = msg.sender.call{value: ethBought}("");
47+
require(sent, "Failed to send user eth.");
48+
require(token.transferFrom(msg.sender, address(this), tokens));
49+
return ethBought;
50+
}
51+
52+
function deposit() public payable returns (uint256) {
53+
uint256 ethReserve = address(this).balance - msg.value;
54+
uint256 tokenReserve = token.balanceOf(address(this));
55+
uint256 tokenAmount = ((msg.value * tokenReserve) / ethReserve) + 1;
56+
uint256 liquidityMinted = (msg.value * lockedLiquidity) / ethReserve;
57+
liquidity[msg.sender] += liquidityMinted;
58+
lockedLiquidity += liquidityMinted;
59+
require(token.transferFrom(msg.sender, address(this), tokenAmount));
60+
return liquidityMinted;
61+
}
62+
63+
function withdraw(uint256 _liquidityAmount) public returns (uint256, uint256) {
64+
uint256 tokenReserve = token.balanceOf(address(this));
65+
uint256 ethAmount = (_liquidityAmount * address(this).balance) / lockedLiquidity;
66+
uint256 tokenAmount = (_liquidityAmount * tokenReserve) / lockedLiquidity;
67+
liquidity[msg.sender] -= _liquidityAmount;
68+
lockedLiquidity -= _liquidityAmount;
69+
(bool sent, ) = msg.sender.call{value: ethAmount}("");
70+
require(sent, "Failed to send user eth.");
71+
require(token.transfer(msg.sender, tokenAmount));
72+
return (ethAmount, tokenAmount);
73+
}
74+
}

hardhat.config.ts

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import * as dotenv from "dotenv";
2+
3+
import { HardhatUserConfig, task } from "hardhat/config";
4+
import "@nomiclabs/hardhat-etherscan";
5+
import "@nomiclabs/hardhat-waffle";
6+
import "@typechain/hardhat";
7+
import "hardhat-gas-reporter";
8+
import "solidity-coverage";
9+
10+
dotenv.config();
11+
12+
// This is a sample Hardhat task. To learn how to create your own go to
13+
// https://hardhat.org/guides/create-task.html
14+
task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
15+
const accounts = await hre.ethers.getSigners();
16+
17+
for (const account of accounts) {
18+
console.log(account.address);
19+
}
20+
});
21+
22+
// You need to export an object to set up your config
23+
// Go to https://hardhat.org/config/ to learn more
24+
25+
const config: HardhatUserConfig = {
26+
solidity: "0.8.4",
27+
networks: {
28+
ropsten: {
29+
url: process.env.ROPSTEN_URL || "",
30+
accounts:
31+
process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [],
32+
},
33+
},
34+
gasReporter: {
35+
enabled: process.env.REPORT_GAS !== undefined,
36+
currency: "USD",
37+
},
38+
etherscan: {
39+
apiKey: process.env.ETHERSCAN_API_KEY,
40+
},
41+
};
42+
43+
export default config;

0 commit comments

Comments
 (0)