Skip to content

Instantly share code, notes, and snippets.

@chrisfranko
Created February 19, 2021 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisfranko/fd9fb7022cc7cd364e65fe22da84d3b6 to your computer and use it in GitHub Desktop.
Save chrisfranko/fd9fb7022cc7cd364e65fe22da84d3b6 to your computer and use it in GitHub Desktop.
eggmaker.sol
pragma solidity 0.6.12;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v3.2.0/contracts/token/ERC20/IERC20.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v3.2.0/contracts/token/ERC20/SafeERC20.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v3.2.0/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