Skip to content

Commit

Permalink
Merge pull request #33 from okx/fix/decimal_check
Browse files Browse the repository at this point in the history
fix decimal check
  • Loading branch information
wanyvic authored May 11, 2023
2 parents 07e37bb + 7320a82 commit 43afd15
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/brc20/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ impl Num {
self.0.sign()
}

pub fn scale(&self) -> i64 {
let (_, scale) = self.0.as_bigint_and_exponent();
scale
}

pub fn checked_to_u128(&self) -> Result<u128, BRC20Error> {
Ok(
self
Expand Down
24 changes: 20 additions & 4 deletions src/brc20/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,19 @@ impl<'a, L: LedgerReadWrite> BRC20Updater<'a, L> {

let supply = Num::from_str(&deploy.max_supply)?;

if supply.sign() == Sign::NoSign || supply > Into::<Num>::into(u64::MAX) {
if supply.sign() == Sign::NoSign
|| supply > Into::<Num>::into(u64::MAX)
|| supply.scale() > dec as i64
{
return Err(Error::BRC20Error(BRC20Error::InvalidSupply(supply)));
}

let limit = Num::from_str(&deploy.mint_limit.map_or(deploy.max_supply, |v| v))?;

if limit.sign() == Sign::NoSign || limit > supply {
if limit.sign() == Sign::NoSign
|| limit > Into::<Num>::into(u64::MAX)
|| limit.scale() > dec as i64
{
return Err(Error::BRC20Error(BRC20Error::MintLimitOutOfRange(
lower_tick.as_str().to_string(),
limit,
Expand Down Expand Up @@ -205,8 +211,13 @@ impl<'a, L: LedgerReadWrite> BRC20Updater<'a, L> {

let base = BIGDECIMAL_TEN.checked_powu(token_info.decimal as u64)?;

let mut amt = Num::from_str(&mint.amount)?.checked_mul(&base)?;
let mut amt = Num::from_str(&mint.amount)?;

if amt.scale() > token_info.decimal as i64 {
return Err(Error::BRC20Error(BRC20Error::AmountOverflow(amt)));
}

amt = amt.checked_mul(&base)?;
if amt.sign() == Sign::NoSign {
return Err(Error::BRC20Error(BRC20Error::InvalidZeroAmount));
}
Expand Down Expand Up @@ -287,8 +298,13 @@ impl<'a, L: LedgerReadWrite> BRC20Updater<'a, L> {

let base = BIGDECIMAL_TEN.checked_powu(token_info.decimal as u64)?;

let amt = Num::from_str(&transfer.amount)?.checked_mul(&base)?;
let mut amt = Num::from_str(&transfer.amount)?;

if amt.scale() > token_info.decimal as i64 {
return Err(Error::BRC20Error(BRC20Error::AmountOverflow(amt)));
}

amt = amt.checked_mul(&base)?;
if amt.sign() == Sign::NoSign || amt > Into::<Num>::into(token_info.supply) {
return Err(Error::BRC20Error(BRC20Error::AmountOverflow(amt)));
}
Expand Down

0 comments on commit 43afd15

Please sign in to comment.