Skip to content

Commit

Permalink
Merge branch 'main' into zz/price-printer/tiny-docstring-improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
zizou0x authored Jan 10, 2025
2 parents d1de753 + 0ad0b2e commit b98b6f8
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 44 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## [0.69.0](https://github.com/propeller-heads/tycho-simulation/compare/0.68.1...0.69.0) (2025-01-10)


### Features

* **price_printer:** dynamically adapt the quote amount to the new decimals when switching row ([65acc96](https://github.com/propeller-heads/tycho-simulation/commit/65acc968e7de203eb4e7e77cccfacdb990fe44dc))

## [0.68.1](https://github.com/propeller-heads/tycho-simulation/compare/0.68.0...0.68.1) (2025-01-07)

## [0.68.0](https://github.com/propeller-heads/tycho-simulation/compare/0.67.0...0.68.0) (2024-12-23)


Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tycho-simulation"
version = "0.68.0"
version = "0.69.0"
edition = "2021"

[workspace]
Expand Down
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
2 changes: 1 addition & 1 deletion tycho_simulation_py/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "_tycho_simulation_py"
version = "0.68.0"
version = "0.69.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
2 changes: 1 addition & 1 deletion tycho_simulation_py/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "maturin"

[project]
name = "tycho-simulation-py"
version = "0.68.0"
version = "0.69.0"
requires-python = ">=3.7"
classifiers = [
"Programming Language :: Rust",
Expand Down

0 comments on commit b98b6f8

Please sign in to comment.