Skip to content

Commit 2857c0b

Browse files
committed
support creating decimal from u128 type
1 parent 45b878f commit 2857c0b

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

contracts/suilend/sources/decimal.move

+8-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ module suilend::decimal {
1414
}
1515
}
1616

17+
public fun from_u128(v: u128): Decimal {
18+
Decimal {
19+
value: (v as u256) * WAD
20+
}
21+
}
22+
1723
public fun from_percent(v: u8): Decimal {
1824
Decimal {
1925
value: (v as u256) * WAD / 100
@@ -144,7 +150,7 @@ module suilend::decimal {
144150

145151
#[test_only]
146152
module suilend::decimal_tests {
147-
use suilend::decimal::{add, sub, mul, div, floor, ceil, pow, lt, gt, le, ge, from, from_percent, saturating_sub, saturating_floor};
153+
use suilend::decimal::{add, sub, mul, div, floor, ceil, pow, lt, gt, le, ge, from, from_percent, saturating_sub, saturating_floor, from_u128};
148154

149155
#[test]
150156
fun test_basic() {
@@ -165,6 +171,7 @@ module suilend::decimal_tests {
165171
assert!(saturating_sub(b, a) == from(1), 0);
166172
assert!(saturating_floor(from(18446744073709551615)) == 18446744073709551615, 0);
167173
assert!(saturating_floor(add(from(18446744073709551615), from(1))) == 18446744073709551615, 0);
174+
assert!(from_u128(340282366920938463463374607431768211455).add(from(1)).to_scaled_val() == 340282366920938463463374607431768211456 * 1000000000000000000, 0);
168175
}
169176

170177
#[test]

0 commit comments

Comments
 (0)