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

flaky unit tests #319

Merged
merged 3 commits into from
Apr 22, 2022
Merged
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
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