-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from shuhuiluo/tick_data_provider
Tick data provider
- Loading branch information
Showing
8 changed files
with
170 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
mod pool; | ||
mod tick; | ||
mod tick_data_provider; | ||
mod tick_list_data_provider; | ||
|
||
pub use pool::Pool; | ||
pub use tick::{Tick, TickTrait}; | ||
pub use tick_data_provider::{NoTickDataError, NoTickDataProvider, TickDataProvider}; | ||
pub use tick_list_data_provider::TickListDataProvider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use super::{Tick, TickTrait}; | ||
use anyhow::Result; | ||
use thiserror::Error; | ||
|
||
/// Provides information about ticks | ||
pub trait TickDataProvider<T: TickTrait> { | ||
/// Return information corresponding to a specific tick | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `tick`: The tick to load | ||
/// | ||
/// returns: Result<impl TickTrait+Sized, Error> | ||
/// | ||
fn get_tick(&self, tick: i32) -> Result<&T>; | ||
|
||
/// Return the next tick that is initialized within a single word | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `tick`: The current tick | ||
/// * `lte`: Whether the next tick should be lte the current tick | ||
/// * `tick_spacing`: The tick spacing of the pool | ||
/// | ||
/// returns: Result<(i32, bool), Error> | ||
/// | ||
fn next_initialized_tick_within_one_word( | ||
&self, | ||
tick: i32, | ||
lte: bool, | ||
tick_spacing: i32, | ||
) -> Result<(i32, bool)>; | ||
} | ||
|
||
#[derive(Error, Debug)] | ||
#[error("No tick data provider was given")] | ||
pub struct NoTickDataError; | ||
|
||
/// This tick data provider does not know how to fetch any tick data. It throws whenever it is required. | ||
/// Useful if you do not need to load tick data for your use case. | ||
pub struct NoTickDataProvider; | ||
|
||
impl TickDataProvider<Tick> for NoTickDataProvider { | ||
fn get_tick(&self, _: i32) -> Result<&Tick> { | ||
Err(NoTickDataError.into()) | ||
} | ||
|
||
fn next_initialized_tick_within_one_word( | ||
&self, | ||
_: i32, | ||
_: bool, | ||
_: i32, | ||
) -> Result<(i32, bool)> { | ||
Err(NoTickDataError.into()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_no_tick_data_provider() { | ||
let tick_data_provider = NoTickDataProvider; | ||
assert_eq!( | ||
tick_data_provider.get_tick(0).unwrap_err().to_string(), | ||
NoTickDataError.to_string() | ||
); | ||
assert_eq!( | ||
tick_data_provider | ||
.next_initialized_tick_within_one_word(0, false, 1) | ||
.unwrap_err() | ||
.to_string(), | ||
NoTickDataError.to_string() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use crate::{ | ||
entities::{Tick, TickDataProvider}, | ||
utils::TickList, | ||
}; | ||
use anyhow::Result; | ||
|
||
/// A data provider for ticks that is backed by an in-memory array of ticks. | ||
pub struct TickListDataProvider { | ||
ticks: Vec<Tick>, | ||
} | ||
|
||
impl TickListDataProvider { | ||
pub fn new(ticks: Vec<Tick>, tick_spacing: i32) -> Self { | ||
ticks.validate_list(tick_spacing); | ||
Self { ticks } | ||
} | ||
} | ||
|
||
impl TickDataProvider<Tick> for TickListDataProvider { | ||
fn get_tick(&self, tick: i32) -> Result<&Tick> { | ||
Ok(self.ticks.get_tick(tick)) | ||
} | ||
|
||
fn next_initialized_tick_within_one_word( | ||
&self, | ||
tick: i32, | ||
lte: bool, | ||
tick_spacing: i32, | ||
) -> Result<(i32, bool)> { | ||
Ok(self | ||
.ticks | ||
.next_initialized_tick_within_one_word(tick, lte, tick_spacing)) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use once_cell::sync::Lazy; | ||
|
||
static PROVIDER: Lazy<TickListDataProvider> = | ||
Lazy::new(|| TickListDataProvider::new(vec![Tick::new(-1, 1, -1), Tick::new(1, 1, 1)], 1)); | ||
|
||
#[test] | ||
fn can_take_an_empty_list_of_ticks() { | ||
TickListDataProvider::new(vec![], 1); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected = "TICK_SPACING_NONZERO")] | ||
fn throws_for_0_tick_spacing() { | ||
TickListDataProvider::new(vec![], 0); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected = "ZERO_NET")] | ||
fn throws_for_uneven_tick_list() { | ||
TickListDataProvider::new(vec![Tick::new(-1, 1, -1), Tick::new(1, 1, 2)], 1); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected = "NOT_CONTAINED")] | ||
fn throws_if_tick_not_in_list() { | ||
PROVIDER.get_tick(0).unwrap(); | ||
} | ||
|
||
#[test] | ||
fn gets_the_smallest_tick_from_the_list() { | ||
let tick = PROVIDER.get_tick(-1).unwrap(); | ||
assert_eq!(tick.liquidity_net, -1); | ||
assert_eq!(tick.liquidity_gross, 1); | ||
} | ||
|
||
#[test] | ||
fn gets_the_largest_tick_from_the_list() { | ||
let tick = PROVIDER.get_tick(1).unwrap(); | ||
assert_eq!(tick.liquidity_net, 1); | ||
assert_eq!(tick.liquidity_gross, 1); | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters