-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #182 from opynfinance/task/exerciser-wrapper
Add Exerciser Wrapper
- Loading branch information
Showing
6 changed files
with
252 additions
and
35 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
pragma solidity 0.5.10; | ||
|
||
import "./interfaces/OtokenInterface.sol"; | ||
import "./interfaces/WethInterface.sol"; | ||
import "./packages/IERC20.sol"; | ||
|
||
|
||
contract Exerciser { | ||
WethInterface public weth; | ||
|
||
event WrapperExercise( | ||
address indexed otoken, | ||
uint256 indexed otokenAmount, | ||
uint256 indexed collateralExercised, | ||
address user | ||
); | ||
|
||
constructor(address payable _weth) public { | ||
weth = WethInterface(_weth); | ||
} | ||
|
||
function exercise( | ||
address _otoken, | ||
uint256 _amount, | ||
address payable[] calldata _owners | ||
) external payable returns (uint256) { | ||
// 1. pull token from user's address | ||
OtokenInterface otoken = OtokenInterface(_otoken); | ||
otoken.transferFrom(msg.sender, address(this), _amount); | ||
// 2. convert eth to weth | ||
weth.deposit.value(msg.value)(); | ||
// 3. exercise | ||
weth.approve(_otoken, msg.value); | ||
otoken.exercise(_amount, _owners); | ||
// 4. transfer collateral to user | ||
address collateral = otoken.collateral(); | ||
uint256 amountToTakeOut = IERC20(collateral).balanceOf(address(this)); | ||
IERC20(collateral).transfer(msg.sender, amountToTakeOut); | ||
// 5. transfer remaining weth back to user | ||
if (weth.balanceOf(address(this)) > 0) { | ||
weth.transfer(msg.sender, weth.balanceOf(address(this))); | ||
} | ||
|
||
emit WrapperExercise(_otoken, _amount, amountToTakeOut, msg.sender); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
pragma solidity ^0.5.10; | ||
|
||
|
||
interface OtokenInterface { | ||
function exercise( | ||
uint256 oTokensToExercise, | ||
address payable[] calldata vaultsToExerciseFrom | ||
) external payable; | ||
|
||
function collateral() external view returns (address); | ||
|
||
function transferFrom( | ||
address sender, | ||
address recipient, | ||
uint256 amount | ||
) external returns (bool); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
pragma solidity ^0.5.10; | ||
|
||
|
||
interface WethInterface { | ||
function deposit() external payable; | ||
|
||
function approve(address sender, uint256 amount) external; | ||
|
||
function balanceOf(address account) external view returns (uint256); | ||
|
||
function transfer(address recipient, uint256 amount) | ||
external | ||
returns (bool); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// Copyright (C) 2015, 2016, 2017 Dapphub | ||
|
||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
// SPDX-License-Identifier: GNU GPL | ||
pragma solidity 0.5.10; | ||
|
||
|
||
/** | ||
* @author Opyn Team | ||
* @title WETH contract | ||
* @dev A wrapper to use ETH as collateral | ||
*/ | ||
contract WETH9 { | ||
string public name = "Wrapped Ether"; | ||
string public symbol = "WETH"; | ||
uint8 public decimals = 18; | ||
|
||
/// @notice emmitted when a sender approve WETH transfer | ||
event Approval(address indexed src, address indexed guy, uint256 wad); | ||
/// @notice emmitted when a sender transfer WETH | ||
event Transfer(address indexed src, address indexed dst, uint256 wad); | ||
/// @notice emitted when a sender deposit ETH into this contract | ||
event Deposit(address indexed dst, uint256 wad); | ||
/// @notice emmited when a sender withdraw ETH from this contract | ||
event Withdrawal(address indexed src, uint256 wad); | ||
|
||
/// @notice mapping between address and WETH balance | ||
mapping(address => uint256) public balanceOf; | ||
/// @notice mapping between addresses and allowance amount | ||
mapping(address => mapping(address => uint256)) public allowance; | ||
|
||
/** | ||
* @notice Wrap deposited ETH into WETH | ||
*/ | ||
function deposit() public payable { | ||
balanceOf[msg.sender] += msg.value; | ||
emit Deposit(msg.sender, msg.value); | ||
} | ||
|
||
/** | ||
* @notice withdraw ETH from contract | ||
* @dev Unwrap from WETH to ETH | ||
* @param _wad amount WETH to unwrap and withdraw | ||
*/ | ||
function withdraw(uint256 _wad) public { | ||
require( | ||
balanceOf[msg.sender] >= _wad, | ||
"WETH9: insufficient sender balance" | ||
); | ||
balanceOf[msg.sender] -= _wad; | ||
msg.sender.transfer(_wad); | ||
emit Withdrawal(msg.sender, _wad); | ||
} | ||
|
||
/** | ||
* @notice get ETH total supply | ||
* @return total supply | ||
*/ | ||
function totalSupply() public view returns (uint256) { | ||
return address(this).balance; | ||
} | ||
|
||
/** | ||
* @notice approve transfer | ||
* @param _guy address to approve | ||
* @param _wad amount of WETH | ||
* @return true if tx succeeded | ||
*/ | ||
function approve(address _guy, uint256 _wad) public returns (bool) { | ||
allowance[msg.sender][_guy] = _wad; | ||
emit Approval(msg.sender, _guy, _wad); | ||
return true; | ||
} | ||
|
||
/** | ||
* @notice transfer WETH | ||
* @param _dst destination address | ||
* @param _wad amount to transfer | ||
* @return true if tx succeeded | ||
*/ | ||
function transfer(address _dst, uint256 _wad) public returns (bool) { | ||
return transferFrom(msg.sender, _dst, _wad); | ||
} | ||
|
||
/** | ||
* @notice transfer from address | ||
* @param _src source address | ||
* @param _dst destination address | ||
* @param _wad amount to transfer | ||
* @return true if tx succeeded | ||
*/ | ||
function transferFrom( | ||
address _src, | ||
address _dst, | ||
uint256 _wad | ||
) public returns (bool) { | ||
require(balanceOf[_src] >= _wad, "WETH9: insufficient source balance"); | ||
|
||
if (_src != msg.sender && allowance[_src][msg.sender] != uint256(-1)) { | ||
require( | ||
allowance[_src][msg.sender] >= _wad, | ||
"WETH9: invalid allowance" | ||
); | ||
allowance[_src][msg.sender] -= _wad; | ||
} | ||
|
||
balanceOf[_src] -= _wad; | ||
balanceOf[_dst] += _wad; | ||
|
||
emit Transfer(_src, _dst, _wad); | ||
|
||
return true; | ||
} | ||
} |
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