foundry-rs / foundry-rs/foundry

cast/forge: preserve contract types when generating interfaces from ABI

Open
#8,837 4 comments 0 reactions 0 assignees View on GitHub
C-cast Cmd-forge-inspect T-feature T-post-V1
Dominant language
Rust
Stars
10.6k
Forks
2.6k
Avg merge
16h 38m
Merged PRs (30d)
511

Description

### Component

Forge

### Have you ensured that all of these are up to date?

- [X] Foundry
- [X] Foundryup

### What version of Foundry are you on?

forge 0.2.0 (143abd6 2024-09-04T00:24:41.963834000Z)

### What command(s) is the bug in?

cast interface / forge inspect

### Operating System

None

### Describe the bug

To reproduce, `forge init` a new project and add this contract:

```solidity
contract Foo {
function bar(Counter x) public view returns (Counter y) {
return x;
}
}
```

When generating an interface, the function inputs and return type both are converted to `address`, which strips information. You can see the `Counter` type is present in the ABI of Foo:

```json
"abi": [
{
"type": "function",
"name": "bar",
"inputs": [
{
"name": "x",
"type": "address",
"internalType": "contract Counter"
}
],
"outputs": [
{
"name": "y",
"type": "address",
"internalType": "contract Counter"
}
],
"stateMutability": "view"
}
],
```

But when generating interfaces:

```sh
$ forge inspect Foo abi --pretty
interface Foo {
function bar(address x) external view returns (address y);
}

$ cast interface Foo
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.4;

interface Foo {
function bar(address x) external view returns (address y);
}
```

I can see the rationale for this being that forge might not know what path to use for the `Counter` import. However, even if no `import` statement is provided, I would still prefer a stronger version of interface generation that preserves the internal `Counter` type. Perhaps this should be behind a `--preserve-internal-types` flag

cc @smartcontracts

Contributor guide

Open the contributing guide

Research direction

Reproduce the issue in a fresh forge project using the Foo and Counter contracts shown, then compare the ABI's internalType values with output from forge inspect Foo abi --pretty and cast interface Foo. Done means interface generation preserves the Counter contract type, with the handling of imports or a preserve-internal-types option clarified.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, solidity
Domain
blockchain, cli, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.