From 3789f2de73b2644f2a7c00784d8fc649a9c49d2c Mon Sep 17 00:00:00 2001 From: CryptoKage2306 <26vivek06@gmail.com> Date: Thu, 26 Sep 2024 20:29:17 +0530 Subject: [PATCH 1/2] fix: remove options --- bindings-test/src/tests.rs | 2 +- .../trade_shield/types/perpetual_assets.rs | 17 ++++++++----- .../src/trade_shield/types/perpetual_order.rs | 24 ++----------------- .../action/execute/create_perpetual_order.rs | 2 -- 4 files changed, 14 insertions(+), 31 deletions(-) diff --git a/bindings-test/src/tests.rs b/bindings-test/src/tests.rs index c50be252..b5f438e0 100644 --- a/bindings-test/src/tests.rs +++ b/bindings-test/src/tests.rs @@ -1,6 +1,6 @@ use cosmwasm_std::{ coin, coins, Addr, Coin, Decimal, Int128, Int64, SignedDecimal, SignedDecimal256, StdError, - Uint128, Uint64, + Uint128, }; use cw_multi_test::Executor; use elys_bindings::{ diff --git a/bindings/src/trade_shield/types/perpetual_assets.rs b/bindings/src/trade_shield/types/perpetual_assets.rs index 4997c13d..86b27933 100644 --- a/bindings/src/trade_shield/types/perpetual_assets.rs +++ b/bindings/src/trade_shield/types/perpetual_assets.rs @@ -4,8 +4,6 @@ use crate::ElysQuerier; use cosmwasm_schema::cw_serde; use cosmwasm_std::{DecCoin, Decimal, Decimal256, SignedDecimal, StdError, StdResult, Uint128}; -use super::Fee; - #[cw_serde] pub struct PerpetualAssets { pub total_perpetual_asset_balance: DecCoin, @@ -26,8 +24,7 @@ pub struct PerpetualAsset { pub health: SignedDecimal, pub profit_price: DecCoin, pub stop_loss: Option, - pub borrow_fee: Fee, - pub funding_fee: Fee, + pub fees: Decimal, } impl PerpetualAsset { @@ -94,8 +91,16 @@ impl PerpetualAsset { }), None => None, }, - borrow_fee: Fee::default(), - funding_fee: Fee::default(), + fees: Decimal::from_atomics( + Uint128::new(mtp.mtp.borrow_interest_paid_collateral.i128() as u128), + collateral_info.asset_info.decimal as u32, + ) + .map_err(|e| { + StdError::generic_err(format!( + "failed to convert borrow_interest_paid_collateral to Decimal256: {}", + e + )) + })?, }) } } diff --git a/bindings/src/trade_shield/types/perpetual_order.rs b/bindings/src/trade_shield/types/perpetual_order.rs index 124ff896..1be645b6 100644 --- a/bindings/src/trade_shield/types/perpetual_order.rs +++ b/bindings/src/trade_shield/types/perpetual_order.rs @@ -3,10 +3,10 @@ use std::str::FromStr; use crate::{trade_shield::states::PENDING_PERPETUAL_ORDER, types::PerpetualPosition}; use cosmwasm_schema::cw_serde; use cosmwasm_std::{ - Coin, DecCoin, OverflowError, SignedDecimal, SignedDecimal256, StdError, StdResult, Storage, + Coin, OverflowError, SignedDecimal, SignedDecimal256, StdError, StdResult, Storage, }; -use super::{Fee, OrderPrice, PerpetualOrderType, Status}; +use super::{OrderPrice, PerpetualOrderType, Status}; #[cw_serde] pub struct PerpetualOrder { @@ -21,10 +21,6 @@ pub struct PerpetualOrder { pub take_profit_price: Option, pub position_id: Option, pub status: Status, - pub size: DecCoin, - pub liquidation: SignedDecimal, - pub borrow_fee: Fee, - pub funding_fee: Fee, } impl PerpetualOrder { @@ -38,10 +34,6 @@ impl PerpetualOrder { take_profit_price: &Option, trigger_price: &Option, order_vec: &Vec, - size: DecCoin, - liquidation: SignedDecimal, - borrow_fee: Fee, - funding_fee: Fee, ) -> StdResult { let status = if order_type == &PerpetualOrderType::MarketOpen { Status::Executed @@ -63,10 +55,6 @@ impl PerpetualOrder { trigger_price: trigger_price.to_owned(), status, position_id: None, - size, - liquidation, - borrow_fee, - funding_fee, }; return Ok(order); @@ -82,10 +70,6 @@ impl PerpetualOrder { trigger_price: &Option, take_profit_price: &Option, order_vec: &Vec, - size: DecCoin, - liquidation: SignedDecimal, - borrow_fee: Fee, - funding_fee: Fee, ) -> StdResult { let order_id: u64 = get_new_id(&order_vec)?; @@ -109,10 +93,6 @@ impl PerpetualOrder { position_id: Some(position_id), leverage: leverage.to_owned(), take_profit_price: take_profit_price.to_owned(), - size, - liquidation, - borrow_fee, - funding_fee, }; Ok(order) diff --git a/contracts/trade-shield-contract/src/action/execute/create_perpetual_order.rs b/contracts/trade-shield-contract/src/action/execute/create_perpetual_order.rs index e0656687..9ae46c4c 100644 --- a/contracts/trade-shield-contract/src/action/execute/create_perpetual_order.rs +++ b/contracts/trade-shield-contract/src/action/execute/create_perpetual_order.rs @@ -1,5 +1,3 @@ -use std::ops::Sub; - use crate::{helper::get_discount, msg::ReplyType}; use super::*; From c181420178687aa4ed452a1474f1f139f223d1a6 Mon Sep 17 00:00:00 2001 From: CryptoKage2306 <26vivek06@gmail.com> Date: Thu, 26 Sep 2024 22:11:48 +0530 Subject: [PATCH 2/2] feat: remove min collateral --- bindings-test/src/multitest.rs | 6 +-- bindings/src/querier.rs | 46 +--------------- bindings/src/query_resp.rs | 52 +++++++++++++++++-- .../action/execute/create_perpetual_order.rs | 8 --- .../closing_a_perpetual_position.rs | 2 +- .../change_trigger_price.rs | 2 +- ...ssue_create_perpetual_market_open_order.rs | 7 +-- ...uccessful_create_perpetual_market_close.rs | 2 +- .../pending_limit_open_long_with_price_met.rs | 4 +- ...ding_limit_open_long_with_price_not_met.rs | 4 +- ...pending_limit_open_short_with_price_met.rs | 4 +- ...ing_limit_open_short_with_price_not_met.rs | 4 +- 12 files changed, 65 insertions(+), 76 deletions(-) diff --git a/bindings-test/src/multitest.rs b/bindings-test/src/multitest.rs index f7ce861c..ad1f49ed 100644 --- a/bindings-test/src/multitest.rs +++ b/bindings-test/src/multitest.rs @@ -362,13 +362,13 @@ impl Module for ElysModule { } => { return Ok(to_json_binary(&PerpetualOpenEstimationRawResponse { position, - min_collateral: coin(0, &collateral.denom), available_liquidity: coin(99999999, &trading_asset), leverage: leverage.to_string(), collateral, trading_asset, discount: discount.to_string(), - valid_collateral: Some(true), + // TODO: Fix + interest_amount: Int128::zero(), position_size: coin(1, "btc"), swap_fee: Decimal::zero().to_string(), open_price: Decimal::zero().to_string(), @@ -383,7 +383,7 @@ impl Module for ElysModule { price_impact: Decimal::zero().to_string(), borrow_fee: Coin::new(0, ""), funding_fee: Coin::new(0, ""), - })?) + })?); } ElysQuery::AssetProfileEntryAll { .. } => { let asset_info = ASSET_INFO.load(storage)?; diff --git a/bindings/src/querier.rs b/bindings/src/querier.rs index d8d85e67..88e3b340 100644 --- a/bindings/src/querier.rs +++ b/bindings/src/querier.rs @@ -153,51 +153,9 @@ impl<'a> ElysQuerier<'a> { let raw_resp: PerpetualOpenEstimationRawResponse = self.querier.query(&request)?; - let resp: PerpetualOpenEstimationResponse = PerpetualOpenEstimationResponse { - position: PerpetualPosition::try_from_i32(raw_resp.position)?, - leverage: SignedDecimal::from_str(&raw_resp.leverage) - .map_or(SignedDecimal::zero(), |leverage| leverage), - trading_asset: raw_resp.trading_asset, - collateral: raw_resp.collateral, - min_collateral: raw_resp.min_collateral, - valid_collateral: raw_resp - .valid_collateral - .map_or(false, |valid_collateral| valid_collateral), - position_size: raw_resp.position_size, - swap_fee: Decimal::from_str(&raw_resp.swap_fee) - .map_or(Decimal::zero(), |swap_fee| swap_fee), - discount: Decimal::from_str(&raw_resp.discount) - .map_or(Decimal::zero(), |discount| discount), - open_price: Decimal::from_str(&raw_resp.open_price) - .map_or(Decimal::zero(), |open_price| open_price), - take_profit_price: SignedDecimal256::from_str(&raw_resp.take_profit_price) - .map_or(SignedDecimal256::zero(), |take_profit_price| { - take_profit_price - }), - liquidation_price: SignedDecimal::from_str(&raw_resp.liquidation_price) - .map_or(SignedDecimal::zero(), |liquidation_price| liquidation_price), - estimated_pnl: raw_resp.estimated_pnl, - estimated_pnl_denom: raw_resp.estimated_pnl_denom, - available_liquidity: raw_resp.available_liquidity, - slippage: Decimal::from_str(&raw_resp.slippage) - .map_or(Decimal::zero(), |slippage| slippage), - weight_balance_ratio: SignedDecimal::from_str(&raw_resp.weight_balance_ratio) - .map_or(SignedDecimal::zero(), |weight_balance_ratio| { - weight_balance_ratio - }), - borrow_interest_rate: SignedDecimal::from_str(&raw_resp.borrow_interest_rate) - .map_or(SignedDecimal::zero(), |borrow_interest_rate| { - borrow_interest_rate - }), - funding_rate: SignedDecimal::from_str(&raw_resp.funding_rate) - .map_or(SignedDecimal::zero(), |funding_rate| funding_rate), - price_impact: SignedDecimal::from_str(&raw_resp.price_impact) - .map_or(SignedDecimal::zero(), |price_impact| price_impact), - borrow_fee: raw_resp.borrow_fee, - funding_fee: raw_resp.funding_fee, - }; + let resp: StdResult = raw_resp.into(); - Ok(resp) + resp } pub fn get_all_asset_profile( diff --git a/bindings/src/query_resp.rs b/bindings/src/query_resp.rs index d427e3c1..35482cde 100644 --- a/bindings/src/query_resp.rs +++ b/bindings/src/query_resp.rs @@ -100,15 +100,13 @@ pub struct AmmSwapEstimationByDenomResponse { pub price_impact: SignedDecimal, pub slippage: Decimal, } - #[cw_serde] pub struct PerpetualOpenEstimationRawResponse { pub position: i32, pub leverage: String, pub trading_asset: String, pub collateral: Coin, - pub min_collateral: Coin, - pub valid_collateral: Option, + pub interest_amount: Int128, pub position_size: Coin, pub swap_fee: String, pub discount: String, @@ -133,8 +131,7 @@ pub struct PerpetualOpenEstimationResponse { pub leverage: SignedDecimal, pub trading_asset: String, pub collateral: Coin, - pub min_collateral: Coin, - pub valid_collateral: bool, + pub interest_amount: Int128, pub position_size: Coin, pub swap_fee: Decimal, pub discount: Decimal, @@ -153,6 +150,51 @@ pub struct PerpetualOpenEstimationResponse { pub funding_fee: Coin, } +impl Into> for PerpetualOpenEstimationRawResponse { + fn into(self) -> StdResult { + Ok(PerpetualOpenEstimationResponse { + position: PerpetualPosition::try_from_i32(self.position)?, + leverage: SignedDecimal::from_str(&self.leverage) + .map_or(SignedDecimal::zero(), |leverage| leverage), + interest_amount: self.interest_amount, + trading_asset: self.trading_asset, + collateral: self.collateral, + position_size: self.position_size, + swap_fee: Decimal::from_str(&self.swap_fee) + .map_or(Decimal::zero(), |swap_fee| swap_fee), + discount: Decimal::from_str(&self.discount) + .map_or(Decimal::zero(), |discount| discount), + open_price: Decimal::from_str(&self.open_price) + .map_or(Decimal::zero(), |open_price| open_price), + take_profit_price: SignedDecimal256::from_str(&self.take_profit_price) + .map_or(SignedDecimal256::zero(), |take_profit_price| { + take_profit_price + }), + liquidation_price: SignedDecimal::from_str(&self.liquidation_price) + .map_or(SignedDecimal::zero(), |liquidation_price| liquidation_price), + estimated_pnl: self.estimated_pnl, + estimated_pnl_denom: self.estimated_pnl_denom, + available_liquidity: self.available_liquidity, + slippage: Decimal::from_str(&self.slippage) + .map_or(Decimal::zero(), |slippage| slippage), + weight_balance_ratio: SignedDecimal::from_str(&self.weight_balance_ratio) + .map_or(SignedDecimal::zero(), |weight_balance_ratio| { + weight_balance_ratio + }), + borrow_interest_rate: SignedDecimal::from_str(&self.borrow_interest_rate) + .map_or(SignedDecimal::zero(), |borrow_interest_rate| { + borrow_interest_rate + }), + funding_rate: SignedDecimal::from_str(&self.funding_rate) + .map_or(SignedDecimal::zero(), |funding_rate| funding_rate), + price_impact: SignedDecimal::from_str(&self.price_impact) + .map_or(SignedDecimal::zero(), |price_impact| price_impact), + borrow_fee: self.borrow_fee, + funding_fee: self.funding_fee, + }) + } +} + #[cw_serde] pub struct PerpetualGetPositionsForAddressResponseRaw { pub mtps: Option>, diff --git a/contracts/trade-shield-contract/src/action/execute/create_perpetual_order.rs b/contracts/trade-shield-contract/src/action/execute/create_perpetual_order.rs index 9ae46c4c..376978a5 100644 --- a/contracts/trade-shield-contract/src/action/execute/create_perpetual_order.rs +++ b/contracts/trade-shield-contract/src/action/execute/create_perpetual_order.rs @@ -148,14 +148,6 @@ fn create_perpetual_open_order( get_discount(deps.querier, info.sender.to_string())?, )?; - if !open_estimation.valid_collateral { - return Err(StdError::generic_err(format!( - "not valid collateral: min collateral: {}", - open_estimation.min_collateral.amount - )) - .into()); - } - if let Some(price) = &trigger_price { if price.rate.is_zero() { return Err(StdError::generic_err("trigger_price: The rate cannot be zero").into()); diff --git a/contracts/trade-shield-contract/src/tests/close_perpetual_position/closing_a_perpetual_position.rs b/contracts/trade-shield-contract/src/tests/close_perpetual_position/closing_a_perpetual_position.rs index e7a0fd74..e334cba4 100644 --- a/contracts/trade-shield-contract/src/tests/close_perpetual_position/closing_a_perpetual_position.rs +++ b/contracts/trade-shield-contract/src/tests/close_perpetual_position/closing_a_perpetual_position.rs @@ -1,4 +1,4 @@ -use cosmwasm_std::{Int128, SignedDecimal, SignedDecimal256, Uint64}; +use cosmwasm_std::{Int128, SignedDecimal, SignedDecimal256}; use elys_bindings::{query_resp::PerpetualGetPositionsForAddressResponse, ElysQuery}; use super::*; diff --git a/contracts/trade-shield-contract/src/tests/create_perpetual_order/change_trigger_price.rs b/contracts/trade-shield-contract/src/tests/create_perpetual_order/change_trigger_price.rs index b615abe9..83729b15 100644 --- a/contracts/trade-shield-contract/src/tests/create_perpetual_order/change_trigger_price.rs +++ b/contracts/trade-shield-contract/src/tests/create_perpetual_order/change_trigger_price.rs @@ -1,4 +1,4 @@ -use cosmwasm_std::{Addr, Decimal, Int128, SignedDecimal, SignedDecimal256, Uint64}; +use cosmwasm_std::{Addr, Decimal, Int128, SignedDecimal, SignedDecimal256}; use elys_bindings::trade_shield::msg::query_resp::GetPerpetualOrderResp; use std::str::FromStr; diff --git a/contracts/trade-shield-contract/src/tests/create_perpetual_order/reproduce_testnet_issue_create_perpetual_market_open_order.rs b/contracts/trade-shield-contract/src/tests/create_perpetual_order/reproduce_testnet_issue_create_perpetual_market_open_order.rs index 73e98441..68cbd5eb 100644 --- a/contracts/trade-shield-contract/src/tests/create_perpetual_order/reproduce_testnet_issue_create_perpetual_market_open_order.rs +++ b/contracts/trade-shield-contract/src/tests/create_perpetual_order/reproduce_testnet_issue_create_perpetual_market_open_order.rs @@ -116,11 +116,8 @@ impl Module for ElysModule { leverage: leverage.clone().to_string(), trading_asset: trading_asset.clone(), collateral: collateral.clone(), - min_collateral: coin( - 8333333, - "ibc/2180E84E20F5679FCC760D8C165B60F42065DEF7F46A72B447CFF1B7DC6C0A65", - ), - valid_collateral: Some(true), + // TODO: Fix + interest_amount: Int128::zero(), position_size: collateral.clone(), swap_fee: Decimal::zero().to_string(), discount: discount.clone().to_string(), diff --git a/contracts/trade-shield-contract/src/tests/create_perpetual_order/successful_create_perpetual_market_close.rs b/contracts/trade-shield-contract/src/tests/create_perpetual_order/successful_create_perpetual_market_close.rs index 7a491123..7db2301b 100644 --- a/contracts/trade-shield-contract/src/tests/create_perpetual_order/successful_create_perpetual_market_close.rs +++ b/contracts/trade-shield-contract/src/tests/create_perpetual_order/successful_create_perpetual_market_close.rs @@ -1,4 +1,4 @@ -use cosmwasm_std::{Addr, SignedDecimal, SignedDecimal256, Uint64}; +use cosmwasm_std::{Addr, SignedDecimal, SignedDecimal256}; use std::str::FromStr; use cosmwasm_std::Int128; diff --git a/contracts/trade-shield-contract/src/tests/process_perpetual_order/pending_limit_open_long_with_price_met.rs b/contracts/trade-shield-contract/src/tests/process_perpetual_order/pending_limit_open_long_with_price_met.rs index 265a7560..ea86aebc 100644 --- a/contracts/trade-shield-contract/src/tests/process_perpetual_order/pending_limit_open_long_with_price_met.rs +++ b/contracts/trade-shield-contract/src/tests/process_perpetual_order/pending_limit_open_long_with_price_met.rs @@ -117,11 +117,11 @@ impl Module for ElysModuleWrapper { position, leverage: leverage.to_string(), trading_asset, - min_collateral: collateral.clone(), position_size: coin(0, ""), collateral, available_liquidity: coin(0, ""), - valid_collateral: Some(true), + // TODO: Fix + interest_amount: Int128::zero(), swap_fee: Decimal::zero().to_string(), discount: discount.to_string(), open_price: Decimal::zero().to_string(), diff --git a/contracts/trade-shield-contract/src/tests/process_perpetual_order/pending_limit_open_long_with_price_not_met.rs b/contracts/trade-shield-contract/src/tests/process_perpetual_order/pending_limit_open_long_with_price_not_met.rs index e56db9c8..094ee602 100644 --- a/contracts/trade-shield-contract/src/tests/process_perpetual_order/pending_limit_open_long_with_price_not_met.rs +++ b/contracts/trade-shield-contract/src/tests/process_perpetual_order/pending_limit_open_long_with_price_not_met.rs @@ -117,11 +117,11 @@ impl Module for ElysModuleWrapper { position, leverage: leverage.to_string(), trading_asset, - min_collateral: collateral.clone(), + // TODO: Fix + interest_amount: Int128::zero(), position_size: coin(0, ""), collateral, available_liquidity: coin(0, ""), - valid_collateral: Some(true), swap_fee: Decimal::zero().to_string(), discount: discount.to_string(), open_price: Decimal::zero().to_string(), diff --git a/contracts/trade-shield-contract/src/tests/process_perpetual_order/pending_limit_open_short_with_price_met.rs b/contracts/trade-shield-contract/src/tests/process_perpetual_order/pending_limit_open_short_with_price_met.rs index a135b7aa..7ecaafa1 100644 --- a/contracts/trade-shield-contract/src/tests/process_perpetual_order/pending_limit_open_short_with_price_met.rs +++ b/contracts/trade-shield-contract/src/tests/process_perpetual_order/pending_limit_open_short_with_price_met.rs @@ -117,11 +117,11 @@ impl Module for ElysModuleWrapper { position, leverage: leverage.to_string(), trading_asset, - min_collateral: collateral.clone(), position_size: coin(0, ""), collateral, available_liquidity: coin(0, ""), - valid_collateral: Some(true), + // TODO: Fix + interest_amount: Int128::zero(), swap_fee: Decimal::zero().to_string(), discount: discount.to_string(), open_price: Decimal::zero().to_string(), diff --git a/contracts/trade-shield-contract/src/tests/process_perpetual_order/pending_limit_open_short_with_price_not_met.rs b/contracts/trade-shield-contract/src/tests/process_perpetual_order/pending_limit_open_short_with_price_not_met.rs index 19da05ae..b81a4c21 100644 --- a/contracts/trade-shield-contract/src/tests/process_perpetual_order/pending_limit_open_short_with_price_not_met.rs +++ b/contracts/trade-shield-contract/src/tests/process_perpetual_order/pending_limit_open_short_with_price_not_met.rs @@ -117,11 +117,11 @@ impl Module for ElysModuleWrapper { position, leverage: leverage.to_string(), trading_asset, - min_collateral: collateral.clone(), + // TODO: Fix + interest_amount: Int128::zero(), position_size: coin(0, ""), collateral, available_liquidity: coin(0, ""), - valid_collateral: Some(true), swap_fee: Decimal::zero().to_string(), discount: discount.to_string(), open_price: Decimal::zero().to_string(),