Skip to content

Commit

Permalink
Managed Decimal into signed cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-marinica committed Jun 28, 2024
1 parent 292867d commit b1d0ff8
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 17 deletions.
2 changes: 1 addition & 1 deletion framework/base/src/err_msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub const VALUE_EXCEEDS_SLICE: &str = "value exceeds target slice";
pub const CAST_TO_I64_ERROR: &str = "cast to i64 error";
pub const BIG_UINT_EXCEEDS_SLICE: &str = "big uint as_bytes exceed target slice";
pub const BIG_UINT_SUB_NEGATIVE: &str = "cannot subtract because result would be negative";
pub const BIG_UINT_NEGATIVE: &str = "cannot convert to unsigned, number is negative";
pub const UNSIGNED_NEGATIVE: &str = "cannot convert to unsigned, number is negative";

pub const DESERIALIZATION_INVALID_BYTE: &str = "call data deserialization error: not a valid byte";
pub const DESERIALIZATION_NOT_32_BYTES: &str =
Expand Down
17 changes: 2 additions & 15 deletions framework/base/src/types/managed/basic/big_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ use core::{convert::TryInto, marker::PhantomData};
use crate::{
abi::{TypeAbiFrom, TypeName},
api::{
const_handles, use_raw_handle, BigIntApiImpl, ErrorApiImpl, HandleConstraints,
ManagedBufferApiImpl, ManagedTypeApi, ManagedTypeApiImpl, RawHandle, StaticVarApiImpl,
const_handles, use_raw_handle, BigIntApiImpl, HandleConstraints, ManagedBufferApiImpl,
ManagedTypeApi, ManagedTypeApiImpl, RawHandle, StaticVarApiImpl,
},
codec::{
DecodeErrorHandler, EncodeErrorHandler, NestedDecode, NestedDecodeInput, NestedEncode,
NestedEncodeOutput, TopDecode, TopDecodeInput, TopEncode, TopEncodeOutput, TryStaticCast,
},
err_msg,
formatter::{hex_util::encode_bytes_as_hex, FormatByteReceiver, SCDisplay},
types::{heap::BoxedBytes, BigUint, ManagedBuffer, ManagedOption, ManagedType, Sign},
};
Expand Down Expand Up @@ -264,18 +263,6 @@ impl<M: ManagedTypeApi> BigInt<M> {
ManagedOption::some(unsafe { self.into_big_uint_unchecked() })
}
}

fn check_non_negative(&self) {
if M::managed_type_impl().bi_sign(self.handle.clone()) == crate::api::Sign::Minus {
M::error_api_impl().signal_error(err_msg::BIG_UINT_SUB_NEGATIVE.as_bytes());
}
}

/// Converts to an unsigned `BigUint`. Stops execution if number is negative.
pub fn into_big_uint_or_fail(self) -> BigUint<M> {
self.check_non_negative();
unsafe { self.into_big_uint_unchecked() }
}
}

impl<M: ManagedTypeApi> TryStaticCast for BigInt<M> {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
const_handles, use_raw_handle, BigFloatApiImpl, BigIntApiImpl, HandleConstraints,
ManagedBufferApiImpl, ManagedTypeApi,
},
err_msg,
formatter::{FormatBuffer, FormatByteReceiver, SCDisplay},
types::{BigFloat, BigInt, BigUint, ManagedBuffer, ManagedType, Sign},
};
Expand Down Expand Up @@ -77,7 +78,10 @@ impl<M: ManagedTypeApi, D: Decimals> ManagedDecimalSigned<M, D> {

pub fn into_unsigned_or_fail(self) -> ManagedDecimal<M, D> {
ManagedDecimal {
data: self.data.into_big_uint_or_fail(),
data: self
.data
.into_big_uint()
.unwrap_or_sc_panic(err_msg::UNSIGNED_NEGATIVE),
decimals: self.decimals,
}
}
Expand Down

0 comments on commit b1d0ff8

Please sign in to comment.