Skip to content

Commit

Permalink
Merge pull request #9 from horizonx-tech/feature/fix-sign-method
Browse files Browse the repository at this point in the history
Refactor sign Method to Avoid Panic on Gas Estimation and Enhance Error Handling
  • Loading branch information
hide-yoshi authored Nov 20, 2024
2 parents 0a3cc72 + 8b20bfc commit 8de4a41
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 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
@@ -1,6 +1,6 @@
[package]
name = "ic-web3-rs"
version = "0.1.9"
version = "0.1.10"
description = "Ethereum JSON-RPC client for IC canisters."
homepage = "https://github.com/horizonx-tech/ic-web3"
repository = "https://github.com/horizonx-tech/ic-web3"
Expand Down
11 changes: 11 additions & 0 deletions src/contract/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ impl std::error::Error for Error {
}
}

impl From<Error> for crate::error::Error {
fn from(e: Error) -> Self {
match e {
Error::InvalidOutputType(s) => crate::error::Error::InvalidResponse(s),
Error::Abi(eth_error) => crate::error::Error::Decoder(format!("{}", eth_error)),
Error::Api(api_error) => api_error,
Error::InterfaceUnsupported => crate::error::Error::Internal,
}
}
}

pub mod deploy {
use crate::{error::Error as ApiError, types::H256};
use derive_more::{Display, From};
Expand Down
15 changes: 10 additions & 5 deletions src/contract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,18 +407,23 @@ mod contract_signing {
max_priority_fee_per_gas: options.max_priority_fee_per_gas,
..Default::default()
};
if let Some(gas) = options.gas {
tx.gas = gas;
tx.gas = if let Some(gas) = options.gas {
gas
} else {
tx.gas = self
match self
._estimate_gas(
Address::from_str(&from.to_string().as_str()).unwrap(),
&tx,
options.call_options.unwrap_or_default(),
)
.await
.unwrap();
}
{
Ok(gas) => gas,
Err(e) => {
return Err(e.into());
}
}
};
if let Some(value) = options.value {
tx.value = value;
}
Expand Down

0 comments on commit 8de4a41

Please sign in to comment.