1

error!

TypeError: Invalid type for argument in function call. Invalid implicit conversion from uint8[9] memory to uint8[256] memory requested.
   --> contracts/Avatar/DistrictPower.sol:208:20:
    |
208 |         setStock(1,[2,3,4,5,6,7,8,9,10],[2,3,4,5,6,7,8,9,10]);
    |                    ^^^^^^^^^^^^^^^^^^^^

code!


constructor() {
        setStock(1,[2,3,4,5,6,7,8,9,10],[2,3,4,5,6,7,8,9,10]);
        setStock(2,[11,12,13,14,15,16,17,18,19,20],[11,12,13,14,15,16,17,18,19,20]);
        setStock(3,[21,22,23,24,25,26,27,28,29,30],[21,22,23,24,25,26,27,28,29,30]);
    }

function setStock(
        uint256 ledgerType,
        uint8[] calldata typeDays,
        uint16[] calldata stock
    ) internal {
        require(ledgerType > 0, "Invalid ledger type");
        require(ledgerType < 6, "Invalid ledger type");
        require(msg.sender == tempAdmin, "Only admin");
        require(stock.length > 0, "Invalid stock array");
        require(typeDays.length == stock.length, "Invalid params");

        _setStock(ledgerType, typeDays, stock);
    }

2 Answers 2

1

Probably the error is by defining/initializing the array. I don't know if is the most efficient way, et I would try:

uint8[] firstArray = new uint8[9]
firstarray[0] = 2
firstarray[1] = 3
...
...
firstarray[8] = 10
setStock(1,firstArray,[2,3,4,5,6,7,8,9,10]);

and the same for the others. hope it helps

2
  • Thank you for your help!I found a solution. Commented Apr 15, 2023 at 8:58
  • great, how was it?, so others can also understand in the future.
    – donoso.eth
    Commented Apr 15, 2023 at 9:23
1

I encapsulated a method

(uint8[] memory typeDays1,uint16[] memory stock1) = generateTypeDaysAndStock(2, 10);
        setStock(1, typeDays1, stock1);

function generateTypeDaysAndStock(uint8 start, uint8 end) internal pure returns (uint8[] memory, uint16[] memory) {
        require(start < end, "Invalid range");

        uint8 length = end - start + 1;
        uint8[] memory typeDays = new uint8[](length);
        uint16[] memory stock = new uint16[](length);

        for (uint8 i = start; i <= end; i++) {
            typeDays[i-start] = i;
            stock[i-start] = i;
        }

        return (typeDays, stock);
    }


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.