-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
4,865 additions
and
3,233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
module interest_math::fixed18; | ||
|
||
use interest_math::uint_macro as macro; | ||
|
||
// === Constants === | ||
|
||
const FIXED_18_BASE: u256 = 1_000_000_000_000_000_000; | ||
|
||
// === Structs === | ||
|
||
public struct Fixed18 has copy, drop, store { value: u256 } | ||
|
||
// === Conversion Functions === | ||
|
||
public fun raw_value(self: Fixed18): u256 { | ||
self.value | ||
} | ||
|
||
public fun from_u256(value: u256): Fixed18 { | ||
Fixed18 { value: (value * FIXED_18_BASE) } | ||
} | ||
|
||
public fun from_u128(value: u128): Fixed18 { | ||
Fixed18 { value: ((value as u256) * FIXED_18_BASE) } | ||
} | ||
|
||
public fun from_u64(value: u64): Fixed18 { | ||
Fixed18 { value: ((value as u256) * FIXED_18_BASE) } | ||
} | ||
|
||
public fun from_raw_u256(value: u256): Fixed18 { | ||
Fixed18 { value } | ||
} | ||
|
||
public fun from_raw_u128(value: u128): Fixed18 { | ||
Fixed18 { value: (value as u256) } | ||
} | ||
|
||
public fun from_raw_u64(value: u64): Fixed18 { | ||
Fixed18 { value: (value as u256) } | ||
} | ||
|
||
public fun to_u256(x: Fixed18, decimals: u8): u256 { | ||
let value = macro::mul_div_down!<u256>(x.value, macro::pow!<u256>(10, decimals), FIXED_18_BASE); | ||
value | ||
} | ||
|
||
public fun to_u128(x: Fixed18, decimals: u8): u128 { | ||
let value = macro::mul_div_down!<u256>(x.value, macro::pow!<u256>(10, decimals), FIXED_18_BASE); | ||
value as u128 | ||
} | ||
|
||
public fun to_u64(x: Fixed18, decimals: u8): u64 { | ||
let value = macro::mul_div_down!<u256>(x.value, macro::pow!<u256>(10, decimals), FIXED_18_BASE); | ||
value as u64 | ||
} | ||
|
||
public fun to_u256_up(x: Fixed18, decimals: u8): u256 { | ||
let value = macro::mul_div_up!<u256>(x.value, macro::pow!<u256>(10, decimals), FIXED_18_BASE); | ||
value | ||
} | ||
|
||
public fun to_u128_up(x: Fixed18, decimals: u8): u128 { | ||
let value = macro::mul_div_up!<u256>(x.value, macro::pow!<u256>(10, decimals), FIXED_18_BASE); | ||
value as u128 | ||
} | ||
|
||
public fun to_u64_up(x: Fixed18, decimals: u8): u64 { | ||
let value = macro::mul_div_up!<u256>(x.value, macro::pow!<u256>(10, decimals), FIXED_18_BASE); | ||
value as u64 | ||
} | ||
|
||
public fun u64_to_d18(x: u64, decimals: u8): Fixed18 { | ||
let value = macro::mul_div_up!(x, FIXED_18_BASE, macro::pow!<u256>(10, decimals)); | ||
Fixed18 { value } | ||
} | ||
|
||
public fun u128_to_d18(x: u128, decimals: u8): Fixed18 { | ||
let value = macro::mul_div_up!((x as u256), FIXED_18_BASE, macro::pow!<u256>(10, decimals)); | ||
Fixed18 { value } | ||
} | ||
|
||
public fun u256_to_d18(x: u256, decimals: u8): Fixed18 { | ||
let value = macro::mul_div_up!(x, FIXED_18_BASE, macro::pow!<u256>(10, decimals)); | ||
Fixed18 { value } | ||
} | ||
|
||
public fun u64_to_d18_up(x: u64, decimals: u8): Fixed18 { | ||
let value = macro::mul_div_up!((x as u256), FIXED_18_BASE, macro::pow!<u256>(10, decimals)); | ||
Fixed18 { value } | ||
} | ||
|
||
public fun u128_to_d18_up(x: u128, decimals: u8): Fixed18 { | ||
let value = macro::mul_div_up!((x as u256), FIXED_18_BASE, macro::pow!<u256>(10, decimals)); | ||
Fixed18 { value } | ||
} | ||
|
||
public fun u256_to_d18_up(x: u256, decimals: u8): Fixed18 { | ||
let value = macro::mul_div_up!(x, FIXED_18_BASE, macro::pow!<u256>(10, decimals)); | ||
Fixed18 { value } | ||
} | ||
|
||
// === Try Functions === | ||
|
||
public fun try_mul_down(x: Fixed18, y: Fixed18): (bool, Fixed18) { | ||
let (pred, value) = macro::try_mul_div_down!(x.value, y.value, FIXED_18_BASE); | ||
(pred, Fixed18 { value }) | ||
} | ||
|
||
public fun try_mul_up(x: Fixed18, y: Fixed18): (bool, Fixed18) { | ||
let (pred, value) = macro::try_mul_div_up!(x.value, y.value, FIXED_18_BASE); | ||
(pred, Fixed18 { value }) | ||
} | ||
|
||
public fun try_div_down(x: Fixed18, y: Fixed18): (bool, Fixed18) { | ||
let (pred, value) = macro::try_mul_div_down!(x.value, FIXED_18_BASE, y.value); | ||
(pred, Fixed18 { value }) | ||
} | ||
|
||
public fun try_div_up(x: Fixed18, y: Fixed18): (bool, Fixed18) { | ||
let (pred, value) = macro::try_mul_div_up!(x.value, FIXED_18_BASE, y.value); | ||
(pred, Fixed18 { value }) | ||
} | ||
|
||
// === Arithmetic Functions === | ||
|
||
public fun mul_down(x: Fixed18, y: Fixed18): Fixed18 { | ||
let value = macro::mul_div_down!(x.value, y.value, FIXED_18_BASE); | ||
Fixed18 { value } | ||
} | ||
|
||
public fun mul_up(x: Fixed18, y: Fixed18): Fixed18 { | ||
let value = macro::mul_div_up!(x.value, y.value, FIXED_18_BASE); | ||
Fixed18 { value } | ||
} | ||
|
||
public fun div_down(x: Fixed18, y: Fixed18): Fixed18 { | ||
let value = macro::mul_div_down!(x.value, FIXED_18_BASE, y.value); | ||
Fixed18 { value } | ||
} | ||
|
||
public fun div_up(x: Fixed18, y: Fixed18): Fixed18 { | ||
let value = macro::mul_div_up!(x.value, FIXED_18_BASE, y.value); | ||
Fixed18 { value } | ||
} | ||
|
||
// === Utility Functions === | ||
|
||
public fun base(): u256 { | ||
FIXED_18_BASE | ||
} |
Oops, something went wrong.