Skip to content

Commit

Permalink
flaky unit tests (#319)
Browse files Browse the repository at this point in the history
* lock time for flaky baseTrackingAccrued unit test

* lock time for other flaky baseTrackingAccrued unit test

* lock start time for flaky accrue test
  • Loading branch information
scott-silver authored Apr 22, 2022
1 parent 1a9b2b5 commit 4624e4b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
19 changes: 11 additions & 8 deletions test/accrue-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,25 @@ describe('accrue', function () {
it('accrue initially succeeds and has the right parameters', async () => {
await ethers.provider.send('hardhat_reset', []); // ensure clean start...

const start = (await getBlock()).timestamp + 100;

const params = {
baseMinForRewards: 12331,
baseTrackingSupplySpeed: 668,
baseTrackingBorrowSpeed: 777,
start
};
const { comet } = await makeProtocol(params);

const t0 = await comet.totalsBasic();
expect(await t0.trackingSupplyIndex).to.be.equal(0);
expect(await t0.trackingBorrowIndex).to.be.equal(0);
expect(await t0.baseSupplyIndex).to.be.equal(exp(1, 15));
expect(await t0.baseBorrowIndex).to.be.equal(exp(1, 15));
expect(await t0.totalSupplyBase).to.be.equal(0);
expect(await t0.totalBorrowBase).to.be.equal(0);

expect(await t0.lastAccrualTime).to.be.approximately(Date.now() / 1000, 80);
expect(t0.trackingSupplyIndex).to.be.equal(0);
expect(t0.trackingBorrowIndex).to.be.equal(0);
expect(t0.baseSupplyIndex).to.be.equal(exp(1, 15));
expect(t0.baseBorrowIndex).to.be.equal(exp(1, 15));
expect(t0.totalSupplyBase).to.be.equal(0);
expect(t0.totalBorrowBase).to.be.equal(0);

expect(t0.lastAccrualTime).to.equal(start);

const a0 = await wait(comet.accrue());
expect(await comet.baseMinForRewards()).to.be.equal(params.baseMinForRewards);
Expand Down
22 changes: 20 additions & 2 deletions test/base-tracking-accrued-test.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
import { ethers, expect, exp, fastForward, makeProtocol } from './helpers';
import { ethers, expect, exp, fastForward, getBlock, makeProtocol } from './helpers';

describe('baseTrackingAccrued', function() {
it('supply updates baseTrackingAccrued to 6 decimal value', async () => {
const start = (await getBlock()).timestamp + 100;

const {
comet, tokens, users: [alice]
} = await makeProtocol({
base: 'USDC',
trackingIndexScale: 1e15,
baseTrackingSupplySpeed: 1e15, // supplySpeed=1 Comp/s
start
});
const { USDC } = tokens;

// allocate and approve transfers
await USDC.allocateTo(alice.address, 2e6);
await USDC.connect(alice).approve(comet.address, 2e6);

await ethers.provider.send("evm_setAutomine", [false]);

// supply once
await comet.connect(alice).supply(USDC.address, 1e6);
const firstSupplyTime = start + 100;
await ethers.provider.send('evm_mine', [firstSupplyTime]);

const userBasic1 = await comet.userBasic(alice.address);
expect(userBasic1.principal).to.eq(1_000_000);
expect(userBasic1.baseTrackingAccrued).to.eq(0);

// supply again
await comet.connect(alice).supply(USDC.address, 1e6);
await ethers.provider.send('evm_mine', [firstSupplyTime + 1]);
await ethers.provider.send("evm_setAutomine", [true]);

const userBasic2 = await comet.userBasic(alice.address);
expect(userBasic2.principal).to.eq(2_000_000);
Expand Down Expand Up @@ -157,13 +166,16 @@ describe('baseTrackingAccrued', function() {
});

it('increases baseTrackingAccrued on borrow', async () => {
const start = (await getBlock()).timestamp + 100;

const {
comet, tokens, users: [alice]
} = await makeProtocol({
base: 'USDC',
trackingIndexScale: 1e15,
baseTrackingBorrowSpeed: 1e15, // borrowSpeed=1 Comp/s per unit of borrowed base
baseMinForRewards: exp(.5, 6)
baseMinForRewards: exp(.5, 6),
start
});
const { USDC, WETH } = tokens;

Expand All @@ -180,15 +192,21 @@ describe('baseTrackingAccrued', function() {
expect(userBasic1.principal).to.eq(0);
expect(userBasic1.baseTrackingAccrued).to.eq(0);

await ethers.provider.send("evm_setAutomine", [false]);

// withdraw base token
await comet.connect(alice).withdraw(USDC.address, 1e6);
const firstWithdrawTime = start + 100;
await ethers.provider.send('evm_mine', [firstWithdrawTime]);

const userBasic2 = await comet.userBasic(alice.address);
expect(userBasic2.principal).to.eq(-1e6);
expect(userBasic2.baseTrackingAccrued).to.eq(0);

// withdraw again
await comet.connect(alice).withdraw(USDC.address, 1e6);
await ethers.provider.send('evm_mine', [firstWithdrawTime + 1]);
await ethers.provider.send("evm_setAutomine", [true]);

const userBasic3 = await comet.userBasic(alice.address);
expect(userBasic3.principal).to.eq(-2e6);
Expand Down

0 comments on commit 4624e4b

Please sign in to comment.