pub struct Price {
pub numerator: BigUint,
pub denominator: BigUint,
}Expand description
Represents a price as a fraction in the token_in -> token_out direction. With units [token_out/token_in].
§Fields
numerator- The amount of token_out (what you receive), including token decimalsdenominator- The amount of token_in (what you pay), including token decimals
A fraction struct is used for price to have flexibility in precision independent of the decimal precisions of the numerator and denominator tokens. This allows for:
- Exact price representation without floating-point errors
- Handling tokens with different decimal places without loss of precision
§Example
If we want to represent that token A is worth 2.5 units of token B:
use num_bigint::BigUint;
use tycho_common::simulation::protocol_sim::Price;
let numerator = BigUint::from(25u32); // Represents 25 units of token B
let denominator = BigUint::from(10u32); // Represents 10 units of token A
let price = Price::new(numerator, denominator);If you want to define a limit price for a trade, where you expect to get at least 120 T1 for 50 T2:
use num_bigint::BigUint;
use tycho_common::simulation::protocol_sim::Price;
let min_amount_out = BigUint::from(120u32); // The minimum amount of T1 you expect
let amount_in = BigUint::from(50u32); // The amount of T2 you are selling
let limit_price = Price::new(min_amount_out, amount_in);Fields§
§numerator: BigUint§denominator: BigUintImplementations§
Trait Implementations§
impl Eq for Price
impl StructuralPartialEq for Price
Auto Trait Implementations§
impl Freeze for Price
impl RefUnwindSafe for Price
impl Send for Price
impl Sync for Price
impl Unpin for Price
impl UnwindSafe for Price
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
🔬This is a nightly-only experimental API. (
clone_to_uninit)Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.