Skip to main content

NamedEngine

Struct NamedEngine 

Source
pub struct NamedEngine {
    pub engine: RiskEngine,
    pub accounts: BTreeMap<String, u16>,
    pub current_slot: u64,
    pub last_oracle_price: u64,
    pub last_funding_rate: i64,
    /* private fields */
}
Expand description

Human-readable wrapper around the Percolator RiskEngine.

Maps string account names to u16 indices, tracks oracle/slot/funding state, and provides ergonomic methods for deposits, withdrawals, trades, and cranks.

Fields§

§engine: RiskEngine§accounts: BTreeMap<String, u16>§current_slot: u64§last_oracle_price: u64§last_funding_rate: i64

Implementations§

Source§

impl NamedEngine

Source

pub fn new(params: RiskParams, init_slot: u64, init_oracle_price: u64) -> Self

Create a new engine with the given risk parameters, slot, and oracle price.

Source

pub fn resolve(&self, name: &str) -> Result<u16, EngineError>

Resolve an account name to its index. Returns error if not found.

Source

pub fn deposit(&mut self, name: &str, amount: u128) -> Result<(), EngineError>

Deposit collateral into an account. Creates the account if it doesn’t exist.

Source

pub fn withdraw(&mut self, name: &str, amount: u128) -> Result<(), EngineError>

Withdraw collateral from an account. Fails if the account would breach margin.

Source

pub fn trade( &mut self, long: &str, short: &str, size_q: i128, exec_price: u64, ) -> Result<(), EngineError>

Execute a trade between two accounts. size_q is in POS_SCALE units.

Source

pub fn crank( &mut self, oracle_price: u64, slot: u64, ) -> Result<CrankOutcome, EngineError>

Run the keeper crank at the given oracle price and slot.

Source

pub fn liquidate(&mut self, name: &str) -> Result<bool, EngineError>

Attempt to liquidate an account. Returns true if liquidation occurred.

Source

pub fn settle(&mut self, name: &str) -> Result<(), EngineError>

Settle an account’s PnL against the vault.

Source

pub fn set_oracle(&mut self, oracle_price: u64) -> Result<(), EngineError>

Source

pub fn set_slot(&mut self, slot: u64)

Source

pub fn set_funding_rate(&mut self, rate: i64)

Source

pub fn snapshot(&self) -> EngineSnapshot

Capture a complete snapshot of the engine state.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.