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

WIP: Add public/get_last_trades_by_currency method, allow for pagination results #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ while let Some(message) = subscription.next().await {
- [x] /public/get_instruments
- [ ] /public/get_last_settlements_by_currency
- [ ] /public/get_last_settlements_by_instrument
- [ ] /public/get_last_trades_by_currency
- [X] /public/get_last_trades_by_currency
- [ ] /public/get_last_trades_by_currency_and_time
- [ ] /public/get_last_trades_by_instrument
- [ ] /public/get_last_trades_by_instrument_and_time
Expand Down
2 changes: 1 addition & 1 deletion src/api_client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::errors::{DeribitError, Result};
use crate::models::{JSONRPCRequest, JSONRPCResponse, Request};
use crate::models::{JSONRPCRequest, JSONRPCResponse, Request, WithPagination};
use crate::WSStream;
use anyhow::Error;
use fehler::throws;
Expand Down
4 changes: 2 additions & 2 deletions src/models/jsonrpc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::models::{Either, Request};
use crate::models::{Either, EitherWrapper, Request};
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize, Clone, Debug)]
Expand All @@ -16,7 +16,7 @@ pub struct JSONRPCResponse<R> {
pub id: i64,
pub testnet: bool,
#[serde(alias = "error")]
pub result: Either<R, ErrorDetail>,
pub result: EitherWrapper<R, ErrorDetail>,
pub us_in: u64,
pub us_out: u64,
pub us_diff: u64,
Expand Down
73 changes: 72 additions & 1 deletion src/models/market_data.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::models::{AssetKind, Currency, Request};
use crate::models::{AssetKind, Currency, Direction, Request, Sorting};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
Expand Down Expand Up @@ -246,3 +246,74 @@ pub enum State {
#[serde(alias = "closed")]
Closed,
}

#[derive(Deserialize, Serialize, Clone, Debug, Default)]
pub struct GetLastTradesByCurrencyRequest {
pub currency: Currency,
#[serde(skip_serializing_if = "Option::is_none")]
pub kind: Option<AssetKind>,
#[serde(skip_serializing_if = "Option::is_none")]
pub start_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub end_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub count: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub include_old: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub sorting: Option<Sorting>,
}

impl GetLastTradesByCurrencyRequest {
pub fn new(currency: Currency) -> Self {
Self {
currency,
..Default::default()
}
}

pub fn include_old(currency: Currency) -> Self {
Self {
currency,
include_old: Some(true),
..Default::default()
}
}

pub fn futures(currency: Currency) -> Self {
Self::with_kind(currency, AssetKind::Future)
}

pub fn options(currency: Currency) -> Self {
Self::with_kind(currency, AssetKind::Option)
}

pub fn with_kind(currency: Currency, kind: AssetKind) -> Self {
Self {
currency,
kind: Some(kind),
..Default::default()
}
}
}

#[derive(Deserialize, Serialize, Clone, Debug)]
pub struct GetLastTradesByCurrencyResponse {
pub amount: Option<f64>,
pub block_trade_id: Option<String>,
pub direction: Direction,
pub index_price: f64,
pub instrument_name: String,
pub iv: Option<f64>,
pub mark_price: f64,
pub price: f64,
pub trade_id: String,
pub timestamp: u64,
pub trade_seq: u64,
pub tick_direction: i64,
}

impl Request for GetLastTradesByCurrencyRequest {
const METHOD: &'static str = "public/get_last_trades_by_currency";
type Response = Vec<GetLastTradesByCurrencyResponse>;
Copy link
Owner

Choose a reason for hiding this comment

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

I think here you can make GetLastTradesByCurrencyResponse into

struct GetLastTradesByCurrencyResponse {
    trades: Vec<LastTrade>,
    has_more: bool
}

and in impl Request it can have

type Response =GetLastTradesByCurrencyResponse;

then

struct LastTrade {
  pub amount: Option<f64>,
    pub block_trade_id: Option<String>,
    pub direction: Direction,
    pub index_price: f64,
    pub instrument_name: String,
    pub iv: Option<f64>,
    pub mark_price: f64,
    pub price: f64,
    pub trade_id: String,
    pub timestamp: u64,
    pub trade_seq: u64,
    pub tick_direction: i64,
}

}
41 changes: 41 additions & 0 deletions src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,22 @@ impl<L, R> Either<L, R> {
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WithPagination<L, R> {
#[serde(alias = "trades")]
pub values: Either<L, R>,
pub has_more: bool,
}

impl<L, R> WithPagination<L, R> {
pub fn left_result(self) -> StdResult<L, R> {
match self.values {
Either::Left(l) => Ok(l),
Either::Right(r) => Err(r),
}
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Any3<O1, O2, O3> {
Expand Down Expand Up @@ -485,3 +501,28 @@ pub enum Any12<O1, O2, O3, O4, O5, O6, O7, O8, O9, O10, O11, O12> {
Eleventh(O11),
Twelfth(O12),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Sorting {
#[serde(rename = "asc")]
Asc,
#[serde(rename = "default")]
Default,
#[serde(rename = "desc")]
Desc,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum EitherWrapper<L, R> {
with_pagination(WithPagination<L, R>),
no_pagination(Either<L, R>),
}
impl<L, R> EitherWrapper<L, R> {
pub fn left_result(self) -> StdResult<L, R> {
match self {
EitherWrapper::with_pagination(l) => l.left_result(),
EitherWrapper::no_pagination(r) => r.left_result(),
}
}
}