trufflesuite / truffle

:warning: The Truffle Suite is being sunset. For information on ongoing support, migration options and FAQs, visit the Consensys blog. Thank you for all the support over the years.
https://consensys.io/blog/consensys-announces-the-sunset-of-truffle-and-ganache-and-new-hardhat?utm_source=github&utm_medium=referral&utm_campaign=2023_Sep_truffle-sunset-2023_announcement_
MIT License
14.02k stars 2.32k forks source link

Allow `version: "pragma"` to let you import across incompatible Solidity versions #4439

Open gnidan opened 3 years ago

gnidan commented 3 years ago

Issue

Truffle's version: "pragma" feature is awesome! It lets you compile your contracts even if they are written for differing Solidity versions.

Problem is, if you import a file with an incompatible version, Truffle just throws an error. In many situations (e.g. importing a library), there's nothing we can do about that...

But there is a common situation that we can handle completely! If the importing file uses the imported file only to reference its contracts externally, then we can ensure this import works perfectly by doing these steps:

Doing this would remove a lot of friction for use cases that involve writing contracts to interface externally with other on-chain contracts.

Steps to Reproduce

Create the following Solidity files:

Then, set version: "pragma" in the solc truffle-config and compile.

Desired behavior

Truffle should observe the incompatible versions and regenerate B.sol on the fly (likely using the abi-to-sol functionality provided by @truffle/resolver). Compilation should just work.

Note that this should work even if B.sol defines a contract with a name other than B; Truffle should look inside B.sol to determine what interface name to produce. This may not be feasible[^1], however, so perhaps we can treat "source file must match contract name" as an okay initial constraint? Blech.

[^1]: It might not be feasible because we'd be forced to run abi-to-sol multiple times (once for each ABI produced when compiling a given source), and doing this has high risk of name collisions that would cause compilation to fail.

The open abi-to-sol issue https://github.com/gnidan/abi-to-sol/issues/41 describes this problem a bit further.

Actual Results

Right now, the version: "pragma" feature detects the incompatible versions and throws a friendly error about it.

Environment

RatanRSur commented 2 years ago

I see that #4420 made some progess on this. Any idea when it could be in at least an experimental version?

gnidan commented 2 years ago

I see that #4420 made some progess on this. Any idea when it could be in at least an experimental version?

@RatanRSur sorry I missed this. Our plans for implementing this fix rely on producing ABI JSON for the imported contract and generating Solidity interfaces for the importer to use. Since Truffle currently uses the [my personal other] library abi-to-sol for this purpose elsewhere in an experimental capacity (in Truffle, you can import .abi.json files directly!), but that library has a nasty limitation in the way I implemented it, and thus this stays open for now. See the original issue writeup above for a bit more on why this issue is blocked.

Sorry about that, but glad to hear you're interested in this feature! Hopefully I'll make some headway the next time I try to refactor abi-to-sol to address the underlying limitation.