Skip to content

Commit 41cc66f

Browse files
committed
add subassign and addassign
1 parent 76a701a commit 41cc66f

File tree

6 files changed

+115
-29
lines changed

6 files changed

+115
-29
lines changed

src/quoting/base_pool.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::quoting::util::{
66
approximate_number_of_tick_spacings_crossed, construct_sorted_ticks, ConstructSortedTicksError,
77
};
88
use alloc::vec::Vec;
9-
use core::ops::{Add, AddAssign};
9+
use core::ops::{Add, AddAssign, Sub, SubAssign};
1010
use num_traits::Zero;
1111

1212
// Resources consumed during any swap execution.
@@ -34,6 +34,23 @@ impl Add for BasePoolResources {
3434
}
3535
}
3636

37+
impl SubAssign for BasePoolResources {
38+
fn sub_assign(&mut self, rhs: Self) {
39+
self.no_override_price_change -= rhs.no_override_price_change;
40+
self.initialized_ticks_crossed -= rhs.initialized_ticks_crossed;
41+
self.tick_spacings_crossed -= rhs.tick_spacings_crossed;
42+
}
43+
}
44+
45+
impl Sub for BasePoolResources {
46+
type Output = Self;
47+
48+
fn sub(mut self, rhs: Self) -> Self::Output {
49+
self -= rhs;
50+
self
51+
}
52+
}
53+
3754
pub const FULL_RANGE_TICK_SPACING: u32 = 0;
3855
pub const MAX_TICK_SPACING: u32 = 698605;
3956

src/quoting/full_range_pool.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::math::swap::{compute_step, is_price_increasing, ComputeStepError};
22
use crate::math::tick::{MAX_SQRT_RATIO, MIN_SQRT_RATIO};
33
use crate::math::uint::U256;
44
use crate::quoting::types::{NodeKey, Pool, Quote, QuoteParams};
5-
use core::ops::{Add, AddAssign};
5+
use core::ops::{Add, AddAssign, Sub, SubAssign};
66
use num_traits::Zero;
77

88
// Resources consumed during any swap execution in a full range pool.
@@ -26,6 +26,21 @@ impl Add for FullRangePoolResources {
2626
}
2727
}
2828

29+
impl SubAssign for FullRangePoolResources {
30+
fn sub_assign(&mut self, rhs: Self) {
31+
self.no_override_price_change -= rhs.no_override_price_change;
32+
}
33+
}
34+
35+
impl Sub for FullRangePoolResources {
36+
type Output = Self;
37+
38+
fn sub(mut self, rhs: Self) -> Self::Output {
39+
self -= rhs;
40+
self
41+
}
42+
}
43+
2944
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
3045
pub enum FullRangePoolQuoteError {
3146
InvalidToken,

src/quoting/mev_resist_pool.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::math::swap::{amount_before_fee, compute_fee};
22
use crate::math::tick::{approximate_sqrt_ratio_to_tick, FULL_RANGE_TICK_SPACING};
33
use crate::quoting::base_pool::{BasePool, BasePoolQuoteError, BasePoolResources, BasePoolState};
44
use crate::quoting::types::{BlockTimestamp, NodeKey, Pool, Quote, QuoteParams};
5-
use core::ops::{Add, AddAssign};
5+
use core::ops::{Add, AddAssign, Sub, SubAssign};
66

77
// Resources consumed during any swap execution in a full range pool.
88
#[derive(Clone, Copy, Default, Debug, PartialEq, Eq)]
@@ -27,6 +27,22 @@ impl Add for MEVResistPoolResources {
2727
}
2828
}
2929

30+
impl SubAssign for MEVResistPoolResources {
31+
fn sub_assign(&mut self, rhs: Self) {
32+
self.state_update_count -= rhs.state_update_count;
33+
self.base_pool_resources -= rhs.base_pool_resources;
34+
}
35+
}
36+
37+
impl Sub for MEVResistPoolResources {
38+
type Output = Self;
39+
40+
fn sub(mut self, rhs: Self) -> Self::Output {
41+
self -= rhs;
42+
self
43+
}
44+
}
45+
3046
#[derive(Clone, Debug, PartialEq, Eq)]
3147
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
3248
pub struct MEVResistPool {
@@ -142,7 +158,7 @@ impl Pool for MEVResistPool {
142158
.map_or(self.last_update_time, |mrps| mrps.last_update_time);
143159

144160
// if the time is updated, fees are accumulated to the current liquidity providers
145-
// this is at least 2 additional SSTOREs
161+
// this causes up to 3 additional SSTOREs (~15k gas)
146162
let state_update_count = if pool_time != current_time { 1 } else { 0 };
147163

148164
if fixed_point_additional_fee == 0 {
@@ -167,12 +183,10 @@ impl Pool for MEVResistPool {
167183

168184
if params.token_amount.amount >= 0 {
169185
// exact input, remove the additional fee from the output
170-
calculated_amount -=
171-
compute_fee(calculated_amount as u128, fixed_point_additional_fee);
186+
calculated_amount -= compute_fee(calculated_amount, fixed_point_additional_fee);
172187
} else {
173-
let input_amount_fee: u128 =
174-
compute_fee(calculated_amount as u128, pool_config.fee);
175-
let input_amount = (calculated_amount as u128) - input_amount_fee;
188+
let input_amount_fee: u128 = compute_fee(calculated_amount, pool_config.fee);
189+
let input_amount = calculated_amount - input_amount_fee;
176190

177191
if let Some(bf) = amount_before_fee(input_amount, fixed_point_additional_fee) {
178192
let fee = bf - input_amount;

src/quoting/oracle_pool.rs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::quoting::full_range_pool::{
55
FullRangePool, FullRangePoolQuoteError, FullRangePoolResources, FullRangePoolState,
66
};
77
use crate::quoting::types::{BlockTimestamp, NodeKey, Pool, Quote, QuoteParams};
8-
use core::ops::Add;
8+
use core::ops::{Add, AddAssign, Sub, SubAssign};
99

1010
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1111
pub struct OraclePoolState {
@@ -19,15 +19,35 @@ pub struct OraclePoolResources {
1919
pub snapshots_written: u32,
2020
}
2121

22+
impl AddAssign for OraclePoolResources {
23+
fn add_assign(&mut self, rhs: Self) {
24+
self.full_range_pool_resources += rhs.full_range_pool_resources;
25+
self.snapshots_written += rhs.snapshots_written;
26+
}
27+
}
28+
2229
impl Add for OraclePoolResources {
2330
type Output = OraclePoolResources;
2431

25-
fn add(self, rhs: Self) -> Self::Output {
26-
OraclePoolResources {
27-
full_range_pool_resources: self.full_range_pool_resources
28-
+ rhs.full_range_pool_resources,
29-
snapshots_written: self.snapshots_written + rhs.snapshots_written,
30-
}
32+
fn add(mut self, rhs: Self) -> Self::Output {
33+
self += rhs;
34+
self
35+
}
36+
}
37+
38+
impl SubAssign for OraclePoolResources {
39+
fn sub_assign(&mut self, rhs: Self) {
40+
self.full_range_pool_resources -= rhs.full_range_pool_resources;
41+
self.snapshots_written -= rhs.snapshots_written;
42+
}
43+
}
44+
45+
impl Sub for OraclePoolResources {
46+
type Output = Self;
47+
48+
fn sub(mut self, rhs: Self) -> Self::Output {
49+
self -= rhs;
50+
self
3151
}
3252
}
3353

src/quoting/twamm_pool.rs

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::quoting::full_range_pool::{
77
use crate::quoting::types::{BlockTimestamp, Config};
88
use crate::quoting::types::{NodeKey, Pool, Quote, QuoteParams, TokenAmount};
99
use alloc::vec::Vec;
10-
use core::ops::Add;
10+
use core::ops::{Add, AddAssign, Sub, SubAssign};
1111
use num_traits::{ToPrimitive, Zero};
1212

1313
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -29,19 +29,39 @@ pub struct TwammPoolResources {
2929
pub virtual_orders_executed: u32,
3030
}
3131

32+
impl AddAssign for TwammPoolResources {
33+
fn add_assign(&mut self, rhs: Self) {
34+
self.full_range_pool_resources += rhs.full_range_pool_resources;
35+
self.virtual_order_delta_times_crossed += rhs.virtual_order_delta_times_crossed;
36+
self.virtual_order_seconds_executed += rhs.virtual_order_seconds_executed;
37+
self.virtual_orders_executed += rhs.virtual_orders_executed;
38+
}
39+
}
40+
3241
impl Add for TwammPoolResources {
3342
type Output = TwammPoolResources;
3443

35-
fn add(self, rhs: Self) -> Self::Output {
36-
TwammPoolResources {
37-
full_range_pool_resources: self.full_range_pool_resources
38-
+ rhs.full_range_pool_resources,
39-
virtual_order_delta_times_crossed: self.virtual_order_delta_times_crossed
40-
+ rhs.virtual_order_delta_times_crossed,
41-
virtual_order_seconds_executed: self.virtual_order_seconds_executed
42-
+ rhs.virtual_order_seconds_executed,
43-
virtual_orders_executed: self.virtual_orders_executed + rhs.virtual_orders_executed,
44-
}
44+
fn add(mut self, rhs: Self) -> Self::Output {
45+
self += rhs;
46+
self
47+
}
48+
}
49+
50+
impl SubAssign for TwammPoolResources {
51+
fn sub_assign(&mut self, rhs: Self) {
52+
self.full_range_pool_resources -= rhs.full_range_pool_resources;
53+
self.virtual_order_delta_times_crossed -= rhs.virtual_order_delta_times_crossed;
54+
self.virtual_order_seconds_executed -= rhs.virtual_order_seconds_executed;
55+
self.virtual_orders_executed -= rhs.virtual_orders_executed;
56+
}
57+
}
58+
59+
impl Sub for TwammPoolResources {
60+
type Output = TwammPoolResources;
61+
62+
fn sub(mut self, rhs: Self) -> Self::Output {
63+
self -= rhs;
64+
self
4565
}
4666
}
4767

src/quoting/types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::math::uint::U256;
22
use core::fmt::Debug;
3-
use core::ops::Add;
3+
use core::ops::{Add, Sub};
44

55
// Unique key identifying the pool.
66
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
@@ -102,7 +102,7 @@ pub struct Quote<R, S> {
102102
pub type BlockTimestamp = u64;
103103

104104
pub trait Pool: Send + Sync + Debug + Clone + PartialEq + Eq {
105-
type Resources: Add + Debug + Default + Copy + PartialEq + Eq;
105+
type Resources: Add + Sub + Debug + Default + Copy + PartialEq + Eq;
106106
type State: Debug + Copy + PartialEq + Eq;
107107
type QuoteError: Debug + Copy;
108108
// Any additional data that is required to compute a quote for this pool, e.g. the block timestamp

0 commit comments

Comments
 (0)