|
| 1 | +/* |
| 2 | + Copyright 2022 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 | +pragma experimental "ABIEncoderV2"; |
| 21 | + |
| 22 | +import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; |
| 23 | +import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; |
| 24 | +import { TokenType, IWrappedfCash } from "../interfaces/IWrappedFCash.sol"; |
| 25 | + |
| 26 | +// mock class using BasicToken |
| 27 | +contract WrappedfCashMock is ERC20, IWrappedfCash { |
| 28 | + |
| 29 | + uint256 private fCashId; |
| 30 | + uint40 private maturity; |
| 31 | + bool private matured; |
| 32 | + uint16 private currencyId; |
| 33 | + uint8 private marketIndex; |
| 34 | + IERC20 private underlyingToken; |
| 35 | + int256 private underlyingPrecision; |
| 36 | + IERC20 private assetToken; |
| 37 | + int256 private assetPrecision; |
| 38 | + TokenType private tokenType; |
| 39 | + |
| 40 | + IERC20 private weth; |
| 41 | + |
| 42 | + bool private revertDecodedID; |
| 43 | + |
| 44 | + uint256 public redeemTokenReturned; |
| 45 | + uint256 public mintTokenSpent; |
| 46 | + |
| 47 | + address internal constant ETH_ADDRESS = address(0); |
| 48 | + |
| 49 | + constructor (IERC20 _assetToken, IERC20 _underlyingToken, IERC20 _weth) public ERC20("FCashMock", "FCM") { |
| 50 | + assetToken = _assetToken; |
| 51 | + underlyingToken = _underlyingToken; |
| 52 | + weth = _weth; |
| 53 | + } |
| 54 | + |
| 55 | + function initialize(uint16 _currencyId, uint40 _maturity) external override { |
| 56 | + currencyId = _currencyId; |
| 57 | + maturity = _maturity; |
| 58 | + } |
| 59 | + |
| 60 | + /// @notice Mints wrapped fCash ERC20 tokens |
| 61 | + function mintViaAsset( |
| 62 | + uint256 depositAmountExternal, |
| 63 | + uint88 fCashAmount, |
| 64 | + address receiver, |
| 65 | + uint32 /* minImpliedRate */ |
| 66 | + ) external override{ |
| 67 | + uint256 assetTokenAmount = mintTokenSpent == 0 ? depositAmountExternal : mintTokenSpent; |
| 68 | + require(assetToken.transferFrom(msg.sender, address(this), assetTokenAmount), "WrappedfCashMock: Transfer failed"); |
| 69 | + _mint(receiver, fCashAmount); |
| 70 | + } |
| 71 | + |
| 72 | + function mintViaUnderlying( |
| 73 | + uint256 depositAmountExternal, |
| 74 | + uint88 fCashAmount, |
| 75 | + address receiver, |
| 76 | + uint32 /* minImpliedRate */ |
| 77 | + ) external override{ |
| 78 | + uint256 underlyingTokenAmount = mintTokenSpent == 0 ? depositAmountExternal : mintTokenSpent; |
| 79 | + bool transferSuccess; |
| 80 | + if(address(underlyingToken) == ETH_ADDRESS) { |
| 81 | + transferSuccess = weth.transferFrom(msg.sender, address(this), underlyingTokenAmount); |
| 82 | + } else { |
| 83 | + transferSuccess = underlyingToken.transferFrom(msg.sender, address(this), underlyingTokenAmount); |
| 84 | + } |
| 85 | + require(transferSuccess, "WrappedfCashMock: Transfer failed"); |
| 86 | + _mint(receiver, fCashAmount); |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + function redeemToAsset( |
| 91 | + uint256 amount, |
| 92 | + address receiver, |
| 93 | + uint32 /* maxImpliedRate */ |
| 94 | + ) external override { |
| 95 | + _burn(msg.sender, amount); |
| 96 | + uint256 assetTokenAmount = redeemTokenReturned == 0 ? amount : redeemTokenReturned; |
| 97 | + require(assetToken.transfer(receiver, assetTokenAmount), "WrappedfCashMock: Transfer failed"); |
| 98 | + } |
| 99 | + |
| 100 | + function redeemToUnderlying( |
| 101 | + uint256 amount, |
| 102 | + address receiver, |
| 103 | + uint32 /* maxImpliedRate */ |
| 104 | + ) external override { |
| 105 | + _burn(msg.sender, amount); |
| 106 | + uint256 underlyingTokenAmount = redeemTokenReturned == 0 ? amount : redeemTokenReturned; |
| 107 | + if(address(underlyingToken) == ETH_ADDRESS) { |
| 108 | + weth.transfer(receiver, underlyingTokenAmount); |
| 109 | + } else { |
| 110 | + underlyingToken.transfer(receiver, underlyingTokenAmount); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /// @notice Returns the underlying fCash ID of the token |
| 115 | + function getfCashId() external override view returns (uint256) { |
| 116 | + return fCashId; |
| 117 | + } |
| 118 | + |
| 119 | + /// @notice True if the fCash has matured, assets mature exactly on the block time |
| 120 | + function hasMatured() external override view returns (bool) { |
| 121 | + return matured; |
| 122 | + } |
| 123 | + |
| 124 | + /// @notice Returns the components of the fCash idd |
| 125 | + function getDecodedID() external override view returns (uint16, uint40) { |
| 126 | + require(!revertDecodedID, "Test revertion DecodedID"); |
| 127 | + return (currencyId, maturity); |
| 128 | + } |
| 129 | + |
| 130 | + /// @notice Returns the current market index for this fCash asset. If this returns |
| 131 | + /// zero that means it is idiosyncratic and cannot be traded. |
| 132 | + function getMarketIndex() external override view returns (uint8) { |
| 133 | + return marketIndex; |
| 134 | + } |
| 135 | + |
| 136 | + /// @notice Returns the token and precision of the token that this token settles |
| 137 | + /// to. For example, fUSDC will return the USDC token address and 1e6. The zero |
| 138 | + /// address will represent ETH. |
| 139 | + function getUnderlyingToken() public override view returns (IERC20, int256) { |
| 140 | + return (underlyingToken, underlyingPrecision); |
| 141 | + } |
| 142 | + |
| 143 | + /// @notice Returns the asset token which the fCash settles to. This will be an interest |
| 144 | + /// bearing token like a cToken or aToken. |
| 145 | + function getAssetToken() public override view returns (IERC20, int256, TokenType) { |
| 146 | + return (assetToken, assetPrecision, tokenType); |
| 147 | + } |
| 148 | + |
| 149 | + function setMatured(bool _matured) external{ |
| 150 | + matured = _matured; |
| 151 | + } |
| 152 | + |
| 153 | + function setRedeemTokenReturned(uint256 _redeemTokenReturned) external{ |
| 154 | + redeemTokenReturned = _redeemTokenReturned; |
| 155 | + } |
| 156 | + |
| 157 | + function setMintTokenSpent(uint256 _mintTokenSpent) external{ |
| 158 | + mintTokenSpent = _mintTokenSpent; |
| 159 | + } |
| 160 | + |
| 161 | + function setRevertDecodedID(bool _revertDecodedID) external{ |
| 162 | + revertDecodedID = _revertDecodedID; |
| 163 | + } |
| 164 | + |
| 165 | + function getToken(bool useUnderlying) public view override returns (IERC20 token, bool isETH) { |
| 166 | + if (useUnderlying) { |
| 167 | + (token, /* */) = getUnderlyingToken(); |
| 168 | + } else { |
| 169 | + (token, /* */, /* */) = getAssetToken(); |
| 170 | + } |
| 171 | + isETH = address(token) == ETH_ADDRESS; |
| 172 | + } |
| 173 | + |
| 174 | + |
| 175 | +} |
0 commit comments