Skip to content

Commit ce2ae7e

Browse files
authored
feat: add WrapExtension (#87)
* v0.0.29 * Add WrapExtension
1 parent b7692d7 commit ce2ae7e

File tree

12 files changed

+1247
-8
lines changed

12 files changed

+1247
-8
lines changed
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/*
2+
Copyright 2021 Index Coop
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
16+
SPDX-License-Identifier: Apache License, Version 2.0
17+
*/
18+
19+
pragma solidity 0.6.10;
20+
21+
import { BaseExtension } from "../lib/BaseExtension.sol";
22+
import { IBaseManager } from "../interfaces/IBaseManager.sol";
23+
import { ISetToken } from "../interfaces/ISetToken.sol";
24+
import { IWrapModule } from "../interfaces/IWrapModule.sol";
25+
26+
/**
27+
* @title WrapExtension
28+
* @author Index Coop
29+
*
30+
* Manager extension for interacting with WrapModule
31+
*/
32+
contract WrapExtension is BaseExtension {
33+
34+
/* ========== State Variables ========= */
35+
36+
// Address of Set Token
37+
ISetToken public immutable setToken;
38+
39+
// Address of WrapModule
40+
IWrapModule public immutable wrapModule;
41+
42+
/* ============ Constructor ============ */
43+
44+
/**
45+
* Sets state variables
46+
*
47+
* @param _manager Manager contract
48+
* @param _wrapModule Set Protocol WrapModule
49+
*/
50+
constructor(IBaseManager _manager, IWrapModule _wrapModule) public BaseExtension(_manager) {
51+
manager = _manager;
52+
setToken = manager.setToken();
53+
wrapModule = _wrapModule;
54+
}
55+
56+
/* ========== External Functions ========== */
57+
58+
/**
59+
* OPERATOR ONLY: Initializes the Set Token on the Wrap Module.
60+
*/
61+
function initialize() external onlyOperator {
62+
bytes memory data = abi.encodeWithSelector(wrapModule.initialize.selector, setToken);
63+
invokeManager(address(wrapModule), data);
64+
}
65+
66+
/**
67+
* OPERATOR ONLY: Calls wrap on the WrapModule.
68+
*
69+
* @param _underlyingToken address of underlying token
70+
* @param _wrappedToken address of wrapped token
71+
* @param _underlyingUnits units of underlying to wrap
72+
* @param _integrationName Set Protocol integreation name for the wrap adapter
73+
*/
74+
function wrap(
75+
address _underlyingToken,
76+
address _wrappedToken,
77+
uint256 _underlyingUnits,
78+
string calldata _integrationName
79+
)
80+
external
81+
onlyOperator
82+
{
83+
bytes memory data = abi.encodeWithSelector(
84+
wrapModule.wrap.selector,
85+
setToken,
86+
_underlyingToken,
87+
_wrappedToken,
88+
_underlyingUnits,
89+
_integrationName
90+
);
91+
invokeManager(address(wrapModule), data);
92+
}
93+
94+
/**
95+
* OPERATOR ONLY: Calls wrapWithEther on the WrapModule.
96+
*
97+
* @param _wrappedToken address of wrapped token
98+
* @param _underlyingUnits units of weth to wrap
99+
* @param _integrationName Set Protocol integreation name for the wrap adapter
100+
*/
101+
function wrapWithEther(
102+
address _wrappedToken,
103+
uint256 _underlyingUnits,
104+
string calldata _integrationName
105+
)
106+
external
107+
onlyOperator
108+
{
109+
bytes memory data = abi.encodeWithSelector(
110+
wrapModule.wrapWithEther.selector,
111+
setToken,
112+
_wrappedToken,
113+
_underlyingUnits,
114+
_integrationName
115+
);
116+
invokeManager(address(wrapModule), data);
117+
}
118+
119+
/**
120+
* OPERATOR ONLY: Calls unwrap on the WrapModule.
121+
*
122+
* @param _underlyingToken address of underlying token
123+
* @param _wrappedToken address of wrapped token
124+
* @param _wrappedUnits units of wrapped token to unwrap
125+
* @param _integrationName Set Protocol integreation name for the wrap adapter
126+
*/
127+
function unwrap(
128+
address _underlyingToken,
129+
address _wrappedToken,
130+
uint256 _wrappedUnits,
131+
string calldata _integrationName
132+
)
133+
external
134+
onlyOperator
135+
{
136+
bytes memory data = abi.encodeWithSelector(
137+
wrapModule.unwrap.selector,
138+
setToken,
139+
_underlyingToken,
140+
_wrappedToken,
141+
_wrappedUnits,
142+
_integrationName
143+
);
144+
invokeManager(address(wrapModule), data);
145+
}
146+
147+
/**
148+
* OPERATOR ONLY: Calls unwrapWithEther on the WrapModule.
149+
*
150+
* @param _wrappedToken address of wrapped token
151+
* @param _wrappedUnits units of wrapped token to unwrap
152+
* @param _integrationName Set Protocol integreation name for the wrap adapter
153+
*/
154+
function unwrapWithEther(
155+
address _wrappedToken,
156+
uint256 _wrappedUnits,
157+
string calldata _integrationName
158+
)
159+
external
160+
onlyOperator
161+
{
162+
bytes memory data = abi.encodeWithSelector(
163+
wrapModule.unwrapWithEther.selector,
164+
setToken,
165+
_wrappedToken,
166+
_wrappedUnits,
167+
_integrationName
168+
);
169+
invokeManager(address(wrapModule), data);
170+
}
171+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Copyright 2021 Index Coop.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
16+
SPDX-License-Identifier: Apache License, Version 2.0
17+
*/
18+
19+
import { ISetToken } from "./ISetToken.sol";
20+
21+
pragma solidity 0.6.10;
22+
23+
interface IWrapModule {
24+
25+
function initialize(ISetToken _setToken) external;
26+
27+
function wrap(
28+
ISetToken _setToken,
29+
address _underlyingToken,
30+
address _wrappedToken,
31+
uint256 _underlyingUnits,
32+
string calldata _integrationName
33+
) external;
34+
35+
function wrapWithEther(
36+
ISetToken _setToken,
37+
address _wrappedToken,
38+
uint256 _underlyingUnits,
39+
string calldata _integrationName
40+
) external;
41+
42+
function unwrap(
43+
ISetToken _setToken,
44+
address _underlyingToken,
45+
address _wrappedToken,
46+
uint256 _wrappedUnits,
47+
string calldata _integrationName
48+
) external;
49+
50+
function unwrapWithEther(
51+
ISetToken _setToken,
52+
address _wrappedToken,
53+
uint256 _wrappedUnits,
54+
string calldata _integrationName
55+
) external;
56+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
Copyright 2021 Index Coop.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
16+
SPDX-License-Identifier: Apache License, Version 2.0
17+
*/
18+
19+
pragma solidity 0.6.10;
20+
21+
import { ERC20 } from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
22+
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
23+
24+
contract WrapAdapterMock is ERC20 {
25+
constructor(address _owner, uint256 _initAmount) public ERC20("Wrapped Token", "WTOKEN") {
26+
_mint(_owner, _initAmount);
27+
}
28+
29+
/* ========= Wrapped Token Functions ========== */
30+
31+
function mint(IERC20 _underlying, uint256 _amount) external {
32+
_underlying.transferFrom(msg.sender, address(this), _amount);
33+
_mint(msg.sender, _amount);
34+
}
35+
36+
function mintWithEther(uint256 _amount) external payable {
37+
require(msg.value == _amount, "msg.value to low");
38+
_mint(msg.sender, _amount);
39+
}
40+
41+
function burn(IERC20 _underlying, uint256 _amount) external {
42+
_burn(msg.sender, _amount);
43+
_underlying.transfer(msg.sender, _amount);
44+
}
45+
46+
function burnWithEther(uint256 _amount) external {
47+
_burn(msg.sender, _amount);
48+
msg.sender.transfer(_amount);
49+
}
50+
51+
receive() external payable {}
52+
53+
/* ========= Wrap Adapter Functions =========== */
54+
55+
address public constant ETH_TOKEN_ADDRESS = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
56+
57+
function getWrapCallData(
58+
address _underlyingToken,
59+
address /* _wrappedToken */,
60+
uint256 _underlyingUnits
61+
)
62+
external
63+
view
64+
returns (address _subject, uint256 _value, bytes memory _calldata)
65+
{
66+
if (_underlyingToken == ETH_TOKEN_ADDRESS) {
67+
bytes memory data = abi.encodeWithSelector(this.mintWithEther.selector, _underlyingUnits);
68+
return (address(this), _underlyingUnits, data);
69+
} else {
70+
bytes memory data = abi.encodeWithSelector(this.mint.selector, _underlyingToken, _underlyingUnits);
71+
return (address(this), 0, data);
72+
}
73+
}
74+
75+
function getUnwrapCallData(
76+
address _underlyingToken,
77+
address /* _wrappedToken */,
78+
uint256 _wrappedTokenUnits
79+
)
80+
external
81+
view
82+
returns
83+
(address _subject, uint256 _value, bytes memory _calldata)
84+
{
85+
if (_underlyingToken == ETH_TOKEN_ADDRESS) {
86+
bytes memory data = abi.encodeWithSelector(this.burnWithEther.selector, _wrappedTokenUnits);
87+
return (address(this), 0, data);
88+
} else {
89+
bytes memory data = abi.encodeWithSelector(this.burn.selector, _underlyingToken, _wrappedTokenUnits);
90+
return (address(this), 0, data);
91+
}
92+
}
93+
94+
function getSpenderAddress(
95+
address /* underlyingToken */,
96+
address /* _wrappedToken */
97+
)
98+
external
99+
view
100+
returns(address)
101+
{
102+
return address(this);
103+
}
104+
}

external/abi/set/WrapModule.json

Lines changed: 10 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)