|
|
@@ -4,6 +4,8 @@ use anyhow::{anyhow, bail, Result};
|
|
|
use reqwest::Client;
|
|
|
use reqwest::header::HeaderMap;
|
|
|
use rust_decimal::Decimal;
|
|
|
+use rust_decimal::prelude::FromPrimitive;
|
|
|
+use rust_decimal_macros::dec;
|
|
|
use serde_json::{json, Value};
|
|
|
use starknet::core::types::Felt;
|
|
|
use tracing::{error, info, warn};
|
|
|
@@ -169,20 +171,51 @@ impl ExtendedRestClient {
|
|
|
let l2_config = self.market_info.get("l2Config").unwrap();
|
|
|
|
|
|
// 涉及到计算的
|
|
|
+ // let maker_fee_rate = dec!(0.0002);
|
|
|
+ let taker_fee_rate = dec!(0.0005);
|
|
|
+ let builder_fee_rate = dec!(0);
|
|
|
+
|
|
|
+ // 手续费及in、out详细数量计算
|
|
|
let is_buying_synthetic = side == "BUY";
|
|
|
- let synthetic_amount = Decimal::from_str(qty)?;
|
|
|
let price = Decimal::from_str(price)?;
|
|
|
- let collateral_amount = synthetic_amount.checked_mul(price)
|
|
|
- .ok_or_else(|| anyhow!("Collateral amount multiplication overflowed"))?;
|
|
|
+ let synthetic_amount = Decimal::from_str(qty)?;
|
|
|
+ let collateral_amount = synthetic_amount.checked_mul(price).ok_or_else(|| anyhow!("Collateral amount multiplication overflowed"))?;
|
|
|
+ let total_fee_rate = taker_fee_rate.checked_add(builder_fee_rate).ok_or_else(|| anyhow!("Total fee addition overflowed"))?;
|
|
|
+ let fee_amount = total_fee_rate.checked_mul(collateral_amount).ok_or_else(|| anyhow!("Fee amount multiplication overflowed"))?;
|
|
|
+
|
|
|
+ let collateral_resolution_dec = Decimal::from_u64(l2_config.get("collateralResolution").unwrap().as_u64().unwrap()).unwrap();
|
|
|
+ let mut stark_collateral_amount_dec = collateral_amount.checked_mul(collateral_resolution_dec).ok_or_else(|| anyhow!("Stark collateral amount decimal multiplication overflowed"))?;
|
|
|
+ let synthetic_resolution_dec = Decimal::from_u64(l2_config.get("syntheticResolution").unwrap().as_u64().unwrap()).unwrap();
|
|
|
+ let mut stark_synthetic_amount_dec = synthetic_amount.checked_mul(synthetic_resolution_dec).ok_or_else(|| anyhow!("Stark synthetic amount decimal multiplication overflowed"))?;
|
|
|
+ if is_buying_synthetic {
|
|
|
+ stark_collateral_amount_dec = stark_collateral_amount_dec.ceil();
|
|
|
+ stark_synthetic_amount_dec = stark_synthetic_amount_dec.ceil();
|
|
|
+ } else {
|
|
|
+ stark_collateral_amount_dec = stark_collateral_amount_dec.floor();
|
|
|
+ }
|
|
|
+ stark_synthetic_amount_dec = stark_synthetic_amount_dec.floor();
|
|
|
+ let mut stark_collateral_amount = stark_collateral_amount_dec.trunc(); // trunc() 截断到整数部分
|
|
|
+ let mut stark_synthetic_amount = stark_synthetic_amount_dec.trunc();
|
|
|
+ let stark_fee_part = fee_amount.checked_mul(collateral_resolution_dec)
|
|
|
+ .ok_or_else(|| anyhow!("Stark fee part multiplication overflowed"))?
|
|
|
+ .ceil() // 向上取整
|
|
|
+ .trunc(); // 截断到整数部分
|
|
|
+ if is_buying_synthetic {
|
|
|
+ stark_collateral_amount = stark_collateral_amount.checked_mul(dec!(-1))
|
|
|
+ .ok_or_else(|| anyhow!("Stark collateral amount negation overflowed"))?;
|
|
|
+ } else {
|
|
|
+ stark_synthetic_amount = stark_synthetic_amount.checked_mul(dec!(-1))
|
|
|
+ .ok_or_else(|| anyhow!("Stark synthetic amount negation overflowed"))?;
|
|
|
+ }
|
|
|
|
|
|
// 其余参数
|
|
|
let position_id = format!("{}", account.vault_number);
|
|
|
let base_asset_id_hex = l2_config.get("syntheticId").unwrap().as_str().unwrap().to_string();
|
|
|
- let base_amount = "100".to_string();
|
|
|
+ let base_amount = stark_synthetic_amount.to_string();
|
|
|
let quote_asset_id_hex = l2_config.get("collateralId").unwrap().as_str().unwrap().to_string();
|
|
|
- let quote_amount = "-156".to_string();
|
|
|
+ let quote_amount = stark_collateral_amount.to_string();
|
|
|
let fee_asset_id_hex = l2_config.get("collateralId").unwrap().as_str().unwrap().to_string();
|
|
|
- let fee_amount = "74".to_string();
|
|
|
+ let fee_amount = stark_fee_part.to_string();
|
|
|
let expiration = format!("{}", (expiry_epoch_millis / 1000) as u64);
|
|
|
let salt = nonce.clone();
|
|
|
let user_public_key_hex = account.stark_public_key.clone();
|