Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: higher leverages #2617

Merged
merged 3 commits into from
Jun 6, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ min_quantity = 1
maintenance_margin_rate = 0.1
order_matching_fee_rate = 0.003
index_price_source = "Bitmex"
max_leverage = 5

[xxi]
off_chain_sync_interval = 5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ min_quantity = 1
maintenance_margin_rate = 0.1
order_matching_fee_rate = 0.003
index_price_source = "Test"
max_leverage = 5

[xxi]
off_chain_sync_interval = 5
Expand Down
9 changes: 8 additions & 1 deletion coordinator/src/orderbook/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,18 @@ pub async fn websocket_connection(stream: WebSocket, state: Arc<AppState>) {
let liquidity_options =
db::liquidity_options::get_all(&mut conn).unwrap_or_default();

let (min_quantity, maintenance_margin_rate, order_matching_fee_rate) = {
let (
min_quantity,
maintenance_margin_rate,
order_matching_fee_rate,
max_leverage,
) = {
let settings = state.settings.read().await;
(
settings.min_quantity,
settings.maintenance_margin_rate,
settings.order_matching_fee_rate,
settings.max_leverage,
)
};

Expand All @@ -239,6 +245,7 @@ pub async fn websocket_connection(stream: WebSocket, state: Arc<AppState>) {
maintenance_margin_rate,
order_matching_fee_rate,
referral_status,
max_leverage,
}))
.await
{
Expand Down
8 changes: 8 additions & 0 deletions coordinator/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ pub struct Settings {

/// Where to get the index price from. This value is used to calculate funding fees.
pub index_price_source: IndexPriceSource,

/// The max leverage a trader can take
pub max_leverage: u8,
}

impl Settings {
Expand Down Expand Up @@ -162,6 +165,7 @@ impl Settings {
maintenance_margin_rate: file.maintenance_margin_rate,
order_matching_fee_rate: file.order_matching_fee_rate,
index_price_source: file.index_price_source,
max_leverage: file.max_leverage,
}
}
}
Expand Down Expand Up @@ -190,6 +194,8 @@ pub struct SettingsFile {
order_matching_fee_rate: f32,

index_price_source: IndexPriceSource,

max_leverage: u8,
}

impl From<Settings> for SettingsFile {
Expand All @@ -210,6 +216,7 @@ impl From<Settings> for SettingsFile {
maintenance_margin_rate: value.maintenance_margin_rate,
order_matching_fee_rate: value.order_matching_fee_rate,
index_price_source: value.index_price_source,
max_leverage: value.max_leverage,
}
}
}
Expand Down Expand Up @@ -246,6 +253,7 @@ mod tests {
maintenance_margin_rate: 0.1,
order_matching_fee_rate: 0.003,
index_price_source: IndexPriceSource::Bitmex,
max_leverage: 5,
};

let serialized = toml::to_string_pretty(&original).unwrap();
Expand Down
1 change: 1 addition & 0 deletions crates/xxi-node/src/commons/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ pub struct TenTenOneConfig {
pub maintenance_margin_rate: f32,
pub order_matching_fee_rate: f32,
pub referral_status: ReferralStatus,
pub max_leverage: u8,
}

#[derive(Serialize, Clone, Deserialize, Debug)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class TenTenOneConfigChangeNotifier extends ChangeNotifier implements Subscriber

List<LiquidityOption> _liquidityOptions = [];
ReferralStatus? _referralStatus;
// will be overwritten once we receive the authenticated event
int _maxLeverage = 5;

TenTenOneConfigChangeNotifier(this.channelInfoService);

Expand All @@ -19,6 +21,8 @@ class TenTenOneConfigChangeNotifier extends ChangeNotifier implements Subscriber

ReferralStatus? get referralStatus => _referralStatus;

double get maxLeverage => _maxLeverage.toDouble();

@override
void notify(bridge.Event event) {
if (event is bridge.Event_Authenticated) {
Expand All @@ -27,6 +31,7 @@ class TenTenOneConfigChangeNotifier extends ChangeNotifier implements Subscriber
_liquidityOptions.sort((a, b) => a.rank.compareTo(b.rank));

_referralStatus = ReferralStatus.from(event.field0.referralStatus);
_maxLeverage = event.field0.maxLeverage;

super.notifyListeners();
}
Expand Down
3 changes: 2 additions & 1 deletion mobile/lib/common/domain/tentenone_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class TenTenOneConfig {
numberOfTotalReferrals: 0,
referralTier: 0,
referralFeeBonus: 0,
bonusStatusType: bridge.BonusStatusType.None));
bonusStatusType: bridge.BonusStatusType.None),
maxLeverage: 5);
}
}
8 changes: 7 additions & 1 deletion mobile/lib/features/trade/leverage_slider.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:get_10101/common/application/tentenone_config_change_notifier.dart';
import 'package:get_10101/common/color.dart';
import 'package:get_10101/features/trade/trade_theme.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';
import 'package:syncfusion_flutter_sliders/sliders.dart';
import 'package:syncfusion_flutter_core/theme.dart' as slider_theme;

const double minLeverage = 1.0;
const double maxLeverage = 5.0;

/// Slider that allows the user to select a leverage between minLeverage and maxLeverage.
/// It uses linear scale and fractional leverage values are rounded to the nearest integer.
Expand All @@ -34,6 +35,11 @@ class _LeverageSliderState extends State<LeverageSlider> {

@override
Widget build(BuildContext context) {
TenTenOneConfigChangeNotifier tentenoneConfigChangeNotifier =
context.read<TenTenOneConfigChangeNotifier>();

var maxLeverage = tentenoneConfigChangeNotifier.maxLeverage;

return InputDecorator(
decoration: InputDecoration(
border: const OutlineInputBorder(),
Expand Down
2 changes: 2 additions & 0 deletions mobile/native/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub struct TenTenOneConfig {
pub min_quantity: u64,
pub maintenance_margin_rate: f32,
pub referral_status: ReferralStatus,
pub max_leverage: u8,
}

impl From<xxi_node::commons::TenTenOneConfig> for TenTenOneConfig {
Expand All @@ -82,6 +83,7 @@ impl From<xxi_node::commons::TenTenOneConfig> for TenTenOneConfig {
min_quantity: value.min_quantity,
maintenance_margin_rate: value.maintenance_margin_rate,
referral_status: value.referral_status.into(),
max_leverage: value.max_leverage,
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions webapp/frontend/lib/services/trade_constraints_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ class TradeConstraints {
@JsonKey(name: 'channel_fee_reserve_sats')
final int channelFeeReserveSats;

/// The max leverage the trader can take
@JsonKey(name: 'max_leverage')
final int maxLeverage;

const TradeConstraints({
required this.maxLocalBalanceSats,
required this.maxCounterpartyBalanceSats,
Expand All @@ -73,6 +77,7 @@ class TradeConstraints {
required this.minMarginSats,
required this.estimatedFundingTxFeeSats,
required this.channelFeeReserveSats,
required this.maxLeverage,
});

factory TradeConstraints.fromJson(Map<String, dynamic> json) => _$TradeConstraintsFromJson(json);
Expand Down
2 changes: 2 additions & 0 deletions webapp/frontend/lib/services/trade_constraints_service.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion webapp/frontend/lib/trade/leverage_slider.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:get_10101/change_notifier/trade_constraint_change_notifier.dart';
import 'package:get_10101/common/color.dart';
import 'package:get_10101/common/theme.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';
import 'package:syncfusion_flutter_sliders/sliders.dart';
import 'package:syncfusion_flutter_core/theme.dart' as slider_theme;

Expand All @@ -11,7 +13,6 @@ const gradientColors = <Color>[Colors.green, Colors.deepOrange];
const LinearGradient gradient = LinearGradient(colors: gradientColors);

const double minLeverage = 1.0;
const double maxLeverage = 5.0;

/// Slider that allows the user to select a leverage between minLeverage and maxLeverage.
/// It uses linear scale and fractional leverage values are rounded to the nearest integer.
Expand All @@ -36,6 +37,12 @@ class _LeverageSliderState extends State<LeverageSlider> {

@override
Widget build(BuildContext context) {
TradeConstraintsChangeNotifier tradeConstraintsChangeNotifier =
context.read<TradeConstraintsChangeNotifier>();

double maxLeverage =
tradeConstraintsChangeNotifier.tradeConstraints?.maxLeverage.toDouble() ?? 5.0;

return InputDecorator(
decoration: InputDecoration(
border: const OutlineInputBorder(),
Expand Down
4 changes: 4 additions & 0 deletions webapp/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use native::api::WalletHistoryItemType;
use native::calculations::calculate_pnl;
use native::channel_trade_constraints;
use native::dlc;
use native::state::try_get_tentenone_config;
use native::trade::order::FailureReason;
use native::trade::order::InvalidSubchannelOffer;
use rust_decimal::prelude::ToPrimitive;
Expand Down Expand Up @@ -965,6 +966,7 @@ pub struct TradeConstraints {
pub min_margin_sats: u64,
pub estimated_funding_tx_fee_sats: u64,
pub channel_fee_reserve_sats: u64,
pub max_leverage: u8,
}

#[utoipa::path(
Expand All @@ -976,6 +978,7 @@ responses(
)]
pub async fn get_trade_constraints() -> Result<Json<TradeConstraints>, AppError> {
let trade_constraints = channel_trade_constraints::channel_trade_constraints()?;
let ten_one_config = try_get_tentenone_config().context("Could not read 10101 config")?;
let fee = dlc::estimated_funding_tx_fee()?;
let channel_fee_reserve = dlc::estimated_fee_reserve()?;
Ok(Json(TradeConstraints {
Expand All @@ -987,6 +990,7 @@ pub async fn get_trade_constraints() -> Result<Json<TradeConstraints>, AppError>
min_margin_sats: trade_constraints.min_margin,
estimated_funding_tx_fee_sats: fee.to_sat(),
channel_fee_reserve_sats: channel_fee_reserve.to_sat(),
max_leverage: ten_one_config.max_leverage,
}))
}

Expand Down
Loading