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

[spot] update bulk order placement response #7

Merged
merged 2 commits into from
Jul 28, 2022
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
2 changes: 1 addition & 1 deletion integration_tests/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ RUN mkdir repos
WORKDIR /repos
# Each Vortex commit needs to specify which sei-chain version it should be deployed on. Once sei-chain enters
# a more stable release cycle, we can change raw commit hash here to release tag.
RUN git clone https://github.com/sei-protocol/sei-chain.git && cd sei-chain && git checkout 5b5ff2ecf767a401fe94bb8c1c2762490a2a5416
RUN git clone https://github.com/sei-protocol/sei-chain.git && cd sei-chain && git checkout a2cb8833ff391dd6539147f1fa7f4fefa89c7724
WORKDIR /repos/sei-chain
RUN make install
RUN /go/bin/seid init integration --chain-id sei-chain
Expand Down
10 changes: 5 additions & 5 deletions integration_tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/CosmWasm/wasmd v0.27.0
github.com/sei-protocol/sei-chain v0.0.0-20220718204703-5b5ff2ecf767
github.com/sei-protocol/sei-chain v0.0.0-20220727214948-a2cb8833ff39
)

require (
Expand Down Expand Up @@ -122,12 +122,12 @@ require (
go.etcd.io/bbolt v1.3.6 // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect
golang.org/x/net v0.0.0-20220412020605-290c469a71a5 // indirect
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e // indirect
golang.org/x/sys v0.0.0-20220610221304-9f5ed59c137d // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/genproto v0.0.0-20220519153652-3a47de7e79bd // indirect
google.golang.org/grpc v1.46.2
google.golang.org/genproto v0.0.0-20220719170305-83ca9fad585f // indirect
google.golang.org/grpc v1.48.0
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
16 changes: 8 additions & 8 deletions integration_tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -875,8 +875,8 @@ github.com/sasha-s/go-deadlock v0.2.1-0.20190427202633-1595213edefa/go.mod h1:F7
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/seccomp/libseccomp-golang v0.9.1/go.mod h1:GbW5+tmTXfcxTToHLXlScSlAvWlF4P2Ca7zGrPiEpWo=
github.com/segmentio/fasthash v1.0.3/go.mod h1:waKX8l2N8yckOgmSsXJi7x1ZfdKZ4x7KRMzBtS3oedY=
github.com/sei-protocol/sei-chain v0.0.0-20220718204703-5b5ff2ecf767 h1:GVMfIUS2SON4FaXt/HKd3Vo6FPbU3AP9fOuqS8dpKo0=
github.com/sei-protocol/sei-chain v0.0.0-20220718204703-5b5ff2ecf767/go.mod h1:3nOBdY1eP+l/5aiKKsRjQl7pfsGRAY/h0AA9a0aA3YQ=
github.com/sei-protocol/sei-chain v0.0.0-20220727214948-a2cb8833ff39 h1:hpRQ2Jz+9hQ7aHkgs5Yt5qigFSDYFRkV9lZKq9hfcsM=
github.com/sei-protocol/sei-chain v0.0.0-20220727214948-a2cb8833ff39/go.mod h1:HC/pKRIWP0I3/6VkOHjHaaPWR1JjjM9SAZpPCJ3ziyc=
github.com/shirou/gopsutil v2.20.5+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
Expand Down Expand Up @@ -1178,8 +1178,8 @@ golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d/go.mod h1:9nx3DQGgdP8bBQD5qx
golang.org/x/net v0.0.0-20210903162142-ad29c8ab022f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210917221730-978cfadd31cf/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211208012354-db4efeb81f4b/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220412020605-290c469a71a5 h1:bRb386wvrE+oBNdF1d/Xh9mQrfQ4ecYhW5qJ5GvTGT4=
golang.org/x/net v0.0.0-20220412020605-290c469a71a5/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e h1:TsQ7F31D3bUCLeqPT0u+yjp1guoArKaNKmCr22PYgTQ=
golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down Expand Up @@ -1308,8 +1308,8 @@ golang.org/x/sys v0.0.0-20211124211545-fe61309f8881/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211210111614-af8b64212486/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad h1:ntjMns5wyP/fN65tdBD4g8J5w8n015+iIIs9rtjXkY0=
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220610221304-9f5ed59c137d h1:Zu/JngovGLVi6t2J3nmAf3AoTDwuzw85YZ3b9o4yU7s=
golang.org/x/sys v0.0.0-20220610221304-9f5ed59c137d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
Expand Down Expand Up @@ -1513,8 +1513,8 @@ google.golang.org/genproto v0.0.0-20211028162531-8db9c33dc351/go.mod h1:5CzLGKJ6
google.golang.org/genproto v0.0.0-20211118181313-81c1377c94b1/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc=
google.golang.org/genproto v0.0.0-20211206160659-862468c7d6e0/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc=
google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc=
google.golang.org/genproto v0.0.0-20220519153652-3a47de7e79bd h1:e0TwkXOdbnH/1x5rc5MZ/VYyiZ4v+RdVfrGMqEwT68I=
google.golang.org/genproto v0.0.0-20220519153652-3a47de7e79bd/go.mod h1:RAyBrSAP7Fh3Nc84ghnVLDPuV51xc9agzmm4Ph6i0Q4=
google.golang.org/genproto v0.0.0-20220719170305-83ca9fad585f h1:P8EiVSxZwC6xH2niv2N66aqwMtYFg+D54gbjpcqKJtM=
google.golang.org/genproto v0.0.0-20220719170305-83ca9fad585f/go.mod h1:GkXuJDJ6aQ7lnJcRF+SJVgFdQhypqgl3LB1C9vabdRE=
google.golang.org/grpc v1.33.2 h1:EQyQC3sa8M+p6Ulc8yy9SWSS2GVwyRc83gAbG8lrl4o=
google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc=
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0/go.mod h1:6Kw0yEErY5E/yWrBtf03jp27GLLJujG4z/JK95pnjjw=
Expand Down
5 changes: 3 additions & 2 deletions integration_tests/parser/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
dextypes "github.com/sei-protocol/sei-chain/x/dex/types"
dextypesutils "github.com/sei-protocol/sei-chain/x/dex/types/utils"
)

func ToSeiOrderPlacement(fundedOrder FundedOrder) dextypes.Order {
order := fundedOrder.Order
positionDirection, err := dextypes.GetPositionDirectionFromStr(order.PositionDirection)
positionDirection, err := dextypesutils.GetPositionDirectionFromStr(order.PositionDirection)
if err != nil {
panic(err)
}
orderType, err := dextypes.GetOrderTypeFromStr(order.OrderType)
orderType, err := dextypesutils.GetOrderTypeFromStr(order.OrderType)
if err != nil {
panic(err)
}
Expand Down
43 changes: 31 additions & 12 deletions src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use crate::balance::{get_balance, save_balance};
use crate::error::ContractError;
use crate::msg::{
BulkOrderPlacementsResponse, GetBalanceResponse, GetOrderResponse, InstantiateMsg, MigrateMsg,
QueryMsg, SudoMsg, ExecuteMsg,
BulkOrderPlacementsResponse, ExecuteMsg, GetBalanceResponse, GetOrderResponse, InstantiateMsg,
MigrateMsg, QueryMsg, SudoMsg, UnsuccessfulOrder,
};
use crate::order::{delete_order, get_order, save_order};
use crate::state::{DepositInfo, OrderPlacement, PositionDirection, SettlementEntry, LiquidationResponse};
use crate::state::{
DepositInfo, LiquidationResponse, OrderPlacement, PositionDirection, SettlementEntry,
};
use crate::utils::decimal_to_u128;
use cosmwasm_std::{
entry_point, to_binary, BankMsg, Binary, Coin, Deps, DepsMut, Env, MessageInfo, Response,
StdError, StdResult, Decimal,
entry_point, to_binary, BankMsg, Binary, Coin, Decimal, Deps, DepsMut, Env, MessageInfo,
Response, StdError, StdResult,
};
use cw2::set_contract_version;
use sei_cosmwasm::SeiQueryWrapper;
Expand Down Expand Up @@ -169,7 +171,7 @@ fn process_bulk_order_placements(
orders: Vec<OrderPlacement>,
deposits: Vec<DepositInfo>,
) -> Result<Response, ContractError> {
let mut unsuccessful_order_ids = vec![];
let mut unsuccessful_orders = vec![];
for deposit in deposits {
let mut balance = get_balance(
deps.storage,
Expand Down Expand Up @@ -210,7 +212,10 @@ fn process_bulk_order_placements(
order.price_denom.to_owned(),
order.id
));
unsuccessful_order_ids.push(order.id);
unsuccessful_orders.push(UnsuccessfulOrder {
id: order.id,
reason: "Insufficient balance".to_owned(),
});
continue;
}
balance.withheld += nominal;
Expand All @@ -224,7 +229,7 @@ fn process_bulk_order_placements(
}

let response = BulkOrderPlacementsResponse {
unsuccessful_order_ids: unsuccessful_order_ids,
unsuccessful_orders: unsuccessful_orders,
};
let serialized_json = match serde_json::to_string(&response) {
Ok(val) => val,
Expand Down Expand Up @@ -337,21 +342,35 @@ fn deposit(deps: DepsMut<SeiQueryWrapper>, info: MessageInfo) -> Result<Response
for coin in info.funds {
let mut balance = get_balance(deps.storage, account.to_owned(), coin.denom.to_owned());
balance.amount += Decimal::from_atomics(coin.amount, 0).unwrap();
save_balance(deps.storage, account.to_owned(), coin.denom.to_owned(), &balance)
save_balance(
deps.storage,
account.to_owned(),
coin.denom.to_owned(),
&balance,
)
}
Ok(Response::default())
}

fn withdraw(deps: DepsMut<SeiQueryWrapper>, info: MessageInfo, coins: Vec<Coin>) -> Result<Response, ContractError> {
fn withdraw(
deps: DepsMut<SeiQueryWrapper>,
info: MessageInfo,
coins: Vec<Coin>,
) -> Result<Response, ContractError> {
let account = info.sender.into_string();
for coin in coins.to_owned() {
let mut balance = get_balance(deps.storage, account.to_owned(), coin.denom.to_owned());
let amount = Decimal::from_atomics(coin.amount, 0).unwrap();
if balance.amount - balance.withheld < amount {
return Err(ContractError::InsufficientFund())
return Err(ContractError::InsufficientFund());
}
balance.amount -= amount;
save_balance(deps.storage, account.to_owned(), coin.denom.to_owned(), &balance)
save_balance(
deps.storage,
account.to_owned(),
coin.denom.to_owned(),
&balance,
)
}
let response = Response::new().add_message(BankMsg::Send {
to_address: account.to_owned(),
Expand Down
12 changes: 9 additions & 3 deletions src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::state::{
Balance, DepositInfo, LiquidationRequest, Order, OrderPlacement, SettlementEntry,
};
use cosmwasm_std::{Coin};
use cosmwasm_std::Coin;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -35,7 +35,13 @@ pub enum SudoMsg {

#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema)]
pub struct BulkOrderPlacementsResponse {
pub unsuccessful_order_ids: Vec<u64>,
pub unsuccessful_orders: Vec<UnsuccessfulOrder>,
}

#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema)]
pub struct UnsuccessfulOrder {
pub id: u64,
pub reason: String,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
Expand All @@ -59,5 +65,5 @@ pub struct GetOrderResponse {
#[serde(rename_all = "snake_case")]
pub enum ExecuteMsg {
Deposit {},
Withdraw {coins: Vec<Coin>, },
Withdraw { coins: Vec<Coin> },
}
1 change: 1 addition & 0 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub struct OrderPlacement {
pub order_type: i32,
pub position_direction: i32,
pub data: String,
pub status_description: String,
}

impl OrderPlacement {
Expand Down
40 changes: 24 additions & 16 deletions src/testing/tests.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::balance::get_balance;
use crate::contract::{instantiate, migrate, query, sudo, execute};
use crate::contract::{execute, instantiate, migrate, query, sudo};
use crate::error::ContractError;
use crate::msg::{
BulkOrderPlacementsResponse, GetBalanceResponse, GetOrderResponse, InstantiateMsg, MigrateMsg,
QueryMsg, SudoMsg, ExecuteMsg,
BulkOrderPlacementsResponse, ExecuteMsg, GetBalanceResponse, GetOrderResponse, InstantiateMsg,
MigrateMsg, QueryMsg, SudoMsg,
};
use crate::order::get_order;
use crate::state::{Balance, DepositInfo, OrderPlacement, PositionDirection, SettlementEntry};
Expand Down Expand Up @@ -72,19 +72,27 @@ fn test_deposit_withdraw() {
.unwrap();

let msg = ExecuteMsg::Deposit {};
execute(deps.as_mut(), mock_env(), mock_info("test", &vec![
Coin::new(100, "usei")
]), msg).unwrap();
execute(
deps.as_mut(),
mock_env(),
mock_info("test", &vec![Coin::new(100, "usei")]),
msg,
)
.unwrap();
let balance = get_balance(deps.as_ref().storage, "test".to_owned(), "usei".to_owned());
assert_eq!(balance.amount, Decimal::from_atomics(100u128, 0).unwrap());
assert_eq!(balance.withheld, Decimal::zero());

let msg = ExecuteMsg::Withdraw { coins: vec![Coin::new(150, "usei")] };
let msg = ExecuteMsg::Withdraw {
coins: vec![Coin::new(150, "usei")],
};
match execute(deps.as_mut(), mock_env(), mock_info("test", &[]), msg) {
Ok(_) => panic!("Withdrawing more than you have is no ok"),
Err(_) => (),
};
let msg = ExecuteMsg::Withdraw { coins: vec![Coin::new(99, "usei")] };
let msg = ExecuteMsg::Withdraw {
coins: vec![Coin::new(99, "usei")],
};
execute(deps.as_mut(), mock_env(), mock_info("test", &[]), msg).unwrap();
let balance = get_balance(deps.as_ref().storage, "test".to_owned(), "usei".to_owned());
assert_eq!(balance.amount, Decimal::one());
Expand Down Expand Up @@ -116,8 +124,8 @@ fn test_bulk_order_placements() {
let deserialized = base64::decode(base64).unwrap();
let str_data = str::from_utf8(&deserialized).unwrap();
let res: BulkOrderPlacementsResponse = serde_json::from_str(str_data).unwrap();
assert_eq!(1, res.unsuccessful_order_ids.len());
assert_eq!(0, res.unsuccessful_order_ids[0]);
assert_eq!(1, res.unsuccessful_orders.len());
assert_eq!(0, res.unsuccessful_orders[0].id);
match get_order(deps.as_mut().storage, 0) {
Ok(_) => panic!("Order shouldn't exist"),
Err(_) => (),
Expand All @@ -139,7 +147,7 @@ fn test_bulk_order_placements() {
let deserialized = base64::decode(base64).unwrap();
let str_data = str::from_utf8(&deserialized).unwrap();
let res: BulkOrderPlacementsResponse = serde_json::from_str(str_data).unwrap();
assert_eq!(0, res.unsuccessful_order_ids.len());
assert_eq!(0, res.unsuccessful_orders.len());
let order = get_order(deps.as_mut().storage, 0).unwrap();
assert_eq!(0, order.id);
let balance = get_balance(deps.as_ref().storage, "test".to_owned(), "usei".to_owned());
Expand All @@ -162,8 +170,8 @@ fn test_bulk_order_placements() {
let deserialized = base64::decode(base64).unwrap();
let str_data = str::from_utf8(&deserialized).unwrap();
let res: BulkOrderPlacementsResponse = serde_json::from_str(str_data).unwrap();
assert_eq!(1, res.unsuccessful_order_ids.len());
assert_eq!(1, res.unsuccessful_order_ids[0]);
assert_eq!(1, res.unsuccessful_orders.len());
assert_eq!(1, res.unsuccessful_orders[0].id);
match get_order(deps.as_mut().storage, 1) {
Ok(_) => panic!("Order shouldn't exist"),
Err(_) => (),
Expand All @@ -190,8 +198,8 @@ fn test_bulk_order_placements() {
let deserialized = base64::decode(base64).unwrap();
let str_data = str::from_utf8(&deserialized).unwrap();
let res: BulkOrderPlacementsResponse = serde_json::from_str(str_data).unwrap();
assert_eq!(1, res.unsuccessful_order_ids.len());
assert_eq!(2, res.unsuccessful_order_ids[0]);
assert_eq!(1, res.unsuccessful_orders.len());
assert_eq!(2, res.unsuccessful_orders[0].id);
match get_order(deps.as_mut().storage, 2) {
Ok(_) => panic!("Order shouldn't exist"),
Err(_) => (),
Expand All @@ -213,7 +221,7 @@ fn test_bulk_order_placements() {
let deserialized = base64::decode(base64).unwrap();
let str_data = str::from_utf8(&deserialized).unwrap();
let res: BulkOrderPlacementsResponse = serde_json::from_str(str_data).unwrap();
assert_eq!(0, res.unsuccessful_order_ids.len());
assert_eq!(0, res.unsuccessful_orders.len());
let order = get_order(deps.as_mut().storage, 2).unwrap();
assert_eq!(2, order.id);
let balance = get_balance(deps.as_ref().storage, "test".to_owned(), "uatom".to_owned());
Expand Down
1 change: 1 addition & 0 deletions src/testing/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub fn vanilla_order_placement() -> OrderPlacement {
order_type: 0,
position_direction: 0,
data: "{\"position_effect\":\"Open\"}".to_owned(),
status_description: "".to_owned(),
}
}

Expand Down