|
| 1 | +pragma solidity ^0.5.16; |
| 2 | + |
| 3 | +// Inheritence |
| 4 | +import "./Owned.sol"; |
| 5 | +import "./Proxyable.sol"; |
| 6 | +import "./MixinResolver.sol"; |
| 7 | +import "./interfaces/IDynamicSynthRedeemer.sol"; |
| 8 | + |
| 9 | +// Libraries |
| 10 | +import "./SafeDecimalMath.sol"; |
| 11 | + |
| 12 | +// Internal references |
| 13 | +import "./interfaces/IIssuer.sol"; |
| 14 | +import "./interfaces/IExchangeRates.sol"; |
| 15 | + |
| 16 | +contract DynamicSynthRedeemer is Owned, IDynamicSynthRedeemer, MixinResolver { |
| 17 | + using SafeDecimalMath for uint; |
| 18 | + |
| 19 | + bytes32 public constant CONTRACT_NAME = "DynamicSynthRedeemer"; |
| 20 | + |
| 21 | + uint public discountRate; |
| 22 | + bool public redemptionActive; |
| 23 | + |
| 24 | + bytes32 internal constant sUSD = "sUSD"; |
| 25 | + |
| 26 | + bytes32 private constant CONTRACT_ISSUER = "Issuer"; |
| 27 | + bytes32 private constant CONTRACT_EXRATES = "ExchangeRates"; |
| 28 | + |
| 29 | + constructor(address _owner, address _resolver) public Owned(_owner) MixinResolver(_resolver) { |
| 30 | + discountRate = SafeDecimalMath.unit(); |
| 31 | + } |
| 32 | + |
| 33 | + /* ========== RESOLVER CONFIG ========== */ |
| 34 | + |
| 35 | + function resolverAddressesRequired() public view returns (bytes32[] memory addresses) { |
| 36 | + addresses = new bytes32[](2); |
| 37 | + addresses[0] = CONTRACT_ISSUER; |
| 38 | + addresses[1] = CONTRACT_EXRATES; |
| 39 | + } |
| 40 | + |
| 41 | + /* ========== INTERNAL VIEWS ========== */ |
| 42 | + |
| 43 | + function _issuer() internal view returns (IIssuer) { |
| 44 | + return IIssuer(requireAndGetAddress(CONTRACT_ISSUER)); |
| 45 | + } |
| 46 | + |
| 47 | + function _exchangeRates() internal view returns (IExchangeRates) { |
| 48 | + return IExchangeRates(requireAndGetAddress(CONTRACT_EXRATES)); |
| 49 | + } |
| 50 | + |
| 51 | + function _redeemingActive() internal view { |
| 52 | + require(redemptionActive, "Redemption deactivated"); |
| 53 | + } |
| 54 | + |
| 55 | + /* ========== EXTERNAL VIEWS ========== */ |
| 56 | + |
| 57 | + function getDiscountRate() external view returns (uint) { |
| 58 | + return discountRate; |
| 59 | + } |
| 60 | + |
| 61 | + /* ========== INTERNAL HELPERS ========== */ |
| 62 | + |
| 63 | + function _proxyAddressForKey(bytes32 currencyKey) internal returns (address) { |
| 64 | + address synth = address(_issuer().synths(currencyKey)); |
| 65 | + require(synth != address(0), "Invalid synth"); |
| 66 | + return address(Proxyable(synth).proxy()); |
| 67 | + } |
| 68 | + |
| 69 | + /* ========== MUTATIVE FUNCTIONS ========== */ |
| 70 | + |
| 71 | + function redeemAll(bytes32[] calldata currencyKeys) external requireRedemptionActive { |
| 72 | + for (uint i = 0; i < currencyKeys.length; i++) { |
| 73 | + address synthProxy = _proxyAddressForKey(currencyKeys[i]); |
| 74 | + _redeem(synthProxy, currencyKeys[i], IERC20(synthProxy).balanceOf(msg.sender)); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + function redeem(bytes32 currencyKey) external requireRedemptionActive { |
| 79 | + address synthProxy = _proxyAddressForKey(currencyKey); |
| 80 | + _redeem(synthProxy, currencyKey, IERC20(synthProxy).balanceOf(msg.sender)); |
| 81 | + } |
| 82 | + |
| 83 | + function redeemPartial(bytes32 currencyKey, uint amountOfSynth) external requireRedemptionActive { |
| 84 | + address synthProxy = _proxyAddressForKey(currencyKey); |
| 85 | + // technically this check isn't necessary - Synth.burn would fail due to safe sub, |
| 86 | + // but this is a useful error message to the user |
| 87 | + require(IERC20(synthProxy).balanceOf(msg.sender) >= amountOfSynth, "Insufficient balance"); |
| 88 | + _redeem(synthProxy, currencyKey, amountOfSynth); |
| 89 | + } |
| 90 | + |
| 91 | + function _redeem( |
| 92 | + address synthProxy, |
| 93 | + bytes32 currencyKey, |
| 94 | + uint amountOfSynth |
| 95 | + ) internal { |
| 96 | + require(amountOfSynth > 0, "No balance of synth to redeem"); |
| 97 | + require(currencyKey != sUSD, "Cannot redeem sUSD"); |
| 98 | + |
| 99 | + // Discount rate applied to chainlink price for dynamic redemptions |
| 100 | + (uint rate, bool invalid) = _exchangeRates().rateAndInvalid(currencyKey); |
| 101 | + uint rateToRedeem = rate.multiplyDecimalRound(discountRate); |
| 102 | + require(rateToRedeem > 0 && !invalid, "Synth not redeemable"); |
| 103 | + |
| 104 | + uint amountInsUSD = amountOfSynth.multiplyDecimalRound(rateToRedeem); |
| 105 | + _issuer().burnAndIssueSynthsWithoutDebtCache(msg.sender, currencyKey, amountOfSynth, amountInsUSD); |
| 106 | + |
| 107 | + emit SynthRedeemed(synthProxy, msg.sender, amountOfSynth, amountInsUSD); |
| 108 | + } |
| 109 | + |
| 110 | + /* ========== MODIFIERS ========== */ |
| 111 | + |
| 112 | + modifier requireRedemptionActive() { |
| 113 | + _redeemingActive(); |
| 114 | + _; |
| 115 | + } |
| 116 | + |
| 117 | + /* ========== RESTRICTED FUNCTIONS ========== */ |
| 118 | + |
| 119 | + function setDiscountRate(uint _newRate) external onlyOwner { |
| 120 | + require(_newRate >= 0 && _newRate <= SafeDecimalMath.unit(), "Invalid rate"); |
| 121 | + discountRate = _newRate; |
| 122 | + emit DiscountRateUpdated(_newRate); |
| 123 | + } |
| 124 | + |
| 125 | + function suspendRedemption() external onlyOwner { |
| 126 | + require(redemptionActive, "Redemption suspended"); |
| 127 | + redemptionActive = false; |
| 128 | + emit RedemptionSuspended(); |
| 129 | + } |
| 130 | + |
| 131 | + function resumeRedemption() external onlyOwner { |
| 132 | + require(!redemptionActive, "Redemption not suspended"); |
| 133 | + redemptionActive = true; |
| 134 | + emit RedemptionResumed(); |
| 135 | + } |
| 136 | + |
| 137 | + /* ========== EVENTS ========== */ |
| 138 | + |
| 139 | + event RedemptionSuspended(); |
| 140 | + event RedemptionResumed(); |
| 141 | + event DiscountRateUpdated(uint discountRate); |
| 142 | + event SynthRedeemed(address synth, address account, uint amountOfSynth, uint amountInsUSD); |
| 143 | +} |
0 commit comments