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

Add tests for zUnipool #2

Open
wants to merge 2 commits into
base: fix/gitcoinTesting
Choose a base branch
from

Conversation

Bobface
Copy link

@Bobface Bobface commented Mar 28, 2020

Test for zUnipool contract

This PR adds tests for the zUnipool contract. The following behaviour is tested as requested in the Gitcoin bounty:

  1. First user is able to stake LP tokens at a price of 1:1
  2. Second user is also able to stake LP, immediately, at a price of 1:1
  3. Immediately, the First User is able to withdraw its LP with the same number of LP tokens
  4. Immediately, the Second User is able to check the value of its zUNI tokens and it should return, at least, the same number of LP tokens that it staked (or higher)
  5. Third User is able to stake LP tokens, after a week of time lapse, but the zUNI Tokens issued to the Third User is not at a price ratio of 1:1, but lesser number of zUNI Tokens compared to the number of LP tokens staked
  6. At the same time, Second User wants to burn a exactly half of its zUNI Tokens, and it is able to receive LP tokens. The LP tokens received at this stage by Second User is higher than half of the LP Tokens staked by the Second User

The tests are supposed to run on a forked mainnet. To do that, run ganache-cli with the -f option providing an web3-provider like Infura: ganache-cli -f https://mainnet.infura.io/v3/<API KEY>. Due to running on a forked mainnet the test might take a while to complete.

Result of tests

The tests are currently not passing - the contract seems to behave differently than expected. For example, the second user gets less zUni tokens when depositing after the first user than he provided LP tokens. Here he provided 2000000000000000000 (in wei) LP tokens and only gets 1999999872560414474 (in wei) zUni tokens:

  1) Contract: zUnipool
       three user interaction:

      wrong second user staked amount
      + expected - actual

      -1999999872560414474
      +2000000000000000000

@amateur-dev
Copy link
Owner

@Bobface Thank you for this. However, I noticed that you have mentioned that the tx and test are failing. Sadly, we will need to know why? We have been digging this issue at our end too. Can you share the test logs with me?

@Bobface
Copy link
Author

Bobface commented Apr 7, 2020

@amateur-dev The reason the tests are failing is because the zUnipool contract does not behave exactly as you have described in your Gitcoin bounty.

For example, your wrote: Second user is also able to stake LP, immediately, at a price of 1:1

This is not the case. The second user gets a tiny amount less LP tokens than the first user. It is almost at a price ration of 1:1 but not exactly - the test detects that difference and fails which is what it is supposed to do in that situation. See the log in my original message:

  1) Contract: zUnipool
       three user interaction:

      wrong second user staked amount
      + expected - actual

      -1999999872560414474
      +2000000000000000000

By the requirements the second user is supposed to have 2 LP tokens. The zUnipool contract only gave him 1.999999872560414474.

Here is the full log of test case 2 failing:

Using network 'development'.


Compiling your contracts...
===========================
> Compiling @openzeppelin/contracts/math/Math.sol
> Compiling @openzeppelin/contracts/math/SafeMath.sol
> Compiling @openzeppelin/contracts/ownership/Ownable.sol
> Compiling @openzeppelin/contracts/token/ERC20/SafeERC20.sol
> Compiling @openzeppelin\contracts\GSN\Context.sol
> Compiling @openzeppelin\contracts\math\SafeMath.sol
> Compiling @openzeppelin\contracts\token\ERC20\IERC20.sol
> Compiling @openzeppelin\contracts\utils\Address.sol
> Compiling .\contracts\IRewardDistributionRecipient.sol
> Compiling .\contracts\Migrations.sol
> Compiling .\contracts\Unipool.sol
> Compiling .\contracts\Unipool_GeneralZap_Flat.sol
> Compiling .\contracts\iUniswapExchangeContract.sol
> Compiling .\contracts\zUniPool.sol
> Compiling .\contracts\IRewardDistributionRecipient.sol
> Compiling .\contracts\Unipool.sol
> Compiling .\contracts\iUniswapExchangeContract.sol
> Compiling contracts/IRewardDistributionRecipient.sol
> Compiling contracts/Migrations.sol
> Compiling contracts/Unipool.sol
> Compiling contracts/iUniswapExchangeContract.sol
> Compiling contracts/zUniPool.sol


  Contract: zUnipool
    1) three user interaction

    Events emitted during test:
    ---------------------------

    Transfer(src: <indexed> 0xb53A06a0891557F302461e16C5b94Bb039e201c5 (address), dst: <indexed> 0x06439B7673938541f5b2AFF67D5bF901b97Df18f (address), wad: 1000000000000000000 (uint256))
    internall(: entering phase 1 (string), : 0 (uint256))
    internall(: howMuchLPStaked (string), : 1000000000000000000 (uint256))
    internall(: tokens2bIssued (string), : 1000000000000000000 (uint256))
    Transfer(src: <indexed> 0x0000000000000000000000000000000000000000 (address), dst: <indexed> 0xb53A06a0891557F302461e16C5b94Bb039e201c5 (address), wad: 1000000000000000000 (uint256))
    internall(: tokens (string), : 1000000000000000000 (uint256))
    Transfer(src: <indexed> 0x06439B7673938541f5b2AFF67D5bF901b97Df18f (address), dst: <indexed> 0x48D7f315feDcaD332F68aafa017c7C158BC54760 (address), wad: 1000000000000000000 (uint256))
    Staked(user: <indexed> 0x06439B7673938541f5b2AFF67D5bF901b97Df18f (address), amount: 1000000000000000000 (uint256))
    Transfer(src: <indexed> 0x23f9DC7B8BdA0C2a5c686301c2a556e435cF3625 (address), dst: <indexed> 0x06439B7673938541f5b2AFF67D5bF901b97Df18f (address), wad: 2000000000000000000 (uint256))
    internall(: entering phase 2 (string), : 1 (uint256))
    internall(: Earnings more than the threshold (string), : 23816231865100 (uint256))
    Transfer(src: <indexed> 0x48D7f315feDcaD332F68aafa017c7C158BC54760 (address), dst: <indexed> 0x06439B7673938541f5b2AFF67D5bF901b97Df18f (address), wad: 23816231865100 (uint256))
    RewardPaid(user: <indexed> 0x06439B7673938541f5b2AFF67D5bF901b97Df18f (address), reward: 23816231865100 (uint256))
    internall(: Claiming Reward (string), : 12 (uint256))
    internall(: got cash (string), : 54122842038 (uint256))
    Transfer(src: <indexed> 0x06439B7673938541f5b2AFF67D5bF901b97Df18f (address), dst: <indexed> 0x3958B4eC427F8fa24eB60F42821760e88d485f7F (address), wad: 11872391584752 (uint256))
    Transfer(src: <indexed> 0x06439B7673938541f5b2AFF67D5bF901b97Df18f (address), dst: <indexed> 0x3958B4eC427F8fa24eB60F42821760e88d485f7F (address), wad: 11943840280348 (uint256))
    Transfer(src: <indexed> 0xe9Cf7887b93150D4F2Da7dFc6D502B216438F244 (address), dst: <indexed> 0x06439B7673938541f5b2AFF67D5bF901b97Df18f (address), wad: 53971506653 (uint256))
    Transfer(src: <indexed> 0x06439B7673938541f5b2AFF67D5bF901b97Df18f (address), dst: <indexed> 0xe9Cf7887b93150D4F2Da7dFc6D502B216438F244 (address), wad: 53810076425 (uint256))
    Transfer(src: <indexed> 0x0000000000000000000000000000000000000000 (address), dst: <indexed> 0x06439B7673938541f5b2AFF67D5bF901b97Df18f (address), wad: 53452774737 (uint256))
    Transfer(src: <indexed> 0x06439B7673938541f5b2AFF67D5bF901b97Df18f (address), dst: <indexed> 0xe9Cf7887b93150D4F2Da7dFc6D502B216438F244 (address), wad: 161430228 (uint256))
    Transfer(src: <indexed> 0x3958B4eC427F8fa24eB60F42821760e88d485f7F (address), dst: <indexed> 0x06439B7673938541f5b2AFF67D5bF901b97Df18f (address), wad: 35297580549 (uint256))
    internall(: LPJustReceived (string), : 53452774737 (uint256))
    Transfer(src: <indexed> 0x06439B7673938541f5b2AFF67D5bF901b97Df18f (address), dst: <indexed> 0x48D7f315feDcaD332F68aafa017c7C158BC54760 (address), wad: 53452774737 (uint256))
    Staked(user: <indexed> 0x06439B7673938541f5b2AFF67D5bF901b97Df18f (address), amount: 53452774737 (uint256))
    internall(: totalLPTokensStaked (string), : 1000000053452774737 (uint256))
    internall(: howMuchLPStaked (string), : 2000000000000000000 (uint256))
    internall(: tokens2bIssued (string), : 1999999893094456240 (uint256))
    Transfer(src: <indexed> 0x0000000000000000000000000000000000000000 (address), dst: <indexed> 0x23f9DC7B8BdA0C2a5c686301c2a556e435cF3625 (address), wad: 1999999893094456240 (uint256))
    internall(: tokens (string), : 1999999893094456240 (uint256))
    Transfer(src: <indexed> 0x06439B7673938541f5b2AFF67D5bF901b97Df18f (address), dst: <indexed> 0x48D7f315feDcaD332F68aafa017c7C158BC54760 (address), wad: 2000000000000000000 (uint256))
    Staked(user: <indexed> 0x06439B7673938541f5b2AFF67D5bF901b97Df18f (address), amount: 2000000000000000000 (uint256))

    ---------------------------

·----------------------------------------|---------------------------|-------------|----------------------------·
|  Solc version: 0.5.13+commit.5b0b510c  ·  Optimizer enabled: true  ·  Runs: 500  ·  Block limit: 6721975 gas  │
·········································|···························|·············|·····························
|  Methods                               ·               10 gwei/gas               ·       172.15 usd/eth       │
······················|··················|·············|·············|·············|··············|··············
|  Contract           ·  Method          ·  Min        ·  Max        ·  Avg        ·  # calls     ·  usd (avg)  │
······················|··················|·············|·············|·············|··············|··············
|  Deployments                           ·                                         ·  % of limit  ·             │
·········································|·············|·············|·············|··············|··············
|  Unipool                               ·          -  ·          -  ·    1269329  ·      18.9 %  ·       2.19  │
·----------------------------------------|-------------|-------------|-------------|--------------|-------------·

  0 passing (4m)
  1 failing

  1) Contract: zUnipool
       three user interaction:

      wrong second user staked amount
      + expected - actual

      -1999999893094456240
      +2000000000000000000

Of course I can update the tests to account for that slight difference and make them pass. However it was unclear to me if that is something you would want, which is why I have been waiting on your feedback. Let me know how you want to continue.

@amateur-dev
Copy link
Owner

hey @Bobface I just went through the tests on a high level, correct me if I am wrong, so we have not been able to test at all the situations of advanceTime (for 1 week, etc etc). Right?

@Bobface
Copy link
Author

Bobface commented Apr 10, 2020

@amateur-dev Correct. All tests are passing when we are working with natural time progession. When we try to use time.increase in test 5 it fails.
It seems to be a bug in ganache-core when using time.increase (or advanceTime to be more specific) when running on forked mainnet (see discussion in #1 ). You can try yourself by uncommenting line 62 and then running the test.

@amateur-dev amateur-dev changed the base branch from master to fix/gitcoinTesting April 10, 2020 13:18
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.

2 participants