fuels_programs/calls/traits/
transaction_tuner.rs

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
use fuels_accounts::Account;
use fuels_core::types::{
    errors::{error, Result},
    transaction::{ScriptTransaction, TxPolicies},
    transaction_builders::{
        BuildableTransaction, ScriptTransactionBuilder, TransactionBuilder, VariableOutputPolicy,
    },
};

use crate::calls::{
    utils::{build_tx_from_contract_calls, sealed, transaction_builder_from_contract_calls},
    ContractCall, ScriptCall,
};

#[async_trait::async_trait]
pub trait TransactionTuner: sealed::Sealed {
    async fn transaction_builder<T: Account>(
        &self,
        tx_policies: TxPolicies,
        variable_output_policy: VariableOutputPolicy,
        account: &T,
    ) -> Result<ScriptTransactionBuilder>;

    async fn build_tx<T: Account>(
        &self,
        tx_policies: TxPolicies,
        variable_output_policy: VariableOutputPolicy,
        account: &T,
    ) -> Result<ScriptTransaction>;
}

#[async_trait::async_trait]
impl TransactionTuner for ContractCall {
    async fn transaction_builder<T: Account>(
        &self,
        tx_policies: TxPolicies,
        variable_output_policy: VariableOutputPolicy,
        account: &T,
    ) -> Result<ScriptTransactionBuilder> {
        transaction_builder_from_contract_calls(
            std::slice::from_ref(self),
            tx_policies,
            variable_output_policy,
            account,
        )
        .await
    }

    async fn build_tx<T: Account>(
        &self,
        tx_policies: TxPolicies,
        variable_output_policy: VariableOutputPolicy,
        account: &T,
    ) -> Result<ScriptTransaction> {
        build_tx_from_contract_calls(
            std::slice::from_ref(self),
            tx_policies,
            variable_output_policy,
            account,
        )
        .await
    }
}

#[async_trait::async_trait]
impl TransactionTuner for ScriptCall {
    async fn transaction_builder<T: Account>(
        &self,
        tx_policies: TxPolicies,
        variable_output_policy: VariableOutputPolicy,
        _account: &T,
    ) -> Result<ScriptTransactionBuilder> {
        let (inputs, outputs) = self.prepare_inputs_outputs()?;

        Ok(ScriptTransactionBuilder::default()
            .with_variable_output_policy(variable_output_policy)
            .with_tx_policies(tx_policies)
            .with_script(self.script_binary.clone())
            .with_script_data(self.compute_script_data()?)
            .with_inputs(inputs)
            .with_outputs(outputs)
            .with_gas_estimation_tolerance(0.05)
            .with_max_fee_estimation_tolerance(0.05))
    }

    async fn build_tx<T: Account>(
        &self,
        tx_policies: TxPolicies,
        variable_output_policy: VariableOutputPolicy,
        account: &T,
    ) -> Result<ScriptTransaction> {
        let mut tb = self
            .transaction_builder(tx_policies, variable_output_policy, account)
            .await?;

        account.add_witnesses(&mut tb)?;
        account.adjust_for_fee(&mut tb, 0).await?;

        tb.build(account.try_provider()?).await
    }
}

impl sealed::Sealed for Vec<ContractCall> {}

#[async_trait::async_trait]
impl TransactionTuner for Vec<ContractCall> {
    async fn transaction_builder<T: Account>(
        &self,
        tx_policies: TxPolicies,
        variable_output_policy: VariableOutputPolicy,
        account: &T,
    ) -> Result<ScriptTransactionBuilder> {
        validate_contract_calls(self)?;

        transaction_builder_from_contract_calls(self, tx_policies, variable_output_policy, account)
            .await
    }

    /// Returns the script that executes the contract calls
    async fn build_tx<T: Account>(
        &self,
        tx_policies: TxPolicies,
        variable_output_policy: VariableOutputPolicy,
        account: &T,
    ) -> Result<ScriptTransaction> {
        validate_contract_calls(self)?;

        build_tx_from_contract_calls(self, tx_policies, variable_output_policy, account).await
    }
}

fn validate_contract_calls(calls: &[ContractCall]) -> Result<()> {
    if calls.is_empty() {
        return Err(error!(
            Other,
            "no calls added. Have you used '.add_calls()'?"
        ));
    }

    Ok(())
}