0

I writing a contract for Staking Token to earn Token. It's similar to pancake MasterChef. The requirement is based on timestamp (not block). When I test the contract with hardhat, I call the deposit function then time increases, so I can't calculate exactly how many tokens I can withdraw. The deposit function:

    function deposit(uint256 _amount) public payable {
        require(rewardsStartTime <= block.timestamp, "Staking has not started");
        require(rewardsExpiration >= block.timestamp, "Staking has ended");
        UserInfo storage user = userInfo[msg.sender];
        _harvest();
        if (_amount > 0) {
            user.amount = user.amount.add(_amount);
            if (msg.value > 0) {
                require(stakedToken == WETH_ADDRESS, "SmartBaryFactory: Invalid LP Token");

                IWETH(WETH_ADDRESS).deposit{ value: msg.value }();
            } else {
                IERC20(stakedToken).transferFrom(address(msg.sender), address(this), _amount);
            }
            tokenShare = tokenShare.add(_amount);
        }
        for (uint256 i = 0; i < rewardTokens.length; i++) {
            user.rewardDebt[rewardTokens[i]] = user.amount.mul(accRewardPerShare[rewardTokens[i]]).div(
                PRECISION_FACTOR[rewardTokens[i]]
            );
        }
    }

The test function:

it("Should deposit bnb to fungible pool", async function () {
        const Pool = await ethers.getContractFactory("ERC20Pool");
        bnbPool = Pool.attach(bnbPoolAddress);

        console.log("bnb Pool: ", bnbPool.address);

        // Mint bnb to Account 1
        await bnb.connect(acc1).mint(parseEther("10000"));

        // Check balance of Account 1
        expect(await bnb.balanceOf(acc1.address)).to.equal(parseEther("10000"));

        // Approve bnb to pool
        await bnb.connect(acc1).approve(bnbPoolAddress, parseEther("10000"));

        // Check timestamp
        console.log("Time: ", await time.latest());

        // Increase time to reward start time
        await time.increaseTo(rewardStartTime);

        // Deposit bnb to pool
        await bnbPool.connect(acc1).deposit(parseEther("10000"));

        // Check timestamp
        console.log("Time deposit: ", await time.latest());

        // Check balance of Account 1
        expect(await bnb.balanceOf(acc1.address)).to.equal(0);

        // Check balance of bnb pool
        expect(await bnb.balanceOf(bnbPool.address)).to.equal(parseEther("10000"));

        // Increase time to reward expiration
        await time.increaseTo(rewardExpiration + 100);

        // Check timestamp
        console.log("Time withdraw: ", await time.latest());

        // Withdraw bnb from pool
        await bnbPool.connect(acc1).withdraw(parseEther("10000"));

        // Check balance of Account 1
        expect(await bnb.balanceOf(acc1.address)).to.equal(parseEther("10000"));

        // Check balance of bnb pool
        expect(await bnb.balanceOf(bnbPool.address)).to.equal(0);

        // Check reward of Account 1
        expect(await usdc.balanceOf(acc1.address)).to.equal(parseEther("1000"));
        expect(await usdt.balanceOf(acc1.address)).to.equal(parseEther("1000"));
    });

The pool is 1000 USDT and 1000 USDC to pool. But when I deposit BNB to pool, timestamp is late for 1 second. I just receive 990 token return.

AssertionError: expected 990000000000000000000 to equal 1000000000000000000000. The numerical values of the given "ethers.BigNumber" and "ethers.BigNumber" inputs were compared, and they differed.
      + expected - actual

      -990000000000000000000
      +1000000000000000000000
    ```
I want to test with exact timestamp. How can I do it?

1 Answer 1

0

Have you tried time.setNextBlockTimestamp?

Or just try await time.increaseTo(rewardExpiration + 100 -1);

2
  • time.setNextBlockTimestamp is not suitable for this purpose. It's can't change the time of block.timestamp. await time.increaseTo(rewardExpiration + 100 -1); is not the best practice. The transaction maybe late more than 1 second. So it's will be wrong
    – MadTik
    Commented Feb 28 at 10:01
  • Why can't it change the time of block timestamp? All three functions: increase,increaseTo or setNextBlockTimestamp refers to evm_setNextBlockTimestamp. All three of them are the same, just utilities. Btw I can't get your concept without withdraw function, could you update your question?
    – CT95
    Commented Feb 29 at 4:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.