Skip to content

SIP-145 Emit the proper cached debt number when debt snapshots are taken. #1325

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

Merged
merged 9 commits into from
Jun 18, 2021
5 changes: 3 additions & 2 deletions contracts/DebtCache.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ contract DebtCache is BaseDebtCache {
_cachedSynthDebt[currencyKeys[i]] = value;
}
_cachedSynthDebt[EXCLUDED_DEBT_KEY] = excludedDebt;
_cachedDebt = snxCollateralDebt.floorsub(excludedDebt);
uint newDebt = snxCollateralDebt.floorsub(excludedDebt);
_cachedDebt = newDebt;
_cacheTimestamp = block.timestamp;
emit DebtCacheUpdated(snxCollateralDebt);
emit DebtCacheUpdated(newDebt);
emit DebtCacheSnapshotTaken(block.timestamp);

// (in)validate the cache if necessary
Expand Down
36 changes: 35 additions & 1 deletion test/contracts/DebtCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -1200,9 +1200,10 @@ contract('DebtCache', async accounts => {
});

describe('after the synths are exchanged into other synths', async () => {
let tx;
beforeEach(async () => {
// Swap some sETH into synthetic dollarydoos.
await synthetix.exchange(sETH, '5', sAUD, { from: account1 });
tx = await synthetix.exchange(sETH, '5', sAUD, { from: account1 });
});

it('non-SNX debt is unchanged', async () => {
Expand All @@ -1214,6 +1215,39 @@ contract('DebtCache', async accounts => {
it('currentDebt is unchanged', async () => {
assert.bnEqual(currentDebt, await debtCache.currentDebt());
});

it('cached debt is properly updated', async () => {
const logs = await getDecodedLogs({
hash: tx.tx,
contracts: [debtCache],
});

const cachedDebt = (await debtCache.cacheInfo())[0];
decodedEventEqual({
event: 'DebtCacheUpdated',
emittedFrom: debtCache.address,
args: [cachedDebt],
log: logs.find(({ name } = {}) => name === 'DebtCacheUpdated'),
});
});
});

it('is properly reflected in a snapshot', async () => {
const currentDebt = (await debtCache.currentDebt())[0];
const cachedDebt = (await debtCache.cacheInfo())[0];
assert.bnEqual(currentDebt, cachedDebt);
const tx = await debtCache.takeDebtSnapshot();
const logs = await getDecodedLogs({
hash: tx.tx,
contracts: [debtCache],
});

decodedEventEqual({
event: 'DebtCacheUpdated',
emittedFrom: debtCache.address,
args: [cachedDebt],
log: logs.find(({ name } = {}) => name === 'DebtCacheUpdated'),
});
});
});

Expand Down