Skip to content

Commit

Permalink
add global counters
Browse files Browse the repository at this point in the history
  • Loading branch information
AlastairHolmes committed Nov 13, 2023
1 parent f5ac333 commit 22c855e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 10 deletions.
42 changes: 32 additions & 10 deletions state-chain/amm/src/limit_orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,12 @@ pub(super) struct PoolState<LiquidityProvider> {
/// that are selling asset `Zero` and all those that are selling asset `One` used the SideMap.
/// Therefore there can be positions stored here that don't provide any liquidity.
positions: SideMap<BTreeMap<(SqrtPriceQ64F96, LiquidityProvider), Position>>,
/// Total fees earned over all time
total_fees_earned: SideMap<Amount>,
/// Total of all swap inputs over all time (not including fees)
total_swap_inputs: SideMap<Amount>,
/// Total of all swap outputs over all time
total_swap_outputs: SideMap<Amount>,
}

impl<LiquidityProvider: Clone + Ord> PoolState<LiquidityProvider> {
Expand All @@ -400,6 +406,9 @@ impl<LiquidityProvider: Clone + Ord> PoolState<LiquidityProvider> {
next_pool_instance: 0,
fixed_pools: Default::default(),
positions: Default::default(),
total_fees_earned: Default::default(),
total_swap_inputs: Default::default(),
total_swap_outputs: Default::default(),
})
}

Expand Down Expand Up @@ -494,18 +503,21 @@ impl<LiquidityProvider: Clone + Ord> PoolState<LiquidityProvider> {
let amount_required_to_consume_pool =
SD::input_amount_ceil(fixed_pool.available, price);

let output_amount = if amount_minus_fees >= amount_required_to_consume_pool {
let (output_amount, swapped_amount, fees_taken) = if amount_minus_fees >=
amount_required_to_consume_pool
{
let fixed_pool = fixed_pool_entry.remove();

let fees = mul_div_ceil(
amount_required_to_consume_pool,
U256::from(self.fee_hundredth_pips),
U256::from(ONE_IN_HUNDREDTH_PIPS - self.fee_hundredth_pips),
); /* Will not overflow as fee_hundredth_pips <= ONE_IN_HUNDREDTH_PIPS / 2 */

// Cannot underflow as amount_minus_fees >= amount_required_to_consume_pool
amount -= amount_required_to_consume_pool +
mul_div_ceil(
amount_required_to_consume_pool,
U256::from(self.fee_hundredth_pips),
U256::from(ONE_IN_HUNDREDTH_PIPS - self.fee_hundredth_pips),
); /* Will not overflow as fee_hundredth_pips <= ONE_IN_HUNDREDTH_PIPS / 2 */

fixed_pool.available
amount -= amount_required_to_consume_pool + fees;

(fixed_pool.available, amount_required_to_consume_pool, fees)
} else {
let initial_output_amount = SD::output_amount_floor(amount_minus_fees, price);

Expand Down Expand Up @@ -538,14 +550,24 @@ impl<LiquidityProvider: Clone + Ord> PoolState<LiquidityProvider> {

fixed_pool.percent_remaining = next_percent_remaining;
fixed_pool.available -= output_amount;

let fees_taken = amount - amount_minus_fees;
amount = Amount::zero();

output_amount
(output_amount, amount_minus_fees, fees_taken)
};

self.total_swap_inputs[SD::INPUT_SIDE] =
self.total_swap_inputs[SD::INPUT_SIDE].saturating_add(swapped_amount);
self.total_fees_earned[SD::INPUT_SIDE] =
self.total_fees_earned[SD::INPUT_SIDE].saturating_add(fees_taken);

total_output_amount = total_output_amount.saturating_add(output_amount);
}

self.total_swap_outputs[!SD::INPUT_SIDE] =
self.total_swap_outputs[!SD::INPUT_SIDE].saturating_add(total_output_amount);

(total_output_amount, amount)
}

Expand Down
3 changes: 3 additions & 0 deletions state-chain/amm/src/limit_orders/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ impl<LiquidityProvider: Ord> From<PoolState<LiquidityProvider>>
})
.collect()
}),
total_fees_earned: Default::default(),
total_swap_inputs: Default::default(),
total_swap_outputs: Default::default(),
}
}
}
17 changes: 17 additions & 0 deletions state-chain/amm/src/range_orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ pub struct PoolState<LiquidityProvider> {
/// ticks where liquidity_gross is non-zero.
liquidity_map: BTreeMap<Tick, TickDelta>,
positions: BTreeMap<(LiquidityProvider, Tick, Tick), Position>,
/// Total fees earned over all time
total_fees_earned: SideMap<Amount>,
/// Total of all swap inputs over all time (not including fees)
total_swap_inputs: SideMap<Amount>,
/// Total of all swap outputs over all time
total_swap_outputs: SideMap<Amount>,
}

pub(super) trait SwapDirection: crate::common::SwapDirection {
Expand Down Expand Up @@ -471,6 +477,9 @@ impl<LiquidityProvider: Clone + Ord> PoolState<LiquidityProvider> {
]
.into(),
positions: Default::default(),
total_fees_earned: Default::default(),
total_swap_inputs: Default::default(),
total_swap_outputs: Default::default(),
})
}

Expand Down Expand Up @@ -823,6 +832,11 @@ impl<LiquidityProvider: Clone + Ord> PoolState<LiquidityProvider> {
)
};

self.total_swap_inputs[SD::INPUT_SIDE] =
self.total_swap_inputs[SD::INPUT_SIDE].saturating_add(amount_swapped);
self.total_fees_earned[SD::INPUT_SIDE] =
self.total_fees_earned[SD::INPUT_SIDE].saturating_add(fees);

// TODO: Prove this does not underflow
amount -= amount_swapped + fees;

Expand Down Expand Up @@ -861,6 +875,9 @@ impl<LiquidityProvider: Clone + Ord> PoolState<LiquidityProvider> {
}
}

self.total_swap_outputs[!SD::INPUT_SIDE] =
self.total_swap_outputs[!SD::INPUT_SIDE].saturating_add(total_output_amount);

(total_output_amount, amount)
}

Expand Down
3 changes: 3 additions & 0 deletions state-chain/amm/src/range_orders/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ impl<LiquidityProvider: Ord> From<PoolState<LiquidityProvider>>
)
})
.collect(),
total_fees_earned: Default::default(),
total_swap_inputs: Default::default(),
total_swap_outputs: Default::default(),
}
}
}

0 comments on commit 22c855e

Please sign in to comment.