Skip to content

Commit 97af36f

Browse files
committed
support creating decimal from u128 type
1 parent b984829 commit 97af36f

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

contracts/suilend/sources/decimal.move

+10-5
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,
@@ -159,7 +165,8 @@ module suilend::decimal_tests {
159165
from,
160166
from_percent,
161167
saturating_sub,
162-
saturating_floor
168+
saturating_floor,
169+
from_u128
163170
};
164171

165172
#[test]
@@ -180,10 +187,8 @@ module suilend::decimal_tests {
180187
assert!(saturating_sub(a, b) == from(0), 0);
181188
assert!(saturating_sub(b, a) == from(1), 0);
182189
assert!(saturating_floor(from(18446744073709551615)) == 18446744073709551615, 0);
183-
assert!(
184-
saturating_floor(add(from(18446744073709551615), from(1))) == 18446744073709551615,
185-
0,
186-
);
190+
assert!(saturating_floor(add(from(18446744073709551615), from(1))) == 18446744073709551615, 0);
191+
assert!(from_u128(340282366920938463463374607431768211455).add(from(1)).to_scaled_val() == 340282366920938463463374607431768211456 * 1000000000000000000, 0);
187192
}
188193

189194
#[test]

0 commit comments

Comments
 (0)