-
Notifications
You must be signed in to change notification settings - Fork 106
Wrap Module V2 #123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Wrap Module V2 #123
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8ac38ea
add WrapModuleV2
ncitron cb4a74c
add deploys
ncitron 8deb71e
add integration tests for WrapModuleV2
ncitron 06924d0
add compound wrap adapter
ncitron 74c191f
add yearn wrap adapter
ncitron d306033
add approval on unwrap
ncitron d8e007c
review fixes
ncitron f78d95b
add comment about why we approve on unwrap
ncitron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
Copyright 2020 Set Labs Inc. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
|
||
SPDX-License-Identifier: Apache License, Version 2.0 | ||
*/ | ||
pragma solidity 0.6.10; | ||
|
||
|
||
/** | ||
* @title IWrapV2Adapter | ||
* @author Set Protocol | ||
* | ||
ncitron marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
*/ | ||
interface IWrapV2Adapter { | ||
|
||
function ETH_TOKEN_ADDRESS() external view returns (address); | ||
|
||
function getWrapCallData( | ||
address _underlyingToken, | ||
address _wrappedToken, | ||
uint256 _underlyingUnits, | ||
address _to, | ||
bytes memory _wrapData | ||
) external view returns (address _subject, uint256 _value, bytes memory _calldata); | ||
|
||
function getUnwrapCallData( | ||
address _underlyingToken, | ||
address _wrappedToken, | ||
uint256 _wrappedTokenUnits, | ||
address _to, | ||
bytes memory _unwrapData | ||
) external view returns (address _subject, uint256 _value, bytes memory _calldata); | ||
|
||
function getSpenderAddress(address _underlyingToken, address _wrappedToken) external view returns(address); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
Copyright 2020 Set Labs Inc. | ||
ncitron marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
|
||
SPDX-License-Identifier: Apache License, Version 2.0 | ||
*/ | ||
|
||
pragma solidity 0.6.10; | ||
|
||
import { ERC20 } from "@openzeppelin/contracts/token/ERC20/ERC20.sol"; | ||
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | ||
|
||
|
||
/** | ||
* @title WrapV2AdapterMock | ||
* @author Set Protocol | ||
* | ||
* ERC20 contract that doubles as a wrap token. The wrapToken accepts any underlying token and | ||
* mints/burns the WrapAdapter Token. | ||
*/ | ||
contract WrapV2AdapterMock is ERC20 { | ||
|
||
address public constant ETH_TOKEN_ADDRESS = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; | ||
|
||
/* ============ Constructor ============ */ | ||
constructor() public ERC20("WrapV2Adapter", "WRAPV2") {} | ||
|
||
/* ============ External Functions ============ */ | ||
|
||
/** | ||
* Mints tokens to the sender of the underlying quantity | ||
*/ | ||
function deposit(address _underlyingToken, uint256 _underlyingQuantity) payable external { | ||
// Do a transferFrom of the underlyingToken | ||
if (_underlyingToken != ETH_TOKEN_ADDRESS) { | ||
IERC20(_underlyingToken).transferFrom(msg.sender, address(this), _underlyingQuantity); | ||
} | ||
|
||
_mint(msg.sender, _underlyingQuantity); | ||
} | ||
|
||
/** | ||
* Burns tokens from the sender of the wrapped asset and returns the underlying | ||
*/ | ||
function withdraw(address _underlyingToken, uint256 _underlyingQuantity) external { | ||
// Transfer the underlying to the sender | ||
if (_underlyingToken == ETH_TOKEN_ADDRESS) { | ||
msg.sender.transfer(_underlyingQuantity); | ||
} else { | ||
IERC20(_underlyingToken).transfer(msg.sender, _underlyingQuantity); | ||
} | ||
|
||
_burn(msg.sender, _underlyingQuantity); | ||
} | ||
|
||
/** | ||
* [x] | ||
ncitron marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
*/ | ||
function getWrapCallData( | ||
address _underlyingToken, | ||
address /* _wrappedToken */, | ||
uint256 _underlyingUnits, | ||
address /* _to */, | ||
bytes memory /* _wrapData */ | ||
) external view returns (address _subject, uint256 _value, bytes memory _calldata) { | ||
uint256 value = _underlyingToken == ETH_TOKEN_ADDRESS ? _underlyingUnits : 0; | ||
bytes memory callData = abi.encodeWithSignature("deposit(address,uint256)", _underlyingToken, _underlyingUnits); | ||
return (address(this), value, callData); | ||
} | ||
|
||
function getUnwrapCallData( | ||
address _underlyingToken, | ||
address /* _wrappedToken */, | ||
uint256 _wrappedTokenUnits, | ||
address /* _to */, | ||
bytes memory /* _wrapData */ | ||
) external view returns (address _subject, uint256 _value, bytes memory _calldata) { | ||
bytes memory callData = abi.encodeWithSignature("withdraw(address,uint256)", _underlyingToken, _wrappedTokenUnits); | ||
return (address(this), 0, callData); | ||
} | ||
|
||
function getSpenderAddress( | ||
address /* _underlyingToken */, | ||
address /* _wrappedToken */ | ||
) external view returns(address) { | ||
return address(this); | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
contracts/protocol/integration/wrap-v2/CompoundWrapV2Adapter.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
Copyright 2021 Set Labs Inc. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
|
||
SPDX-License-Identifier: Apache License, Version 2.0 | ||
*/ | ||
|
||
pragma solidity 0.6.10; | ||
pragma experimental "ABIEncoderV2"; | ||
|
||
import { ICErc20 } from "../../../interfaces/external/ICErc20.sol"; | ||
import { IWrapV2Adapter } from "../../../interfaces/IWrapV2Adapter.sol"; | ||
ncitron marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
import { Compound } from "../lib/Compound.sol"; | ||
|
||
/** | ||
* @title CompoundWrapV2Adapter | ||
* @author Set Protocol, Ember Fund | ||
ncitron marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
* | ||
* Wrap adapter for Compound that returns data for wraps/unwraps of tokens | ||
*/ | ||
contract CompoundWrapV2Adapter { | ||
using Compound for ICErc20; | ||
|
||
|
||
/* ============ Constants ============ */ | ||
|
||
// Compound Mock address to indicate ETH. ETH is used directly in Compound protocol (instead of an abstraction such as WETH) | ||
address public constant ETH_TOKEN_ADDRESS = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; | ||
|
||
/* ============ External Getter Functions ============ */ | ||
|
||
/** | ||
* Generates the calldata to wrap an underlying asset into a wrappedToken. | ||
* | ||
* @param _underlyingToken Address of the component to be wrapped | ||
* @param _wrappedToken Address of the desired wrapped token | ||
* @param _underlyingUnits Total quantity of underlying units to wrap | ||
* | ||
* @return address Target contract address | ||
* @return uint256 Total quantity of underlying units (if underlying is ETH) | ||
* @return bytes Wrap calldata | ||
*/ | ||
function getWrapCallData( | ||
address _underlyingToken, | ||
address _wrappedToken, | ||
uint256 _underlyingUnits, | ||
address /* _to */, | ||
bytes memory /* _wrapData */ | ||
) | ||
external | ||
pure | ||
returns (address, uint256, bytes memory) | ||
{ | ||
uint256 value; | ||
bytes memory callData; | ||
if (_underlyingToken == ETH_TOKEN_ADDRESS) { | ||
value = _underlyingUnits; | ||
( , , callData) = ICErc20(_wrappedToken).getMintCEtherCalldata(_underlyingUnits); | ||
} else { | ||
value = 0; | ||
( , , callData) = ICErc20(_wrappedToken).getMintCTokenCalldata(_underlyingUnits); | ||
} | ||
|
||
return (_wrappedToken, value, callData); | ||
} | ||
|
||
/** | ||
* Generates the calldata to unwrap a wrapped asset into its underlying. | ||
* | ||
* @param _wrappedToken Address of the component to be unwrapped | ||
* @param _wrappedTokenUnits Total quantity of wrapped token units to unwrap | ||
* | ||
* @return address Target contract address | ||
* @return uint256 Total quantity of wrapped token units to unwrap. This will always be 0 for unwrapping | ||
* @return bytes Unwrap calldata | ||
*/ | ||
function getUnwrapCallData( | ||
address /* _underlyingToken */, | ||
address _wrappedToken, | ||
uint256 _wrappedTokenUnits, | ||
address /* _to */, | ||
bytes memory /* _unwrapData */ | ||
) | ||
external | ||
pure | ||
returns (address, uint256, bytes memory) | ||
{ | ||
( , , bytes memory callData) = ICErc20(_wrappedToken).getRedeemCalldata(_wrappedTokenUnits); | ||
return (_wrappedToken, 0, callData); | ||
} | ||
|
||
/** | ||
* Returns the address to approve source tokens for wrapping. | ||
* @param _wrappedToken Address of the wrapped token | ||
* @return address Address of the contract to approve tokens to | ||
*/ | ||
function getSpenderAddress(address /* _underlyingToken */, address _wrappedToken) external pure returns(address) { | ||
return address(_wrappedToken); | ||
} | ||
|
||
} |
126 changes: 126 additions & 0 deletions
126
contracts/protocol/integration/wrap-v2/YearnWrapV2Adapter.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
Copyright 2021 Set Labs Inc. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
|
||
SPDX-License-Identifier: Apache License, Version 2.0 | ||
*/ | ||
|
||
pragma solidity 0.6.10; | ||
pragma experimental "ABIEncoderV2"; | ||
|
||
import { IWrapV2Adapter } from "../../../interfaces/IWrapV2Adapter.sol"; | ||
ncitron marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
import { IYearnVault } from "../../../interfaces/external/IYearnVault.sol"; | ||
|
||
/** | ||
* @title YearnWrapV2Adapter | ||
* @author Set Protocol, Ember Fund | ||
ncitron marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
* | ||
* Wrap adapter for Yearn that returns data for wraps/unwraps of tokens | ||
*/ | ||
contract YearnWrapV2Adapter { | ||
|
||
/* ============ Modifiers ============ */ | ||
|
||
/** | ||
* Throws if the underlying/wrapped token pair is not valid | ||
*/ | ||
modifier _onlyValidTokenPair(address _underlyingToken, address _wrappedToken) { | ||
require(validTokenPair(_underlyingToken, _wrappedToken), "Must be a valid token pair"); | ||
_; | ||
} | ||
|
||
/* ============ Constructor ============ */ | ||
|
||
constructor() public { } | ||
|
||
/* ============ External Getter Functions ============ */ | ||
|
||
/** | ||
* Generates the calldata to wrap an underlying asset into a wrappedToken. | ||
* | ||
* @param _underlyingToken Address of the component to be wrapped | ||
* @param _wrappedToken Address of the desired wrapped token | ||
* @param _underlyingUnits Total quantity of underlying units to wrap | ||
* | ||
* @return address Target contract address | ||
* @return uint256 Total quantity of underlying units (if underlying is ETH) | ||
* @return bytes Wrap calldata | ||
*/ | ||
function getWrapCallData( | ||
address _underlyingToken, | ||
address _wrappedToken, | ||
uint256 _underlyingUnits, | ||
address /* _to */, | ||
bytes memory /* _wrapData */ | ||
) | ||
external | ||
view | ||
_onlyValidTokenPair(_underlyingToken, _wrappedToken) | ||
returns (address, uint256, bytes memory) | ||
{ | ||
bytes memory callData = abi.encodeWithSignature("deposit(uint256)", _underlyingUnits); | ||
return (address(_wrappedToken), 0, callData); | ||
} | ||
|
||
/** | ||
* Generates the calldata to unwrap a wrapped asset into its underlying. | ||
* | ||
* @param _underlyingToken Address of the underlying asset | ||
* @param _wrappedToken Address of the component to be unwrapped | ||
* @param _wrappedTokenUnits Total quantity of wrapped token units to unwrap | ||
* | ||
* @return address Target contract address | ||
* @return uint256 Total quantity of wrapped token units to unwrap. This will always be 0 for unwrapping | ||
* @return bytes Unwrap calldata | ||
*/ | ||
function getUnwrapCallData( | ||
address _underlyingToken, | ||
address _wrappedToken, | ||
uint256 _wrappedTokenUnits, | ||
address /* _to */, | ||
bytes memory /* _unwrapData */ | ||
) | ||
external | ||
view | ||
_onlyValidTokenPair(_underlyingToken, _wrappedToken) | ||
returns (address, uint256, bytes memory) | ||
{ | ||
bytes memory callData = abi.encodeWithSignature("withdraw(uint256)", _wrappedTokenUnits); | ||
return (address(_wrappedToken), 0, callData); | ||
} | ||
|
||
/** | ||
* Returns the address to approve source tokens for wrapping. | ||
* | ||
* @return address Address of the contract to approve tokens to | ||
*/ | ||
function getSpenderAddress(address /* _underlyingToken */, address _wrappedToken) external pure returns(address) { | ||
return address(_wrappedToken); | ||
} | ||
|
||
/* ============ Internal Functions ============ */ | ||
|
||
/** | ||
* Validates the underlying and wrapped token pair | ||
* | ||
* @param _underlyingToken Address of the underlying asset | ||
* @param _wrappedToken Address of the wrapped asset | ||
* | ||
* @return bool Whether or not the wrapped token accepts the underlying token as collateral | ||
*/ | ||
function validTokenPair(address _underlyingToken, address _wrappedToken) internal view returns(bool) { | ||
address unwrappedToken = IYearnVault(_wrappedToken).token(); | ||
return unwrappedToken == _underlyingToken; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.