redgold-schema 0.1.48

Decentralized Portfolio Contracts & Data Lake
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
use crate::constants::{DECIMAL_MULTIPLIER, MAX_COIN_SUPPLY, NANO_DECIMAL_MULTIPLIER, PICO_DECIMAL_MULTIPLIER};
use crate::fee_validator::MIN_RDG_SATS_FEE;
use crate::structs::{CurrencyAmount, CurrencyId, ErrorInfo, NetworkEnvironment, SupportedCurrency};
use crate::{ErrorInfoContext, RgResult};
use num_bigint::BigInt;
use num_traits::{FromPrimitive, ToPrimitive};
use std::iter::Sum;
use std::ops::{Add, Div, Mul, Sub, SubAssign};
use std::str::FromStr;


pub trait RenderCurrencyAmountDecimals {
    fn render_currency_amount_8_decimals(&self) -> String;
    fn render_currency_amount_2_decimals(&self) -> String;
}

impl RenderCurrencyAmountDecimals for f64 {
    fn render_currency_amount_8_decimals(&self) -> String {
        format!("{:.8}", self)
    }

    fn render_currency_amount_2_decimals(&self) -> String {
        format!("{:.2}", self)
    }
}


impl CurrencyAmount {

    pub fn render_8_decimals(&self) -> String {
        self.to_fractional().render_currency_amount_8_decimals()
    }

    pub fn amount_i64(&self) -> i64 {
        self.amount
    }
    pub fn from_fractional(a: impl Into<f64>) -> Result<Self, ErrorInfo> {
        let a = a.into();
        if a <= 0f64 {
            Err(ErrorInfo::error_info("Invalid negative or zero transaction amount"))?
        }
        if a > MAX_COIN_SUPPLY as f64 {
            Err(ErrorInfo::error_info("Invalid transaction amount"))?
        }
        let amount = (a * (DECIMAL_MULTIPLIER as f64)) as i64;
        let mut a = CurrencyAmount::default();
        a.amount = amount;
        Ok(a)
    }
    pub fn from_fractional_basis(a: impl Into<f64>, basis: i64) -> Result<Self, ErrorInfo> {
        let a = a.into();
        if a < 0f64 {
            Err(ErrorInfo::error_info("Invalid negative transaction amount"))?
        }
        let amount = (a * (basis as f64)) as i64;
        let mut a = CurrencyAmount::default();
        a.amount = amount;
        Ok(a)
    }

    pub fn from_fractional_cur(a: impl Into<f64>, cur: SupportedCurrency) -> RgResult<Self> {
        let into = a.into();
        let mut res = match cur {
            SupportedCurrency::Redgold => {Self::from_fractional(into)}
            SupportedCurrency::Bitcoin => {Self::from_fractional(into)}
            SupportedCurrency::Ethereum => {Ok(Self::from_eth_fractional(into))}
            SupportedCurrency::Solana => {Self::from_fractional_basis(into, NANO_DECIMAL_MULTIPLIER)}
            SupportedCurrency::Monero => {Self::from_fractional_basis(into, PICO_DECIMAL_MULTIPLIER)}
            _ => Err(ErrorInfo::error_info("Invalid currency"))
        }?;
        res.currency = Some(cur as i32);
        Ok(res)
    }

    pub fn from_usd(a: impl Into<f64>) -> RgResult<Self> {
        let a = a.into();
        if a <= 0f64 {
            Err(ErrorInfo::error_info("Invalid negative or zero transaction amount"))?
        }
        let amount = (a * (DECIMAL_MULTIPLIER as f64)) as i64;
        let mut a = CurrencyAmount::default();
        a.amount = amount;
        a.currency = Some(SupportedCurrency::Usd as i32);
        Ok(a)
    }


    // Workaround for dealing with u64's etc, drop from e18 precision to e8 precision
    pub fn bigint_offset_denomination() -> BigInt {
        BigInt::from(10_u64.pow(10))
    }
    pub fn bigint_actual_denomination() -> BigInt {
        BigInt::from(10_u64.pow(18))
    }
    pub fn bigint_actual_float_denomination() -> f64 {
        1e18
    }

    pub fn amount_i64_or(&self) -> i64 {
        let curr = self.currency_or();
        if curr == SupportedCurrency::Ethereum {
            self.bigint_amount_offset().and_then(|b| b.to_i64()).unwrap_or(0)
        } else {
            self.amount
        }
    }

    fn bigint_amount_offset(&self) -> Option<BigInt> {
        self.bigint_amount()
            .map(|a| a / Self::bigint_offset_denomination())
    }

    fn bigint_fractional(&self) -> Option<f64> {
        self.bigint_amount()
            .and_then(|a| a.to_f64())
            .map(|a| a / Self::bigint_actual_float_denomination())
    }

    pub fn currency_or(&self) -> SupportedCurrency {
        self.currency
            .and_then(|c| SupportedCurrency::from_i32(c))
            .unwrap_or(SupportedCurrency::Redgold)
    }

    pub fn is_rdg(&self) -> bool {
        self.currency_or() == SupportedCurrency::Redgold
    }

    pub fn is_zero(&self) -> bool {
        self.to_fractional() == 0f64
    }

    pub fn min_fee() -> Self {
        Self::from(MIN_RDG_SATS_FEE)
    }

    pub fn std_pool_fee() -> Self {
        Self::from(100_000)
    }

    pub fn bigint_amount(&self) -> Option<BigInt>  {
        self.string_amount.as_ref().map(|s| BigInt::from_str(s).ok()).flatten()
    }
    pub fn to_fractional(&self) -> f64 {
        let curr = self.currency_or();
        if curr == SupportedCurrency::Ethereum {
            if let Some(decimals) = self.decimals.as_ref() {
                if let Some(b) = self.bigint_amount() {
                    if let Some(d) = BigInt::from_str(decimals).ok() {
                        return (b / d).to_f64().unwrap_or(0f64);
                    }
                }
            }
            self.bigint_fractional().unwrap_or(0f64)
        } else if curr == SupportedCurrency::Monero {
            (self.amount as f64) / (PICO_DECIMAL_MULTIPLIER as f64)
        } else if curr == SupportedCurrency::Solana {
            (self.amount as f64) / (NANO_DECIMAL_MULTIPLIER as f64)
        } else {
            self.to_fractional_std()
        }
    }

    pub fn to_1e8(&self) -> i64 {
        ((self.to_fractional() * 1e8) / (1e8f64)) as i64
    }

    fn to_fractional_std(&self) -> f64 {
        (self.amount as f64) / (DECIMAL_MULTIPLIER as f64)
    }

    pub fn to_rounded_int(&self) -> i64 {
        self.to_fractional() as i64
    }
    pub fn from(amount: i64) -> Self {
        let mut a = Self::default();
        a.amount = amount;
        a
    }

    pub fn from_usd_default(amount: f64) -> Self {
        let mut a = Self::default();
        a.amount = (amount * DECIMAL_MULTIPLIER as f64) as i64;
        a.currency = Some(SupportedCurrency::Usd as i32);
        a
    }
    pub fn from_currency(amount: i64, supported_currency: SupportedCurrency) -> Self {
        let mut a = Self::default();
        a.amount = amount;
        a.currency = Some(supported_currency as i32);
        a
    }
    pub fn zero(supported_currency: SupportedCurrency) -> Self {
        let mut a = Self::default();
        a.currency = Some(supported_currency as i32);
        if supported_currency == SupportedCurrency::Ethereum {
            a.string_amount = Some("0".to_string());
        }
        a
    }

    pub fn from_string(amount: String) -> Self {
        let mut a = Self::default();
        a.string_amount = Some(amount);
        a
    }


    pub fn from_btc(amount: i64) -> Self {
        let mut a = Self::from(amount);
        a.currency = Some(SupportedCurrency::Bitcoin as i32);
        a
    }
    pub fn from_eth_bigint_string(amount: impl Into<String>) -> Self {
        let mut a = Self::from_string(amount.into());
        a.currency = Some(SupportedCurrency::Ethereum as i32);
        a
    }
    pub fn from_eth_network_bigint_string_currency_id_decimals(
        amount: impl Into<String>,
        currency_id: impl Into<CurrencyId>,
        decimals: Option<String>
    ) -> Self {
        let mut a = Self::from_string(amount.into());
        a.currency = Some(SupportedCurrency::Ethereum as i32);
        a.decimals = decimals;
        a.currency_id = Some(currency_id.into());
        a
    }

    pub fn from_eth_bigint(amount: BigInt) -> Self {
        let mut a = Self::from_string(amount.to_string());
        a.currency = Some(SupportedCurrency::Ethereum as i32);
        a
    }
    pub fn from_eth_i64(amount: i64) -> Self {
        let bi = BigInt::from_i64(amount).expect("from_i64") * Self::bigint_offset_denomination();
        Self::from_eth_bigint(bi)
    }
    pub fn from_eth_fractional(amount: f64) -> Self {
        let bi = BigInt::from_f64(amount * 1e18).expect("from_f64");
        Self::from_eth_bigint(bi)
    }

    pub fn from_rdg(amount: i64) -> Self {
        let mut a = Self::from(amount);
        a.currency = Some(SupportedCurrency::Redgold as i32);
        a
    }

    pub fn from_float_string(str: &String) -> Result<Self, ErrorInfo> {
        let amount = str.parse::<f64>()
            .error_info("Invalid transaction amount")?;
        Self::from_fractional(amount)
    }
}

use std::ops::AddAssign;

impl AddAssign for CurrencyAmount {
    fn add_assign(&mut self, other: Self) {
        *self = self.clone() + other;
    }
}

impl SubAssign for CurrencyAmount {
    fn sub_assign(&mut self, other: Self) {
        *self = self.clone() - other;
    }
}

impl Add for CurrencyAmount {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        let mut a = self.clone();
        assert_eq!(a.currency_or(), rhs.currency_or());
        if let Some(ba) = self.bigint_amount() {
            let rhs_ba = rhs.bigint_amount().expect("rhs_bigint_amount");
            let added = ba + rhs_ba;
            a.string_amount = Some(added.to_string());
        } else {
            a.amount += rhs.amount;
        }
        a
    }
}

impl Sub for CurrencyAmount {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self::Output {
        let mut a = self.clone();
        assert_eq!(a.currency_or(), rhs.currency_or());
        if let Some(ba) = self.bigint_amount() {
            let rhs_ba = rhs.bigint_amount().expect("rhs_bigint_amount");
            let added = ba - rhs_ba;
            a.string_amount = Some(added.to_string());
        } else {
            a.amount -= rhs.amount;
        }
        a
    }
}

impl Mul for CurrencyAmount {
    type Output = Self;

    fn mul(self, rhs: Self) -> Self::Output {
        let mut a = self.clone();
        assert_eq!(a.currency_or(), rhs.currency_or());
        if let (Some(ba), Some(rhs_ba)) = (self.bigint_amount(), rhs.bigint_amount()) {
            let added = ba * rhs_ba;
            a.string_amount = Some(added.to_string());
        } else {
            a.amount *= rhs.amount;
        }
        a
    }

}
impl Mul<i64> for CurrencyAmount {
    type Output = Self;

    fn mul(self, rhs: i64) -> Self::Output {
        let mut a = self.clone();
        if let Some(ba) = self.bigint_amount() {
            let added = ba * rhs;
            a.string_amount = Some(added.to_string());
        } else {
            a.amount *= rhs;
        }
        a
    }

}

impl Div for CurrencyAmount {
    type Output = Self;

    fn div(self, rhs: Self) -> Self::Output {
        let mut a = self.clone();
        assert_eq!(a.currency_or(), rhs.currency_or());
        if let Some(ba) = self.bigint_amount() {
            let rhs_ba = rhs.bigint_amount().expect("rhs_bigint_amount");
            let added = ba / rhs_ba;
            a.string_amount = Some(added.to_string());
        } else {
            a.amount /= rhs.amount;
        }
        a
    }

}


impl Sum for CurrencyAmount {
    fn sum<I: Iterator<Item = CurrencyAmount>>(iter: I) -> Self {
        iter.reduce(|a, b| a + b).unwrap_or(CurrencyAmount::default())
    }
}


use std::cmp::Ordering;

impl PartialOrd for CurrencyAmount {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        if let (Some(ba), Some(ob)) = (self.bigint_amount(), other.bigint_amount()) {
            ba.partial_cmp(&ob)
        } else {
            self.amount.partial_cmp(&other.amount)
        }
    }
}

impl Ord for CurrencyAmount {
    fn cmp(&self, other: &Self) -> Ordering {
        if let (Some(ba), Some(ob)) = (self.bigint_amount(), other.bigint_amount()) {
            ba.cmp(&ob)
        } else {
            self.amount.cmp(&other.amount)
        }
    }
}


impl CurrencyAmount {

    pub fn test_send_amount_typed() -> CurrencyAmount {
        // 0.000108594791676 originally as a fee from a testnet transaction (earlier)
        // 0.00128623 originally as a fee from a testnet transaction (current)
        let fee = 0.0005;
        CurrencyAmount::from_eth_fractional(fee)
    }

    pub fn stake_test_amount_typed() -> CurrencyAmount {
        // 0.000108594791676 originally as a fee from a testnet transaction
        let fee = 0.0300;
        CurrencyAmount::from_eth_fractional(fee)
    }

    // TODO: Set by environment.
    pub fn eth_gas_price_fixed_normal_testnet() -> CurrencyAmount {
        // Fee: 0.000171425329026 for 21k gas used * below value
        // 8163110906 for ^
        // Higher seen:
        // 13531134318
        // 23531134318
        // 43531134318
        // 112793670539 -> 0.00236
        // 212793670539 -> 0.0046
        // 412793670539 -> 0.008
        // CurrencyAmount::from_eth_bigint_string("412793670539")
        CurrencyAmount::from_eth_bigint_string("12793670539")
    }

    pub fn eth_gas_price_fixed_normal_mainnet() -> CurrencyAmount {
        // CurrencyAmount::from_eth_bigint_string("4127936705"); // 0.27cents
        CurrencyAmount::from_eth_bigint_string("16511746820") // 1.08cents
    }

    pub fn gas_price_fixed_normal_by_env(env: &NetworkEnvironment) -> CurrencyAmount {
        if env.is_main() {
            Self::eth_gas_price_fixed_normal_mainnet()
        } else {
            Self::eth_gas_price_fixed_normal_testnet()
        }
    }

    pub fn eth_estimated_tx_gas_cost_fixed_normal() -> CurrencyAmount {
        // Fee: 0.000171425329026 for 21k gas used * below value
        CurrencyAmount::from_eth_bigint_string("21000")
    }

    pub fn eth_fee_fixed_normal_testnet() -> CurrencyAmount {
        // Fee: 0.000171425329026 for 21k gas used * below value
        Self::eth_estimated_tx_gas_cost_fixed_normal() * Self::eth_gas_price_fixed_normal_testnet()
    }

    pub fn eth_fee_fixed_normal_mainnet() -> CurrencyAmount {
        // Fee: 0.000171425329026 for 21k gas used * below value
        Self::eth_estimated_tx_gas_cost_fixed_normal() * Self::eth_gas_price_fixed_normal_mainnet()
    }

    pub fn eth_fee_fixed_normal_by_env(env: &NetworkEnvironment) -> CurrencyAmount {
        if env.is_main() {
            Self::eth_fee_fixed_normal_mainnet()
        } else {
            Self::eth_fee_fixed_normal_testnet()
        }
    }

}