0

I'm writing foundry tests for a function which batch mints erc1155 tokens for the users, but I'm getting a panic revert.

The function I'm testing:

    function mintBatch(
        address to_,
        uint256[] calldata tokenIds_,
        uint256[] calldata amounts_,
        bytes calldata data_
    ) external payable {
        for (uint256 i = 0; i < tokenIds_.length; i++) {
            Work storage work = works[tokenIds_[i]];
            FEE_MANAGER.collectMintFee{value: msg.value}(
                this, tokenIds_[i], amounts_[i], msg.sender, address(0), work.strategy
            );
        }

        _batchMint(to_, tokenIds_, amounts_, data_);
    }

The foundry test I wrote:

    function test_batchMint() public {
        uint[] memory tokenIds = new uint[](2);
        tokenIds[1] = 1;
        tokenIds[2] = 2;
        uint[] memory tokenAmts = new uint[](2);
        tokenAmts[1] = 1;
        tokenAmts[2] = 1;
        edition.mintBatch{value: 0.0106 ether}(address(1), tokenIds, tokenAmts, new bytes(0));
        assertEq(edition.totalSupply(1), 1);
        assertEq(edition.totalSupply(2), 1);
    }

The error I'm getting, which I run forge test --mt test_batchMint:

Failing tests:
Encountered 1 failing test in test/editions/Edition.t.sol:EditionTest
[FAIL. Reason: panic: array out-of-bounds access (0x32)] test_batchMint() (gas: 424)

Encountered a total of 1 failing tests, 0 tests succeeded

Pls help me, I tried finding a solution but got none, pls guide me about what's wrong I'm doing, Is I am inputting arrays correctly? Thanks!

1 Answer 1

1

I think the problem is here:

    uint[] memory tokenIds = new uint[](2);
    tokenIds[1] = 1;
    tokenIds[2] = 2;
    uint[] memory tokenAmts = new uint[](2);
    tokenAmts[1] = 1;
    tokenAmts[2] = 1;

You are defining an array of length 2 but you are trying to access the indexes 1 and 2 which, instead, should be 0 and 1. Because the index starts from 0. So the correct way would be:

    uint[] memory tokenIds = new uint[](2);
    tokenIds[0] = 1;
    tokenIds[1] = 2;
    uint[] memory tokenAmts = new uint[](2);
    tokenAmts[0] = 1;
    tokenAmts[1] = 1;
1
  • 1
    Good Catch. Thanks!
    – DevABDee
    Commented Apr 25 at 14:14

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.