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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
use crate::ConsensusParameters;
use fuel_asm::Word;

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TransactionFee {
    pub(crate) min_fee: Word,
    pub(crate) max_fee: Word,
    pub(crate) min_gas: Word,
    pub(crate) max_gas: Word,
}

impl From<TransactionFee> for Word {
    fn from(fee: TransactionFee) -> Word {
        fee.max_fee
    }
}

impl TransactionFee {
    pub const fn new(min_fee: Word, max_fee: Word, min_gas: Word, max_gas: Word) -> Self {
        Self {
            min_fee,
            max_fee,
            min_gas,
            max_gas,
        }
    }

    /// Minimum fee value to pay for the base transaction without script execution.
    pub const fn min_fee(&self) -> Word {
        self.min_fee
    }

    /// Maximum fee value to pay for the transaction with script execution.
    pub const fn max_fee(&self) -> Word {
        self.max_fee
    }

    /// The minimum amount of gas (not fee!) used by this tx
    pub const fn min_gas(&self) -> Word {
        self.min_gas
    }

    /// The max amount of gas (not fee!) usable by this tx
    pub const fn max_gas(&self) -> Word {
        self.max_gas
    }

    /// Convert into a tuple containing the inner min & total fee values
    pub const fn into_inner(self) -> (Word, Word) {
        (self.min_fee, self.max_fee)
    }

    /// Attempt to subtract the maximum fee value from a given balance
    ///
    /// Will return `None` if arithmetic overflow occurs.
    pub fn checked_deduct_total(&self, balance: Word) -> Option<Word> {
        let fee = self.max_fee();

        balance.checked_sub(fee)
    }

    /// Attempt to create a transaction fee from parameters and value arguments
    ///
    /// Will return `None` if arithmetic overflow occurs or `max_fee` less than `min_fee`.
    pub fn checked_from_values(
        params: &ConsensusParameters,
        metered_bytes: Word,
        gas_used_by_predicates: Word,
        gas_limit: Word,
        gas_price: Word,
    ) -> Option<Self> {
        let factor = params.gas_price_factor as u128;

        // TODO: use native div_ceil once stabilized out from nightly
        let bytes_gas = params.gas_per_byte.checked_mul(metered_bytes)?;
        let min_gas = bytes_gas.checked_add(gas_used_by_predicates)?;
        let max_gas = bytes_gas.checked_add(gas_limit)?;

        let max_gas_to_pay = max_gas.checked_mul(gas_price).and_then(|total| {
            num_integer::div_ceil(total as u128, factor).try_into().ok()
        });

        let min_gas_to_pay = min_gas.checked_mul(gas_price).and_then(|bytes| {
            num_integer::div_ceil(bytes as u128, factor).try_into().ok()
        });

        min_gas_to_pay
            .zip(max_gas_to_pay)
            .map(|(min_gas_to_pay, max_gas_to_pay)| {
                Self::new(min_gas_to_pay, max_gas_to_pay, min_gas, max_gas)
            })
    }

    /// Attempt to calculate a gas as asset value, using the price factor defined in the
    /// consensus parameters.
    ///
    /// Will return `None` if overflow occurs
    pub fn gas_refund_value(
        params: &ConsensusParameters,
        gas: Word,
        price: Word,
    ) -> Option<Word> {
        let gas = gas as u128;
        let price = price as u128;
        let factor = params.gas_price_factor as u128;

        gas.checked_mul(price)
            .map(|g| num_integer::div_floor(g, factor))
            .and_then(|g| g.try_into().ok())
    }

    /// Attempt to create a transaction fee from parameters and transaction internals
    ///
    /// Will return `None` if arithmetic overflow occurs.
    pub fn checked_from_tx<T: Chargeable>(
        params: &ConsensusParameters,
        tx: &T,
    ) -> Option<Self> {
        let metered_bytes = tx.metered_bytes_size() as Word;
        let gas_used_by_predicates = tx.gas_used_by_predicates();
        let gas_limit = tx.limit();
        let gas_price = tx.price();

        Self::checked_from_values(
            params,
            metered_bytes,
            gas_used_by_predicates,
            gas_limit,
            gas_price,
        )
    }
}

/// Means that the blockchain charges fee for the transaction.
pub trait Chargeable {
    /// Returns the gas price.
    fn price(&self) -> Word;

    /// Returns the gas limit.
    fn limit(&self) -> Word;

    /// Used for accounting purposes when charging byte based fees.
    fn metered_bytes_size(&self) -> usize;

    /// Used for accounting purposes when charging for predicates.
    fn gas_used_by_predicates(&self) -> Word;
}

#[cfg(test)]
mod tests {
    use crate::{
        ConsensusParameters,
        TransactionFee,
        Word,
    };

    const PARAMS: ConsensusParameters = ConsensusParameters::DEFAULT
        .with_gas_per_byte(2)
        .with_gas_price_factor(3);

    #[test]
    fn base_fee_is_calculated_correctly() {
        let metered_bytes = 5;
        let gas_used_by_predicates = 7;
        let gas_limit = 7;
        let gas_price = 11;

        let fee = TransactionFee::checked_from_values(
            &PARAMS,
            metered_bytes,
            gas_used_by_predicates,
            gas_limit,
            gas_price,
        )
        .expect("failed to calculate fee");

        let expected = PARAMS.gas_per_byte * metered_bytes + gas_limit;
        let expected = expected * gas_price;
        let expected = expected as f64 / PARAMS.gas_price_factor as f64;
        let expected = expected.ceil() as Word;

        assert_eq!(expected, fee.max_fee);
        assert_eq!(expected, fee.min_fee);
    }

    #[test]
    fn base_fee_ceils() {
        let metered_bytes = 5;
        let gas_used_by_predicates = 7;
        let gas_limit = 7;
        let gas_price = 11;

        let fee = TransactionFee::checked_from_values(
            &PARAMS,
            metered_bytes,
            gas_used_by_predicates,
            gas_limit,
            gas_price,
        )
        .expect("failed to calculate fee");

        let expected = PARAMS.gas_per_byte * metered_bytes + gas_limit;
        let expected = expected * gas_price;
        let expected = expected as f64 / PARAMS.gas_price_factor as f64;
        let truncated = expected as Word;
        let expected = expected.ceil() as Word;

        assert_ne!(truncated, expected);
        assert_eq!(expected, fee.max_fee);
        assert_eq!(expected, fee.min_fee);
    }

    #[test]
    fn base_fee_zeroes() {
        let metered_bytes = 5;
        let gas_used_by_predicates = 7;
        let gas_limit = 7;
        let gas_price = 0;

        let fee = TransactionFee::checked_from_values(
            &PARAMS,
            metered_bytes,
            gas_used_by_predicates,
            gas_limit,
            gas_price,
        )
        .expect("failed to calculate fee");

        let expected = 0u64;

        assert_eq!(expected, fee.max_fee);
        assert_eq!(expected, fee.min_fee);
    }

    #[test]
    fn base_fee_wont_overflow_on_bytes() {
        let metered_bytes = Word::MAX;
        let gas_used_by_predicates = 7;
        let gas_limit = 7;
        let gas_price = 11;

        let overflow = TransactionFee::checked_from_values(
            &PARAMS,
            metered_bytes,
            gas_used_by_predicates,
            gas_limit,
            gas_price,
        )
        .is_none();

        assert!(overflow);
    }

    #[test]
    fn base_fee_wont_overflow_on_gas_used_by_predicates() {
        let metered_bytes = 5;
        let gas_used_by_predicates = Word::MAX;
        let gas_limit = 7;
        let gas_price = 11;

        let overflow = TransactionFee::checked_from_values(
            &PARAMS,
            metered_bytes,
            gas_used_by_predicates,
            gas_limit,
            gas_price,
        )
        .is_none();

        assert!(overflow);
    }

    #[test]
    fn base_fee_wont_overflow_on_limit() {
        let metered_bytes = 5;
        let gas_used_by_predicates = 7;
        let gas_limit = Word::MAX;
        let gas_price = 11;

        let overflow = TransactionFee::checked_from_values(
            &PARAMS,
            metered_bytes,
            gas_used_by_predicates,
            gas_limit,
            gas_price,
        )
        .is_none();

        assert!(overflow);
    }

    #[test]
    fn base_fee_wont_overflow_on_price() {
        let metered_bytes = 5;
        let gas_used_by_predicates = 7;
        let gas_limit = 7;
        let gas_price = Word::MAX;

        let overflow = TransactionFee::checked_from_values(
            &PARAMS,
            metered_bytes,
            gas_used_by_predicates,
            gas_limit,
            gas_price,
        )
        .is_none();

        assert!(overflow);
    }

    #[test]
    fn base_fee_gas_limit_less_than_gas_used_by_predicates() {
        let metered_bytes = 5;
        let gas_used_by_predicates = 8;
        let gas_limit = 7;
        let gas_price = 11;

        let fee = TransactionFee::checked_from_values(
            &PARAMS,
            metered_bytes,
            gas_used_by_predicates,
            gas_limit,
            gas_price,
        )
        .expect("failed to calculate fee");

        assert!(fee.min_fee > fee.max_fee);
    }
}