forked from Synthetixio/synthetix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPurgeableSynth.sol
75 lines (56 loc) · 2.66 KB
/
PurgeableSynth.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
pragma solidity ^0.5.16;
// Inheritance
import "./Synth.sol";
// Libraries
import "./SafeDecimalMath.sol";
// Internal References
import "./interfaces/IExchangeRates.sol";
// https://docs.synthetix.io/contracts/source/contracts/purgeablesynth
contract PurgeableSynth is Synth {
using SafeDecimalMath for uint;
// The maximum allowed amount of tokenSupply in equivalent sUSD value for this synth to permit purging
uint public maxSupplyToPurgeInUSD = 100000 * SafeDecimalMath.unit(); // 100,000
bytes32 private constant CONTRACT_EXRATES = "ExchangeRates";
/* ========== CONSTRUCTOR ========== */
constructor(
address payable _proxy,
TokenState _tokenState,
string memory _tokenName,
string memory _tokenSymbol,
address payable _owner,
bytes32 _currencyKey,
uint _totalSupply,
address _resolver
) public Synth(_proxy, _tokenState, _tokenName, _tokenSymbol, _owner, _currencyKey, _totalSupply, _resolver) {}
/* ========== VIEWS ========== */
function resolverAddressesRequired() public view returns (bytes32[] memory addresses) {
bytes32[] memory existingAddresses = Synth.resolverAddressesRequired();
bytes32[] memory newAddresses = new bytes32[](1);
newAddresses[0] = CONTRACT_EXRATES;
addresses = combineArrays(existingAddresses, newAddresses);
}
function exchangeRates() internal view returns (IExchangeRates) {
return IExchangeRates(requireAndGetAddress(CONTRACT_EXRATES));
}
/* ========== MUTATIVE FUNCTIONS ========== */
function purge(address[] calldata addresses) external optionalProxy_onlyOwner {
IExchangeRates exRates = exchangeRates();
uint maxSupplyToPurge = exRates.effectiveValue("sUSD", maxSupplyToPurgeInUSD, currencyKey);
// Only allow purge when total supply is lte the max
require(totalSupply <= maxSupplyToPurge, "Cannot purge as total supply is above threshol.");
for (uint i = 0; i < addresses.length; i++) {
address holder = addresses[i];
uint amountHeld = tokenState.balanceOf(holder);
if (amountHeld > 0) {
exchanger().exchange(holder, holder, currencyKey, amountHeld, "sUSD", holder, false, address(0), bytes32(0));
emitPurged(holder, amountHeld);
}
}
}
/* ========== EVENTS ========== */
event Purged(address indexed account, uint value);
bytes32 private constant PURGED_SIG = keccak256("Purged(address,uint256)");
function emitPurged(address account, uint value) internal {
proxy._emit(abi.encode(value), 2, PURGED_SIG, addressToBytes32(account), 0, 0);
}
}