Created
October 29, 2022 12:27
-
-
Save dobestan/b19786273cd361b4142e2a7cce97eecc to your computer and use it in GitHub Desktop.
디사이퍼
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity >= 0.8.0; | |
import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | |
import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; | |
contract Exchange is ERC20 { | |
IERC20 public token; | |
constructor(address _token) ERC20("Uniswap V1", "UNI-V1") { | |
token = IERC20(_token); | |
} | |
function getInputPrice( | |
uint input_amount, | |
uint input_reserve, | |
uint output_reserve | |
) public pure returns (uint output_amount) { | |
uint numerator = output_reserve * input_amount; | |
uint denominator = input_amount + input_reserve; | |
output_amount = numerator / denominator; | |
return output_amount; | |
} | |
function ethToTokenInput( | |
uint eth_sold, | |
uint min_tokens, | |
uint deadline, | |
address buyer, | |
address recipient | |
) public payable returns (uint tokens_bought) { | |
tokens_bought = getInputPrice( | |
eth_sold, | |
address(this).balance - eth_sold, | |
token.balanceOf(address(this)) | |
); | |
token.transfer(recipient, tokens_bought); | |
} | |
function ethToTokenSwapInput( | |
uint min_tokens, | |
uint deadline | |
) public payable returns (uint) { | |
return ethToTokenInput(msg.value, min_tokens, deadline, msg.sender, msg.sender); | |
} | |
function ethToTokenTransferInput( | |
uint min_tokens, | |
uint deadline, | |
address recipient | |
) public payable returns (uint) { | |
return ethToTokenInput(msg.value, min_tokens, deadline, msg.sender, recipient); | |
} | |
function addLiquidity( | |
uint min_liquidity, | |
uint max_tokens, | |
uint deadline | |
) public payable returns (uint liquidity_minted) { | |
if (totalSupply() > 0) { | |
uint token_amount = msg.value * token.balanceOf(address(this)) / (address(this).balance - msg.value); | |
token.transferFrom(msg.sender, address(this), token_amount); | |
liquidity_minted = msg.value * balanceOf(address(this)) / (address(this).balance - msg.value); | |
_mint(msg.sender, liquidity_minted); | |
} else { | |
token.transferFrom(msg.sender, address(this), max_tokens); | |
_mint(msg.sender, msg.value); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment