Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

convenience contract test cases and revisions #287

Closed

Conversation

Ashar2shahid
Copy link
Contributor

I've created most of the test cases that have 90% test coverage let me know if I've missed anything

@@ -227,6 +227,7 @@ contract Convenience is Ownable {
else
{
delegateState[i] = api3Voting.getVoterState(voteIds[i], delegateAt[i]);
voterState[i] = delegateState[i];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Shouldn't the voterState match the delegateState or am I mistaken?

Copy link
Member

@bbenligiray bbenligiray Jun 7, 2021

Choose a reason for hiding this comment

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

So one way would have been to keep a single voterState and keep the actual voter state if the user didn't have a delegate and keep the delegate state if the user had a delegate (this would reduce the number of variables actually, so may want to do this if we want to return more data and are hitting stack limits). I went with the explicit route instead, keep seperate voter and delegate states and the user of this function will need to decide on which to read based on if the user had a delegate. So according to that convention, the added line is redundant.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

makes sense, I was receiving an array of 0s from the voterState when a delegate was present but according to the convention above, when a delegate is present we will be ignoring this array. Kind of mixed on returning an array of 0s as well because the 0s signify a state as well.

@@ -259,7 +260,7 @@ contract Convenience is Ownable {
return new uint256[](0);
}
uint256 countOpenVote = 0;
for (uint256 i = votesLength - 1; i >= 0; i--)
for (uint256 i = votesLength ; i > 0; i--)
Copy link
Contributor Author

@Ashar2shahid Ashar2shahid Jun 7, 2021

Choose a reason for hiding this comment

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

for (uint256 i = votesLength - 1; i >= 0; i--)

This was leading to underflow

Copy link
Member

Choose a reason for hiding this comment

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

It did with the previous version but not this one because if votesLength is 0 it returns early

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Even in the previous version, at the last iteration of the loop i.e i=0 it will call i-- and cause the i value to underflow for the next comparison of the loop.

Copy link
Member

Choose a reason for hiding this comment

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

Ah right 👍

@Ashar2shahid
Copy link
Contributor Author

Also I don't think this line of code is needed because enum implicitly rejects anything outside its range

@@ -0,0 +1,133 @@
//SPDX-License-Identifier: MIT
Copy link
Member

@bbenligiray bbenligiray Jun 7, 2021

Choose a reason for hiding this comment

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

A mock contract should only include what is needed. I would replace this with

pragma solidity 0.8.4;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract MockApi3Token is ERC20 {
  function getMinterStatus(address minterAddress)
    external
    view
    returns (bool)
    {
      return false;
    }
}

This allows you to skip mintReward() calls without crashing.

@@ -0,0 +1,45 @@
//SPDX-License-Identifier: MIT
Copy link
Member

Choose a reason for hiding this comment

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

Mock contracts don't need an interface, you can remove this

@bbenligiray
Copy link
Member

Also I don't think this line of code is needed because enum implicitly rejects anything outside its range

Can you remove it and try calling it with with 5 for example?

Comment on lines +7 to +15
const VOTER_STATE = ["ABSENT", "YEA", "NAY"].reduce((state, key, index) => {
state[key] = index;
return state;
}, {});

const VOTING_TYPE = ["Primary", "Secondary"].reduce((state, key, index) => {
state[key] = index;
return state;
}, {});
Copy link
Member

Choose a reason for hiding this comment

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

The enum declaration above looks much nicer, I don't know why you went this. Also, even though these are defined as constants, semantically they are enums and enum names tend to be CamelCase.

Copy link
Contributor Author

@Ashar2shahid Ashar2shahid Jun 7, 2021

Choose a reason for hiding this comment

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

The older test cases used this convention. So I kept the same convention. I'll revise it in the update

@@ -1,46 +1,259 @@
const { expect } = require("chai");
Copy link
Member

Choose a reason for hiding this comment

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

The file is not prettified & linted. Husky is supposed to do this automatically but it's broken at the moment, will have to do that manually at the project root.

@bbenligiray
Copy link
Member

@bbenligiray bbenligiray closed this Jun 7, 2021
@Ashar2shahid
Copy link
Contributor Author

Also I don't think this line of code is needed because enum implicitly rejects anything outside its range

Can you remove it and try calling it with with 5 for example?

Yup running it with anything other than 0 and 1 causes

Error: Transaction reverted and Hardhat couldn't infer the reason. Please report this to help us improve Hardhat.
    at Convenience.getDynamicVoteData (contracts/Convenience.sol:172)
    at Convenience.internal@9588 (contracts/Convenience.sol)
    at process._tickCallback (internal/process/next_tick.js:68:7)

@Ashar2shahid
Copy link
Contributor Author

Ashar2shahid commented Jun 7, 2021

  • Revert the changes to the contracts

The underflow error is a problem so I will be leaving that unless there is some other solution.

  • Test coverage should be 100%, the point of coverage reporting is to ensure this

Alright

My bad, will create a PR to the right branch created the new one.

@bbenligiray
Copy link
Member

Yup running it with anything other than 0 and 1 causes

Reverting explicitly with a string is still nice though, but okay

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants