#[repr(C)]
pub struct PerpPosition {
Show 24 fields pub market_index: PerpMarketIndex, pub padding: [u8; 2], pub settle_pnl_limit_window: u32, pub settle_pnl_limit_settled_in_current_window_native: i64, pub base_position_lots: i64, pub quote_position_native: I80F48, pub quote_running_native: i64, pub long_settled_funding: I80F48, pub short_settled_funding: I80F48, pub bids_base_lots: i64, pub asks_base_lots: i64, pub taker_base_lots: i64, pub taker_quote_lots: i64, pub cumulative_long_funding: f64, pub cumulative_short_funding: f64, pub maker_volume: u64, pub taker_volume: u64, pub perp_spot_transfers: i64, pub avg_entry_price_per_base_lot: f64, pub deprecated_realized_trade_pnl_native: I80F48, pub oneshot_settle_pnl_allowance: I80F48, pub recurring_settle_pnl_allowance: i64, pub realized_pnl_for_position_native: I80F48, pub reserved: [u8; 88],
}

Fields§

§market_index: PerpMarketIndex§padding: [u8; 2]§settle_pnl_limit_window: u32

Index of the current settle pnl limit window

§settle_pnl_limit_settled_in_current_window_native: i64

Amount of realized trade pnl and unrealized pnl that was already settled this window.

Will be negative when negative pnl was settled.

Note that this will be adjusted for bookkeeping reasons when the realized_trade settle limitchanges and is not useable for actually tracking how much pnl was settled on balance.

§base_position_lots: i64

Active position size, measured in base lots

§quote_position_native: I80F48

Active position in oracle quote native. At the same time this is 1:1 a settle_token native amount.

Example: Say there’s a perp market on the BTC/USD price using SOL for settlement. The user buys one long contract for $20k, then base = 1, quote = -20k. The price goes to $21k. Now their unsettled pnl is (1 * 21k - 20k) SOL = 1000 SOL. This is because the perp contract arbitrarily decides that each unit of price difference creates 1 SOL worth of settlement. (yes, causing 1 SOL of settlement for each $1 price change implies a lot of extra leverage; likely there should be an extra configurable scaling factor before we use this for cases like that)

§quote_running_native: i64

Tracks what the position is to calculate average entry & break even price

§long_settled_funding: I80F48

Already settled long funding

§short_settled_funding: I80F48

Already settled short funding

§bids_base_lots: i64

Base lots in open bids

§asks_base_lots: i64

Base lots in open asks

§taker_base_lots: i64

Amount of base lots on the EventQueue waiting to be processed

§taker_quote_lots: i64

Amount of quote lots on the EventQueue waiting to be processed

§cumulative_long_funding: f64

Cumulative long funding in quote native units. If the user paid $1 in funding for a long position, this would be 1e6. Beware of the sign!

(Display only)

§cumulative_short_funding: f64

Cumulative short funding in quote native units If the user paid $1 in funding for a short position, this would be -1e6.

(Display only)

§maker_volume: u64

Cumulative maker volume in quote native units

(Display only)

§taker_volume: u64

Cumulative taker volume in quote native units

(Display only)

§perp_spot_transfers: i64

Cumulative number of quote native units transfered from the perp position to the settle token spot position.

For example, if the user settled $1 of positive pnl into their USDC spot position, this would be 1e6.

(Display only)

§avg_entry_price_per_base_lot: f64

The native average entry price for the base lots of the current position. Reset to 0 when the base position reaches or crosses 0.

§deprecated_realized_trade_pnl_native: I80F48

Deprecated field: Amount of pnl that was realized by bringing the base position closer to 0.

§oneshot_settle_pnl_allowance: I80F48

Amount of pnl that can be settled once.

  • The value is signed: a negative number means negative pnl can be settled.
  • A settlement in the right direction will decrease this amount.

Typically added for fees, funding and liquidation.

§recurring_settle_pnl_allowance: i64

Amount of pnl that can be settled in each settle window.

  • Unsigned, the settlement can happen in both directions. Value is >= 0.
  • Previously stored a similar value that was signed, so in migration cases this value can be negative and should be .abs()ed.
  • If this value exceeds the current stable-upnl, it should be decreased, see apply_recurring_settle_pnl_allowance_constraint()

When the base position is reduced, the settle limit contribution from the reduced base position is materialized into this value. When the base position increases, some of the allowance is taken away.

This also gets increased when a liquidator takes over pnl.

§realized_pnl_for_position_native: I80F48

Trade pnl, fees, funding that were added over the current position’s lifetime.

Reset when the position changes sign or goes to zero. Not decreased by settling.

This is tracked for display purposes: this value plus the difference between entry price and current price of the base position is the overall pnl.

§reserved: [u8; 88]

Implementations§

source§

impl PerpPosition

source

pub fn add_taker_trade(&mut self, side: Side, base_lots: i64, quote_lots: i64)

Add taker trade after it has been matched but before it has been process on EventQueue

source

pub fn remove_taker_trade(&mut self, base_change: i64, quote_change: i64)

Remove taker trade after it has been processed on EventQueue

source

pub fn adjust_maker_lots(&mut self, side: Side, base_lots: i64)

source

pub fn is_active(&self) -> bool

source

pub fn is_active_for_market(&self, market_index: PerpMarketIndex) -> bool

source

pub fn base_position_native(&self, market: &PerpMarket) -> I80F48

source

pub fn base_position_lots(&self) -> i64

source

pub fn effective_base_position_lots(&self) -> i64

source

pub fn quote_position_native(&self) -> I80F48

source

pub fn unsettled_funding(&self, perp_market: &PerpMarket) -> I80F48

The amount of funding this account still needs to pay, in native quote

source

pub fn settle_funding(&mut self, perp_market: &PerpMarket)

Move unrealized funding payments into the quote_position

source

pub fn record_trade( &mut self, perp_market: &mut PerpMarket, base_change: i64, quote_change_native: I80F48 ) -> I80F48

Change the base and quote positions as the result of a trade

Returns realized trade pnl

source

pub fn has_open_orders(&self) -> bool

Does the user have any orders on the book?

Note that it’s possible they were matched already: This only becomes false when the fill event is processed or the orders are cancelled.

source

pub fn has_open_taker_fills(&self) -> bool

source

pub fn has_open_orders_or_fills(&self) -> bool

Are there any open orders or fills that haven’t been processed yet?

source

pub fn avg_entry_price(&self, market: &PerpMarket) -> f64

Calculate the average entry price of the position, in native/native units

source

pub fn break_even_price(&self, market: &PerpMarket) -> f64

Calculate the break even price of the position, in native/native units

source

pub fn unsettled_pnl( &self, perp_market: &PerpMarket, price: I80F48 ) -> Result<I80F48>

Calculate the PnL of the position for a given price

source

pub fn update_settle_limit(&mut self, market: &PerpMarket, now_ts: u64)

Updates the perp pnl limit time windowing, resetting the amount of used settle-pnl budget if necessary

source

pub fn settle_limit(&self, market: &PerpMarket) -> (i64, i64)

Returns the (min_pnl, max_pnl) range of quote-native pnl that can be settled this window.

  1. a fraction of the base position stable value, which gives settlement limit equally in both directions
  2. the stored recurring settle allowance, which is mostly allowance from 1. that was materialized when the position was reduced (see recurring_settle_pnl_allowance)
  3. once-only settlement allowance in a single direction (see oneshot_settle_pnl_allowance)
source

pub fn available_settle_limit(&self, market: &PerpMarket) -> (i64, i64)

Returns the (min_pnl, max_pnl) range of quote-native pnl that may still be settled this settle window.

The available settle limit is the settle_limit() adjusted for the amount of limit that was already used up this window.

source

pub fn apply_pnl_settle_limit(&self, market: &PerpMarket, pnl: I80F48) -> I80F48

Given some pnl, applies the pnl settle limit and returns the reduced pnl.

source

pub fn record_settle(&mut self, settled_pnl: I80F48, perp_market: &PerpMarket)

Update the perp position for pnl settlement

If pnl is positive, then that is settled away, deducting from the quote position.

source

pub fn record_trading_fee(&mut self, fee: I80F48)

Update perp position for a maker/taker fee payment

source

pub fn record_liquidation_quote_change(&mut self, change: I80F48)

Adds immediately-settleable realized pnl when a liqor takes over pnl during liquidation

source

pub fn record_liquidation_pnl_takeover( &mut self, change: I80F48, recurring_limit: i64, oneshot_limit: i64 )

Takes over a quote position along with recurring and oneshot settle limit allowance

Trait Implementations§

source§

impl BorshDeserialize for PerpPositionwhere PerpMarketIndex: BorshDeserialize, [u8; 2]: BorshDeserialize, u32: BorshDeserialize, i64: BorshDeserialize, I80F48: BorshDeserialize, f64: BorshDeserialize, u64: BorshDeserialize, [u8; 88]: BorshDeserialize,

source§

fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self, Error>

§

fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>

Deserializes this instance from a given slice of bytes. Updates the buffer to point at the remaining bytes.
§

fn try_from_slice(v: &[u8]) -> Result<Self, Error>

Deserialize this instance from a slice of bytes.
§

fn try_from_reader<R>(reader: &mut R) -> Result<Self, Error>where R: Read,

source§

impl BorshSerialize for PerpPositionwhere PerpMarketIndex: BorshSerialize, [u8; 2]: BorshSerialize, u32: BorshSerialize, i64: BorshSerialize, I80F48: BorshSerialize, f64: BorshSerialize, u64: BorshSerialize, [u8; 88]: BorshSerialize,

source§

fn serialize<W: Write>(&self, writer: &mut W) -> Result<(), Error>

§

fn try_to_vec(&self) -> Result<Vec<u8, Global>, Error>

Serialize this instance into a vector of bytes.
source§

impl Clone for PerpPosition

source§

fn clone(&self) -> PerpPosition

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for PerpPosition

source§

fn fmt(&self, __f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for PerpPosition

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl PartialEq<PerpPosition> for PerpPosition

source§

fn eq(&self, other: &PerpPosition) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Zeroable for PerpPosition

§

fn zeroed() -> Self

source§

impl Copy for PerpPosition

source§

impl Pod for PerpPosition

source§

impl StructuralPartialEq for PerpPosition

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere T: 'a,

§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

§

impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere T: 'a,

§

fn implicit( self, class: Class, constructed: bool, tag: u32 ) -> TaggedParser<'a, Implicit, Self, E>

source§

impl<T> Az for T

source§

fn az<Dst>(self) -> Dstwhere T: Cast<Dst>,

Casts the value.
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<Src, Dst> CastFrom<Src> for Dstwhere Src: Cast<Dst>,

source§

fn cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T> CheckedAs for T

source§

fn checked_as<Dst>(self) -> Option<Dst>where T: CheckedCast<Dst>,

Casts the value.
§

impl<T> CheckedBitPattern for Twhere T: AnyBitPattern,

§

type Bits = T

Self must have the same layout as the specified Bits except for the possible invalid bit patterns being checked during is_valid_bit_pattern.
§

fn is_valid_bit_pattern(_bits: &T) -> bool

If this function returns true, then it must be valid to reinterpret bits as &Self.
source§

impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere Src: CheckedCast<Dst>,

source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
source§

impl<T> DynClone for Twhere T: Clone,

source§

fn __clone_box(&self, _: Private) -> *mut ()

source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

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

const: unstable · 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<Src, Dst> LosslessTryInto<Dst> for Srcwhere Dst: LosslessTryFrom<Src>,

source§

fn lossless_try_into(self) -> Option<Dst>

Performs the conversion.
source§

impl<Src, Dst> LossyInto<Dst> for Srcwhere Dst: LossyFrom<Src>,

source§

fn lossy_into(self) -> Dst

Performs the conversion.
source§

impl<T> OverflowingAs for T

source§

fn overflowing_as<Dst>(self) -> (Dst, bool)where T: OverflowingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere Src: OverflowingCast<Dst>,

source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> SaturatingAs for T

source§

fn saturating_as<Dst>(self) -> Dstwhere T: SaturatingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere Src: SaturatingCast<Dst>,

source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

impl<T> UnwrappedAs for T

source§

fn unwrapped_as<Dst>(self) -> Dstwhere T: UnwrappedCast<Dst>,

Casts the value.
source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dstwhere Src: UnwrappedCast<Dst>,

source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T> WrappingAs for T

source§

fn wrapping_as<Dst>(self) -> Dstwhere T: WrappingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> WrappingCastFrom<Src> for Dstwhere Src: WrappingCast<Dst>,

source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.
§

impl<T> AnyBitPattern for Twhere T: Pod,

§

impl<T> NoUninit for Twhere T: Pod,