├── .eslintrc.json ├── .gitignore ├── Contract ├── Arbitrage │ ├── .env.example │ ├── .eslintignore │ ├── .eslintrc.js │ ├── .gitignore │ ├── .npmignore │ ├── .prettierignore │ ├── .prettierrc │ ├── .solhint.json │ ├── .solhintignore │ ├── README.md │ ├── contracts │ │ ├── BundleExecutor.sol │ │ ├── Greeter.sol │ │ └── UniswapFlash.sol │ ├── hardhat.config.ts │ ├── package-lock.json │ ├── package.json │ ├── scripts │ │ └── deploy.ts │ ├── test │ │ └── index.ts │ └── tsconfig.json └── SandwichBot │ ├── .env.example │ ├── .eslintignore │ ├── .eslintrc.js │ ├── .gitignore │ ├── .npmignore │ ├── .prettierignore │ ├── .prettierrc │ ├── .solhint.json │ ├── .solhintignore │ ├── LICENSE │ ├── README.md │ ├── What is MEV.png │ ├── contracts │ ├── FrontRunning.sol │ ├── Greeter.sol │ ├── SandwichBot.sol │ └── interface │ │ └── IERC20.sol │ ├── hardhat.config.js │ ├── package-lock.json │ ├── package.json │ ├── scripts │ └── deploy.js │ └── test │ ├── FrontRunning.test.js │ ├── Sandwich.test.js │ └── sample.test.js ├── README.md ├── next-env.d.ts ├── next.config.js ├── package.json ├── pages ├── _app.tsx ├── api │ └── hello.ts └── index.tsx ├── public ├── favicon.ico └── vercel.svg ├── styles ├── Home.module.css └── globals.css ├── tsconfig.json ├── utils └── images │ ├── Flashbots.png │ ├── Home.png │ └── MEV.png └── yarn.lock /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "next/core-web-vitals" 3 | } 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # next.js 12 | /.next/ 13 | /out/ 14 | 15 | # production 16 | /build 17 | 18 | # misc 19 | .DS_Store 20 | *.pem 21 | 22 | # debug 23 | npm-debug.log* 24 | yarn-debug.log* 25 | yarn-error.log* 26 | .pnpm-debug.log* 27 | 28 | # local env files 29 | .env*.local 30 | 31 | # vercel 32 | .vercel 33 | 34 | # typescript 35 | *.tsbuildinfo 36 | -------------------------------------------------------------------------------- /Contract/Arbitrage/.env.example: -------------------------------------------------------------------------------- 1 | ETHERSCAN_API_KEY=ABC123ABC123ABC123ABC123ABC123ABC1 2 | ROPSTEN_URL=https://eth-ropsten.alchemyapi.io/v2/ 3 | PRIVATE_KEY=0xabc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc1 4 | -------------------------------------------------------------------------------- /Contract/Arbitrage/.eslintignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | artifacts 3 | cache 4 | coverage 5 | -------------------------------------------------------------------------------- /Contract/Arbitrage/.eslintrc.js: -------------------------------------------------------------------------------- 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 | }; 25 | -------------------------------------------------------------------------------- /Contract/Arbitrage/.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .env 3 | coverage 4 | coverage.json 5 | typechain 6 | 7 | #Hardhat files 8 | cache 9 | artifacts 10 | -------------------------------------------------------------------------------- /Contract/Arbitrage/.npmignore: -------------------------------------------------------------------------------- 1 | hardhat.config.ts 2 | scripts 3 | test 4 | -------------------------------------------------------------------------------- /Contract/Arbitrage/.prettierignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | artifacts 3 | cache 4 | coverage* 5 | gasReporterOutput.json 6 | -------------------------------------------------------------------------------- /Contract/Arbitrage/.prettierrc: -------------------------------------------------------------------------------- 1 | {} 2 | -------------------------------------------------------------------------------- /Contract/Arbitrage/.solhint.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "solhint:recommended", 3 | "rules": { 4 | "compiler-version": ["error", "^0.8.0"], 5 | "func-visibility": ["warn", { "ignoreConstructors": true }] 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Contract/Arbitrage/.solhintignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /Contract/Arbitrage/README.md: -------------------------------------------------------------------------------- 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). 47 | -------------------------------------------------------------------------------- /Contract/Arbitrage/contracts/BundleExecutor.sol: -------------------------------------------------------------------------------- 1 | //SPDX-License-Identifier: UNLICENSED 2 | pragma solidity ^0.8.0 3 | 4 | pragma experimental ABIEncoderV2; 5 | 6 | interface IERC20 { 7 | event Approval(address indexed owner, address indexed spender, uint value); 8 | event Transfer(address indexed from, address indexed to, uint value); 9 | 10 | function name() external view returns (string memory); 11 | function symbol() external view returns (string memory); 12 | function decimals() external view returns (uint8); 13 | function totalSupply() external view returns (uint); 14 | function balanceOf(address owner) external view returns (uint); 15 | function allowance(address owner, address spender) external view returns (uint); 16 | 17 | function approve(address spender, uint value) external returns (bool); 18 | function transfer(address to, uint value) external returns (bool); 19 | function transferFrom(address from, address to, uint value) external returns (bool); 20 | } 21 | 22 | interface IWETH is IERC20 { 23 | function deposit() external payable; 24 | function withdraw(uint) external; 25 | } 26 | 27 | // This contract simply calls multiple targets sequentially, ensuring WETH balance before and after 28 | 29 | contract FlashBotsMultiCall { 30 | address private immutable owner; 31 | address private immutable executor; 32 | IWETH private constant WETH = IWETH(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2); 33 | 34 | modifier onlyExecutor() { 35 | require(msg.sender == executor); 36 | _; 37 | } 38 | 39 | modifier onlyOwner() { 40 | require(msg.sender == owner); 41 | _; 42 | } 43 | 44 | constructor(address _executor) public payable { 45 | owner = msg.sender; 46 | executor = _executor; 47 | if (msg.value > 0) { 48 | WETH.deposit{value: msg.value}(); 49 | } 50 | } 51 | 52 | receive() external payable { 53 | } 54 | 55 | function uniswapWeth(uint256 _wethAmountToFirstMarket, uint256 _ethAmountToCoinbase, address[] memory _targets, bytes[] memory _payloads) external onlyExecutor payable { 56 | require (_targets.length == _payloads.length); 57 | uint256 _wethBalanceBefore = WETH.balanceOf(address(this)); 58 | WETH.transfer(_targets[0], _wethAmountToFirstMarket); 59 | for (uint256 i = 0; i < _targets.length; i++) { 60 | (bool _success, bytes memory _response) = _targets[i].call(_payloads[i]); 61 | require(_success); _response; 62 | } 63 | 64 | uint256 _wethBalanceAfter = WETH.balanceOf(address(this)); 65 | require(_wethBalanceAfter > _wethBalanceBefore + _ethAmountToCoinbase); 66 | if (_ethAmountToCoinbase == 0) return; 67 | 68 | uint256 _ethBalance = address(this).balance; 69 | if (_ethBalance < _ethAmountToCoinbase) { 70 | WETH.withdraw(_ethAmountToCoinbase - _ethBalance); 71 | } 72 | block.coinbase.transfer(_ethAmountToCoinbase); 73 | } 74 | 75 | function call(address payable _to, uint256 _value, bytes calldata _data) external onlyOwner payable returns (bytes memory) { 76 | require(_to != address(0)); 77 | (bool _success, bytes memory _result) = _to.call{value: _value}(_data); 78 | require(_success); 79 | return _result; 80 | } 81 | } -------------------------------------------------------------------------------- /Contract/Arbitrage/contracts/Greeter.sol: -------------------------------------------------------------------------------- 1 | //SPDX-License-Identifier: Unlicense 2 | pragma solidity ^0.8.0; 3 | 4 | import "hardhat/console.sol"; 5 | 6 | contract Greeter { 7 | string private greeting; 8 | 9 | constructor(string memory _greeting) { 10 | console.log("Deploying a Greeter with greeting:", _greeting); 11 | greeting = _greeting; 12 | } 13 | 14 | function greet() public view returns (string memory) { 15 | return greeting; 16 | } 17 | 18 | function setGreeting(string memory _greeting) public { 19 | console.log("Changing greeting from '%s' to '%s'", greeting, _greeting); 20 | greeting = _greeting; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Contract/Arbitrage/contracts/UniswapFlash.sol: -------------------------------------------------------------------------------- 1 | 2 | //SPDX-License-Identifier: MIT 3 | pragma solidity ^0.8.0; 4 | 5 | pragma experimental ABIEncoderV2; 6 | 7 | interface IUniswapV2Pair { 8 | function token0() external view returns (address); 9 | function token1() external view returns (address); 10 | function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast); 11 | } 12 | 13 | abstract contract UniswapV2Factory { 14 | mapping(address => mapping(address => address)) public getPair; 15 | address[] public allPairs; 16 | function allPairsLength() external view virtual returns (uint); 17 | } 18 | 19 | // In order to quickly load up data from Uniswap-like market, this contract allows easy iteration with a single eth_call 20 | contract FlashBotsUniswapQuery { 21 | function getReservesByPairs(IUniswapV2Pair[] calldata _pairs) external view returns (uint256[3][] memory) { 22 | uint256[3][] memory result = new uint256[3][](_pairs.length); 23 | for (uint i = 0; i < _pairs.length; i++) { 24 | (result[i][0], result[i][1], result[i][2]) = _pairs[i].getReserves(); 25 | } 26 | return result; 27 | } 28 | 29 | function getPairsByIndexRange(UniswapV2Factory _uniswapFactory, uint256 _start, uint256 _stop) external view returns (address[3][] memory) { 30 | uint256 _allPairsLength = _uniswapFactory.allPairsLength(); 31 | if (_stop > _allPairsLength) { 32 | _stop = _allPairsLength; 33 | } 34 | require(_stop >= _start, "start cannot be higher than stop"); 35 | uint256 _qty = _stop - _start; 36 | address[3][] memory result = new address[3][](_qty); 37 | for (uint i = 0; i < _qty; i++) { 38 | IUniswapV2Pair _uniswapPair = IUniswapV2Pair(_uniswapFactory.allPairs(_start + i)); 39 | result[i][0] = _uniswapPair.token0(); 40 | result[i][1] = _uniswapPair.token1(); 41 | result[i][2] = address(_uniswapPair); 42 | } 43 | return result; 44 | } 45 | } -------------------------------------------------------------------------------- /Contract/Arbitrage/hardhat.config.ts: -------------------------------------------------------------------------------- 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; 44 | -------------------------------------------------------------------------------- /Contract/Arbitrage/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hardhat-project", 3 | "devDependencies": { 4 | "@nomiclabs/hardhat-ethers": "^2.0.6", 5 | "@nomiclabs/hardhat-etherscan": "^3.1.0", 6 | "@nomiclabs/hardhat-waffle": "^2.0.3", 7 | "@typechain/ethers-v5": "^7.2.0", 8 | "@typechain/hardhat": "^2.3.1", 9 | "@types/chai": "^4.3.1", 10 | "@types/mocha": "^9.1.1", 11 | "@types/node": "^12.20.55", 12 | "@typescript-eslint/eslint-plugin": "^4.33.0", 13 | "@typescript-eslint/parser": "^4.33.0", 14 | "chai": "^4.3.6", 15 | "dotenv": "^16.0.1", 16 | "eslint": "^7.32.0", 17 | "eslint-config-prettier": "^8.5.0", 18 | "eslint-config-standard": "^16.0.3", 19 | "eslint-plugin-import": "^2.26.0", 20 | "eslint-plugin-node": "^11.1.0", 21 | "eslint-plugin-prettier": "^3.4.1", 22 | "eslint-plugin-promise": "^5.2.0", 23 | "ethereum-waffle": "^3.4.4", 24 | "ethers": "^5.6.8", 25 | "hardhat": "^2.9.9", 26 | "hardhat-gas-reporter": "^1.0.8", 27 | "prettier": "^2.7.0", 28 | "prettier-plugin-solidity": "^1.0.0-beta.13", 29 | "solhint": "^3.3.7", 30 | "solidity-coverage": "^0.7.21", 31 | "ts-node": "^10.8.1", 32 | "typechain": "^5.2.0", 33 | "typescript": "^4.7.3" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Contract/Arbitrage/scripts/deploy.ts: -------------------------------------------------------------------------------- 1 | // We require the Hardhat Runtime Environment explicitly here. This is optional 2 | // but useful for running the script in a standalone fashion through `node