Created
February 19, 2021 02:18
-
-
Save chrisfranko/eee3c0a4ca3e59e20387d465ba924097 to your computer and use it in GitHub Desktop.
EggMaker.sol
This file contains hidden or 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
pragma solidity 0.6.12; | |
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/IERC20.sol"; | |
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/SafeERC20.sol"; | |
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/math/SafeMath.sol"; | |
import "https://github.com/eggswap/eggswap/blob/master/contracts/uniswapv2/interfaces/IUniswapV2ERC20.sol"; | |
import "https://github.com/eggswap/eggswap/blob/master/contracts/uniswapv2/interfaces/IUniswapV2Pair.sol"; | |
import "https://github.com/eggswap/eggswap/blob/master/contracts/uniswapv2/interfaces/IUniswapV2Factory.sol"; | |
contract EggMaker { | |
using SafeMath for uint256; | |
using SafeERC20 for IERC20; | |
IUniswapV2Factory public factory; | |
address public coop; | |
address public egg; | |
address public wexp; | |
constructor(IUniswapV2Factory _factory, address _coop, address _egg, address _wexp) public { | |
factory = _factory; | |
egg = _egg; | |
coop = _coop; | |
wexp = _wexp; | |
} | |
function convert(address token0, address token1) public { | |
// At least we try to make front-running harder to do. | |
require(msg.sender == tx.origin, "do not convert from contract"); | |
IUniswapV2Pair pair = IUniswapV2Pair(factory.getPair(token0, token1)); | |
pair.transfer(address(pair), pair.balanceOf(address(this))); | |
pair.burn(address(this)); | |
uint256 wexpAmount = _toWEXP(token0) + _toWEXP(token1); | |
_toEGG(wexpAmount); | |
} | |
function _toWEXP(address token) internal returns (uint256) { | |
if (token == egg) { | |
uint amount = IERC20(token).balanceOf(address(this)); | |
_safeTransfer(token, coop, amount); | |
return 0; | |
} | |
if (token == wexp) { | |
uint amount = IERC20(token).balanceOf(address(this)); | |
_safeTransfer(token, factory.getPair(wexp, egg), amount); | |
return amount; | |
} | |
IUniswapV2Pair pair = IUniswapV2Pair(factory.getPair(token, wexp)); | |
if (address(pair) == address(0)) { | |
return 0; | |
} | |
(uint reserve0, uint reserve1,) = pair.getReserves(); | |
address token0 = pair.token0(); | |
(uint reserveIn, uint reserveOut) = token0 == token ? (reserve0, reserve1) : (reserve1, reserve0); | |
uint amountIn = IERC20(token).balanceOf(address(this)); | |
uint amountInWithFee = amountIn.mul(997); | |
uint numerator = amountInWithFee.mul(reserveOut); | |
uint denominator = reserveIn.mul(1000).add(amountInWithFee); | |
uint amountOut = numerator / denominator; | |
(uint amount0Out, uint amount1Out) = token0 == token ? (uint(0), amountOut) : (amountOut, uint(0)); | |
_safeTransfer(token, address(pair), amountIn); | |
pair.swap(amount0Out, amount1Out, factory.getPair(wexp, egg), new bytes(0)); | |
return amountOut; | |
} | |
function _toEGG(uint256 amountIn) internal { | |
IUniswapV2Pair pair = IUniswapV2Pair(factory.getPair(wexp, egg)); | |
(uint reserve0, uint reserve1,) = pair.getReserves(); | |
address token0 = pair.token0(); | |
(uint reserveIn, uint reserveOut) = token0 == wexp ? (reserve0, reserve1) : (reserve1, reserve0); | |
uint amountInWithFee = amountIn.mul(997); | |
uint numerator = amountInWithFee.mul(reserveOut); | |
uint denominator = reserveIn.mul(1000).add(amountInWithFee); | |
uint amountOut = numerator / denominator; | |
(uint amount0Out, uint amount1Out) = token0 == wexp ? (uint(0), amountOut) : (amountOut, uint(0)); | |
pair.swap(amount0Out, amount1Out, coop, new bytes(0)); | |
} | |
function _safeTransfer(address token, address to, uint256 amount) internal { | |
IERC20(token).safeTransfer(to, amount); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment