Skip to content
This repository has been archived by the owner on Nov 15, 2024. It is now read-only.

Commit

Permalink
feat: add max limit for perpetual limit order
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptokage1996 committed Sep 24, 2024
1 parent c1bd4dc commit 4702b20
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 3 deletions.
3 changes: 2 additions & 1 deletion bindings-test/src/multitest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use cosmwasm_std::{
to_json_binary, Addr, BankMsg, BlockInfo, Coin, Decimal, Empty, Int64, Querier, StdError,
StdResult, Storage,
};
use cosmwasm_std::{Int128, SignedDecimal, Uint128, Uint64};
use cosmwasm_std::{Int128, SignedDecimal, Uint128};
use cw_multi_test::{App, AppResponse, BankKeeper, BankSudo, BasicAppBuilder, Module, WasmKeeper};
use cw_storage_plus::Item;
use elys_bindings::{
Expand Down Expand Up @@ -664,6 +664,7 @@ impl Module for ElysModule {
}
ElysQuery::CommitmentNumberOfCommitments {} => todo!("CommitmentNumberOfCommitments"),
ElysQuery::LeveragelpRewards { .. } => todo!(),
ElysQuery::PerpetualParams {} => todo!(),
}
}

Expand Down
8 changes: 8 additions & 0 deletions bindings/src/querier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,14 @@ impl<'a> ElysQuerier<'a> {
Ok(ParameterParamsResponse { params })
}

pub fn query_perpetual_params(&self) -> StdResult<PerpetualParamsResponse> {
let query = ElysQuery::query_perpetual_params();
let request = QueryRequest::Custom(query);
let PerpetualParamsResponseRaw { params: raw_params } = self.querier.query(&request)?;
let params: Option<PerpetualParams> = raw_params.map(|val| val.into());
Ok(PerpetualParamsResponse { params })
}

#[allow(dead_code)]
#[cfg(feature = "debug")]
fn query_binary(&self, request: &QueryRequest<ElysQuery>) -> StdResult<Binary> {
Expand Down
7 changes: 7 additions & 0 deletions bindings/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ pub enum ElysQuery {
// Define Tier
#[returns(TierCalculateDiscountResponse)]
TierCalculateDiscount { user: String },
// Define PerpetualParams
#[returns(PerpetualParamsResponse)]
PerpetualParams {},
}

impl CustomQuery for ElysQuery {}
Expand Down Expand Up @@ -469,4 +472,8 @@ impl ElysQuery {
pub fn query_leverage_lp_rewards(address: String, ids: Vec<u64>) -> Self {
Self::LeveragelpRewards { address, ids }
}

pub fn query_perpetual_params() -> Self {
Self::PerpetualParams {}
}
}
108 changes: 108 additions & 0 deletions bindings/src/query_resp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,16 +597,84 @@ pub struct LeveragelpParams {
pub epoch_length: i64,
}

#[cw_serde]
pub struct PerpetualParamsRaw {
pub option: Option<bool>,
pub leverage_max: Option<Decimal>,
pub borrow_interest_rate_max: Option<Decimal>,
pub borrow_interest_rate_min: Option<Decimal>,
pub borrow_interest_rate_increase: Option<Decimal>,
pub borrow_interest_rate_decrease: Option<Decimal>,
pub health_gain_factor: Option<Decimal>,
pub epoch_length: Option<i64>,
pub max_open_positions: Option<i64>,
pub pool_open_threshold: Option<Decimal>,
pub force_close_fund_percentage: Option<Decimal>,
pub force_close_fund_address: Option<String>,
pub incremental_borrow_interest_payment_fund_percentage: Option<Decimal>,
pub incremental_borrow_interest_payment_fund_address: Option<String>,
pub safety_factor: Option<Decimal>,
pub incremental_borrow_interest_payment_enabled: Option<bool>,
pub whitelisting_enabled: Option<bool>,
pub invariant_check_epoch: Option<String>,
pub take_profit_borrow_interest_rate_min: Option<Decimal>,
pub funding_fee_base_rate: Option<Decimal>,
pub funding_fee_max_rate: Option<Decimal>,
pub funding_fee_min_rate: Option<Decimal>,
pub funding_fee_collection_address: Option<String>,
pub swap_fee: Option<Decimal>,
pub max_limit_order: Option<i64>,
}

#[cw_serde]
pub struct PerpetualParams {
pub option: bool,
pub leverage_max: Decimal,
pub borrow_interest_rate_max: Decimal,
pub borrow_interest_rate_min: Decimal,
pub borrow_interest_rate_increase: Decimal,
pub borrow_interest_rate_decrease: Decimal,
pub health_gain_factor: Decimal,
pub epoch_length: i64,
pub max_open_positions: i64,
pub pool_open_threshold: Decimal,
pub force_close_fund_percentage: Decimal,
pub force_close_fund_address: String,
pub incremental_borrow_interest_payment_fund_percentage: Decimal,
pub incremental_borrow_interest_payment_fund_address: String,
pub safety_factor: Decimal,
pub incremental_borrow_interest_payment_enabled: bool,
pub whitelisting_enabled: bool,
pub invariant_check_epoch: String,
pub take_profit_borrow_interest_rate_min: Decimal,
pub funding_fee_base_rate: Decimal,
pub funding_fee_max_rate: Decimal,
pub funding_fee_min_rate: Decimal,
pub funding_fee_collection_address: String,
pub swap_fee: Decimal,
pub max_limit_order: i64,
}

#[cw_serde]
pub struct LeveragelpParamsResponseRaw {
pub params: Option<LeveragelpParamsRaw>,
}

#[cw_serde]
pub struct PerpetualParamsResponseRaw {
pub params: Option<PerpetualParamsRaw>,
}

#[cw_serde]
pub struct LeveragelpParamsResponse {
pub params: Option<LeveragelpParams>,
}

#[cw_serde]
pub struct PerpetualParamsResponse {
pub params: Option<PerpetualParams>,
}

#[cw_serde]
pub struct Position {
pub address: String,
Expand Down Expand Up @@ -1145,3 +1213,43 @@ pub struct ParameterParamsResponseRaw {
pub struct ParameterParamsResponse {
pub params: ParameterParams,
}

impl Into<PerpetualParams> for PerpetualParamsRaw {
fn into(self) -> PerpetualParams {
PerpetualParams {
option: self.option.unwrap_or_default(),
leverage_max: self.leverage_max.unwrap_or_default(),
borrow_interest_rate_max: self.borrow_interest_rate_max.unwrap_or_default(),
borrow_interest_rate_min: self.borrow_interest_rate_min.unwrap_or_default(),
borrow_interest_rate_increase: self.borrow_interest_rate_increase.unwrap_or_default(),
borrow_interest_rate_decrease: self.borrow_interest_rate_decrease.unwrap_or_default(),
health_gain_factor: self.health_gain_factor.unwrap_or_default(),
epoch_length: self.epoch_length.unwrap_or_default(),
max_open_positions: self.max_open_positions.unwrap_or_default(),
pool_open_threshold: self.pool_open_threshold.unwrap_or_default(),
force_close_fund_percentage: self.force_close_fund_percentage.unwrap_or_default(),
force_close_fund_address: self.force_close_fund_address.unwrap_or_default(),
incremental_borrow_interest_payment_fund_percentage: self
.incremental_borrow_interest_payment_fund_percentage
.unwrap_or_default(),
incremental_borrow_interest_payment_fund_address: self
.incremental_borrow_interest_payment_fund_address
.unwrap_or_default(),
safety_factor: self.safety_factor.unwrap_or_default(),
incremental_borrow_interest_payment_enabled: self
.incremental_borrow_interest_payment_enabled
.unwrap_or_default(),
whitelisting_enabled: self.whitelisting_enabled.unwrap_or_default(),
invariant_check_epoch: self.invariant_check_epoch.unwrap_or_default(),
take_profit_borrow_interest_rate_min: self
.take_profit_borrow_interest_rate_min
.unwrap_or_default(),
funding_fee_base_rate: self.funding_fee_base_rate.unwrap_or_default(),
funding_fee_max_rate: self.funding_fee_max_rate.unwrap_or_default(),
funding_fee_min_rate: self.funding_fee_min_rate.unwrap_or_default(),
funding_fee_collection_address: self.funding_fee_collection_address.unwrap_or_default(),
swap_fee: self.swap_fee.unwrap_or_default(),
max_limit_order: self.max_limit_order.unwrap_or_default(),
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::ops::Sub;

use crate::{helper::get_discount, msg::ReplyType};

use super::*;
Expand All @@ -7,6 +9,7 @@ use cosmwasm_std::{
};
use cw_utils;
use elys_bindings::query_resp::{Entry, QueryGetEntryResponse};
use query_resp::PerpetualParams;
use PerpetualOrderType::*;

pub fn create_perpetual_order(
Expand Down Expand Up @@ -217,6 +220,21 @@ fn create_perpetual_open_order(

if order_type != MarketOpen {
let number_of_pending_order = NUMBER_OF_PENDING_ORDER.load(deps.storage)? + 1;
let PerpetualParams {
max_limit_order, ..
} = querier
.query_perpetual_params()?
.params
.ok_or(StdError::generic_err("Perpetual Params are not present"))?;
if number_of_pending_order
.sub(&(max_limit_order as u64))
.gt(&0u64)
{
return Err(ContractError::StdError(StdError::generic_err(
"Number of pending orders cannot be greater than maximum number of limit order",
)));
}

NUMBER_OF_PENDING_ORDER.save(deps.storage, &number_of_pending_order)?;

return Ok(resp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn instantiate(
deps: DepsMut<ElysQuery>,
_env: Env,
_info: MessageInfo,
msg: InstantiateMsg,
_msg: InstantiateMsg,
) -> StdResult<Response<ElysMsg>> {
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
MAX_REPLY_ID.save(deps.storage, &0)?;
Expand Down
2 changes: 1 addition & 1 deletion contracts/trade-shield-contract/src/entry_point/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use semver::Version;
pub fn migrate(
deps: DepsMut<ElysQuery>,
_env: Env,
msg: MigrateMsg,
_msg: MigrateMsg,
) -> StdResult<Response<ElysMsg>> {
let admin = "elys16xffmfa6k45j340cx5zyp66lqvuw62a0neaa7w".to_string();
PARAMS_ADMIN.save(deps.storage, &admin)?;
Expand Down

0 comments on commit 4702b20

Please sign in to comment.