Skip to content

Conversation

patitonar
Copy link
Contributor

Context

Partial implementation of #268

Changes proposed in this pull request

Adds new revert unit tests for buyCover. Implemented some of the listed test cases, especially the reverts one.

I couldn't find a way to test Cover: Product not initialized, it seems that when a product is listed it also initializes the parameters so I couldn't reproduce a state of listed but with initialPriceRatio = 0 as the code is checking.

Also, I removed the usage of bnEqual in the buyCover tests as it triggers a warning Warning: Use Hardhat Chai Matchers instead of bnEqual: https://hardhat.org/hardhat-chai-matchers/docs/overview#big-numbers

Test plan

New tests were added in test/unit/Cover/buyCover.js

Checklist

  • Rebased the base branch
  • Attached corresponding Github issue
  • Prefixed the name with the type of change (i.e. feat, chore, test)
  • Performed a self-review of my own code
  • Followed the style guidelines of this project
  • Made corresponding changes to the documentation
  • Didn't generate new warnings
  • Didn't generate failures on existing tests
  • Added tests that prove my fix is effective or that my feature works

Review

When reviewing a PR, please indicate intention in comments using the following emojis:

  • 🍰 = Nice to have but not essential.
  • 💡 = Suggestion or a comment based on personal opinion
  • 🔨 = I believe this should be changed.
  • 🤔 = I don’t understand something, do you mind giving me more context?
  • 🚀 = Feedback

@roxdanila roxdanila linked an issue Oct 27, 2022 that may be closed by this pull request
40 tasks
@roxdanila roxdanila force-pushed the test/cover-buycover-revert-unit-tests branch from 64176d4 to 0136d55 Compare October 27, 2022 10:06
const period = 3600 * 24 * 364; // 30 days

const amount = parseEther('1000');
const targetPriceRatio = '260';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just checked and this pool fixture and the createStakingPool call ar all the same for all the tests currently.

It makes sense to have a beforeEach section which is just creating this default pool to avoid this code duplication.

We will add tests for buying across multiple pools. in those tests we can add the extra pool there but we always need at least this 1 pool.

initializing all these values in a fixture to be used in the rest of the test code would strip this section a code from each test.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So let's do a beforeEach with all this code including the createStakingPool

create a fixture for the values for the pool so that values are not initialized in every test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion! I refactored the code, now the tests code is cleaner

@@ -1,8 +1,7 @@
const { expect } = require('chai');
const { ethers } = require('hardhat');
Copy link
Contributor

@danoctavian danoctavian Oct 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm not seeing a test for

Should revert if product is not initialized

should revert with Cover: Product not initialized

I see you mentioned you can't make it fail, see below 👇

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

await cover.connect(accounts.advisoryBoardMembers[0]).addProducts(

To make it revert with that message add a product like in the setup, which will have productId = 1

and set initialPriceRatio: 0

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the help!

I tried doing that but it reverts with Cover: initialPriceRatio < GLOBAL_MIN_PRICE_RATIO which is set as a constant GLOBAL_MIN_PRICE_RATIO = 100.

Unless I'm missing something, it looks like products could not be in an uninitialized state, so in that case, Is that validation needed? I think @roxdanila mentioned something about new functionality to deprecate products in which case once introduced it would make sense to validate that the product was not deprecated

Copy link
Contributor

@danoctavian danoctavian Oct 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah i see you're right. That validation was added very recently and that require is not useful anymore.

Thanks for clarifying.

Yeah we need that require removed it can't actually happen, and add a deprecation check instead

don't solve for this test in this PR


const difference = daiBalanceBefore.sub(daiBalanceAfter);
bnEqual(difference, expectedPremium);
expect(difference).to.be.equal(expectedPremium);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for fixing these

assert.equal(storedCoverData.productId, productId);
assert.equal(storedCoverData.coverAsset, coverAsset);
bnEqual(storedCoverData.amountPaidOut, amountPaidOut);
expect(storedCoverData.amountPaidOut.toString()).to.be.equal(amountPaidOut);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you shouldn't need that toString() here, i expect

correct me if i'm wrong

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was throwing an error, I updated the code so toString() is not longer needed

assert.equal(segment.period, period);
assert.equal(segment.amount.toString(), amount.toString());
bnEqual(segment.priceRatio, targetPriceRatio);
expect(segment.priceRatio.toString()).to.be.equal(targetPriceRatio);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

Copy link
Contributor

@danoctavian danoctavian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a few changes required.

Also expecting the rest of the tests are coming in a different PR?

Or what's the plan for those unticked ones not covered?

@roxdanila roxdanila force-pushed the test/cover-buycover-revert-unit-tests branch from 0136d55 to 01caff4 Compare October 27, 2022 14:15
@patitonar
Copy link
Contributor Author

Also expecting the rest of the tests are coming in a different PR?

Yes, the rest of the tests will be added in another PR

Copy link
Contributor

@danoctavian danoctavian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@patitonar patitonar mentioned this pull request Oct 28, 2022
9 tasks
@roxdanila roxdanila merged commit 007603b into nexus-v2 Oct 29, 2022
@roxdanila roxdanila deleted the test/cover-buycover-revert-unit-tests branch October 29, 2022 09:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

V2 Cover unit tests: buyCover
3 participants