pub trait ProtocolSim:
Debug
+ Send
+ Sync
+ 'static
+ Serialize
+ Deserialize {
// Required methods
fn fee(&self) -> f64;
fn spot_price(
&self,
base: &Token,
quote: &Token,
) -> Result<f64, SimulationError>;
fn get_amount_out(
&self,
amount_in: BigUint,
token_in: &Token,
token_out: &Token,
) -> Result<GetAmountOutResult, SimulationError>;
fn get_limits(
&self,
sell_token: Bytes,
buy_token: Bytes,
) -> Result<(BigUint, BigUint), SimulationError>;
fn delta_transition(
&mut self,
delta: ProtocolStateDelta,
tokens: &HashMap<Bytes, Token>,
balances: &Balances,
) -> Result<(), TransitionError<String>>;
fn clone_box(&self) -> Box<dyn ProtocolSim>;
fn as_any(&self) -> &dyn Any;
fn as_any_mut(&mut self) -> &mut dyn Any;
fn eq(&self, other: &dyn ProtocolSim) -> bool;
// Provided methods
fn query_pool_swap(
&self,
params: &QueryPoolSwapParams,
) -> Result<PoolSwap, SimulationError> { ... }
fn as_indicatively_priced(
&self,
) -> Result<&dyn IndicativelyPriced, SimulationError> { ... }
}Expand description
ProtocolSim trait This trait defines the methods that a protocol state must implement in order to be used in the trade simulation.
Required Methods§
Sourcefn fee(&self) -> f64
fn fee(&self) -> f64
Returns the fee of the protocol as ratio
E.g. if the fee is 1%, the value returned would be 0.01.
§Panics
Currently panic for protocols with asymmetric fees (e.g. Rocketpool, Uniswap V4), where a single fee value cannot represent the protocol’s fee structure.
Sourcefn spot_price(
&self,
base: &Token,
quote: &Token,
) -> Result<f64, SimulationError>
fn spot_price( &self, base: &Token, quote: &Token, ) -> Result<f64, SimulationError>
Returns the protocol’s current spot buy price for base in units of quote.
The returned price is the amount of quote required to buy exactly 1 unit of base,
accounting for the protocol fee (i.e. price = pre_fee_price / (1.0 - fee))
and assuming zero slippage (i.e., a negligibly small trade size).
§Arguments
base- the token being priced (what you buy). For BTC/USDT, BTC is the base token.quote- the token used to price (pay) forbase. For BTC/USDT, USDT is the quote token.
§Examples
If the BTC/USDT is trading at 1000 with a 20% fee, this returns 1000 / (1.0 - 0.20) = 1250
Sourcefn get_amount_out(
&self,
amount_in: BigUint,
token_in: &Token,
token_out: &Token,
) -> Result<GetAmountOutResult, SimulationError>
fn get_amount_out( &self, amount_in: BigUint, token_in: &Token, token_out: &Token, ) -> Result<GetAmountOutResult, SimulationError>
Returns the amount out given an amount in and input/output tokens.
§Arguments
amount_in- The amount in of the input token.token_in- The input token ERC20 token.token_out- The output token ERC20 token.
§Returns
A Result containing a GetAmountOutResult struct on success or a
SimulationError on failure.
Sourcefn get_limits(
&self,
sell_token: Bytes,
buy_token: Bytes,
) -> Result<(BigUint, BigUint), SimulationError>
fn get_limits( &self, sell_token: Bytes, buy_token: Bytes, ) -> Result<(BigUint, BigUint), SimulationError>
Computes the maximum amount that can be traded between two tokens.
This function calculates the maximum possible trade amount between two tokens, taking into account the protocol’s specific constraints and mechanics. The implementation details vary by protocol - for example:
- For constant product AMMs (like Uniswap V2), this is based on available reserves
- For concentrated liquidity AMMs (like Uniswap V3), this considers liquidity across tick ranges
Note: if there are no limits, the returned amount will be a “soft” limit, meaning that the actual amount traded could be higher but it’s advised to not exceed it.
§Arguments
sell_token- The address of the token being soldbuy_token- The address of the token being bought
§Returns
Ok((BigUint, BigUint))- A tuple containing:- First element: The maximum input amount (sell_token)
- Second element: The maximum output amount (buy_token)
For let res = get_limits(...), the valid input domain for get_amount_out is [0, res.0].
Err(SimulationError)- If any unexpected error occurs
Sourcefn delta_transition(
&mut self,
delta: ProtocolStateDelta,
tokens: &HashMap<Bytes, Token>,
balances: &Balances,
) -> Result<(), TransitionError<String>>
fn delta_transition( &mut self, delta: ProtocolStateDelta, tokens: &HashMap<Bytes, Token>, balances: &Balances, ) -> Result<(), TransitionError<String>>
Decodes and applies a protocol state delta to the state
Will error if the provided delta is missing any required attributes or if any of the attribute values cannot be decoded.
§Arguments
delta- AProtocolStateDeltafrom the tycho indexer
§Returns
Result<(), TransitionError<String>>- AResultcontaining()on success or aTransitionErroron failure.
Sourcefn clone_box(&self) -> Box<dyn ProtocolSim>
fn clone_box(&self) -> Box<dyn ProtocolSim>
Clones the protocol state as a trait object.
This allows the state to be cloned when it is being used as a Box<dyn ProtocolSim>.
Sourcefn as_any_mut(&mut self) -> &mut dyn Any
fn as_any_mut(&mut self) -> &mut dyn Any
Allows downcasting of the trait object to its mutable underlying type.
Sourcefn eq(&self, other: &dyn ProtocolSim) -> bool
fn eq(&self, other: &dyn ProtocolSim) -> bool
Compares two protocol states for equality. This method must be implemented to define how two protocol states are considered equal (used for tests).
Provided Methods§
Sourcefn query_pool_swap(
&self,
params: &QueryPoolSwapParams,
) -> Result<PoolSwap, SimulationError>
fn query_pool_swap( &self, params: &QueryPoolSwapParams, ) -> Result<PoolSwap, SimulationError>
Calculates the swap volume required to achieve the provided goal when trading against this pool.
This method will branch towards different behaviors based on SwapConstraint enum. Please refer to its documentation for further details on each behavior.
In short, the current two options are:
- Maximize your trade while respecting a trade limit price: SwapConstraint::TradeLimitPrice
- Move the pool price to a target price: SwapConstraint::PoolTargetPrice
§Arguments
params- A QueryPoolSwapParams struct containing the inputs for this method.
§Returns
Ok(PoolSwap)- APoolSwapstruct containing the amounts to be traded and the state of the pool after trading.Err(SimulationError)- If:- The calculation encounters numerical issues
- The method is not implemented for this protocol
Sourcefn as_indicatively_priced(
&self,
) -> Result<&dyn IndicativelyPriced, SimulationError>
fn as_indicatively_priced( &self, ) -> Result<&dyn IndicativelyPriced, SimulationError>
Cast as IndicativelyPriced. This is necessary for RFQ protocols
Trait Implementations§
Source§impl Clone for Box<dyn ProtocolSim>
impl Clone for Box<dyn ProtocolSim>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more