|
| 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 | +} |
0 commit comments