Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
asselstine committed Oct 6, 2021
1 parent 8ff4df6 commit 2c2879f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 42 deletions.
10 changes: 3 additions & 7 deletions contracts/libraries/TwabLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,6 @@ library TwabLib {
_accountDetails
);

// Check ring buffer timestamp range and compare to the requested start and end epoch timestamps.
// IF start time is out of range S[o n] OR IF end time is out of range [o n]E the calculation is reverted with out of range error.
require(_startTime >= oldTwab.timestamp && _endTime <= newTwab.timestamp, "TwabLib/twab-out-of-range");

ObservationLib.Observation memory startTwab = _calculateTwab(
_twabs,
_accountDetails,
Expand Down Expand Up @@ -287,7 +283,7 @@ library TwabLib {
// Sum the difference in amounts and divide by the difference in timestamps.
// The time-weighted average balance uses time measured between two epoch timestamps as
// a constaint on the measurement when calculating the time weighted average balance.
return afterOrAt.amount - beforeOrAt.amount / afterOrAt.timestamp - beforeOrAt.timestamp;
return (afterOrAt.amount - beforeOrAt.amount) / (afterOrAt.timestamp - beforeOrAt.timestamp);
}

/** @notice Calculates a user TWAB for a target timestamp using the historical TWAB records.
Expand Down Expand Up @@ -421,9 +417,9 @@ library TwabLib {

_twabs[_accountDetails.nextTwabIndex] = newTwab;

_accountDetails = push(_accountDetails);
AccountDetails memory nextAccountDetails = push(_accountDetails);

return (_accountDetails, newTwab, true);
return (nextAccountDetails, newTwab, true);
}

/// @notice "Pushes" a new element on the AccountDetails ring buffer, and returns the new AccountDetails
Expand Down
70 changes: 35 additions & 35 deletions test/TwabLibraryExposed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,51 +82,51 @@ describe('TwabLib', () => {
await twabLib.increaseBalance(currentBalance, timestamp);
});

it('should fail for pre-history requests', async () => {
await expect(
twabLib.getAverageBalanceBetween(
it('should return an average of zero for pre-history requests', async () => {
expect(
await twabLib.getAverageBalanceBetween(
timestamp - 100,
timestamp - 50,
currentTime,
),
).to.be.revertedWith('TwabLib/twab-out-of-range')
).to.equal('0');
});

it('should fail for pre-history requests when including the first twab', async () => {
await expect(
twabLib.getAverageBalanceBetween(timestamp - 100, timestamp, currentTime),
).to.be.revertedWith('TwabLib/twab-out-of-range')
it('should return an average of zero for pre-history requests when including the first twab', async () => {
expect(
await twabLib.getAverageBalanceBetween(timestamp - 100, timestamp, currentTime),
).to.equal('0');
});

it('should not project into the future', async () => {
// at this time the user has held 1000 tokens for zero seconds
await expect(
twabLib.getAverageBalanceBetween(
expect(
await twabLib.getAverageBalanceBetween(
timestamp - 50,
timestamp + 50,
timestamp,
),
).to.be.revertedWith('TwabLib/twab-out-of-range')
).to.equal('0');
});

it('should fail when the duration is centered over first twab', async () => {
await expect(
twabLib.getAverageBalanceBetween(
it('should return half the minted balance when the duration is centered over first twab', async () => {
expect(
await twabLib.getAverageBalanceBetween(
timestamp - 50,
timestamp + 50,
currentTime,
),
).to.be.revertedWith('TwabLib/twab-out-of-range')
).to.equal('500');
});

it('should fail when the range is after the last twab', async () => {
await expect(
twabLib.getAverageBalanceBetween(
it('should return an accurate average when the range is after the last twab', async () => {
expect(
await twabLib.getAverageBalanceBetween(
timestamp + 50,
timestamp + 51,
currentTime,
),
).to.be.revertedWith('TwabLib/twab-out-of-range')
).to.equal('1000');
});
});

Expand All @@ -151,24 +151,24 @@ describe('TwabLib', () => {
< > | |
*/

it('should fail for pre-history requests', async () => {
await expect(
twabLib.getAverageBalanceBetween(
it('should return an average of zero for pre-history requests', async () => {
expect(
await twabLib.getAverageBalanceBetween(
timestamp1 - 100,
timestamp1 - 50,
currentTime,
),
).to.be.revertedWith('TwabLib/twab-out-of-range')
).to.equal(toWei('0'));
});

it('should fail when the duration is centered over first twab', async () => {
await expect(
twabLib.getAverageBalanceBetween(
it('should return half the minted balance when the duration is centered over first twab', async () => {
expect(
await twabLib.getAverageBalanceBetween(
timestamp1 - 50,
timestamp1 + 50,
currentTime,
),
).to.be.revertedWith('TwabLib/twab-out-of-range')
).to.equal(toWei('500'));
});

it('should return an accurate average when the range is between twabs', async () => {
Expand All @@ -181,24 +181,24 @@ describe('TwabLib', () => {
).to.equal(toWei('1000'));
});

it('should fail when the end is after the last twab', async () => {
await expect(
twabLib.getAverageBalanceBetween(
it('should return an accurate average when the end is after the last twab', async () => {
expect(
await twabLib.getAverageBalanceBetween(
timestamp2 - 50,
timestamp2 + 50,
currentTime,
),
).to.be.revertedWith('TwabLib/twab-out-of-range')
).to.equal(toWei('750'));
});

it('should fail when the range is after twabs', async () => {
await expect(
twabLib.getAverageBalanceBetween(
it('should return an accurate average when the range is after twabs', async () => {
expect(
await twabLib.getAverageBalanceBetween(
timestamp2 + 50,
timestamp2 + 51,
currentTime,
),
).to.be.revertedWith('TwabLib/twab-out-of-range')
).to.equal(toWei('500'));
});
});
});
Expand Down

0 comments on commit 2c2879f

Please sign in to comment.