frangio / hardhat-exposed

Automatically expose internal Solidity functions for smart contract testing.
82 stars 13 forks source link

Incorrect references to internal constants in libraries #18

Closed ItsNickBarry closed 1 year ago

ItsNickBarry commented 1 year ago

Given this library:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import { EnumerableSet } from '../../utils/EnumerableSet.sol';

library AccessControlStorage {
    struct RoleData {
        EnumerableSet.AddressSet members;
        bytes32 adminRole;
    }

    struct Layout {
        mapping(bytes32 => RoleData) roles;
    }

    bytes32 internal constant DEFAULT_ADMIN_ROLE = 0x00;

    bytes32 internal constant STORAGE_SLOT =
        keccak256('solidstate.contracts.storage.AccessControl');

    function layout() internal pure returns (Layout storage l) {
        bytes32 slot = STORAGE_SLOT;
        assembly {
            l.slot := slot
        }
    }
}

The plugin generates this accessor contract:

// SPDX-License-Identifier: UNLICENSED

pragma solidity >=0.6.0;

import "../../../contracts/access/access_control/AccessControlStorage.sol";

contract $AccessControlStorage {
    bytes32 public __hh_exposed_bytecode_marker = "hardhat-exposed";

    constructor() {}

    function $DEFAULT_ADMIN_ROLE() external pure returns (bytes32) {
        return DEFAULT_ADMIN_ROLE;
    }

    function $STORAGE_SLOT() external pure returns (bytes32) {
        return STORAGE_SLOT;
    }

    function $layout() external pure returns (AccessControlStorage.Layout memory) {
        return AccessControlStorage.layout();
    }

    receive() external payable {}
}

The DEFAULT_ADMIN_ROLE and STORAGE_SLOT variables cannot be found because they must be accessed through the library explicitly (AccessControlStorage.DEFAULT_ADMIN_ROLE) rather than implicitly via inheritance. Compilation fails.

This example is taken from here - just install hardhat-exposed on the default branch and compile to reproduce.

frangio commented 1 year ago

Thanks! Fixed in https://github.com/frangio/hardhat-exposed/commit/15f249df743770e7bbc3db1b0947c3df81eeaa63 and published in the latest release.