pub struct BareTradingFunction {
pub fee: u32,
pub p: Amount,
pub q: Amount,
}
Expand description
The data describing a trading function.
This implicitly treats the trading function as being between assets 1 and 2, without specifying what those assets are, to avoid duplicating data (each asset ID alone is twice the size of the trading function). Which assets correspond to asset 1 and 2 is given by the canonical ordering of the pair.
The trading function phi(R) = p*R_1 + q*R_2
is a CFMM with a constant-sum,
and a fee (0 <= fee < 10_000
) expressed in basis points.
The valuations (p
, q
) for each asset inform the rate (or price) at which these
assets trade against each other.
Fields§
§fee: u32
The fee, expressed in basis points.
The fee percentage of the trading function (gamma
) is normalized
according to its maximum value (10_000 bps, i.e. 100%):
gamma = (10_000 - fee) / 10_000
p: Amount
The valuation for the first asset of the pair, according to canonical ordering.
q: Amount
The valuation for the second asset of the pair, according to canonical ordering.
Implementations§
Source§impl BareTradingFunction
impl BareTradingFunction
pub fn new(fee: u32, p: Amount, q: Amount) -> Self
pub fn flip(&self) -> Self
pub fn fill_input( &self, _reserves: &Reserves, _delta_1: Amount, ) -> Option<(Reserves, Amount)>
Sourcepub fn fill_output(
&self,
reserves: &Reserves,
lambda_2: Amount,
) -> Result<Option<(Reserves, Amount)>>
pub fn fill_output( &self, reserves: &Reserves, lambda_2: Amount, ) -> Result<Option<(Reserves, Amount)>>
Determine the amount of asset 1 that can be filled for a given amount of asset 2,
propagating rounding error to the input amount delta_1
rather than the output amount lambda_2
.
Returns None
if the amount of asset 2 is greater than the reserves of asset 2.
§Errors
This method returns an error if an overflow occurs when computing the fillable amount of asset 1.
Sourcepub fn fill(
&self,
delta_1: Amount,
reserves: &Reserves,
) -> Result<(Amount, Reserves, Amount)>
pub fn fill( &self, delta_1: Amount, reserves: &Reserves, ) -> Result<(Amount, Reserves, Amount)>
Fills a trade of asset 1 to asset 2 against the given reserves, returning the unfilled amount of asset 1, the updated reserves, and the output amount of asset 2.
§Errors
This method errors if an overflow occurs when computing the trade output amount, or the fillable amount of asset 1.
Sourcepub fn effective_price_key_bytes(&self) -> [u8; 32]
pub fn effective_price_key_bytes(&self) -> [u8; 32]
Returns a byte key for this trading function with the property that the lexicographic ordering on byte keys is the same as ordering the corresponding trading functions by effective price.
This allows trading functions to be indexed by price using a key-value store.
Sourcepub fn effective_price_inv(&self) -> U128x128
pub fn effective_price_inv(&self) -> U128x128
Returns the inverse of the effective_price
, in other words,
the exchange rate from asset_1
to asset_2
:
delta_1 * effective_price_inv = lambda_2
Sourcepub fn effective_price(&self) -> U128x128
pub fn effective_price(&self) -> U128x128
Returns the exchange rate from asset_2
to asset_1, inclusive of fees:
lambda_2 * effective_price = delta_1`
Sourcepub fn convert_to_lambda_2(&self, delta_1: U128x128) -> Result<U128x128>
pub fn convert_to_lambda_2(&self, delta_1: U128x128) -> Result<U128x128>
Converts an amount delta_1
into lambda_2
, using the inverse of the effective price.
Sourcepub fn convert_to_delta_1(&self, lambda_2: U128x128) -> Result<U128x128>
pub fn convert_to_delta_1(&self, lambda_2: U128x128) -> Result<U128x128>
Converts an amount of lambda_2
into delta_1
, using the effective price.
Sourcepub fn gamma(&self) -> U128x128
pub fn gamma(&self) -> U128x128
Returns gamma
i.e. the fee percentage.
The fee is expressed in basis points (0 <= fee < 5000), where 5000bps = 50%.
§Bounds:
Since the fee f
is bound by 0 <= < 5_000
, we have 1/2 <= gamma <= 1
.
§Examples:
- A fee of 0% (0 bps) results in a discount factor of 1.
- A fee of 30 bps (30 bps) results in a discount factor of 0.997.
- A fee of 100% (10_000bps) results in a discount factor of 0.
Sourcepub fn compose(&self, _phi: BareTradingFunction) -> BareTradingFunction
👎Deprecated: this method is not yet implemented
pub fn compose(&self, _phi: BareTradingFunction) -> BareTradingFunction
Compose two trading functions together
Trait Implementations§
Source§impl Clone for BareTradingFunction
impl Clone for BareTradingFunction
Source§fn clone(&self) -> BareTradingFunction
fn clone(&self) -> BareTradingFunction
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for BareTradingFunction
impl Debug for BareTradingFunction
Source§impl<'de> Deserialize<'de> for BareTradingFunction
impl<'de> Deserialize<'de> for BareTradingFunction
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl DomainType for BareTradingFunction
impl DomainType for BareTradingFunction
Source§impl From<BareTradingFunction> for BareTradingFunction
impl From<BareTradingFunction> for BareTradingFunction
Source§fn from(value: BareTradingFunction) -> Self
fn from(value: BareTradingFunction) -> Self
Source§impl PartialEq for BareTradingFunction
impl PartialEq for BareTradingFunction
Source§impl Serialize for BareTradingFunction
impl Serialize for BareTradingFunction
impl Eq for BareTradingFunction
impl StructuralPartialEq for BareTradingFunction
Auto Trait Implementations§
impl Freeze for BareTradingFunction
impl RefUnwindSafe for BareTradingFunction
impl Send for BareTradingFunction
impl Sync for BareTradingFunction
impl Unpin for BareTradingFunction
impl UnwindSafe for BareTradingFunction
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Conv for T
impl<T> Conv for T
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.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
key
and return true
if they are equal.§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Source§impl<T> EventDomainType for Twhere
T: DomainType,
<T as DomainType>::Proto: ProtoEvent,
Error: From<<T as TryFrom<<T as DomainType>::Proto>>::Error>,
impl<T> EventDomainType for Twhere
T: DomainType,
<T as DomainType>::Proto: ProtoEvent,
Error: From<<T as TryFrom<<T as DomainType>::Proto>>::Error>,
§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request
§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.