16

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;

}

mzaidi
  • 992
  • 2
  • 13
  • 36

3 Answers3

26

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.

clement
  • 4,302
  • 2
  • 17
  • 35
3

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"].

mzaidi
  • 992
  • 2
  • 13
  • 36
  • 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 '21 at 22:41
  • For exemple 0x4300000000000000000000000000000000000000000000000000000000000000 = 30304960855078848021012718732742538383473003799210616369701789564571014397952 in decimals. – clement Mar 31 '21 at 22:42
  • Whereas 0x0000000000000000000000000000000000000000000000000000000000000043 = 67, the value you want. – clement Mar 31 '21 at 22:43
  • 1
    You can check my answer to this question for more info. – clement Mar 31 '21 at 22:44
2

Most of the time we try to provide a string but it has to be bytes32 in the following format:

// 'ETH' as a bytes32 string
0x4554480000000000000000000000000000000000000000000000000000000000
Throvn
  • 231
  • 1
  • 9