From cbb5c3b7cfef6b4ace036c667ee5e3834d1ce44a Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 10 Dec 2024 14:30:01 +0100 Subject: [PATCH 1/2] fix: expose error detail when swap simulation fails --- state-chain/custom-rpc/src/lib.rs | 5 +++-- state-chain/runtime/src/lib.rs | 4 ++-- state-chain/runtime/src/runtime_apis.rs | 6 +++++- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/state-chain/custom-rpc/src/lib.rs b/state-chain/custom-rpc/src/lib.rs index 93e68c1581..a454854f7f 100644 --- a/state-chain/custom-rpc/src/lib.rs +++ b/state-chain/custom-rpc/src/lib.rs @@ -1181,10 +1181,11 @@ impl From for ErrorObjectOwned { other => internal_error(other), }, CfApiError::DispatchError(dispatch_error) => match dispatch_error { - DispatchErrorWithMessage::Module(message) => match std::str::from_utf8(&message) { + DispatchErrorWithMessage::Module(message) | + DispatchErrorWithMessage::AdHoc(message) => match std::str::from_utf8(&message) { Ok(message) => call_error(std::format!("DispatchError: {message}")), Err(error) => - internal_error(format!("Unable to decode DispatchError: {error}")), + internal_error(format!("Unable to decode Module Error Message: {error}")), }, DispatchErrorWithMessage::Other(error) => internal_error(format!("Unable to decode DispatchError: {error:?}")), diff --git a/state-chain/runtime/src/lib.rs b/state-chain/runtime/src/lib.rs index 32186dc511..a5214973cd 100644 --- a/state-chain/runtime/src/lib.rs +++ b/state-chain/runtime/src/lib.rs @@ -1675,11 +1675,11 @@ impl_runtime_apis! { ], ) ], - ).map_err(|e| DispatchErrorWithMessage::Other(match e { + ).map_err(|e| match e { BatchExecutionError::SwapLegFailed { .. } => DispatchError::Other("Swap leg failed."), BatchExecutionError::PriceViolation { .. } => DispatchError::Other("Price Violation: Some swaps failed due to Price Impact Limitations."), BatchExecutionError::DispatchError { error } => error, - }))?; + })?; let ( network_fee, diff --git a/state-chain/runtime/src/runtime_apis.rs b/state-chain/runtime/src/runtime_apis.rs index b8caefcb0d..6a91d35b6e 100644 --- a/state-chain/runtime/src/runtime_apis.rs +++ b/state-chain/runtime/src/runtime_apis.rs @@ -196,6 +196,7 @@ impl From for SimulatedSwapInformation { #[derive(Debug, Decode, Encode, TypeInfo)] pub enum DispatchErrorWithMessage { Module(Vec), + AdHoc(Vec), Other(DispatchError), } impl> From for DispatchErrorWithMessage { @@ -203,6 +204,8 @@ impl> From for DispatchErrorWithMessage { match error.into() { DispatchError::Module(sp_runtime::ModuleError { message: Some(message), .. }) => DispatchErrorWithMessage::Module(message.as_bytes().to_vec()), + DispatchError::Other(message) => + DispatchErrorWithMessage::AdHoc(message.as_bytes().to_vec()), error => DispatchErrorWithMessage::Other(error), } } @@ -212,7 +215,8 @@ impl> From for DispatchErrorWithMessage { impl core::fmt::Display for DispatchErrorWithMessage { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> { match self { - DispatchErrorWithMessage::Module(message) => write!( + DispatchErrorWithMessage::Module(message) | + DispatchErrorWithMessage::AdHoc(message) => write!( f, "{}", str::from_utf8(message).unwrap_or("") From f6a87e4d95b829de9d28fa9b89fe3c4c2982460b Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 13 Dec 2024 12:52:35 +0100 Subject: [PATCH 2/2] chore: rename --- state-chain/custom-rpc/src/lib.rs | 2 +- state-chain/runtime/src/runtime_apis.rs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/state-chain/custom-rpc/src/lib.rs b/state-chain/custom-rpc/src/lib.rs index a454854f7f..3a62560b59 100644 --- a/state-chain/custom-rpc/src/lib.rs +++ b/state-chain/custom-rpc/src/lib.rs @@ -1182,7 +1182,7 @@ impl From for ErrorObjectOwned { }, CfApiError::DispatchError(dispatch_error) => match dispatch_error { DispatchErrorWithMessage::Module(message) | - DispatchErrorWithMessage::AdHoc(message) => match std::str::from_utf8(&message) { + DispatchErrorWithMessage::RawMessage(message) => match std::str::from_utf8(&message) { Ok(message) => call_error(std::format!("DispatchError: {message}")), Err(error) => internal_error(format!("Unable to decode Module Error Message: {error}")), diff --git a/state-chain/runtime/src/runtime_apis.rs b/state-chain/runtime/src/runtime_apis.rs index 6a91d35b6e..92bdfbbea6 100644 --- a/state-chain/runtime/src/runtime_apis.rs +++ b/state-chain/runtime/src/runtime_apis.rs @@ -196,7 +196,7 @@ impl From for SimulatedSwapInformation { #[derive(Debug, Decode, Encode, TypeInfo)] pub enum DispatchErrorWithMessage { Module(Vec), - AdHoc(Vec), + RawMessage(Vec), Other(DispatchError), } impl> From for DispatchErrorWithMessage { @@ -205,7 +205,7 @@ impl> From for DispatchErrorWithMessage { DispatchError::Module(sp_runtime::ModuleError { message: Some(message), .. }) => DispatchErrorWithMessage::Module(message.as_bytes().to_vec()), DispatchError::Other(message) => - DispatchErrorWithMessage::AdHoc(message.as_bytes().to_vec()), + DispatchErrorWithMessage::RawMessage(message.as_bytes().to_vec()), error => DispatchErrorWithMessage::Other(error), } } @@ -216,7 +216,7 @@ impl core::fmt::Display for DispatchErrorWithMessage { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> { match self { DispatchErrorWithMessage::Module(message) | - DispatchErrorWithMessage::AdHoc(message) => write!( + DispatchErrorWithMessage::RawMessage(message) => write!( f, "{}", str::from_utf8(message).unwrap_or("")