1
+ /*
2
+ Copyright 2021 Set Labs Inc.
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
+
25
+ /**
26
+ * @title WrapV2AdapterMock
27
+ * @author Set Protocol
28
+ *
29
+ * ERC20 contract that doubles as a wrap token. The wrapToken accepts any underlying token and
30
+ * mints/burns the WrapAdapter Token.
31
+ */
32
+ contract WrapV2AdapterMock is ERC20 {
33
+
34
+ address public constant ETH_TOKEN_ADDRESS = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE ;
35
+
36
+ /* ============ Constructor ============ */
37
+ constructor () public ERC20 ("WrapV2Adapter " , "WRAPV2 " ) {}
38
+
39
+ /* ============ External Functions ============ */
40
+
41
+ /**
42
+ * Mints tokens to the sender of the underlying quantity
43
+ */
44
+ function deposit (address _underlyingToken , uint256 _underlyingQuantity ) payable external {
45
+ // Do a transferFrom of the underlyingToken
46
+ if (_underlyingToken != ETH_TOKEN_ADDRESS) {
47
+ IERC20 (_underlyingToken).transferFrom (msg .sender , address (this ), _underlyingQuantity);
48
+ }
49
+
50
+ _mint (msg .sender , _underlyingQuantity);
51
+ }
52
+
53
+ /**
54
+ * Burns tokens from the sender of the wrapped asset and returns the underlying
55
+ */
56
+ function withdraw (address _underlyingToken , uint256 _underlyingQuantity ) external {
57
+ // Transfer the underlying to the sender
58
+ if (_underlyingToken == ETH_TOKEN_ADDRESS) {
59
+ msg .sender .transfer (_underlyingQuantity);
60
+ } else {
61
+ IERC20 (_underlyingToken).transfer (msg .sender , _underlyingQuantity);
62
+ }
63
+
64
+ _burn (msg .sender , _underlyingQuantity);
65
+ }
66
+
67
+ /**
68
+ * Generates the calldata to wrap an underlying asset into a wrappedToken.
69
+ *
70
+ * @param _underlyingToken Address of the component to be wrapped
71
+ * @param _underlyingUnits Total quantity of underlying units to wrap
72
+ *
73
+ * @return _subject Target contract address
74
+ * @return _value Total quantity of underlying units (if underlying is ETH)
75
+ * @return _calldata Wrap calldata
76
+ */
77
+ function getWrapCallData (
78
+ address _underlyingToken ,
79
+ address /* _wrappedToken */ ,
80
+ uint256 _underlyingUnits ,
81
+ address /* _to */ ,
82
+ bytes memory /* _wrapData */
83
+ ) external view returns (address _subject , uint256 _value , bytes memory _calldata ) {
84
+ uint256 value = _underlyingToken == ETH_TOKEN_ADDRESS ? _underlyingUnits : 0 ;
85
+ bytes memory callData = abi.encodeWithSignature ("deposit(address,uint256) " , _underlyingToken, _underlyingUnits);
86
+ return (address (this ), value, callData);
87
+ }
88
+
89
+ /**
90
+ * Generates the calldata to unwrap a wrapped asset into its underlying.
91
+ *
92
+ * @param _underlyingToken Address of the underlying of the component to be unwrapped
93
+ * @param _wrappedTokenUnits Total quantity of wrapped token units to unwrap
94
+ *
95
+ * @return _subject Target contract address
96
+ * @return _value Total quantity of wrapped token units to unwrap. This will always be 0 for unwrapping
97
+ * @return _calldata Unwrap calldata
98
+ */
99
+ function getUnwrapCallData (
100
+ address _underlyingToken ,
101
+ address /* _wrappedToken */ ,
102
+ uint256 _wrappedTokenUnits ,
103
+ address /* _to */ ,
104
+ bytes memory /* _wrapData */
105
+ ) external view returns (address _subject , uint256 _value , bytes memory _calldata ) {
106
+ bytes memory callData = abi.encodeWithSignature ("withdraw(address,uint256) " , _underlyingToken, _wrappedTokenUnits);
107
+ return (address (this ), 0 , callData);
108
+ }
109
+
110
+ /**
111
+ * Returns the address to approve source tokens for wrapping.
112
+ *
113
+ * @return address Address of the contract to approve tokens to
114
+ */
115
+ function getSpenderAddress (
116
+ address /* _underlyingToken */ ,
117
+ address /* _wrappedToken */
118
+ ) external view returns (address ) {
119
+ return address (this );
120
+ }
121
+ }
0 commit comments