Skip to content

Commit 7487046

Browse files
authored
wad representation (#498)
* wad representation * move errors to mod.rs
1 parent 6ceeeba commit 7487046

File tree

6 files changed

+945
-30
lines changed

6 files changed

+945
-30
lines changed

packages/contract-utils/src/math/i128_fixed_point.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ SOFTWARE.
2626

2727
use soroban_sdk::{panic_with_error, Env, I256};
2828

29-
use crate::math::soroban_fixed_point::{SorobanFixedPoint, SorobanFixedPointError};
29+
use crate::math::{soroban_fixed_point::SorobanFixedPoint, SorobanFixedPointError};
3030

3131
/// Performs floor(r / z)
3232
fn div_floor(r: i128, z: i128) -> Option<i128> {
@@ -66,7 +66,7 @@ impl SorobanFixedPoint for i128 {
6666
fn scaled_mul_div_floor(x: &i128, env: &Env, y: &i128, z: &i128) -> i128 {
6767
match x.checked_mul(*y) {
6868
Some(r) => div_floor(r, *z)
69-
.unwrap_or_else(|| panic_with_error!(env, SorobanFixedPointError::ZeroDenominator)),
69+
.unwrap_or_else(|| panic_with_error!(env, SorobanFixedPointError::DivisionByZero)),
7070
None => {
7171
// scale to i256 and retry
7272
let res = crate::math::i256_fixed_point::mul_div_floor(
@@ -76,7 +76,7 @@ fn scaled_mul_div_floor(x: &i128, env: &Env, y: &i128, z: &i128) -> i128 {
7676
&I256::from_i128(env, *z),
7777
);
7878
res.to_i128()
79-
.unwrap_or_else(|| panic_with_error!(env, SorobanFixedPointError::ResultOverflow))
79+
.unwrap_or_else(|| panic_with_error!(env, SorobanFixedPointError::Overflow))
8080
}
8181
}
8282
}
@@ -85,7 +85,7 @@ fn scaled_mul_div_floor(x: &i128, env: &Env, y: &i128, z: &i128) -> i128 {
8585
fn scaled_mul_div_ceil(x: &i128, env: &Env, y: &i128, z: &i128) -> i128 {
8686
match x.checked_mul(*y) {
8787
Some(r) => div_ceil(r, *z)
88-
.unwrap_or_else(|| panic_with_error!(env, SorobanFixedPointError::ZeroDenominator)),
88+
.unwrap_or_else(|| panic_with_error!(env, SorobanFixedPointError::DivisionByZero)),
8989
None => {
9090
// scale to i256 and retry
9191
let res = crate::math::i256_fixed_point::mul_div_ceil(
@@ -95,7 +95,7 @@ fn scaled_mul_div_ceil(x: &i128, env: &Env, y: &i128, z: &i128) -> i128 {
9595
&I256::from_i128(env, *z),
9696
);
9797
res.to_i128()
98-
.unwrap_or_else(|| panic_with_error!(env, SorobanFixedPointError::ResultOverflow))
98+
.unwrap_or_else(|| panic_with_error!(env, SorobanFixedPointError::Overflow))
9999
}
100100
}
101101
}

packages/contract-utils/src/math/i256_fixed_point.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ SOFTWARE.
2626

2727
use soroban_sdk::{panic_with_error, Env, I256};
2828

29-
use crate::math::soroban_fixed_point::{SorobanFixedPoint, SorobanFixedPointError};
29+
use crate::math::{soroban_fixed_point::SorobanFixedPoint, SorobanFixedPointError};
3030

3131
impl SorobanFixedPoint for I256 {
3232
fn fixed_mul_floor(&self, env: &Env, y: &I256, denominator: &I256) -> I256 {
@@ -44,7 +44,7 @@ pub(crate) fn mul_div_floor(env: &Env, x: &I256, y: &I256, z: &I256) -> I256 {
4444
let r = x.mul(y);
4545

4646
if z.clone() == zero {
47-
panic_with_error!(env, SorobanFixedPointError::ZeroDenominator);
47+
panic_with_error!(env, SorobanFixedPointError::DivisionByZero);
4848
}
4949

5050
if r < zero || (r > zero && z.clone() < zero) {
@@ -64,7 +64,7 @@ pub(crate) fn mul_div_ceil(env: &Env, x: &I256, y: &I256, z: &I256) -> I256 {
6464
let r = x.mul(y);
6565

6666
if z.clone() == zero {
67-
panic_with_error!(env, SorobanFixedPointError::ZeroDenominator);
67+
panic_with_error!(env, SorobanFixedPointError::DivisionByZero);
6868
}
6969

7070
if z.clone() < zero || r <= zero {

packages/contract-utils/src/math/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,20 @@ pub mod fixed_point;
22
mod i128_fixed_point;
33
mod i256_fixed_point;
44
mod soroban_fixed_point;
5+
pub mod wad;
56

67
mod test;
8+
9+
use soroban_sdk::contracterror;
10+
11+
// ################## ERRORS ##################
12+
13+
#[contracterror]
14+
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
15+
#[repr(u32)]
16+
pub enum SorobanFixedPointError {
17+
/// Arithmetic overflow occurred
18+
Overflow = 1500,
19+
/// Division by zero
20+
DivisionByZero = 1501,
21+
}

packages/contract-utils/src/math/soroban_fixed_point.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ SOFTWARE.
2424
// Based on the Soroban fixed-point mathematics library
2525
// Original implementation: https://github.com/script3/soroban-fixed-point-math
2626

27-
use soroban_sdk::{contracterror, Env};
27+
use soroban_sdk::Env;
2828

2929
// @dev - more detail about the forced panic can be found here: https://github.com/stellar/rs-soroban-env/pull/1091
3030
//
@@ -50,17 +50,3 @@ pub trait SorobanFixedPoint: Sized {
5050
/// occurs, or the result does not fit in Self.
5151
fn fixed_mul_ceil(&self, env: &Env, y: &Self, denominator: &Self) -> Self;
5252
}
53-
54-
// ################## ERRORS ##################
55-
56-
#[contracterror]
57-
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
58-
#[repr(u32)]
59-
pub enum SorobanFixedPointError {
60-
/// The operation failed because the denominator is 0.
61-
ZeroDenominator = 1500,
62-
/// The operation failed because a phantom overflow occurred.
63-
PhantomOverflow = 1501,
64-
/// The operation failed because the result does not fit in Self.
65-
ResultOverflow = 1502,
66-
}

0 commit comments

Comments
 (0)