0

I have a very basic string helper library that defines a function to compare two strings like so:

library StringLib {
  function compareTwoStrings(string memory s1, string memory s2)
    public
    pure
    returns (bool)
  {
    return keccak256(abi.encodePacked(s1)) == keccak256(abi.encodePacked(s2));
  }
}

I import it in my contract with import './StringLib.sol'; and use it with StringLib.compareTwoStrings(s1, s2).

For some reason, this results in a bytecode error: Error: invalid bytecode (argument="bytecode",. I've tried using hardcoded strings (StringLib.compareTwoStrings('A', 'B')) and hit the same issue.

However, if I define the exact same function directly in my contract code, it works without a problem.

Any ideas why this might be happening?

1

I'm not sure what you're doing wrong at the implementation contract, but I've been able to successfully use your lib.

Code:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.9;

library StringLib {
  function compareTwoStrings(string memory s1, string memory s2)
    public
    pure
    returns (bool)
  {
    return keccak256(abi.encodePacked(s1)) == keccak256(abi.encodePacked(s2));
  }
}

// SPDX-License-Identifier: MIT
pragma solidity 0.8.9;

contract Contract{
    function compare(string memory a, string memory b) public pure returns (bool){
        return StringLib.compareTwoStrings(a, b);
    }
}

Note: you could use an import statement to import the library, it doesn't make any difference. Resulting code would effectively be same as above.

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.