From 3d8d778dc378399ea6af4ce9d27ffc71e826084e Mon Sep 17 00:00:00 2001 From: fatcat22 Date: Wed, 10 May 2023 22:23:15 +0800 Subject: [PATCH] brc20 key case sensitive --- src/brc20/num.rs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/brc20/num.rs b/src/brc20/num.rs index 81776ff1c5..4c1cb9b2ee 100644 --- a/src/brc20/num.rs +++ b/src/brc20/num.rs @@ -1,6 +1,6 @@ use crate::brc20::error::BRC20Error; use crate::brc20::params::MAX_DECIMAL_WIDTH; -use bigdecimal::num_bigint::{BigInt, Sign}; +use bigdecimal::num_bigint::{BigInt, Sign, ToBigInt}; use bigdecimal::{BigDecimal, FromPrimitive, ToPrimitive, Zero}; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use std::fmt::{Display, Formatter}; @@ -59,11 +59,18 @@ impl Num { } pub fn checked_to_u128(&self) -> Result { - Ok(self.0.clone().to_u128().ok_or(BRC20Error::Overflow { - op: String::from("to_u128"), - org: self.clone(), - other: Self(BigDecimal::from(BigInt::from(u128::MAX))), // TODO: change overflow error to others - })?) + Ok( + self + .0 + .to_bigint() + .unwrap() + .to_u128() + .ok_or(BRC20Error::Overflow { + op: String::from("to_u128"), + org: self.clone(), + other: Self(BigDecimal::from(BigInt::from(u128::MAX))), // TODO: change overflow error to others + })?, + ) } } @@ -276,4 +283,10 @@ mod tests { Num::from_str("1000000000000000000").unwrap() ); } + + #[test] + fn test_checked_to_u128() { + let n = Num::from_str(&format!("{}", u128::MAX)).unwrap(); + assert_eq!(n.checked_to_u128().unwrap(), u128::MAX); + } }