Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
fix: better file name to ident conversion (#2688)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Nov 29, 2023
1 parent c6efa65 commit 302207c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
22 changes: 20 additions & 2 deletions ethers-contract/ethers-contract-abigen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ mod verbatim;
pub use ethers_core::types::Address;

use contract::{Context, ExpandedContract};
use eyre::Result;
use eyre::{Result, WrapErr};
use proc_macro2::{Ident, TokenStream};
use quote::ToTokens;
use std::{collections::HashMap, fmt, fs, io, path::Path};
Expand Down Expand Up @@ -146,7 +146,16 @@ impl Abigen {
let name = source.as_local().unwrap().file_name().unwrap().to_str().unwrap();
// name is an absolute path and not empty
let name = name.split('.').next().unwrap();
Ok(Self::new_raw(syn::parse_str(name)?, source))

// best effort ident cleanup
let name = name.replace('-', "_").replace(|c: char| c.is_whitespace(), "");

Ok(Self::new_raw(
syn::parse_str(&util::safe_identifier_name(name)).wrap_err_with(|| {
format!("failed convert file name to contract identifier {}", path)
})?,
source,
))
}

/// Manually adds a solidity event alias to specify what the event struct and function name will
Expand Down Expand Up @@ -429,4 +438,13 @@ mod tests {
let out = gen.tokens.to_string();
assert!(out.contains("pub struct ConstructorParams"));
}

// <https://github.com/foundry-rs/foundry/issues/6010>
#[test]
fn parse_empty_abigen() {
let path = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../tests/solidity-contracts/draft-IERCPermit.json");
let abigen = Abigen::from_file(path).unwrap();
assert_eq!(abigen.name().to_string(), "draft_IERCPermit");
}
}
12 changes: 12 additions & 0 deletions ethers-contract/tests/solidity-contracts/draft-IERCPermit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"abi": [],
"bytecode": {
"object": "0x",
"linkReferences": {}
},
"deployedBytecode": {
"object": "0x",
"linkReferences": {}
},
"id": 217
}

0 comments on commit 302207c

Please sign in to comment.