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

ability for cachedDebt to become negative #2251

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions contracts/BaseDebtCache.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ contract BaseDebtCache is Owned, MixinSystemSettings, IDebtCache {
using SafeMath for uint;
using SafeDecimalMath for uint;

uint internal _cachedDebt;
int internal _cachedDebt;
mapping(bytes32 => uint) internal _cachedSynthDebt;
mapping(bytes32 => uint) internal _excludedIssuedDebt;
uint internal _cacheTimestamp;
Expand Down Expand Up @@ -111,7 +111,7 @@ contract BaseDebtCache is Owned, MixinSystemSettings, IDebtCache {
return getDebtSnapshotStaleTime();
}

function cachedDebt() external view returns (uint) {
function cachedDebt() external view returns (int) {
return _cachedDebt;
}

Expand Down Expand Up @@ -316,7 +316,7 @@ contract BaseDebtCache is Owned, MixinSystemSettings, IDebtCache {
external
view
returns (
uint debt,
int debt,
uint timestamp,
bool isInvalid,
bool isStale
Expand Down
15 changes: 7 additions & 8 deletions contracts/DebtCache.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ contract DebtCache is BaseDebtCache {

// Subtract out the excluded non-SNX backed debt from our total
_cachedSynthDebt[EXCLUDED_DEBT_KEY] = excludedDebt;
uint newDebt = snxCollateralDebt.floorsub(excludedDebt);
_cachedDebt = newDebt;
uint newDebt = snxCollateralDebt - excludedDebt;
_cachedDebt = int(newDebt);
_cacheTimestamp = block.timestamp;
emit DebtCacheUpdated(newDebt);
emit DebtCacheUpdated(int(newDebt));
emit DebtCacheSnapshotTaken(block.timestamp);

// (in)validate the cache if necessary
Expand Down Expand Up @@ -87,11 +87,10 @@ contract DebtCache is BaseDebtCache {
uint delta = SafeDecimalMath.abs(amount);
if (amount > 0) {
_cachedSynthDebt[sUSD] = _cachedSynthDebt[sUSD].add(delta);
_cachedDebt = _cachedDebt.add(delta);
} else {
_cachedSynthDebt[sUSD] = _cachedSynthDebt[sUSD].sub(delta);
_cachedDebt = _cachedDebt.sub(delta);
}
_cachedDebt = _cachedDebt + amount;

emit DebtCacheUpdated(_cachedDebt);
}
Expand Down Expand Up @@ -131,10 +130,10 @@ contract DebtCache is BaseDebtCache {

// Apply the debt update.
if (cachedSum != currentSum) {
uint debt = _cachedDebt;
int debt = _cachedDebt;
// apply the delta between the cachedSum and currentSum
// add currentSum before sub cachedSum to prevent overflow as cachedSum > debt for large amount of excluded debt
debt = debt.add(currentSum).sub(cachedSum);
debt = debt + int(currentSum) - int(cachedSum);
_cachedDebt = debt;
emit DebtCacheUpdated(debt);
}
Expand All @@ -147,7 +146,7 @@ contract DebtCache is BaseDebtCache {

/* ========== EVENTS ========== */

event DebtCacheUpdated(uint cachedDebt);
event DebtCacheUpdated(int cachedDebt);
event DebtCacheSnapshotTaken(uint timestamp);
event DebtCacheValidityChanged(bool indexed isInvalid);
}
4 changes: 2 additions & 2 deletions contracts/interfaces/IDebtCache.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "./IIssuer.sol";
interface IDebtCache {
// Views

function cachedDebt() external view returns (uint);
function cachedDebt() external view returns (int);

function cachedSynthDebt(bytes32 currencyKey) external view returns (uint);

Expand Down Expand Up @@ -37,7 +37,7 @@ interface IDebtCache {
external
view
returns (
uint debt,
int debt,
uint timestamp,
bool isInvalid,
bool isStale
Expand Down
Loading