|
| 1 | +pragma solidity 0.5.11; |
| 2 | + |
| 3 | +import "../governance/Governable.sol"; |
| 4 | +import "../token/OUSD.sol"; |
| 5 | +import "../interfaces/Tether.sol"; |
| 6 | +import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; |
| 7 | +import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/SafeERC20.sol"; |
| 8 | + |
| 9 | +// Contract to exchange usdt, usdc, dai from and to ousd. |
| 10 | +// - 1 to 1. No slippage |
| 11 | +// - Optimized for low gas usage |
| 12 | +// - No guarantee of availability |
| 13 | + |
| 14 | + |
| 15 | +contract FlipperDev is Governable { |
| 16 | + using SafeERC20 for IERC20; |
| 17 | + |
| 18 | + uint256 constant MAXIMUM_PER_TRADE = (25000 * 1e18); |
| 19 | + |
| 20 | + // Settable coin addresses allow easy testing and use of mock currencies. |
| 21 | + IERC20 dai = IERC20(0); |
| 22 | + OUSD ousd = OUSD(0); |
| 23 | + IERC20 usdc = IERC20(0); |
| 24 | + Tether usdt = Tether(0); |
| 25 | + |
| 26 | + // --------------------- |
| 27 | + // Dev constructor |
| 28 | + // --------------------- |
| 29 | + constructor( |
| 30 | + address dai_, |
| 31 | + address ousd_, |
| 32 | + address usdc_, |
| 33 | + address usdt_ |
| 34 | + ) public { |
| 35 | + dai = IERC20(dai_); |
| 36 | + ousd = OUSD(ousd_); |
| 37 | + usdc = IERC20(usdc_); |
| 38 | + usdt = Tether(usdt_); |
| 39 | + require(address(ousd) != address(0)); |
| 40 | + require(address(dai) != address(0)); |
| 41 | + require(address(usdc) != address(0)); |
| 42 | + require(address(usdt) != address(0)); |
| 43 | + } |
| 44 | + |
| 45 | + // ----------------- |
| 46 | + // Trading functions |
| 47 | + // ----------------- |
| 48 | + |
| 49 | + /// @notice Purchase OUSD with Dai |
| 50 | + /// @param amount Amount of OUSD to purchase, in 18 fixed decimals. |
| 51 | + function buyOusdWithDai(uint256 amount) external { |
| 52 | + require(amount <= MAXIMUM_PER_TRADE, "Amount too large"); |
| 53 | + require(dai.transferFrom(msg.sender, address(this), amount)); |
| 54 | + require(ousd.transfer(msg.sender, amount)); |
| 55 | + } |
| 56 | + |
| 57 | + /// @notice Sell OUSD for Dai |
| 58 | + /// @param amount Amount of OUSD to sell, in 18 fixed decimals. |
| 59 | + function sellOusdForDai(uint256 amount) external { |
| 60 | + require(amount <= MAXIMUM_PER_TRADE, "Amount too large"); |
| 61 | + require(dai.transfer(msg.sender, amount)); |
| 62 | + require(ousd.transferFrom(msg.sender, address(this), amount)); |
| 63 | + } |
| 64 | + |
| 65 | + /// @notice Purchase OUSD with USDC |
| 66 | + /// @param amount Amount of OUSD to purchase, in 18 fixed decimals. |
| 67 | + function buyOusdWithUsdc(uint256 amount) external { |
| 68 | + require(amount <= MAXIMUM_PER_TRADE, "Amount too large"); |
| 69 | + // Potential rounding error is an intentional tradeoff |
| 70 | + require(usdc.transferFrom(msg.sender, address(this), amount / 1e12)); |
| 71 | + require(ousd.transfer(msg.sender, amount)); |
| 72 | + } |
| 73 | + |
| 74 | + /// @notice Sell OUSD for USDC |
| 75 | + /// @param amount Amount of OUSD to sell, in 18 fixed decimals. |
| 76 | + function sellOusdForUsdc(uint256 amount) external { |
| 77 | + require(amount <= MAXIMUM_PER_TRADE, "Amount too large"); |
| 78 | + require(usdc.transfer(msg.sender, amount / 1e12)); |
| 79 | + require(ousd.transferFrom(msg.sender, address(this), amount)); |
| 80 | + } |
| 81 | + |
| 82 | + /// @notice Purchase OUSD with USDT |
| 83 | + /// @param amount Amount of OUSD to purchase, in 18 fixed decimals. |
| 84 | + function buyOusdWithUsdt(uint256 amount) external { |
| 85 | + require(amount <= MAXIMUM_PER_TRADE, "Amount too large"); |
| 86 | + // Potential rounding error is an intentional tradeoff |
| 87 | + // USDT does not return a boolean and reverts, |
| 88 | + // so no need for a require. |
| 89 | + usdt.transferFrom(msg.sender, address(this), amount / 1e12); |
| 90 | + require(ousd.transfer(msg.sender, amount)); |
| 91 | + } |
| 92 | + |
| 93 | + /// @notice Sell OUSD for USDT |
| 94 | + /// @param amount Amount of OUSD to sell, in 18 fixed decimals. |
| 95 | + function sellOusdForUsdt(uint256 amount) external { |
| 96 | + require(amount <= MAXIMUM_PER_TRADE, "Amount too large"); |
| 97 | + // USDT does not return a boolean and reverts, |
| 98 | + // so no need for a require. |
| 99 | + usdt.transfer(msg.sender, amount / 1e12); |
| 100 | + require(ousd.transferFrom(msg.sender, address(this), amount)); |
| 101 | + } |
| 102 | + |
| 103 | + // -------------------- |
| 104 | + // Governance functions |
| 105 | + // -------------------- |
| 106 | + |
| 107 | + /// @dev Opting into yield reduces the gas cost per transfer by about 4K, since |
| 108 | + /// ousd needs to do less accounting and one less storage write. |
| 109 | + function rebaseOptIn() external onlyGovernor nonReentrant { |
| 110 | + ousd.rebaseOptIn(); |
| 111 | + } |
| 112 | + |
| 113 | + /// @notice Owner function to withdraw a specific amount of a token |
| 114 | + function withdraw(address token, uint256 amount) |
| 115 | + external |
| 116 | + onlyGovernor |
| 117 | + nonReentrant |
| 118 | + { |
| 119 | + IERC20(token).safeTransfer(_governor(), amount); |
| 120 | + } |
| 121 | + |
| 122 | + /// @notice Owner function to withdraw all tradable tokens |
| 123 | + /// @dev Equivalent to "pausing" the contract. |
| 124 | + function withdrawAll() external onlyGovernor nonReentrant { |
| 125 | + IERC20(dai).safeTransfer(_governor(), dai.balanceOf(address(this))); |
| 126 | + IERC20(ousd).safeTransfer(_governor(), ousd.balanceOf(address(this))); |
| 127 | + IERC20(address(usdt)).safeTransfer( |
| 128 | + _governor(), |
| 129 | + usdt.balanceOf(address(this)) |
| 130 | + ); |
| 131 | + IERC20(usdc).safeTransfer(_governor(), usdc.balanceOf(address(this))); |
| 132 | + } |
| 133 | +} |
0 commit comments