foundry-rs / compilers

Utilities for working with native solc and compiling projects.
Apache License 2.0
59 stars 36 forks source link

fix: create verifiable Standard JSON for projects with external files #36

Closed tash-2s closed 6 months ago

tash-2s commented 6 months ago

Currently, Foundry projects containing Solidity files outside the project root directory face contract verification failures on block explorers. This issue occurs from the Standard JSON including unusable source paths for external files, represented as full absolute paths in their host file systems.

This PR addresses the issue by improving the path conversion process. For files not located under the project root directory, relative parent directory paths (..) are used, enabling the compiler to find the files within the json.

Steps to reproduce the issue are detailed in the linked issue above. Additionally, a test case representing that scenario has been added.

With this change, the json created in the reproduction scenario will appear as follows:

{
  "language": "Solidity",
  "sources": {
    "src/Counter.sol": {
      "content": "// SPDX-License-Identifier: UNLICENSED\npragma solidity ^0.8.13;\n\nimport \"@remapped/Parent.sol\";\n\ncontract Counter {\n    uint256 public number;\n\n    function setNumber(uint256 newNumber) public {\n        number = newNumber;\n    }\n\n    function increment() public {\n        number++;\n    }\n}\n"
    },
    "../remapped/Parent.sol": {
      "content": "// SPDX-License-Identifier: UNLICENSED\npragma solidity ^0.8.13;\nimport \"./Child.sol\";\n"
    },
    "../remapped/Child.sol": {
      "content": "// SPDX-License-Identifier: UNLICENSED\npragma solidity ^0.8.13;\n"
    }
  },
  "settings": {
    "remappings": [
      "@remapped/=../remapped/",
      "ds-test/=lib/forge-std/lib/ds-test/src/",
      "forge-std/=lib/forge-std/src/"
    ],
    "optimizer": {
      "enabled": true,
      "runs": 200
    },
    "metadata": {
      "useLiteralContent": false,
      "bytecodeHash": "ipfs",
      "appendCBOR": true
    },
    "outputSelection": {
      "*": {
        "": [
          "ast"
        ],
        "*": [
          "abi",
          "evm.bytecode",
          "evm.deployedBytecode",
          "evm.methodIdentifiers",
          "metadata"
        ]
      }
    },
    "evmVersion": "paris",
    "libraries": {}
  }
}

The source path is now aligned with the project root.

I have successfully deployed and verified the contract on Etherscan using this change.

forge create --rpc-url "wss://ethereum-holesky.publicnode.com" --verify --verifier-url "https://api-holesky.etherscan.io/api" --etherscan-api-key "..." --private-key "..." src/Counter.sol:Counter

https://holesky.etherscan.io/address/0xe08c332706185521fc8bc2b224f67adf814b1880#code

tash-2s commented 6 months ago

Thank you for your review!

I've created a companion PR in foundry with this patch. Also, I have confirmed that the tests in foundry passed successfully.

tash-2s commented 6 months ago

The workflow test for this PR failed due to a failure in nextest installation. As this issue is unrelated to the changes made, re-running the test should resolve it. https://github.com/foundry-rs/compilers/actions/runs/7331166894/job/19979490471

PaulRBerg commented 6 months ago

Amazing work.

Doesn't this PR also address issue https://github.com/foundry-rs/foundry/issues/3507?

tash-2s commented 6 months ago

@PaulRBerg I haven't looked into it deeply, but it seems like a different matter from this PR and #35, as they don't involve --via-ir.