diff --git a/src/quoting/base_pool.rs b/src/quoting/base_pool.rs index bfb8858..e5d70e1 100644 --- a/src/quoting/base_pool.rs +++ b/src/quoting/base_pool.rs @@ -122,6 +122,7 @@ impl Pool for BasePool { type Resources = BasePoolResources; type State = BasePoolState; type QuoteError = BasePoolQuoteError; + type Meta = (); fn get_key(&self) -> NodeKey { self.key @@ -133,7 +134,7 @@ impl Pool for BasePool { fn quote( &self, - params: QuoteParams, + params: QuoteParams, ) -> Result, Self::QuoteError> { let amount = params.token_amount.amount; let token = params.token_amount.token; @@ -318,7 +319,7 @@ impl Pool for BasePool { #[cfg(test)] mod tests { use super::*; - use crate::quoting::types::{Block, QuoteMeta, TokenAmount}; + use crate::quoting::types::TokenAmount; use alloc::vec; const TOKEN0: U256 = U256([1, 0, 0, 0]); @@ -353,9 +354,7 @@ mod tests { }, sqrt_ratio_limit: None, override_state: None, - meta: QuoteMeta { - block: Block { number: 1, time: 2 }, - }, + meta: (), }; let quote = pool.quote(params).expect("Failed to get quote"); @@ -383,9 +382,7 @@ mod tests { }, sqrt_ratio_limit: None, override_state: None, - meta: QuoteMeta { - block: Block { number: 1, time: 2 }, - }, + meta: (), }; let quote = pool.quote(params).expect("Failed to get quote"); @@ -424,9 +421,7 @@ mod tests { }, sqrt_ratio_limit: None, override_state: None, - meta: QuoteMeta { - block: Block { number: 1, time: 2 }, - }, + meta: (), }; let quote = pool.quote(params).expect("Failed to get quote"); @@ -465,9 +460,7 @@ mod tests { }, sqrt_ratio_limit: None, override_state: None, - meta: QuoteMeta { - block: Block { number: 1, time: 2 }, - }, + meta: (), }; let quote = pool.quote(params).expect("Failed to get quote"); diff --git a/src/quoting/oracle_pool.rs b/src/quoting/oracle_pool.rs index 4e30558..75aadda 100644 --- a/src/quoting/oracle_pool.rs +++ b/src/quoting/oracle_pool.rs @@ -4,7 +4,7 @@ use crate::quoting::base_pool::{ MAX_SQRT_RATIO_AT_MAX_TICK_SPACING, MAX_TICK_AT_MAX_TICK_SPACING, MAX_TICK_SPACING, MIN_SQRT_RATIO_AT_MAX_TICK_SPACING, MIN_TICK_AT_MAX_TICK_SPACING, }; -use crate::quoting::types::{NodeKey, Pool, Quote, QuoteParams, Tick}; +use crate::quoting::types::{BlockTimestamp, NodeKey, Pool, Quote, QuoteParams, Tick}; use alloc::vec; use core::ops::Add; use num_traits::ToPrimitive; @@ -87,6 +87,7 @@ impl Pool for OraclePool { type Resources = OraclePoolResources; type State = OraclePoolState; type QuoteError = BasePoolQuoteError; + type Meta = BlockTimestamp; fn get_key(&self) -> NodeKey { self.base_pool.get_key() @@ -101,18 +102,18 @@ impl Pool for OraclePool { fn quote( &self, - params: QuoteParams, + params: QuoteParams, ) -> Result, Self::QuoteError> { - let block_time = params.meta.block.time; + let block_time = params.meta; let pool_time = params .override_state .map_or(self.last_snapshot_time, |os| os.last_snapshot_time); let result = self.base_pool.quote(QuoteParams { sqrt_ratio_limit: params.sqrt_ratio_limit, - meta: params.meta, override_state: params.override_state.map(|s| s.base_pool_state), token_amount: params.token_amount, + meta: (), })?; Ok(Quote { @@ -145,7 +146,7 @@ mod tests { MIN_SQRT_RATIO_AT_MAX_TICK_SPACING, MIN_TICK_AT_MAX_TICK_SPACING, }; use crate::quoting::oracle_pool::OraclePool; - use crate::quoting::types::{Block, Pool, QuoteMeta, QuoteParams, TokenAmount}; + use crate::quoting::types::{Pool, QuoteParams, TokenAmount}; #[test] fn test_max_values() { @@ -181,9 +182,7 @@ mod tests { }, sqrt_ratio_limit: None, override_state: None, - meta: QuoteMeta { - block: Block { number: 1, time: 2 }, - }, + meta: 2, }; let quote = pool.quote(params).expect("Failed to get quote"); @@ -219,9 +218,7 @@ mod tests { }, sqrt_ratio_limit: None, override_state: None, - meta: QuoteMeta { - block: Block { number: 1, time: 2 }, - }, + meta: 2, }; let quote = pool.quote(params).expect("Failed to get quote"); diff --git a/src/quoting/twamm_pool.rs b/src/quoting/twamm_pool.rs index f4bdd48..3ed4e07 100644 --- a/src/quoting/twamm_pool.rs +++ b/src/quoting/twamm_pool.rs @@ -5,6 +5,7 @@ use crate::quoting::base_pool::{ MAX_SQRT_RATIO_AT_MAX_TICK_SPACING, MAX_TICK_AT_MAX_TICK_SPACING, MAX_TICK_SPACING, MIN_SQRT_RATIO_AT_MAX_TICK_SPACING, MIN_TICK_AT_MAX_TICK_SPACING, }; +use crate::quoting::types::BlockTimestamp; use crate::quoting::types::{NodeKey, Pool, Quote, QuoteParams, Tick, TokenAmount}; use alloc::vec; use alloc::vec::Vec; @@ -121,6 +122,7 @@ impl Pool for TwammPool { type Resources = TwammPoolResources; type State = TwammPoolState; type QuoteError = TwammPoolQuoteError; + type Meta = BlockTimestamp; fn get_key(&self) -> NodeKey { self.base_pool.get_key() @@ -137,7 +139,7 @@ impl Pool for TwammPool { fn quote( &self, - params: QuoteParams, + params: QuoteParams, ) -> Result, Self::QuoteError> { let QuoteParams { token_amount, @@ -146,7 +148,7 @@ impl Pool for TwammPool { meta, } = params; - let current_time = meta.block.time; + let current_time = meta; let initial_state = override_state.unwrap_or_else(|| self.get_state()); let mut next_sqrt_ratio = initial_state.base_pool_state.sqrt_ratio; @@ -228,7 +230,7 @@ impl Pool for TwammPool { }, sqrt_ratio_limit: Some(next_sqrt_ratio), override_state: base_pool_state_override, - meta, + meta: (), }) .map_err(TwammPoolQuoteError::BasePoolQuoteError)?; @@ -259,7 +261,7 @@ impl Pool for TwammPool { }, sqrt_ratio_limit: Some(sqrt_ratio_limit), override_state: base_pool_state_override, - meta, + meta: (), }) .map_err(TwammPoolQuoteError::BasePoolQuoteError)?; @@ -295,7 +297,7 @@ impl Pool for TwammPool { .quote(QuoteParams { token_amount, sqrt_ratio_limit, - meta, + meta: (), override_state: base_pool_state_override, }) .map_err(TwammPoolQuoteError::BasePoolQuoteError)?; @@ -334,7 +336,7 @@ mod tests { MAX_SQRT_RATIO_AT_MAX_TICK_SPACING, MIN_SQRT_RATIO_AT_MAX_TICK_SPACING, }; use crate::quoting::twamm_pool::{TwammPool, TwammSaleRateDelta}; - use crate::quoting::types::{Block, Pool, QuoteMeta, QuoteParams, TokenAmount}; + use crate::quoting::types::{Pool, QuoteParams, TokenAmount}; use alloc::vec; const TOKEN0: U256 = U256([1, 0, 0, 0]); @@ -361,12 +363,7 @@ mod tests { token: TOKEN0, }, sqrt_ratio_limit: Some(MIN_SQRT_RATIO), - meta: QuoteMeta { - block: Block { - number: 1, - time: 32, - }, - }, + meta: 32, override_state: None, }); @@ -401,12 +398,7 @@ mod tests { token: TOKEN1, }, sqrt_ratio_limit: Some(MAX_SQRT_RATIO), - meta: QuoteMeta { - block: Block { - number: 1, - time: 32, - }, - }, + meta: 32, override_state: None, }); @@ -441,12 +433,7 @@ mod tests { token: TOKEN0, }, sqrt_ratio_limit: Some(MIN_SQRT_RATIO), - meta: QuoteMeta { - block: Block { - number: 1, - time: 32, - }, - }, + meta: 32, override_state: None, }); @@ -481,12 +468,7 @@ mod tests { token: TOKEN1, }, sqrt_ratio_limit: Some(MAX_SQRT_RATIO), - meta: QuoteMeta { - block: Block { - number: 1, - time: 32, - }, - }, + meta: 32, override_state: None, }); @@ -524,12 +506,7 @@ mod tests { token: TOKEN1, }, sqrt_ratio_limit: Some(MAX_SQRT_RATIO), - meta: QuoteMeta { - block: Block { - number: 1, - time: 32, - }, - }, + meta: 32, override_state: None, }); @@ -570,12 +547,7 @@ mod tests { amount: 1000, token: TOKEN1, }, - meta: QuoteMeta { - block: Block { - number: 1, - time: 32, - }, - }, + meta: 32, sqrt_ratio_limit: None, override_state: None, }) @@ -620,12 +592,7 @@ mod tests { amount: 1000, token: TOKEN1, }, - meta: QuoteMeta { - block: Block { - number: 1, - time: 32, - }, - }, + meta: 32, sqrt_ratio_limit: None, override_state: None, }); @@ -672,12 +639,7 @@ mod tests { amount: 1000, token: TOKEN0, }, - meta: QuoteMeta { - block: Block { - number: 1u64, - time: 32u64, - }, - }, + meta: 32, sqrt_ratio_limit: None, override_state: None, }) @@ -722,12 +684,7 @@ mod tests { amount: 1000, token: TOKEN0, }, - meta: QuoteMeta { - block: Block { - number: 1u64, - time: 32u64, - }, - }, + meta: 32, sqrt_ratio_limit: None, override_state: None, }); @@ -770,12 +727,7 @@ mod tests { token: TOKEN1, }, sqrt_ratio_limit: None, - meta: QuoteMeta { - block: Block { - number: 1u64, - time: 32u64, - }, - }, + meta: 32, override_state: None, }); @@ -817,12 +769,7 @@ mod tests { token: TOKEN0, }, sqrt_ratio_limit: None, - meta: QuoteMeta { - block: Block { - number: 1u64, - time: 32u64, - }, - }, + meta: 32, override_state: None, }); @@ -864,12 +811,7 @@ mod tests { token: TOKEN1, }, sqrt_ratio_limit: None, - meta: QuoteMeta { - block: Block { - number: 1u64, - time: 32u64, - }, - }, + meta: 32, override_state: None, }); @@ -911,12 +853,7 @@ mod tests { token: TOKEN1, }, sqrt_ratio_limit: None, - meta: QuoteMeta { - block: Block { - number: 1u64, - time: 32u64, - }, - }, + meta: 32, override_state: None, }); @@ -958,12 +895,7 @@ mod tests { token: TOKEN0, }, sqrt_ratio_limit: None, - meta: QuoteMeta { - block: Block { - number: 1u64, - time: 32u64, - }, - }, + meta: 32, override_state: None, }); @@ -1005,12 +937,7 @@ mod tests { token: TOKEN0, }, sqrt_ratio_limit: None, - meta: QuoteMeta { - block: Block { - number: 1u64, - time: 32u64, - }, - }, + meta: 32, override_state: None, }); @@ -1056,12 +983,7 @@ mod tests { token: TOKEN0, }, sqrt_ratio_limit: None, - meta: QuoteMeta { - block: Block { - number: 1u64, - time: 32u64, - }, - }, + meta: 32, override_state: None, }); @@ -1107,12 +1029,7 @@ mod tests { token: TOKEN0, }, sqrt_ratio_limit: None, - meta: QuoteMeta { - block: Block { - number: 1u64, - time: 32u64, - }, - }, + meta: 32, override_state: None, }); @@ -1156,12 +1073,7 @@ mod tests { token: TOKEN0, }, sqrt_ratio_limit: Some(to_sqrt_ratio(693147i32).unwrap()), - meta: QuoteMeta { - block: Block { - number: 1u64, - time: 43_200u64, - }, - }, + meta: 43_200, override_state: None, }) .expect("first swap succeeds"); @@ -1173,12 +1085,7 @@ mod tests { token: TOKEN0, }, sqrt_ratio_limit: Some(to_sqrt_ratio(693147).unwrap()), - meta: QuoteMeta { - block: Block { - number: 1u64, - time: 86_400u64, - }, - }, + meta: 86_400, override_state: None, }) .expect("second swap succeeds"); @@ -1190,12 +1097,7 @@ mod tests { token: TOKEN0, }, sqrt_ratio_limit: Some(to_sqrt_ratio(693147).unwrap()), - meta: QuoteMeta { - block: Block { - number: 1u64, - time: 86_400u64, - }, - }, + meta: 86_400, override_state: Some(first.state_after), }) .expect("third is ok"); @@ -1226,12 +1128,7 @@ mod tests { amount: 0, token: TOKEN0, }, - meta: QuoteMeta { - block: Block { - number: 1u64, - time: 60u64, - }, - }, + meta: 60, sqrt_ratio_limit: None, override_state: None, }) @@ -1242,12 +1139,7 @@ mod tests { amount: 0, token: TOKEN0, }, - meta: QuoteMeta { - block: Block { - number: 1u64, - time: 90u64, - }, - }, + meta: 90, sqrt_ratio_limit: None, override_state: None, }) @@ -1259,12 +1151,7 @@ mod tests { amount: 0, token: TOKEN0, }, - meta: QuoteMeta { - block: Block { - number: 1u64, - time: 120u64, - }, - }, + meta: 120, sqrt_ratio_limit: None, override_state: None, }) @@ -1278,12 +1165,7 @@ mod tests { amount: 10u128.pow(18) as i128, token: TOKEN0, }, - meta: QuoteMeta { - block: Block { - number: 1u64, - time: 120u64, - }, - }, + meta: 120, sqrt_ratio_limit: None, override_state: Some(state_after_fully_executed), }) @@ -1297,12 +1179,7 @@ mod tests { token: TOKEN0, amount: 10u128.pow(18) as i128, }, - meta: QuoteMeta { - block: Block { - number: 1u64, - time: 120u64, - }, - }, + meta: (), override_state: Some(state_after_fully_executed.base_pool_state), sqrt_ratio_limit: None, }) @@ -1317,12 +1194,7 @@ mod tests { token: TOKEN1, }, sqrt_ratio_limit: Some(to_sqrt_ratio(693147).unwrap()), - meta: QuoteMeta { - block: Block { - number: 1u64, - time: 120u64, - }, - }, + meta: 120, override_state: Some(state_after_fully_executed), }) .expect("quote token1 with override"); @@ -1336,12 +1208,7 @@ mod tests { token: TOKEN1, amount: 10u128.pow(18) as i128, }, - meta: QuoteMeta { - block: Block { - number: 1u64, - time: 120u64, - }, - }, + meta: (), override_state: Some(fully_executed_twamm.state_after.base_pool_state), sqrt_ratio_limit: Some(to_sqrt_ratio(693147).unwrap()), }) @@ -1372,12 +1239,7 @@ mod tests { amount: (10_000u128 * 10u128.pow(18)) as i128, token: TOKEN0, }, - meta: QuoteMeta { - block: Block { - number: 1u64, - time: 2_040u64, - }, - }, + meta: 2_040, sqrt_ratio_limit: None, override_state: None, }) @@ -1405,12 +1267,7 @@ mod tests { amount: (10_000u128 * 10u128.pow(18)) as i128, token: TOKEN0, }, - meta: QuoteMeta { - block: Block { - number: 2u64, - time: 2_100u64, - }, - }, + meta: 2_100, sqrt_ratio_limit: None, override_state: Some(first_swap.state_after), }) @@ -1455,12 +1312,7 @@ mod tests { token: TOKEN0, }, sqrt_ratio_limit: None, - meta: QuoteMeta { - block: Block { - number: 1u64, - time: 2_040u64, - }, - }, + meta: 2_040, override_state: None, }) .expect("first swap succeeds"); @@ -1472,12 +1324,7 @@ mod tests { token: TOKEN1, }, sqrt_ratio_limit: None, - meta: QuoteMeta { - block: Block { - number: 2u64, - time: 2_100u64, - }, - }, + meta: 2_100, override_state: Some(first_swap.state_after), }) .expect("second swap succeeds"); diff --git a/src/quoting/types.rs b/src/quoting/types.rs index cde441d..6b44ef2 100644 --- a/src/quoting/types.rs +++ b/src/quoting/types.rs @@ -19,19 +19,6 @@ pub struct Tick { pub liquidity_delta: i128, } -// Information about a block necessary for quoting against some pools -#[derive(Clone, Copy)] -pub struct Block { - pub number: u64, - pub time: u64, -} - -// Information about the state of the network necessary for quoting against some kinds of pools (not used by base pools) -#[derive(Clone, Copy)] -pub struct QuoteMeta { - pub block: Block, -} - // Amount and token information. #[derive(Clone, Copy)] pub struct TokenAmount { @@ -41,11 +28,11 @@ pub struct TokenAmount { // Parameters for a quote operation. #[derive(Clone, Copy)] -pub struct QuoteParams { +pub struct QuoteParams { pub token_amount: TokenAmount, pub sqrt_ratio_limit: Option, pub override_state: Option, - pub meta: QuoteMeta, + pub meta: M, } // The result of all pool swaps is some input and output delta @@ -59,10 +46,15 @@ pub struct Quote { pub fees_paid: u128, } -pub trait Pool { +// Commonly used as meta +pub type BlockTimestamp = u64; + +pub trait Pool: Send + Sync { type Resources: Add + Default + Copy; type State: Copy; type QuoteError: Debug; + // Any additional data that is required to compute a quote for this pool, e.g. the block timestamp + type Meta: Copy; fn get_key(&self) -> NodeKey; @@ -70,7 +62,7 @@ pub trait Pool { fn quote( &self, - params: QuoteParams, + params: QuoteParams, ) -> Result, Self::QuoteError>; fn has_liquidity(&self) -> bool;