Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,6 @@ redis = { version = "0.25.4", features = ["r2d2"] }

[dependencies.relayer-core]
git = "https://git@github.com/twilight-project/relayer-core.git"
tag = "v0.2.0"
# tag = "v0.2.0"
branch = "bug-indentify"
# path = "../relayer-core"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE risk_engine_update
DROP COLUMN IF EXISTS total_pending_long_btc,
DROP COLUMN IF EXISTS total_pending_short_btc;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE risk_engine_update
ADD COLUMN total_pending_long_btc DOUBLE PRECISION NOT NULL DEFAULT 0.0,
ADD COLUMN total_pending_short_btc DOUBLE PRECISION NOT NULL DEFAULT 0.0;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE risk_params_update
DROP COLUMN IF EXISTS mm_ratio;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE risk_params_update
ADD COLUMN mm_ratio DOUBLE PRECISION NOT NULL DEFAULT 0.4;
16 changes: 16 additions & 0 deletions src/archiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,19 @@ impl DatabaseArchiver {
relayer::RiskEngineCommand::SetPausePriceFeed(_) => {
("SetPausePriceFeed".to_string(), None, None)
}
relayer::RiskEngineCommand::AddPendingExposure(pt, amt) => (
"AddPendingExposure".to_string(),
Some(pt.clone().into()),
Some(*amt),
),
relayer::RiskEngineCommand::RemovePendingExposure(pt, amt) => (
"RemovePendingExposure".to_string(),
Some(pt.clone().into()),
Some(*amt),
),
relayer::RiskEngineCommand::RecalculateExposure => {
("RecalculateExposure".to_string(), None, None)
}
};

let record = NewRiskEngineUpdate {
Expand All @@ -1040,6 +1053,8 @@ impl DatabaseArchiver {
amount,
total_long_btc: risk_state.total_long_btc,
total_short_btc: risk_state.total_short_btc,
total_pending_long_btc: risk_state.total_pending_long_btc,
total_pending_short_btc: risk_state.total_pending_short_btc,
manual_halt: risk_state.manual_halt,
manual_close_only: risk_state.manual_close_only,
pause_funding: risk_state.pause_funding,
Expand All @@ -1066,6 +1081,7 @@ impl DatabaseArchiver {
max_position_pct: params.max_position_pct,
min_position_btc: params.min_position_btc,
max_leverage: params.max_leverage,
mm_ratio: params.mm_ratio,
timestamp: Utc::now(),
};
self.risk_params_update(record)?;
Expand Down
6 changes: 6 additions & 0 deletions src/database/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2744,6 +2744,8 @@ pub struct RiskEngineUpdateRow {
pub amount: Option<f64>,
pub total_long_btc: f64,
pub total_short_btc: f64,
pub total_pending_long_btc: f64,
pub total_pending_short_btc: f64,
pub manual_halt: bool,
pub manual_close_only: bool,
pub pause_funding: bool,
Expand All @@ -2759,6 +2761,8 @@ pub struct NewRiskEngineUpdate {
pub amount: Option<f64>,
pub total_long_btc: f64,
pub total_short_btc: f64,
pub total_pending_long_btc: f64,
pub total_pending_short_btc: f64,
pub manual_halt: bool,
pub manual_close_only: bool,
pub pause_funding: bool,
Expand Down Expand Up @@ -2795,6 +2799,7 @@ pub struct RiskParamsUpdateRow {
pub min_position_btc: f64,
pub max_leverage: f64,
pub timestamp: DateTime<Utc>,
pub mm_ratio: f64,
}

#[derive(Serialize, Deserialize, Debug, Clone, Insertable)]
Expand All @@ -2805,6 +2810,7 @@ pub struct NewRiskParamsUpdate {
pub max_position_pct: f64,
pub min_position_btc: f64,
pub max_leverage: f64,
pub mm_ratio: f64,
pub timestamp: DateTime<Utc>,
}

Expand Down
3 changes: 3 additions & 0 deletions src/database/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ diesel::table! {
amount -> Nullable<Float8>,
total_long_btc -> Float8,
total_short_btc -> Float8,
total_pending_long_btc -> Float8,
total_pending_short_btc -> Float8,
manual_halt -> Bool,
manual_close_only -> Bool,
pause_funding -> Bool,
Expand All @@ -247,6 +249,7 @@ diesel::table! {
min_position_btc -> Float8,
max_leverage -> Float8,
timestamp -> Timestamptz,
mm_ratio -> Float8,
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/rpc/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,13 +532,19 @@ pub struct AccountSummaryByTAddressResponse {

// --- Market Risk Stats (moved from relayer-core) ---

fn default_mm_ratio() -> f64 {
0.4
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct RiskParams {
pub max_oi_mult: f64,
pub max_net_mult: f64,
pub max_position_pct: f64,
pub min_position_btc: f64,
pub max_leverage: f64,
#[serde(default = "default_mm_ratio")]
pub mm_ratio: f64,
}

impl RiskParams {
Expand All @@ -564,6 +570,10 @@ impl RiskParams {
.unwrap_or("50.0".to_string())
.parse::<f64>()
.unwrap_or(50.0),
mm_ratio: std::env::var("RISK_MM_RATIO")
.unwrap_or("0.4".to_string())
.parse::<f64>()
.unwrap_or(0.4),
}
}
}
Expand All @@ -580,6 +590,8 @@ pub struct MarketRiskStatsResponse {
pub pool_equity_btc: f64,
pub total_long_btc: f64,
pub total_short_btc: f64,
pub total_pending_long_btc: f64,
pub total_pending_short_btc: f64,
pub open_interest_btc: f64,
pub net_exposure_btc: f64,
pub long_pct: f64,
Expand Down
2 changes: 2 additions & 0 deletions src/rpc/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ pub fn compute_market_risk_stats(
pool_equity_btc,
total_long_btc: total_long,
total_short_btc: total_short,
total_pending_long_btc: risk_state.total_pending_long_btc,
total_pending_short_btc: risk_state.total_pending_short_btc,
open_interest_btc: oi_btc,
net_exposure_btc: net_btc,
long_pct,
Expand Down
Loading