From f56146025a943d06b9989d3fba86634099831934 Mon Sep 17 00:00:00 2001 From: Bjerg Date: Mon, 6 Jun 2022 15:42:45 +0200 Subject: [PATCH] feat: source map getters for deployed bytecode (#1348) * feat: source map getters for deployed bytecode * fix: borrow checker --- ethers-solc/src/artifact_output/mod.rs | 20 ++++++++++++++++++-- ethers-solc/src/artifacts/bytecode.rs | 7 +++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/ethers-solc/src/artifact_output/mod.rs b/ethers-solc/src/artifact_output/mod.rs index d73c8f22..db5be8f9 100644 --- a/ethers-solc/src/artifact_output/mod.rs +++ b/ethers-solc/src/artifact_output/mod.rs @@ -401,7 +401,7 @@ pub trait Artifact { self.get_contract_bytecode().abi } - /// Returns the `sourceMap` of the contract + /// Returns the `sourceMap` of the creation bytecode /// /// Returns `None` if no `sourceMap` string was included in the compiler output /// Returns `Some(Err)` if parsing the sourcemap failed @@ -409,13 +409,29 @@ pub trait Artifact { self.get_bytecode()?.source_map() } - /// Returns the `sourceMap` as str if it was included in the compiler output + /// Returns the creation bytecode `sourceMap` as str if it was included in the compiler output fn get_source_map_str(&self) -> Option> { match self.get_bytecode()? { Cow::Borrowed(code) => code.source_map.as_deref().map(Cow::Borrowed), Cow::Owned(code) => code.source_map.map(Cow::Owned), } } + + /// Returns the `sourceMap` of the runtime bytecode + /// + /// Returns `None` if no `sourceMap` string was included in the compiler output + /// Returns `Some(Err)` if parsing the sourcemap failed + fn get_source_map_deployed(&self) -> Option> { + self.get_deployed_bytecode()?.source_map() + } + + /// Returns the runtime bytecode `sourceMap` as str if it was included in the compiler output + fn get_source_map_deployed_str(&self) -> Option> { + match self.get_bytecode()? { + Cow::Borrowed(code) => code.source_map.as_deref().map(Cow::Borrowed), + Cow::Owned(code) => code.source_map.map(Cow::Owned), + } + } } impl Artifact for T diff --git a/ethers-solc/src/artifacts/bytecode.rs b/ethers-solc/src/artifacts/bytecode.rs index eae27f1a..f90f9fff 100644 --- a/ethers-solc/src/artifacts/bytecode.rs +++ b/ethers-solc/src/artifacts/bytecode.rs @@ -411,6 +411,13 @@ impl CompactDeployedBytecode { pub fn empty() -> Self { Self { bytecode: Some(CompactBytecode::empty()), immutable_references: Default::default() } } + + /// Returns the parsed source map + /// + /// See also + pub fn source_map(&self) -> Option> { + self.bytecode.as_ref().and_then(|bytecode| bytecode.source_map()) + } } impl From for CompactDeployedBytecode {