0

I have this test suite written with Hardhat

      it('Should reveal bid', async () => {
        const { blindAuction, bidder3, bidData } = await loadFixture(
          deployBlindAuctionFixture
        );

        const values = bidData['bidder3'].values.map(parseEther);
        const secrets = bidData['bidder3'].secrets.map(encodeBytes32String);

        bidData['bidder3'].values.forEach(async (value, index) => {
          const hashedBid = solidityPackedKeccak256(
            ['uint', 'bytes32'],
            [
              parseEther(value),
              encodeBytes32String(bidData.bidder3.secrets[index]),
            ]
          );

          const deposit = generateRandomDeposit();

          await expect(blindAuction.connect(bidder3).bid(hashedBid))
            .to.emit(blindAuction, 'SuccessfulBid')
            .withArgs(bidder3.address);
        });

        const TIME = 60 * 60 * 2;

        time.increase(TIME);

        console.log(
          'bids',
          await blindAuction.connect(bidder3).getBidsByBidder(bidder3.address)
        );

        await blindAuction.connect(bidder3).revealBids(values, secrets);
      });

and here is how the solidity functions are defined.

    function getBidsByBidder(
        address bidder
    ) external view returns (Bid[] memory) {
        return bids[bidder];
    }

    function bid(
        bytes32 _hashedBid
    ) external payable biddingIsActive(block.timestamp) {
        console.log("Deposit is %o", msg.value);
        bids[msg.sender].push(Bid({blindBid: _hashedBid, deposit: msg.value}));
        emit SuccessfulBid(msg.sender);
    }

    function revealBids(
        uint[] calldata _values,
        bytes32[] calldata _secrets
    ) external biddingHasEnded(block.timestamp) {
        uint lengthOfBids = bids[msg.sender].length;

        console.log("Length of bids is %o", lengthOfBids);

        if (_values.length != lengthOfBids && _secrets.length != lengthOfBids) {
            revert InCompleteBidData(msg.sender);
        }

        uint totalRefund = 0;

        for (uint i = 0; i < bids[msg.sender].length; i++) {
            (uint value, bytes32 secret) = (_values[i], _secrets[i]);

            Bid storage currentBid = bids[msg.sender][i];

            console.log("Bid Deposit is %o", currentBid.deposit);

            if (
                currentBid.blindBid ==
                keccak256(abi.encodePacked(value, secret))
            ) {
                if (value <= highestBid || msg.sender == highestBidder) {
                    totalRefund += currentBid.deposit;
                } else if (value > highestBid) {
                    if (highestBidder != address(0)) {
                        refunds[highestBidder] += highestBid;
                    }

                    highestBid = value;
                    highestBidder = msg.sender;

                    emit HighestBidIncreased(msg.sender);
                } else {
                    refunds[msg.sender] += currentBid.deposit;
                }
            }

            currentBid.blindBid = bytes32(0);
        }

        if (msg.sender != highestBidder) {
            refunds[msg.sender] = totalRefund;
        }

        console.log("Total Refund is %o ", totalRefund);
    }

These are the issues I am currently facing:

  1. For some reason, when I pass a value with the bid function, I get an error that I am passing incomplete bid data but when I don't pass a value, the function runs correctly.
  2. When I run the function to get the bids by a bidder inside the loop, I get a response but when I run it outside the loop, it doesn't work. I am not quite sure why this happens and yes, my mapping is defined as public.
  3. Is there a better way, I could have written my loop better?

I would appreciate any help towards this. Thanks.

1 Answer 1

0
const options = {
    value: ethers.parseEther("0.001"),
};

await expect(blindAuction.connect(bidder3).bid(hashedBid), options)
    .to.emit(blindAuction, "SuccessfulBid")
    .withArgs(bidder3.address);


The options.value property is equivalent to the msg.value that will be captured in the function qualified by payable.


See also

1
  • Thanks. I tried this but it still throws the same error. Commented Dec 17, 2023 at 14:52

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.