Skip to content

Commit

Permalink
better error msgng
Browse files Browse the repository at this point in the history
  • Loading branch information
nicarq committed Oct 25, 2024
1 parent 57766a7 commit 25afe6a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 12 deletions.
18 changes: 6 additions & 12 deletions shinkai-bin/shinkai-node/src/wallet/local_ether_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl LocalEthersWallet {
}

pub async fn prepare_transaction_request(
_from_wallet: &LocalEthersWallet,
from_wallet: &LocalEthersWallet,
to_wallet: PublicAddress,
token: Option<Asset>,
send_amount: U256,
Expand All @@ -186,7 +186,9 @@ impl LocalEthersWallet {
println!("chain_id: {:?}", chain_id);
println!("provider: {:?}", provider);

let mut tx = Eip1559TransactionRequest::new().chain_id(chain_id);
let mut tx = Eip1559TransactionRequest::new()
.chain_id(chain_id)
.from(from_wallet.wallet.address());

if let Some(token) = token {
println!("token: {:?}", token);
Expand Down Expand Up @@ -264,14 +266,6 @@ impl LocalEthersWallet {
send_amount: U256,
invoice_id: String,
) -> Result<H256, WalletError> {
// Add detailed logging here
eprintln!("Preparing ERC20 transfer transaction:");
eprintln!("Contract Address: {:?}", to_wallet.address_id);
eprintln!("Token: {:?}", token);
eprintln!("Send Amount: {:?}", send_amount);
eprintln!("Provider URL: {:?}", self.provider.url());
eprintln!("Invoice ID: {:?}", invoice_id);

let tx_request = Self::prepare_transaction_request(
self,
to_wallet,
Expand All @@ -288,11 +282,11 @@ impl LocalEthersWallet {
let pending_tx = signer
.send_transaction(tx_request, None)
.await
.map_err(|e| WalletError::ProviderError(e.to_string()))?;
.map_err(|e| WalletError::ProviderError(format!("SignerMiddleware error: {:?}", e)))?;

let receipt = pending_tx
.await
.map_err(|e| WalletError::ProviderError(e.to_string()))?;
.map_err(|e| WalletError::ProviderError(format!("Pending transaction error: {:?}", e)))?;

if let Some(receipt) = receipt {
let tx_hash = receipt.transaction_hash;
Expand Down
49 changes: 49 additions & 0 deletions shinkai-bin/shinkai-node/src/wallet/wallet_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ pub enum WalletError {
EllipticCurveError(elliptic_curve::Error),
HexError(FromHexError),
ProviderError(String),
DetailedJsonRpcError {
code: i32,
message: String,
data: Option<String>,
},
NetworkMismatch,
InvalidAmount(String),
InvalidAddress(String),
Expand All @@ -33,6 +38,7 @@ pub enum WalletError {
LanceDBError(String),
ParsingError(String),
MissingToAddress,
InsufficientBalance(String),
// Add other error types as needed
}

Expand All @@ -45,6 +51,9 @@ impl fmt::Display for WalletError {
WalletError::EllipticCurveError(e) => write!(f, "EllipticCurveError: {}", e),
WalletError::HexError(e) => write!(f, "HexError: {}", e),
WalletError::ProviderError(e) => write!(f, "ProviderError: {}", e),
WalletError::DetailedJsonRpcError { code, message, data } => {
write!(f, "JSON-RPC error: code {}, message: {}, data: {:?}", code, message, data)
},
WalletError::NetworkMismatch => write!(f, "NetworkMismatch"),
WalletError::InvalidAmount(e) => write!(f, "InvalidAmount: {}", e),
WalletError::InvalidAddress(e) => write!(f, "InvalidAddress: {}", e),
Expand All @@ -70,6 +79,7 @@ impl fmt::Display for WalletError {
WalletError::LanceDBError(e) => write!(f, "LanceDBError: {}", e),
WalletError::ParsingError(e) => write!(f, "ParsingError: {}", e),
WalletError::MissingToAddress => write!(f, "MissingToAddress"),
WalletError::InsufficientBalance(e) => write!(f, "InsufficientBalance: {}", e),
}
}
}
Expand Down Expand Up @@ -106,6 +116,8 @@ impl Error for WalletError {
WalletError::LanceDBError(_) => None,
WalletError::ParsingError(_) => None,
WalletError::MissingToAddress => None,
WalletError::InsufficientBalance(_) => None,
WalletError::DetailedJsonRpcError { .. } => None,
}
}
}
Expand Down Expand Up @@ -133,3 +145,40 @@ impl From<ShinkaiLanceDBError> for WalletError {
WalletError::FunctionExecutionError(error.to_string())
}
}

impl From<ethers::providers::ProviderError> for WalletError {
fn from(error: ethers::providers::ProviderError) -> Self {
match error {
ethers::providers::ProviderError::JsonRpcClientError(e) => {
WalletError::ProviderError(format!("JsonRpcClientError: {:?}", e))
},
ethers::providers::ProviderError::EnsError(e) => {
WalletError::ProviderError(format!("EnsError: {}", e))
},
ethers::providers::ProviderError::EnsNotOwned(e) => {
WalletError::ProviderError(format!("EnsNotOwned: {}", e))
},
ethers::providers::ProviderError::SerdeJson(e) => {
WalletError::ProviderError(format!("SerdeJson: {}", e))
},
ethers::providers::ProviderError::HexError(e) => {
WalletError::ProviderError(format!("HexError: {}", e))
},
ethers::providers::ProviderError::HTTPError(e) => {
WalletError::ProviderError(format!("HTTPError: {}", e))
},
ethers::providers::ProviderError::CustomError(e) => {
WalletError::ProviderError(format!("CustomError: {}", e))
},
ethers::providers::ProviderError::UnsupportedRPC => {
WalletError::ProviderError("UnsupportedRPC".to_string())
},
ethers::providers::ProviderError::UnsupportedNodeClient => {
WalletError::ProviderError("UnsupportedNodeClient".to_string())
},
ethers::providers::ProviderError::SignerUnavailable => {
WalletError::ProviderError("SignerUnavailable".to_string())
},
}
}
}

0 comments on commit 25afe6a

Please sign in to comment.