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