When a base class uses a library, do derived classes also use the same library?

For example:

contract A {
    using SafeMath for uint256;
contract B is A {
    //is code here using SafeMath for uint256? 


In Solidity 0.7.0 and later versions, the effect of using ... for is no longer inherited.

Quoting from the 0.7.0 changelog:

using A for B only affects the contract it is mentioned in. Previously, the effect was inherited. Now, you have to repeat the using statement in all derived contracts that make use of the feature.


You are right

pragma solidity ^0.4.24;

library Bytes32 {
    function toString(bytes32) public pure returns (string) {
        return "1";

contract Parent {
    using Bytes32 for bytes32;

    function bar() public pure returns(string) {
        return bytes32(0).toString();              // returns "1"

contract Child is Parent {
    function foo() public pure returns(string) {
        return bytes32(0).toString();              // returns "1"
  • Note that this example will throw an error when compiled with solc ^0.7.0 – GViz May 29 at 2:26

Caveat: This answer is outdated and incorrect when used with solc compiler ^0.7.0.


As noted in the official docs,

When a contract inherits from multiple contracts, only a single contract is created on the blockchain, and the code from all the base contracts is copied into the created contract.

This copying of code includes using...for statements, which apply to the contract as a whole rather than specific functions.

You can verify this for yourself by testing contract B for the behavior that the library provides to contract A. In this example you can try to cause an overflow in a test on contract B and see that it throws an error instead, if the library is properly included and linked with B's parent type, contract A.

You can also see this in OpenZeppelin's implementation of ERC20 tokens.

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.