Skip to content

Commit

Permalink
more auth check
Browse files Browse the repository at this point in the history
  • Loading branch information
jbernal87 committed Mar 4, 2024
1 parent 21a8bc7 commit ed4cef1
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 85 deletions.
69 changes: 33 additions & 36 deletions contracts/injective-cosmwasm-mock/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use crate::{
error::ContractError,
msg::{ExecuteMsg, InstantiateMsg, QueryMsg},
};
use cosmwasm_std::{to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult, SubMsg, ReplyOn, Reply};
use crate::{error::ContractError, msg::{ExecuteMsg, InstantiateMsg, QueryMsg}, types};
use cosmwasm_std::{to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, Reply, ReplyOn, Response, StdResult, SubMsg};

Check warning on line 2 in contracts/injective-cosmwasm-mock/src/contract.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `ReplyOn`

Check warning on line 2 in contracts/injective-cosmwasm-mock/src/contract.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `ReplyOn`
use cw2::set_contract_version;
use injective_cosmwasm::{create_deposit_msg, create_spot_market_order_msg, InjectiveMsgWrapper, InjectiveQuerier, InjectiveQueryWrapper, OrderInfo, OrderType, SpotOrder};

use injective_cosmwasm::{
create_deposit_msg, InjectiveMsgWrapper, InjectiveQuerier, InjectiveQueryWrapper, OrderType
};
use prost::Message;

use crate::order_management::{create_spot_market_order, create_stargate_msg};
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use injective_math::FPDecimal;
use crate::msg::MSG_CREATE_SPOT_MARKET_ORDER_ENDPOINT;

const CONTRACT_NAME: &str = "crates.io:injective:dummy";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
Expand All @@ -24,7 +25,7 @@ pub fn instantiate(deps: DepsMut, _env: Env, _info: MessageInfo, _msg: Instantia

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
_deps: DepsMut<InjectiveQueryWrapper>,
deps: DepsMut<InjectiveQueryWrapper>,
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
Expand All @@ -34,28 +35,29 @@ pub fn execute(
Ok(Response::new().add_message(create_deposit_msg(env.contract.address, subaccount_id, amount)))
}
ExecuteMsg::TestTraderTransientSpotOrders { market_id, subaccount_id } => {
let order_info = OrderInfo{
subaccount_id,
fee_recipient: None,
price: FPDecimal::must_from_str("1"),
quantity: FPDecimal::must_from_str("1"),
cid: None,
};
let spot_order = SpotOrder{
market_id,
order_info,
order_type: OrderType::Buy,
trigger_price: None
};
let spot_order_message = create_spot_market_order_msg(info.sender, spot_order);
let querier: InjectiveQuerier = InjectiveQuerier::new(&deps.querier);
let spot_market = querier.query_spot_market(&market_id).unwrap().market.unwrap();

deps.api.debug(&info.sender.as_str());
let order_msg = create_spot_market_order(
FPDecimal::must_from_str("1"),
FPDecimal::must_from_str("1"),
OrderType::Buy,
&info.sender.as_str(),
subaccount_id.as_str(),
&spot_market,
);

let mut order_bytes = vec![];
types::MsgCreateSpotMarketOrder::encode(&order_msg, &mut order_bytes).unwrap();

let order_submessage = SubMsg::reply_on_success(
create_stargate_msg(MSG_CREATE_SPOT_MARKET_ORDER_ENDPOINT, order_bytes).unwrap(),
CREATE_SPOT_ORDER_REPLY_ID,
);

Ok(Response::new().add_submessage(order_submessage))

let spot_order_message = SubMsg{
id: CREATE_SPOT_ORDER_REPLY_ID,
msg: spot_order_message,
gas_limit: None,
reply_on: ReplyOn::Success,
};
Ok(Response::new().add_submessage(spot_order_message))
}
ExecuteMsg::TestTraderTransientDerivativeOrders { market_id, subaccount_id } => {

Check warning on line 62 in contracts/injective-cosmwasm-mock/src/contract.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused variable: `market_id`

Check warning on line 62 in contracts/injective-cosmwasm-mock/src/contract.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused variable: `subaccount_id`

Check warning on line 62 in contracts/injective-cosmwasm-mock/src/contract.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused variable: `market_id`

Check warning on line 62 in contracts/injective-cosmwasm-mock/src/contract.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused variable: `subaccount_id`
// to_json_binary(&querier.query_trader_transient_derivative_orders(&market_id, &subaccount_id)?)
Expand Down Expand Up @@ -140,18 +142,13 @@ pub fn query(deps: Deps<InjectiveQueryWrapper>, _env: Env, msg: QueryMsg) -> Std
}
}


#[cfg_attr(not(feature = "library"), entry_point)]
pub fn reply(
deps: DepsMut<InjectiveQueryWrapper>,
env: Env,
msg: Reply,
) -> Result<Response, ContractError> {
pub fn reply(deps: DepsMut<InjectiveQueryWrapper>, env: Env, msg: Reply) -> Result<Response, ContractError> {

Check warning on line 146 in contracts/injective-cosmwasm-mock/src/contract.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused variable: `env`

Check warning on line 146 in contracts/injective-cosmwasm-mock/src/contract.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused variable: `env`
match msg.id {
CREATE_SPOT_ORDER_REPLY_ID => {
deps.api.debug("I am here");
Ok(Default::default())
},
}
_ => Err(ContractError::UnrecognizedReply(msg.id)),
}
}
2 changes: 2 additions & 0 deletions contracts/injective-cosmwasm-mock/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ pub mod msg;
#[cfg(test)]
mod testing;

mod order_management;
mod types;
#[cfg(test)]
pub mod utils;

Expand Down
13 changes: 5 additions & 8 deletions contracts/injective-cosmwasm-mock/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,18 @@ use injective_math::FPDecimal;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

pub const MSG_CREATE_SPOT_MARKET_ORDER_ENDPOINT: &str =
"/injective.exchange.v1beta1.MsgCreateSpotMarketOrder";

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct InstantiateMsg {}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ExecuteMsg {
TestDepositMsg { subaccount_id: SubaccountId, amount: Coin },
TestTraderTransientSpotOrders {
market_id: MarketId,
subaccount_id: SubaccountId,
},
TestTraderTransientDerivativeOrders {
market_id: MarketId,
subaccount_id: SubaccountId,
},
TestTraderTransientSpotOrders { market_id: MarketId, subaccount_id: SubaccountId },
TestTraderTransientDerivativeOrders { market_id: MarketId, subaccount_id: SubaccountId },
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
Expand Down
36 changes: 36 additions & 0 deletions contracts/injective-cosmwasm-mock/src/order_management.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use crate::types;
use cosmwasm_std::{to_json_binary, Coin, CosmosMsg, StdResult, SubMsg, Uint128, WasmMsg};

Check warning on line 2 in contracts/injective-cosmwasm-mock/src/order_management.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused imports: `Coin`, `SubMsg`, `Uint128`, `WasmMsg`, `to_json_binary`

Check warning on line 2 in contracts/injective-cosmwasm-mock/src/order_management.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused imports: `Coin`, `SubMsg`, `Uint128`, `WasmMsg`, `to_json_binary`
use injective_cosmwasm::{InjectiveMsgWrapper, OrderType, SpotMarket};
use injective_math::FPDecimal;


pub fn create_stargate_msg(type_url: &str, value: Vec<u8>) -> StdResult<CosmosMsg<InjectiveMsgWrapper>> {
Ok(CosmosMsg::Stargate {
type_url: type_url.to_string(),
value: value.into(),
})
}

pub fn create_spot_market_order(
price: FPDecimal,
quantity: FPDecimal,
order_type: OrderType,
sender: &str,
subaccount_id: &str,
market: &SpotMarket,
) -> types::MsgCreateSpotMarketOrder {
types::MsgCreateSpotMarketOrder {
sender: sender.to_string(),
order: Some(types::SpotOrder {
market_id: market.market_id.as_str().into(),
order_info: Some(types::OrderInfo {
subaccount_id: subaccount_id.to_string(),
fee_recipient: sender.to_string(),
price: price.to_string(),
quantity: quantity.to_string(),
}),
order_type: order_type as i32,
trigger_price: "".to_string(),
}),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
},
};

use crate::utils::execute_all_authorizations;
use cosmwasm_std::{Addr, Coin};
use injective_cosmwasm::exchange::response::QueryOrderbookResponse;
use injective_cosmwasm::exchange::types::VolumeByType;
Expand All @@ -25,7 +26,6 @@ use injective_std::types::injective::exchange::v1beta1::{
};
use injective_test_tube::injective_cosmwasm::get_default_subaccount_id_for_checked_address;
use injective_test_tube::{Account, Exchange, Module, RunnerResult, Wasm};
use crate::utils::execute_all_authorizations;

#[test]
#[cfg_attr(not(feature = "integration"), ignore)]
Expand Down Expand Up @@ -747,26 +747,21 @@ fn test_query_trader_transient_spot_orders() {

let subaccount_id = checked_address_to_subaccount_id(&Addr::unchecked(env.users[0].account.address()), 0u32);

execute_all_authorizations(
&env.app,
&env.users[0].account,
env.contract_address.clone(),
);
execute_all_authorizations(&env.app, &env.users[0].account, env.contract_address.clone());
println!("{}", &env.users[0].account.address());

let res = wasm.execute(
&env.contract_address,
&ExecuteMsg::TestTraderTransientSpotOrders {
market_id: MarketId::new(market_id).unwrap(),
subaccount_id: subaccount_id.clone(),

},
&[],
&env.users[0].account,
);

println!("{:?}", res);
assert_eq!(1,2);

assert_eq!(1, 2);
}

#[test]
Expand Down
97 changes: 97 additions & 0 deletions contracts/injective-cosmwasm-mock/src/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(
Clone,
PartialEq,
Eq,
::prost::Message,
::serde::Serialize,
::serde::Deserialize,
::schemars::JsonSchema,
// CosmwasmExt,
)]
// #[proto_message(type_url = "/injective.exchange.v1beta1.OrderInfo")]
pub struct OrderInfo {
/// bytes32 subaccount ID that created the order
#[prost(string, tag = "1")]
#[serde(alias = "subaccountID")]
pub subaccount_id: ::prost::alloc::string::String,
/// address fee_recipient address that will receive fees for the order
#[prost(string, tag = "2")]
pub fee_recipient: ::prost::alloc::string::String,
/// price of the order
#[prost(string, tag = "3")]
pub price: ::prost::alloc::string::String,
/// quantity of the order
#[prost(string, tag = "4")]
pub quantity: ::prost::alloc::string::String,
}

#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(
Clone,
PartialEq,
Eq,
::prost::Message,
::serde::Serialize,
::serde::Deserialize,
::schemars::JsonSchema,
// CosmwasmExt,
)]
// #[proto_message(type_url = "/injective.exchange.v1beta1.SpotOrder")]
pub struct SpotOrder {
/// market_id represents the unique ID of the market
#[prost(string, tag = "1")]
#[serde(alias = "marketID")]
pub market_id: ::prost::alloc::string::String,
/// order_info contains the information of the order
#[prost(message, optional, tag = "2")]
pub order_info: ::core::option::Option<OrderInfo>,
/// order types
#[prost(enumeration = "OrderType", tag = "3")]
// #[serde(
// serialize_with = "crate::serde::as_str::serialize",
// deserialize_with = "crate::serde::as_str::deserialize"
// )]
pub order_type: i32,
/// trigger_price is the trigger price used by stop/take orders
#[prost(string, tag = "4")]
pub trigger_price: ::prost::alloc::string::String,
}

/// MsgCreateSpotMarketOrder defines a SDK message for creating a new spot market
/// order.
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(
Clone,
PartialEq,
Eq,
::prost::Message,
::serde::Serialize,
::serde::Deserialize,
::schemars::JsonSchema,
// CosmwasmExt,
)]
// #[proto_message(type_url = "/injective.exchange.v1beta1.MsgCreateSpotMarketOrder")]
pub struct MsgCreateSpotMarketOrder {
#[prost(string, tag = "1")]
pub sender: ::prost::alloc::string::String,
#[prost(message, optional, tag = "2")]
pub order: ::core::option::Option<SpotOrder>,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
#[repr(i32)]
#[derive(::serde::Serialize, ::serde::Deserialize, ::schemars::JsonSchema)]
pub enum OrderType {
Unspecified = 0,
Buy = 1,
Sell = 2,
StopBuy = 3,
StopSell = 4,
TakeBuy = 5,
TakeSell = 6,
BuyPo = 7,
SellPo = 8,
BuyAtomic = 9,
SellAtomic = 10,
}
Loading

0 comments on commit ed4cef1

Please sign in to comment.