alloy-rs / core

High-performance, well-tested & documented core libraries for Ethereum, in Rust
https://alloy.rs
Apache License 2.0
763 stars 135 forks source link

[Bug] Error when using sol! to parse a Solidity file #636

Closed alexfertel closed 3 months ago

alexfertel commented 3 months ago

Component

sol! macro

What version of Alloy are you on?

v0.7.4

Operating System

macOS (Apple Silicon)

Describe the bug

From the docs, I understand that I should be able to use the sol! macro like this as well: sol!(Erc20, "../examples/erc20/src/test.sol");

Screenshot 2024-05-27 at 13 12 11

However, when I try to do that, I get a "names are not allowed outside of JSON ABI" error, which comes from https://github.com/alloy-rs/core/blob/f5e04aaf2e62799bba27c49942350ac2e3c8e45d/crates/sol-macro-input/src/input.rs#L128

prestwich commented 3 months ago

when importing .sol files, there's no need for a name, as the name will be picked up from the contract MyContract definition in the solidity file. Given a file may contain multiple contract _____ definitions, renaming by adding a name there would also be pretty unreliable. So for those reasons, solidity files can only be imported without a name

The following should work for you:

sol!("../examples/erc20/src/test.sol");
alexfertel commented 3 months ago

Gotcha, makes sense. Maybe update the error message with a suggestion?

Also, I'm guessing if you wanted rpc calls you just do:

sol!(
  #[sol(rpc)]
  "../examples/erc20/src/test.sol"
);

Feel free to close 👍

prestwich commented 3 months ago

638

prestwich commented 3 months ago

yes, I think the invocation with #[sol(rpc)] will work as exepcted