Skip to content

Commit

Permalink
"Clean stdout string from vyper compiler if more than just the byteco…
Browse files Browse the repository at this point in the history
…de is printed"
  • Loading branch information
crypdoughdoteth committed Feb 27, 2024
1 parent 01f7a11 commit fa7a578
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ members = [

[package]
name = "vyper-rs"
version = "2.0.0"
version = "2.0.1"
edition = "2021"
authors = ["Crypdoughdoteth"]
license = "MIT"
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ mod test {
let mut vyper_contract = Vyper::new(&path);
vyper_contract.compile().unwrap();
vyper_contract.gen_abi().unwrap();
assert!(vyper_contract.bytecode.unwrap().starts_with("0x"));
}

#[test]
Expand Down
71 changes: 52 additions & 19 deletions src/vyper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ impl<'a> Display for Vyper<'a> {
}

impl<'a> Vyper<'a> {
/// Constructor function that takes in the path to your vyper contract
/// Constructor function that takes in the path to your vyper contract
pub fn new(path: &'a Path) -> Self {
let np = path.with_extension("json");
let np = path.with_extension("json");
Self {
path_to_code: path,
bytecode: None,
Expand Down Expand Up @@ -137,10 +137,15 @@ impl<'a> Vyper<'a> {
.output()?;
if compiler_output.status.success() {
let mut out = String::from_utf8_lossy(&compiler_output.stdout).to_string();
for _ in 0..2 {
for _ in 0..1 {
out.pop();
}
self.bytecode = Some(out);
if !out.starts_with("0x") {
self.bytecode = out.split(":").last().map(|s| s.to_owned());
} else {
self.bytecode = Some(out);
}

Ok(())
} else {
Err(VyperErrors::CompilerError(
Expand All @@ -156,8 +161,15 @@ impl<'a> Vyper<'a> {
.arg(self.path_to_code)
.output()?;
if compiler_output.status.success() {
let out = String::from_utf8_lossy(&compiler_output.stdout).to_string();
self.bytecode = Some(out);
let mut out = String::from_utf8_lossy(&compiler_output.stdout).to_string();
for _ in 0..1 {
out.pop();
}
if !out.starts_with("0x") {
self.bytecode = out.split(":").last().map(|s| s.to_owned());
} else {
self.bytecode = Some(out);
}
Ok(())
} else {
Err(VyperErrors::CompilerError(
Expand All @@ -176,11 +188,14 @@ impl<'a> Vyper<'a> {

if compiler_output.status.success() {
let mut out = String::from_utf8_lossy(&compiler_output.stdout).to_string();
for _ in 0..2 {
for _ in 0..1 {
out.pop();
}
self.bytecode = Some(out);

if !out.starts_with("0x") {
self.bytecode = out.split(":").last().map(|s| s.to_owned());
} else {
self.bytecode = Some(out);
}
Ok(())
} else {
Err(VyperErrors::CompilerError(
Expand Down Expand Up @@ -519,10 +534,19 @@ impl Vypers {
if compiler_output.status.success() {
let mut out =
String::from_utf8_lossy(&compiler_output.stdout).to_string();
for _ in 0..2 {

for _ in 0..1 {
out.pop();
}
Ok(out)
if !out.starts_with("0x") {
if let Some(e) = out.split(":").last() {
Ok(e.to_owned())
} else {
Err(VyperErrors::StringParsingError)
}
} else {
Ok(out)
}
} else {
Err(VyperErrors::CompilerError(
String::from_utf8_lossy(&compiler_output.stderr).to_string(),
Expand All @@ -540,10 +564,7 @@ impl Vypers {
}

/// Compile multiple vyper contracts concurrently on new threads, updates the ABI field in Vypers. `Ver` arg is for specifying EVM version to compile each contract to.
pub async fn compile_many_ver(
&mut self,
ver: Evm,
) -> Result<(), VyperErrors> {
pub async fn compile_many_ver(&mut self, ver: Evm) -> Result<(), VyperErrors> {
let path = Arc::new(self.path_to_code.clone());
let vy = Arc::new(self.get_vyper());
let mut out_vec: Vec<String> = Vec::with_capacity(self.path_to_code.len());
Expand All @@ -562,10 +583,18 @@ impl Vypers {
if compiler_output.status.success() {
let mut out =
String::from_utf8_lossy(&compiler_output.stdout).to_string();
for _ in 0..2 {
for _ in 0..1 {
out.pop();
}
Ok(out)
if !out.starts_with("0x") {
if let Some(e) = out.split(":").last() {
Ok(e.to_owned())
} else {
Err(VyperErrors::StringParsingError)
}
} else {
Ok(out)
}
} else {
Err(VyperErrors::CompilerError(
String::from_utf8_lossy(&compiler_output.stderr).to_string(),
Expand Down Expand Up @@ -605,7 +634,9 @@ impl Vypers {
let file = File::create(&abi[i])?;
to_writer_pretty(file, &json)?;
} else {
Err(VyperErrors::CompilerError(String::from_utf8_lossy(&compiler_output.stderr).to_string()))?
Err(VyperErrors::CompilerError(
String::from_utf8_lossy(&compiler_output.stderr).to_string(),
))?
}
Ok(())
});
Expand Down Expand Up @@ -636,7 +667,9 @@ impl Vypers {
))?;
Ok(json)
} else {
Err(VyperErrors::CompilerError(String::from_utf8_lossy(&compiler_output.stderr).to_string()))?
Err(VyperErrors::CompilerError(
String::from_utf8_lossy(&compiler_output.stderr).to_string(),
))?
}
});
threads.push(cthread);
Expand Down
11 changes: 9 additions & 2 deletions src/vyper_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,33 @@ pub enum VyperErrors {
VenvError(String),
BlueprintError(String),
IntParseError(ParseIntError),
StringParsingError,
}

impl Display for VyperErrors {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
VyperErrors::IoError(err) => {
write!(f, "An error occured while using system IO: {}", err)
},
}
VyperErrors::SerializationError(s) => write!(
f,
"An error occurred while serializing or deserializing data: {}",
s,
),
VyperErrors::CompilerError(msg) => write!(f, "{}", msg),
VyperErrors::PipError(msg) => write!(f, "{}", msg),
VyperErrors::ConcurrencyError(je) => write!(f, "Failed to join async tasks: {}", je),
VyperErrors::ConcurrencyError(je) => {
write!(f, "Failed to join async tasks: {}", je)
}
VyperErrors::DirError(msg) => write!(f, "{}", msg),
VyperErrors::VenvError(msg) => write!(f, "{}", msg),
VyperErrors::BlueprintError(msg) => write!(f, "{}", msg),
VyperErrors::IntParseError(e) => write!(f, "{}", e),
VyperErrors::StringParsingError => write!(
f,
"An error occurred while parsing bytecode from vyper compiler output"
),
}
}
}
Expand Down

0 comments on commit fa7a578

Please sign in to comment.