Solidity Arrays – Invalid Conversion from address[2] memory to address[] memory

addressesarraysconstructor

This is so frustrating and I don't know why these errors don't give more information.

I compiled and deployed a contract in remix using the left panel and everything is working fine. I then proceeded to make a test file for my contract in the /tests folder in Remix.

Here is my test file:

pragma solidity ^0.8.0;

import "../contracts/Buds.sol";

contract BudsTester {
    Buds codeTested;
    
    function beforeEach() public {
        codeTested = new Buds(0x988401257b1D1630681e0C51176db25b0d3a69e5, 0x0CF74C6B34560FF9116CA9E19fc06A6FB5Cf65c0, [0xB4a925BAe55743AcF3Dc65a8de0b9507F0491617, 0x288071244112050c93389A950d02c9E626D611dD]);
    }
    
    function constructorParamatersShouldWork() public {
        //implement logic to test constructor fires correctly
    }
}

As you can see I call the constructor of the contract I'm testing in beforeEach(). The problem is my contract's constructor() is set up with the following parameters:

constructor(address goodBudWalletAddress, address smokeOutWalletAddress, address[] memory teamMembers) {
//logic in here
}

The error I get in the beforeEach() function with auto compile is this:

TypeError: Invalid type for argument in function call. Invalid implicit conversion from address[2] memory to address[] memory requested. --> tests/Buds_test.sol:9:119: 
 | 
9| ... B34560FF9116CA9E19fc06A6FB5Cf65c0, [0xB4a925BAe55743AcF3Dc65a8de0b9507F0491617, 0x288071244112050c93389A950d02c9E626D611dD]);
 |

Thank you for any help you provide, I greatly appreciate it!

Best Answer

It is not possible to cast between fixed size arrays and dynamic size arrays.

You have to create a temporary dynamic array and copy the elements.

address[] memory t = new address[](2);

t[0] = 0xB4a925BAe55743AcF3Dc65a8de0b9507F0491617;
t[1] = 0x288071244112050c93389A950d02c9E626D611dD;

codeTested = new Buds(param1, param2, t);
Related Topic