-
Notifications
You must be signed in to change notification settings - Fork 3
/
RedemptionVaultWithBUIDL.sol
177 lines (149 loc) · 6.07 KB
/
RedemptionVaultWithBUIDL.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// SPDX-License-Identifier: MIT
pragma solidity 0.8.9;
import {IERC20Upgradeable as IERC20} from "@openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol";
import {SafeERC20Upgradeable as SafeERC20} from "@openzeppelin/contracts-upgradeable/token/ERC20/utils/SafeERC20Upgradeable.sol";
import "./RedemptionVault.sol";
import "./interfaces/buidl/IRedemption.sol";
import "./interfaces/buidl/ILiquiditySource.sol";
import "./interfaces/buidl/ISettlement.sol";
import "./libraries/DecimalsCorrectionLibrary.sol";
/**
* @title RedemptionVault
* @notice Smart contract that handles mTBILL redemptions
* @author RedDuck Software
*/
contract RedemptionVaultWIthBUIDL is RedemptionVault {
using DecimalsCorrectionLibrary for uint256;
using SafeERC20 for IERC20;
IRedemption public buidlRedemption;
IERC20 public buidl;
ILiquiditySource public buidlLiquiditySource;
ISettlement public buidlSettlement;
uint256[50] private __gap;
/**
* @notice upgradeable pattern contract`s initializer
* @param _ac address of MidasAccessControll contract
* @param _mTokenInitParams init params for mToken
* @param _receiversInitParams init params for receivers
* @param _instantInitParams init params for instant operations
* @param _sanctionsList address of sanctionsList contract
* @param _variationTolerance percent of prices diviation 1% = 100
* @param _minAmount basic min amount for operations
* @param _fiatRedemptionInitParams params fiatAdditionalFee, fiatFlatFee, minFiatRedeemAmount
* @param _buidlRedemption BUIDL redemption contract address
* @param _requestRedeemer address is designated for standard redemptions, allowing tokens to be pulled from this address
*/
function initialize(
address _ac,
MTokenInitParams calldata _mTokenInitParams,
ReceiversInitParams calldata _receiversInitParams,
InstantInitParams calldata _instantInitParams,
address _sanctionsList,
uint256 _variationTolerance,
uint256 _minAmount,
FiatRedeptionInitParams calldata _fiatRedemptionInitParams,
address _requestRedeemer,
address _buidlRedemption
) external initializer {
__RedemptionVault_init(
_ac,
_mTokenInitParams,
_receiversInitParams,
_instantInitParams,
_sanctionsList,
_variationTolerance,
_minAmount,
_fiatRedemptionInitParams,
_requestRedeemer
);
_validateAddress(_buidlRedemption, false);
buidlRedemption = IRedemption(_buidlRedemption);
buidlSettlement = ISettlement(buidlRedemption.settlement());
buidlLiquiditySource = ILiquiditySource(buidlRedemption.liquidity());
buidl = IERC20(buidlRedemption.asset());
}
/**
* @notice redeem mToken to USDC if daily limit and allowance not exceeded
* If contract don't have enough USDC, BUIDL redemption flow will be triggered
* Burns mToken from the user.
* Transfers fee in mToken to feeReceiver
* Transfers tokenOut to user.
* @param tokenOut token out address, always ignore
* @param amountMTokenIn amount of mToken to redeem
* @param minReceiveAmount minimum expected amount of tokenOut to receive (decimals 18)
*/
function redeemInstant(
address tokenOut,
uint256 amountMTokenIn,
uint256 minReceiveAmount
)
external
override
whenFnNotPaused(this.redeemInstant.selector)
onlyGreenlisted(msg.sender)
onlyNotBlacklisted(msg.sender)
onlyNotSanctioned(msg.sender)
{
address user = msg.sender;
tokenOut = buidlLiquiditySource.token();
(
uint256 feeAmount,
uint256 amountMTokenWithoutFee
) = _calcAndValidateRedeem(user, tokenOut, amountMTokenIn, true, false);
_requireAndUpdateLimit(amountMTokenIn);
uint256 tokenDecimals = _tokenDecimals(tokenOut);
uint256 amountMTokenInCopy = amountMTokenIn;
address tokenOutCopy = tokenOut;
uint256 minReceiveAmountCopy = minReceiveAmount;
(uint256 amountMTokenInUsd, uint256 mTokenRate) = _convertMTokenToUsd(
amountMTokenInCopy
);
(uint256 amountTokenOut, uint256 tokenOutRate) = _convertUsdToToken(
amountMTokenInUsd,
tokenOutCopy
);
_requireAndUpdateAllowance(tokenOutCopy, amountTokenOut);
mToken.burn(user, amountMTokenWithoutFee);
if (feeAmount > 0)
_tokenTransferFromUser(address(mToken), feeReceiver, feeAmount, 18);
uint256 amountTokenOutWithoutFee = (amountMTokenWithoutFee *
mTokenRate) / tokenOutRate;
require(
amountTokenOutWithoutFee >= minReceiveAmountCopy,
"RVB: minReceiveAmount > actual"
);
uint256 amountTokenOutWithoutFeeFrom18 = amountTokenOutWithoutFee
.convertFromBase18(tokenDecimals);
_checkAndRedeemBUIDL(tokenOutCopy, amountTokenOutWithoutFeeFrom18);
_tokenTransferToUser(
tokenOutCopy,
user,
amountTokenOutWithoutFeeFrom18.convertToBase18(tokenDecimals),
tokenDecimals
);
emit RedeemInstant(
user,
tokenOutCopy,
amountMTokenInCopy,
feeAmount,
amountTokenOutWithoutFee
);
}
/**
* @notice Check if contract have enough USDC balance for redeem
* if don't have trigger BUIDL redemption flow
* @param tokenOut tokenOut address
* @param amountTokenOut amount of tokenOut
*/
function _checkAndRedeemBUIDL(address tokenOut, uint256 amountTokenOut)
internal
{
uint256 contractBalanceTokenOut = IERC20(tokenOut).balanceOf(
address(this)
);
if (contractBalanceTokenOut >= amountTokenOut) return;
uint256 buidlToRedeem = amountTokenOut - contractBalanceTokenOut;
buidl.safeIncreaseAllowance(address(buidlRedemption), buidlToRedeem);
buidlRedemption.redeem(buidlToRedeem);
}
}