3

I have a smart contract here that have mints NFTs and claim reward functions, and I have used custom errors instead of require

error stakeNotOver();

 function claimReward(uint256 stakeId) external nonReentrant returns (bool){
        if(addressStaked[_msgSender()] == false)
        revert notParticipant();
        if(stakerTokensInfo[_msgSender()][stakeId].endTS > block.timestamp)
        revert stakeNotOver();
        if(stakerTokensInfo[_msgSender()][stakeId].claimed > 0)
        revert alreadyClaimed();
        for(uint256 i =0; i< stakingId[msg.sender].length; i++){
            if(stakeId != stakingId[msg.sender][i])
            revert NoSuchStakingExists();
        }

but now as I am writing test cases using chai, I don't know how to deal with them. I tried something I found on the internet but it didn't work either

    it("Should not unstake if the time is not completed", async function () {

        await testToken.connect(owner).transfer(hyperStaking.address, 1000)
        await hyperStaking.connect(owner).stakeToken(500, 1)
        await expect(hyperStaking.connect(owner).claimReward(1)).to.be.revertedWithCustomError(hyperStaking, "stakeNotOver");

    });

Please let me know if I am doing anything wrong or is there no support for custom errors as of yet

1
  • The code looks correct. Are you sure your query will throw an error? What error is it throwing right now? Feb 28, 2023 at 9:21

1 Answer 1

1

What error is it? I tried it by myself and see the error Invalid Chai property: revertedWithCustomError. Did you try to change revertedWithCustomError => revertedWith like below?

 it("Should not unstake if the time is not completed", async function () {
        await testToken.connect(owner).transfer(hyperStaking.address, 1000)
        await hyperStaking.connect(owner).stakeToken(500, 1)
        await expect(hyperStaking.connect(owner).claimReward(1)).to.be.revertedWith("stakeNotOver");

    });
1

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.