Skip to content

Commit

Permalink
fix(price-printer): do not panic during amount calculation math.
Browse files Browse the repository at this point in the history
Currently if the users substract too much, it panics due to a BigUint error. This commit makes it catch errors during BigUint sub and default to 1 if the users tries to substract too much.
  • Loading branch information
zizou0x committed Mar 10, 2025
1 parent 7d98695 commit eb3f7f9
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions examples/price_printer/ui.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::{cmp::max, str::FromStr, time::Instant};
use std::{str::FromStr, time::Instant};

use futures::StreamExt;
use itertools::Itertools;
use num_bigint::BigUint;
use num_traits::One;
use num_traits::{CheckedSub, One};
use ratatui::{
crossterm::event::{self, Event, KeyCode, KeyEventKind},
layout::{Constraint, Flex, Layout, Margin, Rect},
Expand Down Expand Up @@ -252,10 +252,10 @@ impl App {
if increase {
self.quote_amount += BigUint::from(10u64).pow(decimals as u32);
} else {
self.quote_amount = max(
&self.quote_amount - BigUint::from(10u64).pow(decimals as u32),
BigUint::one(),
);
self.quote_amount = self
.quote_amount
.checked_sub(&BigUint::from(10u64).pow(decimals as u32))
.unwrap_or(BigUint::one());
}
}
}
Expand Down

0 comments on commit eb3f7f9

Please sign in to comment.