near-api-types 0.8.6

Rust types library to interact with NEAR Protocol via RPC API
Documentation
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
use near_token::NearToken;
use serde::{Deserialize, Serialize};

use crate::errors::DecimalNumberParsingError;

/// Static instance of [FTBalance] for USDT token with correct decimals and symbol.
pub const USDT_BALANCE: FTBalance = FTBalance::with_decimals_and_symbol(6, "USDT");
/// Static instance of [FTBalance] for USDC token with correct decimals and symbol.
pub const USDC_BALANCE: FTBalance = FTBalance::with_decimals_and_symbol(6, "USDC");
/// Static instance of [FTBalance] for wNEAR token with correct decimals and symbol.
pub const W_NEAR_BALANCE: FTBalance = FTBalance::with_decimals_and_symbol(24, "wNEAR");

/// The cost of storage per byte in NEAR.
pub const STORAGE_COST_PER_BYTE: NearToken = NearToken::from_yoctonear(10u128.pow(19));

/// A helper type that represents the fungible token balance with a given precision.
///
/// The type is created to simplify the usage of fungible tokens in similar way as the [NearToken] type does.
///
/// The symbol is used only for display purposes.
///
/// The type has static instances for some of the most popular tokens with correct decimals and symbol.
/// * [USDT_BALANCE] - USDT token with 6 decimals
/// * [USDC_BALANCE] - USDC token with 6 decimals
/// * [W_NEAR_BALANCE] - wNEAR token with 24 decimals
///
/// # Examples
///
/// ## Defining 2.5 USDT
/// ```rust
/// use near_api_types::tokens::FTBalance;
///
/// let usdt_balance = FTBalance::with_decimals(6).with_float_str("2.5").unwrap();
///
/// assert_eq!(usdt_balance.amount(), 2_500_000);
/// ```
///
/// ## Defining 3 USDT using smaller precision
/// ```rust
/// use near_api_types::tokens::FTBalance;
///
/// let usdt = FTBalance::with_decimals(6);
///
/// let usdt_balance = usdt.with_amount(3 * 10u128.pow(6));
///
/// assert_eq!(usdt_balance, usdt.with_whole_amount(3));
/// ```
///
/// ## Defining 3 wETH using 18 decimals
/// ```rust
/// use near_api_types::tokens::FTBalance;
///
/// let weth = FTBalance::with_decimals_and_symbol(18, "wETH");
/// let weth_balance = weth.with_whole_amount(3);
///
/// assert_eq!(weth_balance, weth.with_amount(3 * 10u128.pow(18)));
/// ```
#[derive(Debug, Clone, PartialEq, Default, Eq, Serialize, Deserialize)]
pub struct FTBalance {
    amount: u128,
    decimals: u8,
    symbol: &'static str,
}

impl FTBalance {
    /// Creates a new [FTBalance] with a given precision.
    ///
    /// The balance is initialized to 0.
    pub const fn with_decimals(decimals: u8) -> Self {
        Self {
            amount: 0,
            decimals,
            symbol: "FT",
        }
    }

    /// Creates a new [FTBalance] with a given precision and symbol.
    ///
    /// The balance is initialized to 0.
    pub const fn with_decimals_and_symbol(decimals: u8, symbol: &'static str) -> Self {
        Self {
            amount: 0,
            decimals,
            symbol,
        }
    }

    /// Stores the given amount without any transformations.
    ///
    /// The [NearToken] equivalent to this method is [NearToken::from_yoctonear].
    ///
    /// ## Example
    /// ```rust
    /// use near_api_types::tokens::FTBalance;
    ///
    /// let usdt_balance = FTBalance::with_decimals(6).with_amount(2_500_000);
    /// assert_eq!(usdt_balance.amount(), 2_500_000);
    /// assert_eq!(usdt_balance.to_whole(), 2);
    /// ```
    pub const fn with_amount(&self, amount: u128) -> Self {
        Self {
            amount,
            decimals: self.decimals,
            symbol: self.symbol,
        }
    }

    /// Stores the number as an integer token value utilizing the given precision.
    ///
    /// The [NearToken] equivalent to this method is [NearToken::from_near].
    ///
    /// ## Example
    /// ```rust
    /// use near_api_types::tokens::FTBalance;
    ///
    /// let usdt_balance = FTBalance::with_decimals(6).with_whole_amount(3);
    /// assert_eq!(usdt_balance.amount(), 3 * 10u128.pow(6));
    /// assert_eq!(usdt_balance.to_whole(), 3);
    /// ```
    pub const fn with_whole_amount(&self, amount: u128) -> Self {
        Self {
            amount: amount * 10u128.pow(self.decimals as u32),
            decimals: self.decimals,
            symbol: self.symbol,
        }
    }

    /// Parses float string and stores the value in defined precision.
    ///
    /// # Examples
    ///
    /// ## Defining 2.5 USDT
    /// ```rust
    /// use near_api_types::tokens::FTBalance;
    ///
    /// let usdt_balance = FTBalance::with_decimals(6).with_float_str("2.515").unwrap();
    ///
    /// assert_eq!(usdt_balance.amount(), 2_515_000);
    /// ```
    pub fn with_float_str(&self, float_str: &str) -> Result<Self, DecimalNumberParsingError> {
        parse_decimal_number(float_str, 10u128.pow(self.decimals as u32))
            .map(|amount| self.with_amount(amount))
    }

    /// Returns the amount without any transformations.
    ///
    /// The [NearToken] equivalent to this method is [NearToken::as_yoctonear].
    pub const fn amount(&self) -> u128 {
        self.amount
    }

    /// Returns the amount as a whole number in the integer precision.
    ///
    /// The method rounds down the fractional part, so 2.5 USDT will be 2.
    ///
    /// The [NearToken] equivalent to this method is [NearToken::as_near].
    pub const fn to_whole(&self) -> u128 {
        self.amount / 10u128.pow(self.decimals as u32)
    }

    /// Returns the number of decimals used by the token.
    pub const fn decimals(&self) -> u8 {
        self.decimals
    }
}

impl PartialOrd for FTBalance {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        if self.decimals != other.decimals || self.symbol != other.symbol {
            return None;
        }

        Some(self.amount.cmp(&other.amount))
    }
}

impl std::fmt::Display for FTBalance {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let whole_part = self.to_whole();
        let fractional_part = self.amount % 10u128.pow(self.decimals as u32);

        let fractional_part_str = format!(
            "{:0width$}",
            fractional_part,
            width = self.decimals as usize
        );
        let fractional_part_str = fractional_part_str.trim_end_matches('0');

        if fractional_part_str.is_empty() {
            return write!(f, "{} {}", whole_part, self.symbol);
        }

        write!(f, "{}.{} {}", whole_part, fractional_part_str, self.symbol)
    }
}

/// Account balance on the NEAR blockchain.
///
/// This balance doesn't include staked NEAR tokens or storage
#[derive(Debug, Clone, PartialEq, Default, Eq, Serialize, Deserialize)]
pub struct UserBalance {
    /// The total amount of NEAR tokens in the account.
    ///
    /// Please note that this is the total amount of NEAR tokens in the account, not the amount available for use.
    pub total: NearToken,
    /// The amount of NEAR tokens locked in the account for storage usage.
    ///
    /// The storage lock equal to [Self::storage_usage] * [STORAGE_COST_PER_BYTE]
    pub storage_locked: NearToken,
    /// The storage usage by the account in bytes.
    pub storage_usage: u64,
    /// The amount of NEAR tokens staked on a protocol level.
    /// Applicable for staking pools only in 99.99% of the cases.
    ///
    /// The PoS allows particular users to stake funds to become a validator, but the protocol itself
    /// doesn't allow other users to delegate tokens to the validator.
    /// This is why, the [NEP-27](https://github.com/near/core-contracts/tree/master/staking-pool) defines a Staking Pool smart contract
    /// that allows other users to delegate tokens to the validator.
    ///
    /// Even though, the user can stake and become validator itself, it's highly unlikely and this field will be 0
    /// for almost all the users, and not 0 for StakingPool contracts.
    ///
    /// Please note that this is not related to your delegations into the staking pools.
    /// To get your delegation information in the staking pools, use [near-api::Delegation]
    pub locked: NearToken,
}

/// Parsing decimal numbers from `&str` type in `u128`.
/// Function also takes a value of metric prefix in u128 type.
/// `parse_str` use the `u128` type, and have the same max and min values.
///
/// If the fractional part is longer than several zeros in the prefix, it will return the error `DecimalNumberParsingError::LongFractional`.
///
/// If the string slice has invalid chars, it will return the error `DecimalNumberParsingError::InvalidNumber`.
///
/// If the whole part of the number has a value more than the `u64` maximum value, it will return the error `DecimalNumberParsingError::LongWhole`.
fn parse_decimal_number(s: &str, pref_const: u128) -> Result<u128, DecimalNumberParsingError> {
    let (int, fraction) = if let Some((whole, fractional)) = s.trim().split_once('.') {
        let int: u128 = whole
            .parse()
            .map_err(|_| DecimalNumberParsingError::InvalidNumber(s.to_owned()))?;
        let mut fraction: u128 = fractional
            .parse()
            .map_err(|_| DecimalNumberParsingError::InvalidNumber(s.to_owned()))?;
        let len = u32::try_from(fractional.len())
            .map_err(|_| DecimalNumberParsingError::InvalidNumber(s.to_owned()))?;
        fraction = fraction
            .checked_mul(
                pref_const
                    .checked_div(10u128.checked_pow(len).ok_or_else(|| {
                        DecimalNumberParsingError::LongFractional(fractional.to_owned())
                    })?)
                    .filter(|n| *n != 0u128)
                    .ok_or_else(|| {
                        DecimalNumberParsingError::LongFractional(fractional.to_owned())
                    })?,
            )
            .ok_or_else(|| DecimalNumberParsingError::LongFractional(fractional.to_owned()))?;
        (int, fraction)
    } else {
        let int: u128 = s
            .parse()
            .map_err(|_| DecimalNumberParsingError::InvalidNumber(s.to_owned()))?;
        (int, 0)
    };
    let result = fraction
        .checked_add(
            int.checked_mul(pref_const)
                .ok_or_else(|| DecimalNumberParsingError::LongWhole(int.to_string()))?,
        )
        .ok_or_else(|| DecimalNumberParsingError::LongWhole(int.to_string()))?;
    Ok(result)
}

#[cfg(test)]
mod tests {
    use crate::{errors::DecimalNumberParsingError, tokens::parse_decimal_number};

    use super::FTBalance;

    #[test]
    fn ft_balance_default() {
        assert_eq!(
            FTBalance::with_decimals(5).with_whole_amount(5).amount(),
            500000
        );
        assert_eq!(FTBalance::with_decimals(5).with_amount(5).amount(), 5);

        assert_eq!(
            FTBalance::with_decimals(5).with_whole_amount(5).to_whole(),
            5
        );
    }

    #[test]
    fn ft_balance_str() {
        assert_eq!(
            FTBalance::with_decimals(5)
                .with_float_str("5")
                .unwrap()
                .amount(),
            500000
        );
        assert_eq!(
            FTBalance::with_decimals(5)
                .with_float_str("5.00001")
                .unwrap()
                .amount(),
            500001
        );
        assert_eq!(
            FTBalance::with_decimals(5)
                .with_float_str("5.55")
                .unwrap()
                .amount(),
            555000
        );
    }

    const TEST: [(u128, &str, u128); 6] = [
        (129_380_000_001_u128, "129.380000001", 10u128.pow(9)),
        (
            12_938_000_000_100_000_000_u128,
            "12938000000.1",
            10u128.pow(9),
        ),
        (129_380_000_001_u128, "0.129380000001", 10u128.pow(12)),
        (129_380_000_001_000_u128, "129.380000001000", 10u128.pow(12)),
        (
            9_488_129_380_000_001_u128,
            "9488.129380000001",
            10u128.pow(12),
        ),
        (129_380_000_001_u128, "00.129380000001", 10u128.pow(12)),
    ];

    #[test]
    fn parse_test() {
        for (expected_value, str_value, precision) in TEST {
            let parsed_value = parse_decimal_number(str_value, precision).unwrap();
            assert_eq!(parsed_value, expected_value)
        }
    }

    #[test]
    fn test_long_fraction() {
        let data = "1.23456";
        let prefix = 10000u128;
        assert_eq!(
            parse_decimal_number(data, prefix),
            Err(DecimalNumberParsingError::LongFractional(23456.to_string()))
        );
    }

    #[test]
    fn invalid_number_whole() {
        let num = "1h4.7859";
        let prefix: u128 = 10000;
        assert_eq!(
            parse_decimal_number(num, prefix),
            Err(DecimalNumberParsingError::InvalidNumber(
                "1h4.7859".to_owned()
            ))
        );
    }
    #[test]
    fn invalid_number_fraction() {
        let num = "14.785h9";
        let prefix: u128 = 10000;
        assert_eq!(
            parse_decimal_number(num, prefix),
            Err(DecimalNumberParsingError::InvalidNumber(
                "14.785h9".to_owned()
            ))
        );
    }

    #[test]
    fn max_long_fraction() {
        let max_data = 10u128.pow(17) + 1;
        let data = "1.".to_string() + max_data.to_string().as_str();
        let prefix = 10u128.pow(17);
        assert_eq!(
            parse_decimal_number(data.as_str(), prefix),
            Err(DecimalNumberParsingError::LongFractional(
                max_data.to_string()
            ))
        );
    }

    #[test]
    fn parse_u128_error_test() {
        let test_data = u128::MAX.to_string();
        let gas = parse_decimal_number(&test_data, 10u128.pow(9));
        assert_eq!(
            gas,
            Err(DecimalNumberParsingError::LongWhole(u128::MAX.to_string()))
        );
    }

    #[test]
    fn test() {
        let data = "1.000000000000000000000000000000000000001";
        let prefix = 100u128;
        assert_eq!(
            parse_decimal_number(data, prefix),
            Err(DecimalNumberParsingError::LongFractional(
                "000000000000000000000000000000000000001".to_string()
            ))
        );
    }
}