Skip to content

Commit

Permalink
parse reason - remove api request on fail status
Browse files Browse the repository at this point in the history
  • Loading branch information
BiancaIalangi committed Oct 18, 2024
1 parent fde6c00 commit c898e1b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1,019 deletions.
8 changes: 2 additions & 6 deletions framework/snippets-base/src/network_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,9 @@ pub fn parse_tx_response(tx: TransactionOnNetwork, return_code: ReturnCode) -> T
fn process_signal_error(tx: &TransactionOnNetwork, return_code: ReturnCode) -> TxResponseStatus {
if let Some(event) = find_log(tx, LOG_IDENTIFIER_SIGNAL_ERROR) {
let topics = event.topics.as_ref();
if let Some(error) = process_topics_error(topics) {
return TxResponseStatus::signal_error(&error);
}

let error_raw = base64_decode(topics.unwrap().get(1).unwrap());
let error = String::from_utf8(error_raw).unwrap();
return TxResponseStatus::new(return_code, &error);
let error = topics.unwrap().first().unwrap();
return TxResponseStatus::new(return_code, error);
}

TxResponseStatus::default()
Expand Down
63 changes: 26 additions & 37 deletions sdk/core/src/retrieve_tx_on_network.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::{
data::transaction::{Events, TransactionOnNetwork},
data::{
sdk_address::SdkAddress,
transaction::{ApiLogs, Events, LogData, TransactionOnNetwork},
},
gateway::{GetTxInfo, GetTxProcessStatus},
utils::base64_encode,
};
use log::info;
use multiversx_chain_core::types::ReturnCode;
use multiversx_chain_core::types::{Address, ReturnCode};

use crate::gateway::GatewayAsyncService;

Expand Down Expand Up @@ -42,20 +44,13 @@ pub async fn retrieve_tx_on_network<GatewayProxy: GatewayAsyncService>(
},
"fail" => {
let (error_code, error_message) = parse_reason(&reason);
let mut transaction_info_with_results: TransactionOnNetwork = proxy
.request(GetTxInfo::new(&tx_hash).with_results())
.await
.unwrap();
replace_with_error_message(
&mut transaction_info_with_results,
error_message,
);
let failed_transaction_info: TransactionOnNetwork =
create_tx_failed(&error_message);

info!(
"Transaction retrieved successfully, with status {}: {:#?}",
status, transaction_info_with_results
"Transaction failed with status {status} and message {error_message}",
);
return (transaction_info_with_results, error_code);
return (failed_transaction_info, error_code);
},
_ => {
continue;
Expand Down Expand Up @@ -87,7 +82,7 @@ pub async fn retrieve_tx_on_network<GatewayProxy: GatewayAsyncService>(

pub fn parse_reason(reason: &str) -> (ReturnCode, String) {
if reason.is_empty() {
return (ReturnCode::UserError, String::new());
return (ReturnCode::UserError, "invalid transaction".to_string());
}

let (code, mut message) = find_code_and_message(reason);
Expand All @@ -98,15 +93,15 @@ pub fn parse_reason(reason: &str) -> (ReturnCode, String) {
ReturnCode::message(return_code).clone_into(&mut message);
}

(return_code, base64_encode(message))
(return_code, message)
},
None => {
if message.is_empty() {
message = extract_message_from_string_reason(reason);
}
let return_code = ReturnCode::from_message(&message).unwrap_or(ReturnCode::UserError);

(return_code, base64_encode(message))
(return_code, message)
},
}
}
Expand Down Expand Up @@ -141,26 +136,20 @@ pub fn extract_message_from_string_reason(reason: &str) -> String {
return contract_error.last().unwrap_or(&"").split(']').collect();
}

pub fn replace_with_error_message(tx: &mut TransactionOnNetwork, error_message: String) {
if error_message.is_empty() {
return;
}
fn create_tx_failed(error_message: &str) -> TransactionOnNetwork {
let mut failed_transaction_info = TransactionOnNetwork::default();

if let Some(event) = find_log(tx) {
if let Some(event_topics) = event.topics.as_mut() {
if event_topics.len() == 2 {
event_topics[1] = error_message;
}
}
}
}
let log: ApiLogs = ApiLogs {
address: SdkAddress(Address::zero()),
events: vec![Events {
address: SdkAddress(Address::zero()),
identifier: LOG_IDENTIFIER_SIGNAL_ERROR.to_string(),
topics: Some(vec![error_message.to_string()]),
data: LogData::default(),
}],
};

fn find_log(tx: &mut TransactionOnNetwork) -> Option<&mut Events> {
if let Some(logs) = tx.logs.as_mut() {
logs.events
.iter_mut()
.find(|event| event.identifier == LOG_IDENTIFIER_SIGNAL_ERROR)
} else {
None
}
failed_transaction_info.logs = Some(log);

failed_transaction_info
}
Loading

0 comments on commit c898e1b

Please sign in to comment.