Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OTE-407][OTE-409][OTE-426]Update wasm bindings to use custom encoder #1809

Merged
merged 3 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 1 addition & 34 deletions protocol/contracts/dydx-messages-example/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,40 +37,7 @@ pub fn execute(
) -> Result<Response<DydxMsg>, ContractError> {
match msg {
ExecuteMsg::DydxMsg(dydx_msg) => {
match dydx_msg{
DydxMsg::DepositToSubaccount { sender, recipient, asset_id, quantums } => {
let deposit = DydxMsg::DepositToSubaccount {
sender: sender,
recipient: recipient,
asset_id: asset_id,
quantums: quantums,
};
Ok(Response::new().add_message(deposit))
}
DydxMsg::WithdrawFromSubaccount { sender, recipient, asset_id, quantums } => {
let withdraw = DydxMsg::WithdrawFromSubaccount {
sender: sender,
recipient: recipient,
asset_id: asset_id,
quantums: quantums,
};
Ok(Response::new().add_message(withdraw))
}
DydxMsg::PlaceOrder { order } => {
let place_order = DydxMsg::PlaceOrder {
order: order,
};
Ok(Response::new().add_message(place_order))
}
DydxMsg::CancelOrder { order_id, good_til_oneof } => {
let cancel_order = DydxMsg::CancelOrder {
order_id: order_id,
good_til_oneof: good_til_oneof,
};
Ok(Response::new().add_message(cancel_order))
}
_ => Err(ContractError::InvalidDydxMsg{}),
}
Ok(Response::new().add_message(dydx_msg))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion protocol/contracts/dydx-messages-example/src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::{Addr, Coin};
use dydx_cosmwasm::{OrderConditionType, OrderSide, OrderTimeInForce, SubaccountId, Order, OrderId, Transfer};
use dydx_cosmwasm::{OrderConditionType, OrderSide, OrderTimeInForce, SubaccountId};
use cw_utils::Expiration;
use dydx_cosmwasm::MarketPriceResponse;
use dydx_cosmwasm::DydxMsg;
Expand Down
2 changes: 1 addition & 1 deletion protocol/dydx-cosmwasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod dydx_types;
mod proto_structs;
mod serializable_int;

pub use msg::{DydxMsg, Transfer, Order, OrderSide, OrderTimeInForce, OrderConditionType, OrderId};
pub use msg::{DydxMsg, OrderSide, OrderTimeInForce, OrderConditionType};
pub use querier::DydxQuerier;
pub use query::{
MarketPriceResponse, PerpetualClobDetailsResponse, SubaccountResponse, DydxQuery, DydxQueryWrapper};
Expand Down
156 changes: 105 additions & 51 deletions protocol/dydx-cosmwasm/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,13 @@ use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_repr::*;
use cosmwasm_std::{
to_json_string,
CosmosMsg,
CustomMsg,
};

use crate::SubaccountId;

// TODO(OTE-407): handle issue with `GoodTilOneof` in `PlaceOrder` and `CancelOrder` not serializing correctly

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct Transfer {
pub sender: SubaccountId,
pub recipient: SubaccountId,
pub asset_id: u32,
pub amount: u64,
}

#[derive(Serialize_repr, Deserialize_repr, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[repr(u32)]
Expand All @@ -43,61 +35,46 @@ pub enum OrderConditionType {
TakeProfit = 2,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct OrderId {
pub subaccount_id: SubaccountId,
pub client_id: u32,
pub order_flags: u32,
pub clob_pair_id: u32,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct Order {
pub order_id: OrderId,
pub side: OrderSide,
pub quantums: u64,
pub subticks: u64,
pub good_til_oneof: GoodTilOneof,
pub time_in_force: OrderTimeInForce,
pub reduce_only: bool,
pub client_metadata: u32,
pub condition_type: OrderConditionType,
pub conditional_order_trigger_subticks: u64,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum GoodTilOneof {
GoodTilBlock(u32),
GoodTilBlockTime(u32),
}


#[non_exhaustive]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum DydxMsg {
affanv14 marked this conversation as resolved.
Show resolved Hide resolved
CreateTransfer {
transfer: Transfer,
},
DepositToSubaccount {
sender: String,
// Maps to https://github.com/dydxprotocol/v4-chain/blob/main/proto/dydxprotocol/sending/transfer.proto#L31 message on protocol
DepositToSubaccountV1 {
recipient: SubaccountId,
asset_id: u32,
quantums: u64,
},
WithdrawFromSubaccount {
sender: SubaccountId,
// Maps to https://github.com/dydxprotocol/v4-chain/blob/main/proto/dydxprotocol/sending/transfer.proto#L50 message on protocol
WithdrawFromSubaccountV1 {
subaccount_number: u32,
recipient: String,
asset_id: u32,
quantums: u64,
},
PlaceOrder {
order: Order,
// Maps to https://github.com/dydxprotocol/v4-chain/blob/main/proto/dydxprotocol/clob/tx.proto#L78 message on protocol
PlaceOrderV1{
subaccount_number: u32,
client_id: u32,
order_flags: u32,
clob_pair_id: u32,
side: OrderSide,
quantums: u64,
subticks: u64,
good_til_block_time: u32,
time_in_force: OrderTimeInForce,
reduce_only: bool,
client_metadata: u32,
condition_type: OrderConditionType,
conditional_order_trigger_subticks: u64,
},
CancelOrder {
order_id: OrderId,
good_til_oneof: GoodTilOneof,
// Maps to https://github.com/dydxprotocol/v4-chain/blob/main/proto/dydxprotocol/clob/tx.proto#L84 on protocol
CancelOrderV1 {
subaccount_number: u32,
client_id: u32,
order_flags: u32,
clob_pair_id: u32,
good_til_block_time: u32,
}
}

Expand All @@ -108,3 +85,80 @@ impl From<DydxMsg> for CosmosMsg<DydxMsg> {
}

impl CustomMsg for DydxMsg {}

#[cfg(test)]
affanv14 marked this conversation as resolved.
Show resolved Hide resolved
mod tests {
use super::*;

#[test]
fn deposit_to_subaccount_msg_json_validation() {
let msg: DydxMsg = DydxMsg::DepositToSubaccountV1 {
recipient: SubaccountId {
owner: "b".to_string(),
number: 0,
},
asset_id: 0,
quantums: 10000000000,
};
let json = to_json_string(&msg).unwrap();
assert_eq!(
String::from_utf8_lossy(&json),
r#"{"deposit_to_subaccount_v1":{"recipient":{"owner":"b","number":0},"asset_id":0,"quantums":10000000000}}"#
);
}

#[test]
fn withdraw_from_subaccount_msg_json_validation() {
let msg: DydxMsg = DydxMsg::WithdrawFromSubaccountV1 {
subaccount_number: 0,
recipient: "b".to_string(),
asset_id: 0,
quantums: 10000000000,
};
let json = to_json_string(&msg).unwrap();
assert_eq!(
String::from_utf8_lossy(&json),
r#"{"withdraw_from_subaccount_v1":{"subaccount_number":0,"recipient":"b","asset_id":0,"quantums":10000000000}}"#
);
}

#[test]
fn place_order_msg_json_validation() {
let msg: DydxMsg = DydxMsg::PlaceOrderV1 {
subaccount_number: 0,
client_id: 0,
order_flags: 0,
clob_pair_id: 0,
side: OrderSide::Buy,
quantums: 10000000000,
subticks: 10000000000,
good_til_block_time: 0,
time_in_force: OrderTimeInForce::Ioc,
reduce_only: false,
client_metadata: 0,
condition_type: OrderConditionType::StopLoss,
conditional_order_trigger_subticks: 10000000000,
};
let json = to_json_string(&msg).unwrap();
assert_eq!(
String::from_utf8_lossy(&json),
r#"{"place_order_v1":{"subaccount_number":0,"client_id":0,"order_flags":0,"clob_pair_id":0,"side":1,"quantums":10000000000,"subticks":10000000000,"good_til_block_time":0,"time_in_force":1,"reduce_only":false,"client_metadata":0,"condition_type":1,"conditional_order_trigger_subticks":10000000000}}"#
);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure you want to keep the enums (OrderSide::Buy, OrderTimeInForce::Ioc, OrderConditionType::StopLoss) as integers?

You can turn them into strings in JSON like this. But that's a matter of taste.

  1. make order (de)serialize CosmWasm/cosmwasm#2174
  2. tests and docs for Order serialization CosmWasm/cosmwasm#2177

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is what is expected from the protocol side - so wanted to keep things consistent

}

#[test]
fn cancel_order_msg_json_validation() {
let msg: DydxMsg = DydxMsg::CancelOrderV1 {
subaccount_number: 0,
client_id: 0,
order_flags: 0,
clob_pair_id: 0,
good_til_block_time: 0,
};
let json = to_json_string(&msg).unwrap();
assert_eq!(
String::from_utf8_lossy(&json),
r#"{"cancel_order_v1":{"subaccount_number":0,"client_id":0,"order_flags":0,"clob_pair_id":0,"good_til_block_time":0}}"#
);
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like you are not running cargo fmt. Would be good to do that in editor and do CI checks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good callout! added a task for myself to do this

51 changes: 42 additions & 9 deletions protocol/wasmbinding/bindings/msg.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,48 @@
package bindings

import (
sendingtypes "github.com/dydxprotocol/v4-chain/protocol/x/sending/types"

clobtypes "github.com/dydxprotocol/v4-chain/protocol/x/clob/types"
subaccounttypes "github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types"
)

type SendingMsg struct {
CreateTransfer *sendingtypes.MsgCreateTransfer `json:"create_transfer,omitempty"`
DepositToSubaccount *sendingtypes.MsgDepositToSubaccount `json:"deposit_to_subaccount,omitempty"`
WithdrawFromSubaccount *sendingtypes.MsgWithdrawFromSubaccount `json:"withdraw_from_subaccount,omitempty"`
PlaceOrder *clobtypes.MsgPlaceOrder `json:"place_order,omitempty"`
CancelOrder *clobtypes.MsgCancelOrder `json:"cancel_order,omitempty"`
type DydxCustomWasmMessage struct {
DepositToSubaccountV1 *DepositToSubaccountV1 `json:"deposit_to_subaccount_v1,omitempty"`
WithdrawFromSubaccountV1 *WithdrawFromSubaccountV1 `json:"withdraw_from_subaccount_v1,omitempty"`
PlaceOrderV1 *PlaceOrderV1 `json:"place_order_v1,omitempty"`
CancelOrderV1 *CancelOrderV1 `json:"cancel_order_v1,omitempty"`
}

type DepositToSubaccountV1 struct {
Recipient subaccounttypes.SubaccountId `json:"recipient"`
AssetId uint32 `json:"asset_id"`
Quantums uint64 `json:"quantums"`
}

type WithdrawFromSubaccountV1 struct {
SubaccountNumber uint32 `json:"subaccount_number"`
Recipient string `json:"recipient"`
AssetId uint32 `json:"asset_id"`
Quantums uint64 `json:"quantums"`
}

type PlaceOrderV1 struct {
SubaccountNumber uint32 `json:"subaccount_number"`
ClientId uint32 `json:"client_id"`
OrderFLags uint32 `json:"order_flags"`
ClobPairId uint32 `json:"clob_pair_id"`
Side int32 `json:"side"`
Quantums uint64 `json:"quantums"`
Subticks uint64 `json:"subticks"`
GoodTilBlockTime uint32 `json:"good_til_block_time"`
ReduceOnly bool `json:"reduce_only"`
ClientMetadata uint32 `json:"client_metadata"`
ConditionType int32 `json:"condition_type"`
ConditionalOrderTriggerSubticks uint64 `json:"conditional_order_trigger_subticks"`
affanv14 marked this conversation as resolved.
Show resolved Hide resolved
}

type CancelOrderV1 struct {
SubaccountNumber uint32 `json:"subaccount_number"`
ClientId uint32 `json:"client_id"`
OrderFLags uint32 `json:"order_flags"`
ClobPairId uint32 `json:"clob_pair_id"`
GoodTilBlockTime uint32 `json:"good_til_block_time"`
}
Loading
Loading