Skip to content
Open
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
23 changes: 23 additions & 0 deletions crates/gem_hypercore/src/core/actions/agent/update_leverage.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use primitives::PerpetualMarginType;

// IMPORTANT: Field order matters for msgpack serialization and hash calculation
// Do not change field order unless you know the exact order in Python SDK.
#[derive(Clone, serde::Serialize)]
Expand All @@ -18,4 +20,25 @@ impl UpdateLeverage {
leverage,
}
}

pub fn from_margin_type(asset: u32, margin_type: &PerpetualMarginType, leverage: u8) -> Self {
Self::new(asset, *margin_type == PerpetualMarginType::Cross, leverage)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_from_margin_type_cross() {
let leverage = UpdateLeverage::from_margin_type(1, &PerpetualMarginType::Cross, 10);
assert!(leverage.is_cross);
}

#[test]
fn test_from_margin_type_isolated() {
let leverage = UpdateLeverage::from_margin_type(1, &PerpetualMarginType::Isolated, 10);
assert!(!leverage.is_cross);
}
}
21 changes: 21 additions & 0 deletions crates/gem_hypercore/src/models/info_request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use serde::Serialize;

#[derive(Serialize)]
pub struct InfoPayload<'a> {
#[serde(rename = "type")]
request_type: &'a str,
user: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
dex: Option<&'a str>,
}

impl<'a> InfoPayload<'a> {
pub fn new(request_type: &'a str, user: &'a str) -> Self {
Self { request_type, user, dex: None }
}

pub fn dex(mut self, dex: Option<&'a str>) -> Self {
self.dex = dex;
self
}
}
2 changes: 2 additions & 0 deletions crates/gem_hypercore/src/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
pub mod action;
pub mod balance;
pub mod candlestick;
pub mod info_request;
pub mod metadata;
pub mod order;
pub mod perp_dex;
pub mod portfolio;
pub mod position;
pub mod referral;
Expand Down
8 changes: 8 additions & 0 deletions crates/gem_hypercore/src/models/perp_dex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PerpDex {
pub name: String,
pub is_active: Option<bool>,
}
186 changes: 186 additions & 0 deletions crates/gem_hypercore/src/provider/hip3_mapper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
use std::collections::BTreeMap;

use primitives::{
chart::ChartDateValue,
perpetual::{PerpetualBalance, PerpetualPositionsSummary},
portfolio::{PerpetualAccountSummary, PerpetualPortfolio, PerpetualPortfolioTimeframeData},
};

use crate::models::position::AssetPositions;

const HIP3_PERP_ASSET_OFFSET: u32 = 100_000;
const HIP3_PERP_ASSET_STRIDE: u32 = 10_000;

pub fn perp_asset_index(perp_dex_index: u32, meta_index: u32) -> u32 {
if perp_dex_index == 0 {
meta_index
} else {
HIP3_PERP_ASSET_OFFSET + perp_dex_index * HIP3_PERP_ASSET_STRIDE + meta_index
}
}

pub fn map_account_summary_aggregate(positions: &[AssetPositions]) -> PerpetualAccountSummary {
let account_value: f64 = positions.iter().map(|p| p.margin_summary.account_value.parse().unwrap_or(0.0)).sum();
let total_ntl_pos: f64 = positions.iter().map(|p| p.margin_summary.total_ntl_pos.parse().unwrap_or(0.0)).sum();
let total_margin_used: f64 = positions.iter().map(|p| p.margin_summary.total_margin_used.parse().unwrap_or(0.0)).sum();
let unrealized_pnl: f64 = positions
.iter()
.flat_map(|p| &p.asset_positions)
.map(|p| p.position.unrealized_pnl.parse().unwrap_or(0.0))
.sum();

let account_leverage = if account_value > 0.0 { total_ntl_pos / account_value } else { 0.0 };
let margin_usage = if account_value > 0.0 { total_margin_used / account_value } else { 0.0 };

PerpetualAccountSummary {
account_value,
account_leverage,
margin_usage,
unrealized_pnl,
}
}

pub fn merge_positions_summaries(summaries: Vec<PerpetualPositionsSummary>) -> PerpetualPositionsSummary {
let (positions, balance) = summaries.into_iter().fold(
(Vec::new(), PerpetualBalance { available: 0.0, reserved: 0.0, withdrawable: 0.0 }),
|(mut acc_pos, mut acc_bal), summary| {
acc_pos.extend(summary.positions);
acc_bal.available += summary.balance.available;
acc_bal.reserved += summary.balance.reserved;
acc_bal.withdrawable += summary.balance.withdrawable;
(acc_pos, acc_bal)
},
);
PerpetualPositionsSummary { positions, balance }
}

pub fn merge_perpetual_portfolios(portfolios: Vec<PerpetualPortfolio>, account_summary: Option<PerpetualAccountSummary>) -> PerpetualPortfolio {
let mut day = Vec::new();
let mut week = Vec::new();
let mut month = Vec::new();
let mut all_time = Vec::new();

for portfolio in portfolios {
day.extend(portfolio.day);
week.extend(portfolio.week);
month.extend(portfolio.month);
all_time.extend(portfolio.all_time);
}

PerpetualPortfolio {
day: merge_portfolio_timeframes(day),
week: merge_portfolio_timeframes(week),
month: merge_portfolio_timeframes(month),
all_time: merge_portfolio_timeframes(all_time),
account_summary,
}
}

fn merge_portfolio_timeframes(values: Vec<PerpetualPortfolioTimeframeData>) -> Option<PerpetualPortfolioTimeframeData> {
if values.is_empty() {
return None;
}

let mut account_value_histories = Vec::new();
let mut pnl_histories = Vec::new();
let mut volume = 0.0;

for value in values {
account_value_histories.push(value.account_value_history);
pnl_histories.push(value.pnl_history);
volume += value.volume;
}

Some(PerpetualPortfolioTimeframeData {
account_value_history: merge_chart_histories(account_value_histories),
pnl_history: merge_chart_histories(pnl_histories),
volume,
})
}

fn merge_chart_histories(values: Vec<Vec<ChartDateValue>>) -> Vec<ChartDateValue> {
let mut grouped = BTreeMap::new();
for history in values {
for point in history {
let entry = grouped.entry(point.date).or_insert(0.0);
*entry += point.value;
}
}

grouped.into_iter().map(|(date, value)| ChartDateValue { date, value }).collect()
}

#[cfg(test)]
mod tests {
use super::*;
use primitives::portfolio::PerpetualAccountSummary;

#[test]
fn test_perp_asset_index() {
assert_eq!(perp_asset_index(0, 0), 0);
assert_eq!(perp_asset_index(0, 5), 5);
assert_eq!(perp_asset_index(1, 0), 110_000);
assert_eq!(perp_asset_index(1, 3), 110_003);
assert_eq!(perp_asset_index(2, 0), 120_000);
assert_eq!(perp_asset_index(2, 7), 120_007);
}

#[test]
fn test_map_account_summary_aggregate() {
let positions = vec![AssetPositions::mock(), AssetPositions::mock()];
let summary = map_account_summary_aggregate(&positions);

assert_eq!(summary.account_value, 20000.0);
assert_eq!(summary.account_leverage, 0.5);
assert_eq!(summary.margin_usage, 0.2);
assert_eq!(summary.unrealized_pnl, 0.0);
}

#[test]
fn test_merge_chart_histories() {
use chrono::{TimeZone, Utc};

let d1 = Utc.with_ymd_and_hms(2024, 1, 1, 0, 0, 0).unwrap();
let d2 = Utc.with_ymd_and_hms(2024, 1, 2, 0, 0, 0).unwrap();
let d3 = Utc.with_ymd_and_hms(2024, 1, 3, 0, 0, 0).unwrap();

let histories = vec![
vec![ChartDateValue { date: d1, value: 100.0 }, ChartDateValue { date: d2, value: 200.0 }],
vec![ChartDateValue { date: d1, value: 50.0 }, ChartDateValue { date: d3, value: 300.0 }],
];

let merged = merge_chart_histories(histories);
assert_eq!(merged.len(), 3);
assert_eq!(merged[0].value, 150.0);
assert_eq!(merged[1].value, 200.0);
assert_eq!(merged[2].value, 300.0);
}

#[test]
fn test_merge_perpetual_portfolios() {
use chrono::{TimeZone, Utc};

let d1 = Utc.with_ymd_and_hms(2024, 1, 1, 0, 0, 0).unwrap();
let portfolios = vec![
PerpetualPortfolio::mock_with_day(d1, 100.0, 10.0, 500.0),
PerpetualPortfolio::mock_with_day(d1, 200.0, 20.0, 300.0),
];
let summary = PerpetualAccountSummary { account_value: 1000.0, account_leverage: 2.0, margin_usage: 0.5, unrealized_pnl: 30.0 };

let merged = merge_perpetual_portfolios(portfolios, Some(summary));

let day = merged.day.unwrap();
assert_eq!(day.volume, 800.0);
assert_eq!(day.account_value_history.len(), 1);
assert_eq!(day.account_value_history[0].value, 300.0);
assert_eq!(day.pnl_history[0].value, 30.0);
assert!(merged.week.is_none());
assert_eq!(merged.account_summary.unwrap().account_value, 1000.0);
}

#[test]
fn test_merge_portfolio_timeframes_empty() {
let result = merge_portfolio_timeframes(vec![]);
assert!(result.is_none());
}
}
1 change: 1 addition & 0 deletions crates/gem_hypercore/src/provider/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use gem_client::Client;
pub mod balances;
pub mod balances_mapper;
pub mod fee_calculator;
pub mod hip3_mapper;
pub mod perpetual;
pub mod perpetual_mapper;
pub mod preload;
Expand Down
Loading
Loading