0

I can't find any up-to-dates (and clear enough for my level of understanding) responses to this question that has also been raised very recently on a hardhat issue, with no response to date (https://github.com/NomicFoundation/hardhat/issues/2592)

using the example code therein:

//SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

struct AB {
  uint8 a;
  uint8 b;
}

contract TheContract {
  function add(AB memory ab) public pure returns(uint8) {
    return ab.a + ab.b;
  }
}

library TheLibrary {
  function add(AB memory ab) public pure returns(uint8) {
    return ab.a + ab.b;
  }
}

any call to TheLibrary.add would revert with "Transaction reverted: library was called directly"

Considering the fact that this may be a hardhat issue, I deployed the lib I am working on on rinkeby: https://rinkeby.etherscan.io/address/0xcA9f1e2eE0f17b225C87372e17135207b5B2BBBF#readContract

here I have a similar problem (though not 100% sure because etherscan only returns "Error: Returned error: execution reverted"):

etherscan attempts

Similar question

Can a library function operating on a struct work for non "storage" structs?)

  • response from 2016
  • updated response uses internal
1
  • 1
    I believe it's a bug on the solidity side, I explained it on the issue you linked. But maybe I'm wrong...
    – hroussille
    Apr 19 at 16:55

1 Answer 1

0

Solidity libraries are not designed to be called off-chain. Smart contracts call library functions. That's why you are getting this error: Transaction reverted: library was called directly

The error can be fixed by making the library a contract.

1
  • 1
    That's not true for @ClementWalter example, the function is pure and a direct call should work.
    – hroussille
    Apr 19 at 17:25

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.