0

I'm stuck on my last test for the NftMarketPlace unit tests. I want it to revert if the withdrawal is not successful. I have searched how to write tests for boolean values but I didn't find anything helpful.

The way I had thought of doing it, is having a player 3 who tries to withdraw the funds and since the nft doesn't belong to them then the transfer fails. See my test. This doesn't work as fails with a massive error

error (error is way too long - just pasting the first 3 lines)

 reverts if transfer fails:
     Error: invalid signer or provider (argument="signerOrProvider", value={"interface":{"fragments":[{"name":null,"type":"constructor","inputs":[],"payable":false,"stateMutability":"nonpayable","gas":null,"_isFragment":true},{"type":"error","name":"NftMarketplace__AlreadyListed","inputs":[{"name":"nftAddress","type":"address","indexed":null,"components":null,"arrayLength":null,"arrayChildren":null,"baseType":"address","_isParamType":true},{"name":"tokenId","type":"uint256","indexed":null,"components":null,"arrayLength":null,"arrayChildren":null,"baseType":"uint256","_isParamType":true}] .................................................(error is way too long)

.sol snipet

function withdrawProceeds() external {
        uint256 proceeds = s_proceeds[msg.sender];
        if (proceeds <= 0) {
            revert NftMarketplace__NoProceeds();
        }
        s_proceeds[msg.sender] = 0; // setting state first to avoid any chance for RE-ENTRANCY ATTACK
        (bool success, ) = payable(msg.sender).call{value: proceeds}("");
        if (!success) {
            revert NftMarketplace__TransferFailed();
        }
    }

My test

it("reverts if transfer fails", async () => {
                player1 = accounts[2]
                await nftMarketplace.listItem(basicNft.address, TOKEN_ID, PRICE) // list the nft
                const playerConnectedNftMarketplace = nftMarketplace.connect(player) // connect player 1
                await playerConnectedNftMarketplace.buyItem(basicNft.address, TOKEN_ID, { // have player 1 buy the nft so deployer can have some proceeds. 
                    //This is to avoid reverting with NftMarketplace__NoProceeds()
                    // buys nft
                    value: PRICE,
                })
                const proceeds = await nftMarketplace.getProceeds(deployer.address) // so there are proceeds at this point
                const player1NftMarketplace = nftMarketplace.connect(player1)
                nftMarketplace = await ethers.getContract("NftMarketplace", player1NftMarketplace)
                await expect(
                    player1NftMarketplace.withdrawProceeds()
                ).to.be.revertedWithCustomError(nftMarketplace, "NftMarketplace__TransferFailed")
            })

Repo: https://github.com/kihiuFrank/hardhat-nft-marketplace

3
  • Consider switching to Foundry, writing tests like this would be 10x easier. Commented Nov 20, 2022 at 12:25
  • @PaulRazvanBerg How would you write a test for the if (!success)? Just the logic.
    – kihiuFrank
    Commented Nov 20, 2022 at 13:15
  • with assertEq(bool,bool) Commented Nov 20, 2022 at 17:43

0

Your Answer

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

Browse other questions tagged or ask your own question.