Skip to content

Commit

Permalink
market make
Browse files Browse the repository at this point in the history
  • Loading branch information
jayy04 committed Jan 19, 2024
1 parent 2263f3f commit 980c8e3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
44 changes: 34 additions & 10 deletions protocol/contracts/dydx-messages-example/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use cosmwasm_std::{
to_binary,
};
use cw2::set_contract_version;
use dydx_cosmwasm::{DydxQuerier, DydxQueryWrapper, MarketPrice, Order, OrderId, DydxMsg, SubaccountId};
use dydx_cosmwasm::{DydxQuerier, DydxQueryWrapper, MarketPrice, Order, OrderId, DydxMsg, SubaccountId, OrderSide};

use crate::error::ContractError;
use crate::msg::{ArbiterResponse, ExecuteMsg, InstantiateMsg, QueryMsg};
Expand Down Expand Up @@ -42,7 +42,7 @@ pub fn instantiate(

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
deps: DepsMut,
deps: DepsMut<DydxQueryWrapper>,
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
Expand All @@ -65,7 +65,7 @@ pub fn execute(
client_metadata,
condition_type,
conditional_order_trigger_subticks,
} => execute_place_order(
} => execute_market_make(
deps,
Order {
order_id: OrderId {
Expand All @@ -90,7 +90,7 @@ pub fn execute(
}

fn execute_approve(
deps: DepsMut,
deps: DepsMut<DydxQueryWrapper>,
env: Env,
info: MessageInfo,
quantity: Option<u64>,
Expand Down Expand Up @@ -122,18 +122,42 @@ fn execute_approve(
Ok(send_tokens(env.contract.address, config.recipient, amount, "approve"))
}

fn execute_place_order(
deps: DepsMut,
fn execute_market_make(
deps: DepsMut<DydxQueryWrapper>,
order: Order,
) -> Result<Response<DydxMsg>, ContractError> {
let place_order_msg = DydxMsg::PlaceOrder { order };

let querier = DydxQuerier::new(&deps.querier);
let res = querier.query_market_price(order.order_id.clob_pair_id);
let market_price = res.unwrap();

// Hard-code some values for BTC.
let exponent = market_price.exponent - (-9) + (-10) - (-6);
let subticks = market_price.price * 10i64.pow(exponent as u32);
// Round to the nearest multiple.
let buy_price = subticks as f64 * 0.99;
let sell_price = subticks as f64 * 1.01;
let rounded_buy_subticks = (buy_price.round() as u64) / 100000 * 100000;
let rounded_sell_subticks = (sell_price.round() as u64) / 100000 * 100000;

// Construct the buy order.
let mut buy_order = order.clone();
buy_order.subticks = rounded_buy_subticks;
buy_order.side = OrderSide::Buy;
let buy_order_msg = DydxMsg::PlaceOrder { order: buy_order };

// Construct the sell order.
let mut sell_order = order.clone();
sell_order.subticks = rounded_sell_subticks;
sell_order.side = OrderSide::Sell;
let sell_order_msg = DydxMsg::PlaceOrder { order: sell_order };

// Market make!
Ok(Response::new()
.add_message(place_order_msg)
.add_messages(vec![buy_order_msg, sell_order_msg])
.add_attribute("action", "place_order"))
}

fn execute_refund(deps: DepsMut, env: Env, _info: MessageInfo) -> Result<Response<DydxMsg>, ContractError> {
fn execute_refund(deps: DepsMut<DydxQueryWrapper>, env: Env, _info: MessageInfo) -> Result<Response<DydxMsg>, ContractError> {
let config = CONFIG.load(deps.storage)?;
// anyone can try to refund, as long as the contract is expired
if let Some(expiration) = config.expiration {
Expand Down
4 changes: 2 additions & 2 deletions protocol/wasmbinding/msg_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (m *CustomMessenger) placeOrder(
// Only process short term orders in CheckTx because short term order placements
// are never on chain.
if ctx.IsCheckTx() {
fmt.Println("Placing short term order")
fmt.Printf("Placing short term order: %+v\n", order)
_, _, err = m.clob.PlaceShortTermOrder(ctx, &clobtypes.MsgPlaceOrder{Order: order})
}
} else {
Expand All @@ -226,7 +226,7 @@ func (m *CustomMessenger) placeOrder(
return nil, nil, nil
}

fmt.Println("Placing stateful order")
fmt.Printf("Placing stateful order: %+v\n", order)
processProposerMatchesEvents := m.clob.GetProcessProposerMatchesEvents(ctx)

if err := m.clob.PlaceStatefulOrder(ctx, &clobtypes.MsgPlaceOrder{Order: order}); err != nil {
Expand Down

0 comments on commit 980c8e3

Please sign in to comment.