Skip to content

Commit e5158fe

Browse files
zyzekliamzebedee
authored andcommitted
SIP-145 Emit the proper cached debt number when debt snapshots are taken. (#1325)
1 parent 374dbaa commit e5158fe

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

contracts/DebtCache.sol

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ contract DebtCache is BaseDebtCache {
3434
_cachedSynthDebt[currencyKeys[i]] = value;
3535
}
3636
_cachedSynthDebt[EXCLUDED_DEBT_KEY] = excludedDebt;
37-
_cachedDebt = snxCollateralDebt.floorsub(excludedDebt);
37+
uint newDebt = snxCollateralDebt.floorsub(excludedDebt);
38+
_cachedDebt = newDebt;
3839
_cacheTimestamp = block.timestamp;
39-
emit DebtCacheUpdated(snxCollateralDebt);
40+
emit DebtCacheUpdated(newDebt);
4041
emit DebtCacheSnapshotTaken(block.timestamp);
4142

4243
// (in)validate the cache if necessary

test/contracts/DebtCache.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1200,9 +1200,10 @@ contract('DebtCache', async accounts => {
12001200
});
12011201

12021202
describe('after the synths are exchanged into other synths', async () => {
1203+
let tx;
12031204
beforeEach(async () => {
12041205
// Swap some sETH into synthetic dollarydoos.
1205-
await synthetix.exchange(sETH, '5', sAUD, { from: account1 });
1206+
tx = await synthetix.exchange(sETH, '5', sAUD, { from: account1 });
12061207
});
12071208

12081209
it('non-SNX debt is unchanged', async () => {
@@ -1214,6 +1215,39 @@ contract('DebtCache', async accounts => {
12141215
it('currentDebt is unchanged', async () => {
12151216
assert.bnEqual(currentDebt, await debtCache.currentDebt());
12161217
});
1218+
1219+
it('cached debt is properly updated', async () => {
1220+
const logs = await getDecodedLogs({
1221+
hash: tx.tx,
1222+
contracts: [debtCache],
1223+
});
1224+
1225+
const cachedDebt = (await debtCache.cacheInfo())[0];
1226+
decodedEventEqual({
1227+
event: 'DebtCacheUpdated',
1228+
emittedFrom: debtCache.address,
1229+
args: [cachedDebt],
1230+
log: logs.find(({ name } = {}) => name === 'DebtCacheUpdated'),
1231+
});
1232+
});
1233+
});
1234+
1235+
it('is properly reflected in a snapshot', async () => {
1236+
const currentDebt = (await debtCache.currentDebt())[0];
1237+
const cachedDebt = (await debtCache.cacheInfo())[0];
1238+
assert.bnEqual(currentDebt, cachedDebt);
1239+
const tx = await debtCache.takeDebtSnapshot();
1240+
const logs = await getDecodedLogs({
1241+
hash: tx.tx,
1242+
contracts: [debtCache],
1243+
});
1244+
1245+
decodedEventEqual({
1246+
event: 'DebtCacheUpdated',
1247+
emittedFrom: debtCache.address,
1248+
args: [cachedDebt],
1249+
log: logs.find(({ name } = {}) => name === 'DebtCacheUpdated'),
1250+
});
12171251
});
12181252
});
12191253

0 commit comments

Comments
 (0)