Skip to content

feat(forge fmt): add --fix-checksum flag to automatically fix invalid checksum addresses in contracts #10245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sambacha opened this issue Apr 4, 2025 · 6 comments
Assignees
Labels
Cmd-forge-fmt Command: forge fmt first issue A good way to start contributing T-feature Type: feature

Comments

@sambacha
Copy link
Contributor

sambacha commented Apr 4, 2025

Component

Forge

Describe the feature you would like

fmt checksum would automatically fix checksum issues:

Motivating Case

0x7a250d5630B4cf539739df2c5dacb4c659f2488d
0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D

Existing Error

Error: Compiler run failed:
Error (9429): This looks like an address but has an invalid checksum. Correct checksummed address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48". If this is not used as an address, please prepend '00'. For more information please see https://docs.soliditylang.org/en/develop/types.html#address-literals

Additional context

When fmt checksum fixes any issue it SHOULD alert user that it fixed checksum issue and that used as an address, please prepend '00'. in case otherwise

@sambacha sambacha added T-feature Type: feature T-needs-triage Type: this issue needs to be labelled labels Apr 4, 2025
@github-project-automation github-project-automation bot moved this to Todo in Foundry Apr 4, 2025
@grandizzy grandizzy added Cmd-forge-fmt Command: forge fmt and removed T-needs-triage Type: this issue needs to be labelled labels Apr 8, 2025
@Mouzayan
Copy link

Mouzayan commented Apr 8, 2025

can i get assigned to this one?

@zerosnacks zerosnacks changed the title Feat: fmt checksum to automatically fix invalid checksum addresses in contracts feat(forge fmt): add --fix-checksum flag to automatically fix invalid checksum addresses in contracts Apr 8, 2025
@zerosnacks
Copy link
Member

@Mouzayan assigned!

@zerosnacks zerosnacks added the first issue A good way to start contributing label Apr 8, 2025
@Mouzayan
Copy link

Mouzayan commented Apr 9, 2025

I’m currently working on this issue, to track my progress see here.

@zerosnacks
Copy link
Member

zerosnacks commented Apr 9, 2025

Great! Tip: you can use Alloy Core's Address to transform the address into a checksummed version.

@zerosnacks
Copy link
Member

It can be useful to look at the Parsed version of the source code, you can find the implementation here: https://github.com/foundry-rs/foundry/blob/master/crates/fmt/src/helpers.rs.

You can also have a look at

Type::Address => write_chunk!(self, loc.start(), "address")?,
Type::AddressPayable => write_chunk!(self, loc.start(), "address payable")?,
and
Expression::HexNumberLiteral(loc, val, unit) => {
// ref: https://docs.soliditylang.org/en/latest/types.html?highlight=address%20literal#address-literals
let val = if val.len() == 42 {
Address::from_str(val).expect("").to_string()
} else {
val.to_owned()
};
write_chunk!(self, loc.start(), loc.end(), "{val}")?;
self.write_unit(unit)?;
}
and how they enforce specific formatting rules

@DaniPopes
Copy link
Member

As you can see above, addresses are already checksummed in the Address... .to_string() :

Expression::HexNumberLiteral(loc, val, unit) => {
// ref: https://docs.soliditylang.org/en/latest/types.html?highlight=address%20literal#address-literals
let val = if val.len() == 42 {
Address::from_str(val).expect("").to_string()
} else {
val.to_owned()
};
write_chunk!(self, loc.start(), loc.end(), "{val}")?;
self.write_unit(unit)?;
}

You can try it out for yourself:

function f() {
    address a = 0x7a250d5630B4cf539739df2c5dacb4c659f2488d;
}
$ forge build
Error (9429): This looks like an address but has an invalid checksum. ...
$ forge fmt
$ forge build
# OK

@github-project-automation github-project-automation bot moved this from Todo to Done in Foundry Apr 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Cmd-forge-fmt Command: forge fmt first issue A good way to start contributing T-feature Type: feature
Projects
Status: Done
Development

No branches or pull requests

5 participants