Skip to content

Commit

Permalink
handled "execution failed error signaled by smart contract" error in …
Browse files Browse the repository at this point in the history
…simulated calls
  • Loading branch information
gfusee committed Oct 23, 2024
1 parent c3e02f6 commit 5a57586
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
4 changes: 3 additions & 1 deletion executor/src/error/simulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ pub enum SimulationError {
code: String
},

NoSmartContractResult
NoSmartContractResult,
SmartContractExecutionError { status: u64, message: String },
CannotDecodeSmartContractResult
}

impl From<SimulationError> for ExecutorError {
Expand Down
26 changes: 21 additions & 5 deletions executor/src/network/simulate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ use novax_request::gateway::client::GatewayClient;

use crate::{ExecutorError, GatewayError, SimulationError, SimulationGatewayRequest, SimulationGatewayResponse, TransactionExecutor, TransactionOnNetwork, TransactionOnNetworkTransactionSmartContractResult};
use crate::call_result::CallResult;
use crate::error::transaction::TransactionError;
use crate::network::models::simulate::request::SimulationGatewayRequestBody;
use crate::network::utils::address::get_address_info;
use crate::network::utils::network::get_network_config;
use crate::utils::transaction::normalization::NormalizationInOut;
use crate::utils::transaction::results::find_smart_contract_result;
use crate::utils::transaction::results::{find_sc_error, find_smart_contract_result};
use crate::utils::transaction::token_transfer::TokenTransfer;

/// Type alias for `BaseSimulationNetworkExecutor` with the `String` type as the generic `Client`.
Expand Down Expand Up @@ -184,11 +183,28 @@ impl<Client: GatewayClient> TransactionExecutor for BaseSimulationNetworkExecuto
})
.collect();

let mut raw_result = find_smart_contract_result(&Some(scrs), data.logs.as_ref())?
.unwrap_or_default();
let opt_smart_contract_results = find_smart_contract_result(&Some(scrs), data.logs.as_ref())?;

let mut raw_result = match opt_smart_contract_results {
None => {
if let Some(logs) = data.logs.as_ref() {
if let Ok(Some(error_log)) = find_sc_error(logs) {
return Err(SimulationError::SmartContractExecutionError { // TODO add tests for this
status: error_log.status,
message: error_log.message
}.into());
}
}

vec![]
}
Some(results) => {
results
}
};

let Ok(output_managed) = OutputManaged::multi_decode(&mut raw_result) else {
return Err(TransactionError::CannotDecodeSmartContractResult.into())
return Err(SimulationError::CannotDecodeSmartContractResult.into())
};

let mut response = TransactionOnNetwork::default();
Expand Down
10 changes: 9 additions & 1 deletion executor/src/utils/transaction/results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use multiversx_sdk::utils::base64_decode;
use crate::{ExecutorError, TransactionOnNetworkTransactionLogs, TransactionOnNetworkTransactionLogsEvents, TransactionOnNetworkTransactionSmartContractResult};
use crate::error::transaction::TransactionError;

const ERROR_SIGNALLED_BY_SMART_CONTRACT: &str = "error signalled by smartcontract";

#[derive(Clone, Debug)]
pub(crate) struct SmartContractError {
pub status: u64,
Expand Down Expand Up @@ -49,8 +51,14 @@ pub(crate) fn find_sc_error(logs: &TransactionOnNetworkTransactionLogs) -> Resul
}

let error = decode_topic(topics.get(1).unwrap())?;
let status = if error.contains(ERROR_SIGNALLED_BY_SMART_CONTRACT) {
10
} else {
4
};

let result = SmartContractError {
status: 4,
status,
message: error,
};
return Ok(Some(result));
Expand Down
13 changes: 11 additions & 2 deletions tester/core/tests/network_simulate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tokio::sync::Mutex;

use novax::Address;
use novax::errors::NovaXError;
use novax::executor::{BaseSimulationNetworkExecutor, SimulationNetworkExecutor, TokenTransfer};
use novax::executor::{BaseSimulationNetworkExecutor, ExecutorError, SimulationError, SimulationNetworkExecutor, TokenTransfer};
use novax::tester::tester::TesterContract;
use novax_request::error::request::RequestError;
use novax_request::gateway::client::GatewayClient;
Expand Down Expand Up @@ -362,7 +362,16 @@ async fn test_autoscale_zap_in_error_signaled_by_smart_contract() -> Result<(),
.err()
.unwrap();

todo!();
let expected = NovaXError::Executor(
ExecutorError::Simulation(
SimulationError::SmartContractExecutionError {
status: 10,
message: "error signalled by smartcontract".to_string()
}
)
);

assert_eq!(result, expected);

Ok(())
}
Expand Down

0 comments on commit 5a57586

Please sign in to comment.