Skip to content

Commit

Permalink
✏️ fix: fn names
Browse files Browse the repository at this point in the history
  • Loading branch information
josemvcerqueira committed Dec 23, 2024
1 parent b2f4762 commit 2e59b6d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
12 changes: 6 additions & 6 deletions sources/fixed18.move
Original file line number Diff line number Diff line change
Expand Up @@ -70,32 +70,32 @@ public fun to_u64_up(x: Fixed18, decimals: u8): u64 {
value as u64
}

public fun u64_to_d18(x: u64, decimals: u8): Fixed18 {
public fun u64_to_fixed18(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 {
public fun u128_to_fixed18(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 {
public fun u256_to_fixed18(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 {
public fun u64_to_fixed18_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 {
public fun u128_to_fixed18_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 {
public fun u256_to_fixed18_up(x: u256, decimals: u8): Fixed18 {
let value = macro::mul_div_up!(x, FIXED_18_BASE, macro::pow!<u256>(10, decimals));
Fixed18 { value }
}
Expand Down
27 changes: 17 additions & 10 deletions tests/fixed18.move
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ fun test_convert_functions() {
assert_eq(fixed18::from_raw_u64(3).raw_value(), 3);
assert_eq(fixed18::from_u64(3).raw_value(), 3 * FIXED_18_BASE);

assert_eq(fixed18::u64_to_d18(3 * 1000000000, 9).raw_value(), 3 * FIXED_18_BASE);
assert_eq(fixed18::u64_to_d18(3 * 1000000000, 9).to_u64(9), 3 * 1000000000);
assert_eq(fixed18::u64_to_fixed18(3 * 1000000000, 9).raw_value(), 3 * FIXED_18_BASE);
assert_eq(fixed18::u64_to_fixed18(3 * 1000000000, 9).to_u64(9), 3 * 1000000000);

assert_eq(fixed18::u128_to_d18(3 * 1000000000000000000, 18).raw_value(), 3 * FIXED_18_BASE);
assert_eq(fixed18::u128_to_d18(3 * 1000000000000000000, 18).to_u128(18), 3 * 1000000000000000000);
assert_eq(fixed18::u128_to_fixed18(3 * 1000000000000000000, 18).raw_value(), 3 * FIXED_18_BASE);
assert_eq(fixed18::u128_to_fixed18(3 * 1000000000000000000, 18).to_u128(18), 3 * 1000000000000000000);

assert_eq(fixed18::u256_to_d18(3 * 100000000000000000000000000000000000000000000000000, 50).raw_value(), 3 * FIXED_18_BASE);
assert_eq(fixed18::u256_to_d18(3 * 100000000000000000000000000000000000000000000000000, 50).to_u256(50), 3 * 100000000000000000000000000000000000000000000000000);
assert_eq(fixed18::u256_to_fixed18(3 * 100000000000000000000000000000000000000000000000000, 50).raw_value(), 3 * FIXED_18_BASE);
assert_eq(fixed18::u256_to_fixed18(3 * 100000000000000000000000000000000000000000000000000, 50).to_u256(50), 3 * 100000000000000000000000000000000000000000000000000);
}

#[test]
Expand Down Expand Up @@ -227,10 +227,17 @@ fun test_div_up() {
}

#[test]
fun test_to_d18() {
assert_eq(fixed18::u256_to_d18(FIXED_18_BASE, 18).raw_value(), FIXED_18_BASE);
assert_eq(fixed18::u256_to_d18(2, 1).raw_value(), 2 * FIXED_18_BASE / 10);
assert_eq(fixed18::u256_to_d18(20 * FIXED_18_BASE, 18).raw_value(), 20 * FIXED_18_BASE);
fun test_to_fixed18() {
assert_eq(fixed18::u256_to_fixed18(FIXED_18_BASE, 18).raw_value(), FIXED_18_BASE);
assert_eq(fixed18::u256_to_fixed18(2, 1).raw_value(), 2 * FIXED_18_BASE / 10);
assert_eq(fixed18::u256_to_fixed18(20 * FIXED_18_BASE, 18).raw_value(), 20 * FIXED_18_BASE);

assert_eq(fixed18::u256_to_fixed18_up(FIXED_18_BASE, 18).raw_value(), FIXED_18_BASE);
assert_eq(fixed18::u256_to_fixed18_up(2, 1).raw_value(), (2 * FIXED_18_BASE + 9) / 10);
assert_eq(fixed18::u256_to_fixed18_up(20 * FIXED_18_BASE, 18).raw_value(), 20 * FIXED_18_BASE);

assert_eq(fixed18::u64_to_fixed18_up(2, 1).raw_value(), (2 * FIXED_18_BASE + 9) / 10);
assert_eq(fixed18::u128_to_fixed18_up(2, 1).raw_value(), (2 * FIXED_18_BASE + 9) / 10);
}

#[test]
Expand Down

0 comments on commit 2e59b6d

Please sign in to comment.