-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlib.rs
More file actions
38 lines (34 loc) · 1.34 KB
/
lib.rs
File metadata and controls
38 lines (34 loc) · 1.34 KB
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
use scrypto::prelude::*;
use scrypto_math::*;
fn calculate_output(amount: PreciseDecimal) -> Option<PreciseDecimal> {
// the ?-operator is used for convenience
// it returns None if the intermediate result is None
let output_1 = amount.pow(pdec!("2.54"))?;
let output_2 = amount.exp()?.log10()?;
(output_1 + output_2).checked_sqrt()
}
#[blueprint]
mod advanced_math {
struct AdvancedMathDemo {
oci_vault: Vault,
}
impl AdvancedMathDemo {
pub fn instantiate() -> Global<AdvancedMathDemo> {
let oci_bucket: FungibleBucket =
ResourceBuilder::new_fungible(OwnerRole::None).mint_initial_supply(1000);
Self {
oci_vault: Vault::with_bucket(oci_bucket.into()),
}
.instantiate()
.prepare_to_globalize(OwnerRole::None)
.globalize()
}
pub fn free_tokens(&mut self, amount: Decimal) -> (Bucket, Decimal) {
let amount = PreciseDecimal::try_from(amount).expect("Value too large.");
let output_amount = calculate_output(amount).expect("Failed output calculation.");
let output_amount_decimal = output_amount.try_into().expect("Value too large");
let output = self.oci_vault.take(output_amount_decimal);
(output, output_amount_decimal)
}
}
}