Perp: More comments on fields and funding computation (#497)

This commit is contained in:
Christian Kamm 2023-03-07 20:53:07 +01:00 committed by GitHub
parent 0625fabf2c
commit 6ee3cb1e19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 14 deletions

View File

@ -202,20 +202,34 @@ pub struct PerpPosition {
/// Amount of quote lots on the EventQueue waiting to be processed
pub taker_quote_lots: i64,
// (Display only)
// Cumulative long funding in base native units
/// 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)
pub cumulative_long_funding: f64,
// (Display only)
// Cumulative short funding in base native units
/// Cumulative short funding in quote native units
/// If the user paid $1 in funding for a short position, this would be -1e6.
///
/// (Display only)
pub cumulative_short_funding: f64,
// (Display only)
// Cumulative maker volume in quote native units
/// Cumulative maker volume in quote native units
///
/// (Display only)
pub maker_volume: u64,
// (Display only)
// Cumulative taker volume in quote native units
/// Cumulative taker volume in quote native units
///
/// (Display only)
pub taker_volume: u64,
// (Display only)
// Cumulative realized pnl in quote native units
/// 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)
pub perp_spot_transfers: i64,
/// The native average entry price for the base lots of the current position.

View File

@ -101,7 +101,7 @@ pub struct PerpMarket {
pub impact_quantity: i64,
/// Current long funding value. Increasing it means that every long base lot
/// needs to pay that amount in funding.
/// needs to pay that amount of quote native in funding.
///
/// PerpPosition uses and tracks it settle funding. Updated by the perp
/// keeper instruction.
@ -275,7 +275,7 @@ impl PerpMarket {
book.bookside(Side::Ask)
.impact_price(self.impact_quantity, now_ts, oracle_price_lots);
let diff_price = match (bid, ask) {
let funding_rate = match (bid, ask) {
(Some(bid), Some(ask)) => {
// calculate mid-market rate
let mid_price = (bid + ask) / 2;
@ -291,7 +291,9 @@ impl PerpMarket {
let diff_ts = I80F48::from_num(now_ts - self.funding_last_updated as u64);
let time_factor = diff_ts / DAY_I80F48;
let base_lot_size = I80F48::from_num(self.base_lot_size);
let funding_delta = index_price * diff_price * base_lot_size * time_factor;
// The number of native quote that one base lot should pay in funding
let funding_delta = index_price * base_lot_size * funding_rate * time_factor;
self.long_funding += funding_delta;
self.short_funding += funding_delta;
@ -309,7 +311,7 @@ impl PerpMarket {
stable_price: self.stable_price().to_bits(),
fees_accrued: self.fees_accrued.to_bits(),
open_interest: self.open_interest,
instantaneous_funding_rate: diff_price.to_bits(),
instantaneous_funding_rate: funding_rate.to_bits(),
});
Ok(())