1

I am building the following smart contract on Remix and I'm currently receiving these errors when trying to call addBook("Goosebumps", "RL Stine", 1).

pragma solidity ^0.8;

contract Library {

    struct Book {
        uint256 id;
        bytes32 title;
        bytes32 author;
        CoverType cover;
    }

    enum CoverType { HardCover, Paperback }

    uint256 lastBookId;
    mapping(uint256 => Book) books;

    function addBook(bytes32 _title, bytes32 _author, CoverType _cover) public {
        lastBookId++;
        Book memory newBook;
        newBook = Book(lastBookId, _title, _author, _cover);
    }
}

From Tutorial Point they state

In Solidity we can assign String literal to a byte32 type variable easily. Solidity considers it as a byte32 literal.

contract SolidityTest {
  bytes32 data = "test";
}

Am I missing something? Here's the error that prints to the console.

transact to Library.addBook errored: Error encoding arguments: Error: invalid arrayify value (argument="value", value="Goosebumps", code=INVALID_ARGUMENT, version=bytes/5.5.0)

1 Answer 1

1

For pass bytes32 value you must convert your string (in this case) in exadecimal and after this operation you must to add remaining zeros needed get a hexadecimal of length 64 (without 0x).

Example:

Word: "test"

Exadecimal: 227465737422

Bytes32: 0x2274657374220000000000000000000000000000000000000000000000000000

In the last operation you must add 0x before 64 characters.

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.