Skip to content

Commit 1332598

Browse files
liamzebedeeeternauta1337Synthetix Teamjacko125
authored
Alioth release (#1263)
* Remove bridge migrator (#1257) * Skips multicollateral prod tests if max debt has been reached (#1259) * SIP-136: MultiCollateral/CollateralEth debt is not excluded correctly from debt calculations (#1243) * SIP-112: EtherWrapper (#1178) * Deploy 2.45 to kovan (#1260) * Prepublish step * 2.45.0-alpha * set mint fee and burn fee per sccp 100 Co-authored-by: Alejandro Santander <[email protected]> Co-authored-by: Synthetix Team <[email protected]> Co-authored-by: Jackson C <[email protected]>
1 parent 960abdb commit 1332598

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+19218
-21054
lines changed

.solcover.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ module.exports = {
99
'EscrowChecker.sol',
1010
'ExchangeRatesWithoutInvPricing.sol',
1111
'IssuerWithoutLiquidations.sol',
12-
'BridgeMigrator.sol',
1312
],
1413
providerOptions: {
1514
default_balance_ether: 10000000000000, // extra zero just in case (coverage consumes more gas)

contracts/BaseDebtCache.sol

Lines changed: 69 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import "./interfaces/IEtherCollateral.sol";
1818
import "./interfaces/IEtherCollateralsUSD.sol";
1919
import "./interfaces/IERC20.sol";
2020
import "./interfaces/ICollateralManager.sol";
21+
import "./interfaces/IEtherWrapper.sol";
2122

2223
// https://docs.synthetix.io/contracts/source/contracts/debtcache
2324
contract BaseDebtCache is Owned, MixinSystemSettings, IDebtCache {
@@ -43,21 +44,23 @@ contract BaseDebtCache is Owned, MixinSystemSettings, IDebtCache {
4344
bytes32 private constant CONTRACT_ETHERCOLLATERAL = "EtherCollateral";
4445
bytes32 private constant CONTRACT_ETHERCOLLATERAL_SUSD = "EtherCollateralsUSD";
4546
bytes32 private constant CONTRACT_COLLATERALMANAGER = "CollateralManager";
47+
bytes32 private constant CONTRACT_ETHER_WRAPPER = "EtherWrapper";
4648

4749
constructor(address _owner, address _resolver) public Owned(_owner) MixinSystemSettings(_resolver) {}
4850

4951
/* ========== VIEWS ========== */
5052

5153
function resolverAddressesRequired() public view returns (bytes32[] memory addresses) {
5254
bytes32[] memory existingAddresses = MixinSystemSettings.resolverAddressesRequired();
53-
bytes32[] memory newAddresses = new bytes32[](7);
55+
bytes32[] memory newAddresses = new bytes32[](8);
5456
newAddresses[0] = CONTRACT_ISSUER;
5557
newAddresses[1] = CONTRACT_EXCHANGER;
5658
newAddresses[2] = CONTRACT_EXRATES;
5759
newAddresses[3] = CONTRACT_SYSTEMSTATUS;
5860
newAddresses[4] = CONTRACT_ETHERCOLLATERAL;
5961
newAddresses[5] = CONTRACT_ETHERCOLLATERAL_SUSD;
6062
newAddresses[6] = CONTRACT_COLLATERALMANAGER;
63+
newAddresses[7] = CONTRACT_ETHER_WRAPPER;
6164
addresses = combineArrays(existingAddresses, newAddresses);
6265
}
6366

@@ -89,6 +92,10 @@ contract BaseDebtCache is Owned, MixinSystemSettings, IDebtCache {
8992
return ICollateralManager(requireAndGetAddress(CONTRACT_COLLATERALMANAGER));
9093
}
9194

95+
function etherWrapper() internal view returns (IEtherWrapper) {
96+
return IEtherWrapper(requireAndGetAddress(CONTRACT_ETHER_WRAPPER));
97+
}
98+
9299
function debtSnapshotStaleTime() external view returns (uint) {
93100
return getDebtSnapshotStaleTime();
94101
}
@@ -120,59 +127,48 @@ contract BaseDebtCache is Owned, MixinSystemSettings, IDebtCache {
120127
return _cacheStale(_cacheTimestamp);
121128
}
122129

123-
function _issuedSynthValues(bytes32[] memory currencyKeys, uint[] memory rates) internal view returns (uint[] memory) {
130+
function _issuedSynthValues(bytes32[] memory currencyKeys, uint[] memory rates)
131+
internal
132+
view
133+
returns (uint[] memory values)
134+
{
124135
uint numValues = currencyKeys.length;
125-
uint[] memory values = new uint[](numValues);
136+
values = new uint[](numValues);
126137
ISynth[] memory synths = issuer().getSynths(currencyKeys);
127138

128139
for (uint i = 0; i < numValues; i++) {
129-
bytes32 key = currencyKeys[i];
130140
address synthAddress = address(synths[i]);
131141
require(synthAddress != address(0), "Synth does not exist");
132142
uint supply = IERC20(synthAddress).totalSupply();
133-
134-
if (collateralManager().isSynthManaged(key)) {
135-
uint collateralIssued = collateralManager().long(key);
136-
137-
// this is an edge case --
138-
// if a synth other than sUSD is only issued by non SNX collateral
139-
// the long value will exceed the supply if there was a minting fee,
140-
// so we check explicitly and 0 it out to prevent
141-
// a safesub overflow.
142-
143-
if (collateralIssued > supply) {
144-
supply = 0;
145-
} else {
146-
supply = supply.sub(collateralIssued);
147-
}
148-
}
149-
150-
bool isSUSD = key == sUSD;
151-
if (isSUSD || key == sETH) {
152-
IEtherCollateral etherCollateralContract =
153-
isSUSD ? IEtherCollateral(address(etherCollateralsUSD())) : etherCollateral();
154-
uint etherCollateralSupply = etherCollateralContract.totalIssuedSynths();
155-
supply = supply.sub(etherCollateralSupply);
156-
}
157-
158143
values[i] = supply.multiplyDecimalRound(rates[i]);
159144
}
160-
return values;
145+
146+
return (values);
161147
}
162148

163149
function _currentSynthDebts(bytes32[] memory currencyKeys)
164150
internal
165151
view
166-
returns (uint[] memory snxIssuedDebts, bool anyRateIsInvalid)
152+
returns (
153+
uint[] memory snxIssuedDebts,
154+
uint _excludedDebt,
155+
bool anyRateIsInvalid
156+
)
167157
{
168158
(uint[] memory rates, bool isInvalid) = exchangeRates().ratesAndInvalidForCurrencies(currencyKeys);
169-
return (_issuedSynthValues(currencyKeys, rates), isInvalid);
159+
uint[] memory values = _issuedSynthValues(currencyKeys, rates);
160+
(uint excludedDebt, bool isAnyNonSnxDebtRateInvalid) = _totalNonSnxBackedDebt();
161+
return (values, excludedDebt, isInvalid || isAnyNonSnxDebtRateInvalid);
170162
}
171163

172164
function currentSynthDebts(bytes32[] calldata currencyKeys)
173165
external
174166
view
175-
returns (uint[] memory debtValues, bool anyRateIsInvalid)
167+
returns (
168+
uint[] memory debtValues,
169+
uint excludedDebt,
170+
bool anyRateIsInvalid
171+
)
176172
{
177173
return _currentSynthDebts(currencyKeys);
178174
}
@@ -190,22 +186,53 @@ contract BaseDebtCache is Owned, MixinSystemSettings, IDebtCache {
190186
return _cachedSynthDebts(currencyKeys);
191187
}
192188

189+
// Returns the total sUSD debt backed by non-SNX collateral.
190+
function totalNonSnxBackedDebt() external view returns (uint excludedDebt, bool isInvalid) {
191+
return _totalNonSnxBackedDebt();
192+
}
193+
194+
function _totalNonSnxBackedDebt() internal view returns (uint excludedDebt, bool isInvalid) {
195+
// Calculate excluded debt.
196+
// 1. Ether Collateral.
197+
excludedDebt = excludedDebt.add(etherCollateralsUSD().totalIssuedSynths()); // Ether-backed sUSD
198+
199+
uint etherCollateralTotalIssuedSynths = etherCollateral().totalIssuedSynths();
200+
// We check the supply > 0 as on L2, we may not yet have up-to-date rates for sETH.
201+
if (etherCollateralTotalIssuedSynths > 0) {
202+
(uint sETHRate, bool sETHRateIsInvalid) = exchangeRates().rateAndInvalid(sETH);
203+
isInvalid = isInvalid || sETHRateIsInvalid;
204+
excludedDebt = excludedDebt.add(etherCollateralTotalIssuedSynths.multiplyDecimalRound(sETHRate)); // Ether-backed sETH
205+
}
206+
207+
// 2. MultiCollateral long debt + short debt.
208+
(uint longValue, bool anyTotalLongRateIsInvalid) = collateralManager().totalLong();
209+
(uint shortValue, bool anyTotalShortRateIsInvalid) = collateralManager().totalShort();
210+
isInvalid = isInvalid || anyTotalLongRateIsInvalid || anyTotalShortRateIsInvalid;
211+
excludedDebt = excludedDebt.add(longValue).add(shortValue);
212+
213+
// 3. EtherWrapper.
214+
// Subtract sETH and sUSD issued by EtherWrapper.
215+
excludedDebt = excludedDebt.add(etherWrapper().totalIssuedSynths());
216+
217+
return (excludedDebt, isInvalid);
218+
}
219+
193220
function _currentDebt() internal view returns (uint debt, bool anyRateIsInvalid) {
194-
(uint[] memory values, bool isInvalid) = _currentSynthDebts(issuer().availableCurrencyKeys());
221+
bytes32[] memory currencyKeys = issuer().availableCurrencyKeys();
222+
(uint[] memory rates, bool isInvalid) = exchangeRates().ratesAndInvalidForCurrencies(currencyKeys);
223+
224+
// Sum all issued synth values based on their supply.
225+
uint[] memory values = _issuedSynthValues(currencyKeys, rates);
226+
(uint excludedDebt, bool isAnyNonSnxDebtRateInvalid) = _totalNonSnxBackedDebt();
227+
195228
uint numValues = values.length;
196229
uint total;
197230
for (uint i; i < numValues; i++) {
198231
total = total.add(values[i]);
199232
}
233+
total = total < excludedDebt ? 0 : total.sub(excludedDebt);
200234

201-
// subtract the USD value of all shorts.
202-
(uint susdValue, bool shortInvalid) = collateralManager().totalShort();
203-
204-
total = total.sub(susdValue);
205-
206-
isInvalid = isInvalid || shortInvalid;
207-
208-
return (total, isInvalid);
235+
return (total, isInvalid || isAnyNonSnxDebtRateInvalid);
209236
}
210237

211238
function currentDebt() external view returns (uint debt, bool anyRateIsInvalid) {

contracts/BaseSynthetix.sol

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ contract BaseSynthetix is IERC20, ExternStateToken, MixinResolver, ISynthetix {
8686
return issuer().totalIssuedSynths(currencyKey, false);
8787
}
8888

89+
// TODO: refactor the name of this function. It also incorporates the exclusion of
90+
// issued sETH by the EtherWrapper.
8991
function totalIssuedSynthsExcludeEtherCollateral(bytes32 currencyKey) external view returns (uint) {
9092
return issuer().totalIssuedSynths(currencyKey, true);
9193
}

contracts/BridgeMigrator.sol

Lines changed: 0 additions & 130 deletions
This file was deleted.

0 commit comments

Comments
 (0)