1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
use std::fmt::{Debug, Formatter};
use std::time::Duration;

use async_trait::async_trait;
use multiversx_sc::codec::TopDecodeMulti;
use multiversx_sc::imports::CodeMetadata;
use num_bigint::BigUint;

use novax_data::{Address, NativeConvertible};

use crate::base::deploy::DeployExecutor;
use crate::base::transaction::TransactionExecutor;
use crate::call_result::CallResult;
use crate::error::executor::ExecutorError;
use crate::error::transaction::TransactionError;
use crate::network::transaction::interactor::{BlockchainInteractor, Interactor, TransactionRefreshStrategy};
use crate::network::utils::wallet::Wallet;
use crate::utils::transaction::deploy::get_deploy_call_input;
use crate::utils::transaction::normalization::NormalizationInOut;
use crate::utils::transaction::results::{find_sc_deploy_event, find_sc_error, find_smart_contract_result};
use crate::utils::transaction::token_transfer::TokenTransfer;

/// Alias for the `BaseTransactionNetworkExecutor` struct, parameterized with the `Interactor` type.
pub type NetworkExecutor = BaseTransactionNetworkExecutor<Interactor>;

/// A struct representing the executor for handling transactions in a real blockchain environment.
///
/// This executor is designed to interact with a blockchain network via a specified gateway URL and a wallet
/// for signing transactions. It is parameterized by a type `Interactor` that encapsulates the blockchain interaction logic.
pub struct BaseTransactionNetworkExecutor<Interactor: BlockchainInteractor> {
    interactor: Interactor
}

impl BaseTransactionNetworkExecutor<Interactor> {
    pub fn set_refresh_strategy(&mut self, strategy: TransactionRefreshStrategy) {
        self.interactor.refresh_strategy = strategy;
    }

    pub fn set_timeout(&mut self, timeout: Duration) {
        self.interactor.timeout = timeout;
    }
}

/// Custom implementation of `Clone` for `BaseTransactionNetworkExecutor`, when `Interactor` is `Clone`.
impl<Interactor> Clone for BaseTransactionNetworkExecutor<Interactor>
    where
        Interactor: BlockchainInteractor + Clone
{
    fn clone(&self) -> Self {
        Self {
            interactor: self.interactor.clone()
        }
    }
}

/// Custom implementation of `Debug` for `BaseTransactionNetworkExecutor`, when `Interactor` is `Debug`.
///
/// The implementation is basic, it prefixes "BaseTransactionNetworkExecutor" before the `Interactor`'s debug implementation.
impl<Interactor> Debug for BaseTransactionNetworkExecutor<Interactor>
    where
        Interactor: BlockchainInteractor + Debug
{
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "BaseTransactionNetworkExecutor - ")?;
        self.interactor.fmt(f)
    }
}

impl<Interactor: BlockchainInteractor> BaseTransactionNetworkExecutor<Interactor> {
    /// Creates a new instance of `BaseTransactionNetworkExecutor`.
    ///
    /// This function is async because the Interactor may perform some requests, such as retrieving the network configuration.
    /// Those async operations might fail, thus the Result return type.
    pub async fn new(gateway_url: String, wallet: Wallet) -> Result<Self, ExecutorError> {
        let interactor = Interactor::new(
            gateway_url,
            wallet
        ).await?;

        Ok(
            BaseTransactionNetworkExecutor {
                interactor
            }
        )
    }
}

#[async_trait]
impl<Interactor: BlockchainInteractor> TransactionExecutor for BaseTransactionNetworkExecutor<Interactor> {
    async fn sc_call<OutputManaged>(
        &mut self,
        to: &Address,
        function: String,
        arguments: Vec<Vec<u8>>,
        gas_limit: u64,
        egld_value: BigUint,
        esdt_transfers: Vec<TokenTransfer>
    ) -> Result<CallResult<OutputManaged::Native>, ExecutorError>
        where
            OutputManaged: TopDecodeMulti + NativeConvertible + Send + Sync
    {
        let function_name = if function.is_empty() {
            None
        } else {
            Some(function)
        };

        let normalized = NormalizationInOut {
            sender: self.interactor.get_sender_address().to_bech32_string()?,
            receiver: to.to_bech32_string()?,
            function_name,
            arguments,
            egld_value,
            esdt_transfers,
        }.normalize()?;

        let receiver = normalized.receiver.clone();
        let egld_value = normalized.egld_value.clone();
        let transaction_data = normalized.get_transaction_data();

        let result = self.interactor.sc_call(
            receiver,
            egld_value,
            transaction_data,
            gas_limit,
        )
            .await?;

        let Some(mut sc_result) = find_smart_contract_result(
            &result.transaction.smart_contract_results,
            result.transaction.logs.as_ref()
        )? else {
            if let Some(logs) = result.transaction.logs.as_ref() {
                if let Ok(Some(error_log)) = find_sc_error(logs) {
                    return Err(TransactionError::SmartContractExecutionError { // TODO add tests for this
                        status: error_log.status,
                        message: error_log.message
                    }.into())
                }
            }

            return Err(TransactionError::NoSmartContractResult.into())
        };

        let managed_result = OutputManaged::multi_decode(&mut sc_result)
            .map_err(|_| TransactionError::CannotDecodeSmartContractResult)?;

        let native_result = managed_result.to_native();

        let call_result = CallResult {
            response: result,
            result: Some(native_result),
        };

        Ok(call_result)
    }
}

/// Implementation of the `DeployExecutor` trait for the `BaseTransactionNetworkExecutor` struct.
/// This implementation enables the deployment of smart contracts on the blockchain
/// using a specified blockchain interactor.
#[async_trait]
impl<Interactor: BlockchainInteractor> DeployExecutor for BaseTransactionNetworkExecutor<Interactor> {

    /// Asynchronously deploys a smart contract to the blockchain.
    async fn sc_deploy<
        OutputManaged
    >(
        &mut self,
        bytes: Vec<u8>,
        code_metadata: CodeMetadata,
        egld_value: BigUint,
        arguments: Vec<Vec<u8>>,
        gas_limit: u64
    ) -> Result<(Address, CallResult<OutputManaged::Native>), ExecutorError>
        where
            OutputManaged: TopDecodeMulti + NativeConvertible + Send + Sync
    {
        let deploy_call_input = get_deploy_call_input(
            bytes,
            code_metadata,
            egld_value,
            arguments,
            gas_limit
        );

        let deploy_result = self.sc_call::<OutputManaged>(
            &deploy_call_input.to,
            deploy_call_input.function,
            deploy_call_input.arguments,
            deploy_call_input.gas_limit,
            deploy_call_input.egld_value,
            deploy_call_input.esdt_transfers
        )
            .await?;

        let Some(logs) = deploy_result.response.transaction.logs.as_ref() else {
            return Err(TransactionError::NoSCDeployLogInTheResponse.into())
        };

        let Some(sc_deploy_event) = find_sc_deploy_event(&logs.events) else {
            return Err(TransactionError::NoSCDeployLogInTheResponse.into())
        };

        let deployed_address = Address::from_bech32_string(&sc_deploy_event.address)?;

        Ok((deployed_address, deploy_result))
    }
}