-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
40 lines (32 loc) · 1.2 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use anyhow::{Ok, Result};
use regex::Regex;
use substreams_ethereum::Abigen;
use std::fs;
fn main() -> Result<(), anyhow::Error> {
let file_names = [
"abi/usdc_contract.abi.json",
];
let file_output_names = [
"src/abi/usdc_contract.rs",
];
let mut i = 0;
for f in file_names {
let contents = fs::read_to_string(f)
.expect("Should have been able to read the file");
// sanitize fields and attributes starting with an underscore
let regex = Regex::new(r#"("\w+"\s?:\s?")_(\w+")"#).unwrap();
let sanitized_abi_file = regex.replace_all(contents.as_str(), "${1}u_${2}");
// sanitize fields and attributes with multiple consecutive underscores
let re = Regex::new(r"_+").unwrap();
let re_sanitized_abi_file = re.replace_all(&sanitized_abi_file, |caps: ®ex::Captures| {
let count = caps[0].len();
let replacement = format!("{}_", "_u".repeat(count - 1));
replacement
});
Abigen::from_bytes("Contract", re_sanitized_abi_file.as_bytes())?
.generate()?
.write_to_file(file_output_names[i])?;
i = i+1;
}
Ok(())
}