From 980c8e33cb178b96f80a2762d4c69a05e21a3375 Mon Sep 17 00:00:00 2001 From: Jay Yu <103467857+jayy04@users.noreply.github.com> Date: Fri, 19 Jan 2024 08:27:47 -0500 Subject: [PATCH] market make --- .../dydx-messages-example/src/contract.rs | 44 ++++++++++++++----- protocol/wasmbinding/msg_plugin.go | 4 +- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/protocol/contracts/dydx-messages-example/src/contract.rs b/protocol/contracts/dydx-messages-example/src/contract.rs index e44328dd42..a5cdce80f1 100644 --- a/protocol/contracts/dydx-messages-example/src/contract.rs +++ b/protocol/contracts/dydx-messages-example/src/contract.rs @@ -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}; @@ -42,7 +42,7 @@ pub fn instantiate( #[cfg_attr(not(feature = "library"), entry_point)] pub fn execute( - deps: DepsMut, + deps: DepsMut, env: Env, info: MessageInfo, msg: ExecuteMsg, @@ -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 { @@ -90,7 +90,7 @@ pub fn execute( } fn execute_approve( - deps: DepsMut, + deps: DepsMut, env: Env, info: MessageInfo, quantity: Option, @@ -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, order: Order, ) -> Result, 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, ContractError> { +fn execute_refund(deps: DepsMut, env: Env, _info: MessageInfo) -> Result, 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 { diff --git a/protocol/wasmbinding/msg_plugin.go b/protocol/wasmbinding/msg_plugin.go index 458268edb9..6d68b9cb7d 100644 --- a/protocol/wasmbinding/msg_plugin.go +++ b/protocol/wasmbinding/msg_plugin.go @@ -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 { @@ -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 {