Skip to content

Commit

Permalink
Merge pull request #124 from propeller-heads/zz/price_printer/dynamic…
Browse files Browse the repository at this point in the history
…ally-adapt-amount-on-line-switch

feat(price_printer): dynamically adapt the quote amount to the new decimals when switching row
  • Loading branch information
zizou0x authored Jan 10, 2025
2 parents 7e0b35f + 07e63d3 commit c44fe58
Showing 1 changed file with 37 additions and 39 deletions.
76 changes: 37 additions & 39 deletions examples/price_printer/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,38 +98,43 @@ impl App {
items: data_vec,
}
}
pub fn next_row(&mut self) {
let i = match self.state.selected() {
Some(i) => {
if i >= self.items.len() - 1 {
0
} else {
i + 1
}

pub fn move_row(&mut self, direction: isize) {
// Get current decimals, if any
let current_decimals = self.state.selected().map(|idx| {
let comp = &self.items[idx].component;
if self.zero2one {
comp.tokens[0].decimals
} else {
comp.tokens[1].decimals
}
None => 0,
};
self.state.select(Some(i));
self.scroll_state = self
.scroll_state
.position(i * ITEM_HEIGHT);
}
});

pub fn previous_row(&mut self) {
let i = match self.state.selected() {
// Calculate the new index based on direction
let new_index = match self.state.selected() {
Some(i) => {
if i == 0 {
self.items.len() - 1
} else {
i - 1
}
((i as isize + direction + self.items.len() as isize) % self.items.len() as isize)
as usize
}
None => 0,
};
self.state.select(Some(i));

// Update state and scroll position
self.state.select(Some(new_index));
self.scroll_state = self
.scroll_state
.position(i * ITEM_HEIGHT);
.position(new_index * ITEM_HEIGHT);

// Adjust quote amount if decimals have changed
if let Some(prev_decimals) = current_decimals {
let comp = &self.items[new_index].component;
let decimals = comp.tokens[if self.zero2one { 0 } else { 1 }].decimals;
if decimals >= prev_decimals {
self.quote_amount *= BigUint::from(10u64).pow((decimals - prev_decimals) as u32);
} else {
self.quote_amount /= BigUint::from(10u64).pow((prev_decimals - decimals) as u32);
}
}
}

pub fn update_data(&mut self, update: BlockUpdate) {
Expand Down Expand Up @@ -206,7 +211,7 @@ impl App {
self.show_popup = !self.show_popup
}
},
KeyCode::Char('j') | KeyCode::Down => self.next_row(),
KeyCode::Char('j') | KeyCode::Down => self.move_row(1),
KeyCode::Char('+') => {
self.modify_quote(true)
},
Expand All @@ -217,7 +222,7 @@ impl App {
self.zero2one = !self.zero2one;
self.quote_amount = BigUint::one();
}
KeyCode::Char('k') | KeyCode::Up => self.previous_row(),
KeyCode::Char('k') | KeyCode::Up => self.move_row(-1),
KeyCode::Enter => self.show_popup = !self.show_popup,
_ => {}
}
Expand Down Expand Up @@ -367,21 +372,14 @@ impl App {
if self.quote_amount > BigUint::ZERO {
let comp = &self.items[idx].component;
let state = &self.items[idx].state;

let start = Instant::now();
let res = if self.zero2one {
state.get_amount_out(
self.quote_amount.clone(),
&comp.tokens[0],
&comp.tokens[1],
)
let (token_in, token_out) = if self.zero2one {
(&comp.tokens[0], &comp.tokens[1])
} else {
state.get_amount_out(
self.quote_amount.clone(),
&comp.tokens[1],
&comp.tokens[0],
)
(&comp.tokens[1], &comp.tokens[0])
};

let start = Instant::now();
let res = state.get_amount_out(self.quote_amount.clone(), token_in, token_out);
let duration = start.elapsed();

let text = res
Expand Down

0 comments on commit c44fe58

Please sign in to comment.