Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SIP-182 Wrapper Factory #1489

Merged
merged 47 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
6059132
theoretical contract changes
dbeal-eth Aug 31, 2021
c7f9a84
add tests for wrapper, and stub for wrapperfactory
dbeal-eth Sep 2, 2021
4fc47ec
fix tests
dbeal-eth Sep 2, 2021
64dc546
add comment
dbeal-eth Sep 8, 2021
b2c5571
fix tests for WrapperFactory
dbeal-eth Sep 9, 2021
55fc210
more test fixes
dbeal-eth Sep 9, 2021
5369678
implement excludedDebt in DebtCache, fixed associated tests
dbeal-eth Sep 12, 2021
56a9c72
re-add old ether wrapper in case we need
dbeal-eth Sep 13, 2021
fc5e171
add missing mock file
dbeal-eth Sep 14, 2021
7460d21
Merge branch 'abstract-wrapper-with-old-ether' into abstract-eth-wrapper
dbeal-eth Sep 23, 2021
d61d750
Merge remote-tracking branch 'origin/develop' into abstract-eth-wrapper
dbeal-eth Sep 23, 2021
72c1249
fix test failures and misses
dbeal-eth Sep 24, 2021
fc6167d
fix accidential synths local-ovm update
dbeal-eth Sep 24, 2021
4d3ed73
add missing tests
dbeal-eth Sep 24, 2021
1e7c4b2
fix duplicate maxTokenAmountCall
dbeal-eth Sep 24, 2021
586818d
better naming pattern
dbeal-eth Sep 24, 2021
db11994
replace with safemath
dbeal-eth Sep 24, 2021
3fe239e
try fix test failures
dbeal-eth Sep 24, 2021
5a23fc2
fix math error
dbeal-eth Sep 28, 2021
8aa3ff9
fix local-ovm deployment oops
dbeal-eth Sep 28, 2021
9d1a5e8
remove unused totalIssuedSynths
dbeal-eth Sep 28, 2021
0d70923
update to allow for negative fees
dbeal-eth Oct 1, 2021
34c2b7b
fix comment
dbeal-eth Oct 5, 2021
19c8bc4
fix(test): extract correct address from event
leckylao Oct 9, 2021
d8e71b8
Try to fix CI by loading the contract with ethers
leckylao Oct 11, 2021
68fd43e
using ethers from hardhat
leckylao Oct 11, 2021
89389aa
Try to fix integration test
leckylao Oct 11, 2021
3057c1a
deploying WETH for OVM integration test
leckylao Oct 12, 2021
4d04cd4
perf(test): fix WrapperFactory integration test:
leckylao Oct 12, 2021
fac5931
revert "-d" option on Ops start
leckylao Oct 12, 2021
d65f99b
Add fallback coverage
barrasso Oct 12, 2021
f2fbc13
Merge develop into abstract-eth-wrapper
barrasso Oct 13, 2021
e160741
Fix merge conflict
barrasso Oct 13, 2021
d147d8a
fix slither security alerts
barrasso Oct 13, 2021
7017dd0
remove unused code
barrasso Oct 13, 2021
9192ea5
Merge branch 'develop' into abstract-eth-wrapper
barrasso Oct 15, 2021
4f6e1b6
Merge branch develop into abstract-eth-wrapper and fix conflicts
barrasso Oct 20, 2021
2b76dca
changes requested by isiro
dbeal-eth Oct 26, 2021
1f2847e
fix lint test
barrasso Oct 26, 2021
d41faf2
update _totalNonSnxBackedDebt to better handle excluded debt
dbeal-eth Oct 26, 2021
077b0e8
checkout to dev branch updateCachedSynthDebtsWithRates
dbeal-eth Oct 26, 2021
61a0fad
Merge branch 'abstract-eth-wrapper' of https://github.com/Synthetixio…
dbeal-eth Oct 26, 2021
447401c
re-add debt-cache comment
dbeal-eth Oct 26, 2021
1fc0b87
fix isInvalid
dbeal-eth Oct 26, 2021
66e944e
fix unneeded constants
dbeal-eth Oct 26, 2021
f52432a
remove convenience function and accompanying modifier
dbeal-eth Oct 26, 2021
5ddb558
fix minor things
dbeal-eth Oct 26, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 45 additions & 9 deletions contracts/BaseDebtCache.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import "./interfaces/IExchangeRates.sol";
import "./interfaces/ISystemStatus.sol";
import "./interfaces/IERC20.sol";
import "./interfaces/ICollateralManager.sol";
import "./interfaces/IEtherWrapper.sol";
import "./interfaces/IWrapperFactory.sol";

// https://docs.synthetix.io/contracts/source/contracts/debtcache
contract BaseDebtCache is Owned, MixinSystemSettings, IDebtCache {
Expand All @@ -25,6 +25,7 @@ contract BaseDebtCache is Owned, MixinSystemSettings, IDebtCache {

uint internal _cachedDebt;
mapping(bytes32 => uint) internal _cachedSynthDebt;
mapping(bytes32 => uint) internal _excludedIssuedDebt;
uint internal _cacheTimestamp;
bool internal _cacheInvalid = true;

Expand All @@ -40,7 +41,7 @@ contract BaseDebtCache is Owned, MixinSystemSettings, IDebtCache {
bytes32 private constant CONTRACT_EXRATES = "ExchangeRates";
bytes32 private constant CONTRACT_SYSTEMSTATUS = "SystemStatus";
bytes32 private constant CONTRACT_COLLATERALMANAGER = "CollateralManager";
bytes32 private constant CONTRACT_ETHER_WRAPPER = "EtherWrapper";
bytes32 private constant CONTRACT_WRAPPERFACTORY = "WrapperFactory";

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

Expand All @@ -54,7 +55,7 @@ contract BaseDebtCache is Owned, MixinSystemSettings, IDebtCache {
newAddresses[2] = CONTRACT_EXRATES;
newAddresses[3] = CONTRACT_SYSTEMSTATUS;
newAddresses[4] = CONTRACT_COLLATERALMANAGER;
newAddresses[5] = CONTRACT_ETHER_WRAPPER;
newAddresses[5] = CONTRACT_WRAPPERFACTORY;
addresses = combineArrays(existingAddresses, newAddresses);
}

Expand All @@ -78,8 +79,8 @@ contract BaseDebtCache is Owned, MixinSystemSettings, IDebtCache {
return ICollateralManager(requireAndGetAddress(CONTRACT_COLLATERALMANAGER));
}

function etherWrapper() internal view returns (IEtherWrapper) {
return IEtherWrapper(requireAndGetAddress(CONTRACT_ETHER_WRAPPER));
function wrapperFactory() internal view returns (IWrapperFactory) {
return IWrapperFactory(requireAndGetAddress(CONTRACT_WRAPPERFACTORY));
}

function debtSnapshotStaleTime() external view returns (uint) {
Expand Down Expand Up @@ -144,6 +145,11 @@ contract BaseDebtCache is Owned, MixinSystemSettings, IDebtCache {
(uint[] memory rates, bool isInvalid) = exchangeRates().ratesAndInvalidForCurrencies(currencyKeys);
uint[] memory values = _issuedSynthValues(currencyKeys, rates);
(uint excludedDebt, bool isAnyNonSnxDebtRateInvalid) = _totalNonSnxBackedDebt();

for (uint i = 0; i < currencyKeys.length; i++) {
excludedDebt = excludedDebt.add(_excludedIssuedDebt[currencyKeys[i]].multiplyDecimalRound(rates[i]));
dbeal-eth marked this conversation as resolved.
Show resolved Hide resolved
}

return (values, excludedDebt, isInvalid || isAnyNonSnxDebtRateInvalid);
}

Expand Down Expand Up @@ -172,7 +178,20 @@ contract BaseDebtCache is Owned, MixinSystemSettings, IDebtCache {
return _cachedSynthDebts(currencyKeys);
}

// Returns the total sUSD debt backed by non-SNX collateral.
function _excludedIssuedDebts(bytes32[] memory currencyKeys) internal view returns (uint[] memory) {
uint numKeys = currencyKeys.length;
uint[] memory debts = new uint[](numKeys);
for (uint i = 0; i < numKeys; i++) {
debts[i] = _excludedIssuedDebt[currencyKeys[i]];
}
return debts;
}

function excludedIssuedDebts(bytes32[] calldata currencyKeys) external view returns (uint[] memory excludedDebts) {
return _excludedIssuedDebts(currencyKeys);
}

// Returns the total sUSD debt backed by non-SNX collateral, excluding debts recorded with _excludedIssuedDebt
function totalNonSnxBackedDebt() external view returns (uint excludedDebt, bool isInvalid) {
return _totalNonSnxBackedDebt();
}
Expand All @@ -185,9 +204,9 @@ contract BaseDebtCache is Owned, MixinSystemSettings, IDebtCache {
isInvalid = anyTotalLongRateIsInvalid || anyTotalShortRateIsInvalid;
excludedDebt = longValue.add(shortValue);

// 2. EtherWrapper.
// Subtract sETH and sUSD issued by EtherWrapper.
excludedDebt = excludedDebt.add(etherWrapper().totalIssuedSynths());
dbeal-eth marked this conversation as resolved.
Show resolved Hide resolved
// 2. Wrapper.
// Subtract totalIssuedSynths issued by all wrappers.
excludedDebt = excludedDebt.add(wrapperFactory().totalIssuedSynths());
dbeal-eth marked this conversation as resolved.
Show resolved Hide resolved

return (excludedDebt, isInvalid);
}
Expand All @@ -204,6 +223,7 @@ contract BaseDebtCache is Owned, MixinSystemSettings, IDebtCache {
uint total;
for (uint i; i < numValues; i++) {
total = total.add(values[i]);
excludedDebt = excludedDebt.add(_excludedIssuedDebt[currencyKeys[i]].multiplyDecimalRound(rates[i]));
}
total = total < excludedDebt ? 0 : total.sub(excludedDebt);

Expand Down Expand Up @@ -245,6 +265,8 @@ contract BaseDebtCache is Owned, MixinSystemSettings, IDebtCache {

function takeDebtSnapshot() external {}

function recordExcludedDebtChange(bytes32 currencyKey, int256 delta) external {}

/* ========== MODIFIERS ========== */

function _requireSystemActiveIfNotOwner() internal view {
Expand Down Expand Up @@ -275,4 +297,18 @@ contract BaseDebtCache is Owned, MixinSystemSettings, IDebtCache {
_onlyIssuerOrExchanger();
_;
}

function _onlyDebtIssuer() internal view {
bool isWrapper = wrapperFactory().isWrapper(msg.sender);

// owner included for debugging and fixing in emergency situation
bool isOwner = msg.sender == owner;

require(isOwner || isWrapper, "Only debt issuers may call this");
}

modifier onlyDebtIssuer() {
_onlyDebtIssuer();
_;
}
}
25 changes: 12 additions & 13 deletions contracts/DebtCache.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,36 @@ contract DebtCache is BaseDebtCache {

function updateCachedSynthDebts(bytes32[] calldata currencyKeys) external requireSystemActiveIfNotOwner {
(uint[] memory rates, bool anyRateInvalid) = exchangeRates().ratesAndInvalidForCurrencies(currencyKeys);
_updateCachedSynthDebtsWithRates(currencyKeys, rates, anyRateInvalid, false);
_updateCachedSynthDebtsWithRates(currencyKeys, rates, anyRateInvalid);
}

function updateCachedSynthDebtWithRate(bytes32 currencyKey, uint currencyRate) external onlyIssuer {
bytes32[] memory synthKeyArray = new bytes32[](1);
synthKeyArray[0] = currencyKey;
uint[] memory synthRateArray = new uint[](1);
synthRateArray[0] = currencyRate;
_updateCachedSynthDebtsWithRates(synthKeyArray, synthRateArray, false, false);
_updateCachedSynthDebtsWithRates(synthKeyArray, synthRateArray, false);
}

function updateCachedSynthDebtsWithRates(bytes32[] calldata currencyKeys, uint[] calldata currencyRates)
external
onlyIssuerOrExchanger
{
_updateCachedSynthDebtsWithRates(currencyKeys, currencyRates, false, false);
_updateCachedSynthDebtsWithRates(currencyKeys, currencyRates, false);
}

function updateDebtCacheValidity(bool currentlyInvalid) external onlyIssuer {
_updateDebtCacheValidity(currentlyInvalid);
}

function recordExcludedDebtChange(bytes32 currencyKey, int256 delta) external onlyDebtIssuer {
int256 newExcludedDebt = int256(_excludedIssuedDebt[currencyKey]) + delta;

require(newExcludedDebt >= 0, "Excluded debt cannot become negative");

_excludedIssuedDebt[currencyKey] = uint(newExcludedDebt);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dbeal-eth Do we want to update the debt cache cachedDebt value as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after discussion this will be reviewed and replaced if necessary before mainnet later this week

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As comment above, consider updating a single value to roll up excludedDebt saves iterating over a mapping of all excluded debts when taking snapshot

}

/* ========== INTERNAL FUNCTIONS ========== */

function _updateDebtCacheValidity(bool currentlyInvalid) internal {
Expand All @@ -82,8 +90,7 @@ contract DebtCache is BaseDebtCache {
function _updateCachedSynthDebtsWithRates(
bytes32[] memory currencyKeys,
uint[] memory currentRates,
bool anyRateIsInvalid,
bool recomputeExcludedDebt
bool anyRateIsInvalid
) internal {
uint numKeys = currencyKeys.length;
require(numKeys == currentRates.length, "Input array lengths differ");
Expand All @@ -102,16 +109,8 @@ contract DebtCache is BaseDebtCache {
_cachedSynthDebt[key] = currentSynthDebt;
}

// Always update the cached value of the excluded debt -- it's computed anyway.
if (recomputeExcludedDebt) {
(uint excludedDebt, bool anyNonSnxDebtRateIsInvalid) = _totalNonSnxBackedDebt();
anyRateIsInvalid = anyRateIsInvalid || anyNonSnxDebtRateIsInvalid;
excludedDebtSum = excludedDebt;
}

cachedSum = cachedSum.floorsub(_cachedSynthDebt[EXCLUDED_DEBT_KEY]);
currentSum = currentSum.floorsub(excludedDebtSum);
_cachedSynthDebt[EXCLUDED_DEBT_KEY] = excludedDebtSum;

// Compute the difference and apply it to the snapshot
if (cachedSum != currentSum) {
Expand Down
Loading