5

Since we cannot pass string arrays as parameters in solidity I used a byte32 array in a function. The code gives no error at compile time. But when I give input to the drawCard function as below in Remix IDE

"this is token uri", ["maham",67,"Silver","First Token","image url"]

I get the following error:

transact to CryptoGogos.drawCard errored: Error encoding arguments: Error: invalid arrayify value (argument="value", value="maham", code=INVALID_ARGUMENT, version=bytes/5.0.5)

My code is as follows:

   function drawCard(string memory _tokenURI, bytes32[] memory params) public {
        require(uint(params[1])<total_supply,
        "Input supply is not less than total supply of cards.");
        _tokenIds.increment();
        uint256 newNftTokenId = _tokenIds.current();
        
        Cards memory c;
        c.name = string(abi.encodePacked(params[0]));
        c.supply = uint(params[1]);
        c.cat = string(abi.encodePacked(params[2]));
        c.description = string(abi.encodePacked(params[3]));
        c.image_url= string(abi.encodePacked(params[4]));
        c.card_id = newNftTokenId;
        
        card.push(c);
        tokeninfo[newNftTokenId] = c;
}
11

The problem is that you populate the params array with uint and string arguments whereas it only accepts bytes32 values.

Basically a bytes32 is an hexadecimal of length 64 without the 0x prefix (1 byte = 2 hex characters). To convert something to bytes32, convert it to hexadecimal first, then add as many 0s as needed at the beginning to get a hexadecimal of length 64 (without 0x).

Example with maham :

hex -> 6d6168616d

bytes32 -> 0000000000000000000000000000000000000000000000000000006d6168616d

Note that the 0x prefix is mandatory with remix so the lenght of each of your bytes32 argument should be equal to 66.

0
2

I was giving wrong input to a bytes32 array. The correct way was to convert each value to 0x followed by a 64-character hexadecimal string. Thus "this is token uri", ["maham",67,"Silver","first token","image url"] became ["0x6d6168616d000000000000000000000000000000000000000000000000000000","0x4300000000000000000000000000000000000000000000000000000000000000","0x53696c7665720000000000000000000000000000000000000000000000000000","0x666972737420746f6b656e000000000000000000000000000000000000000000","0x696d6167652075726c0000000000000000000000000000000000000000000000"].

4
  • 1
    You are correct about the 0x followed by a 64-character hexadecimal string part. However you should add the zeros at the beginning, not the end.
    – clement
    Mar 31 at 22:41
  • For exemple 0x4300000000000000000000000000000000000000000000000000000000000000 = 30304960855078848021012718732742538383473003799210616369701789564571014397952 in decimals.
    – clement
    Mar 31 at 22:42
  • Whereas 0x0000000000000000000000000000000000000000000000000000000000000043 = 67, the value you want.
    – clement
    Mar 31 at 22:43
  • 1
    You can check my answer to this question for more info.
    – clement
    Mar 31 at 22:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.