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

fix: okex get position error on empty api response #365

Merged
merged 3 commits into from
Apr 21, 2023
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
47 changes: 6 additions & 41 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ serde = { version = "1.0.158", features = ["derive"] }
serde_yaml = "0.9.19"
tokio = "1.26.0"
futures = "0.3.27"
tonic = "0.8.3"
tonic = "0.9.2"
url = { version = "2.3.1", features = ["serde"] }
rust_decimal = "1.29.0"
opentelemetry = "0.18.0"
Expand Down
3 changes: 3 additions & 0 deletions hedging/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ rust_decimal = "1.29.0"
uuid = "1.3.0"
serde_with = { version = "2.3.1", features = ["chrono_0_4"] }

# To fix vulnerability
h2 = "0.3.17"

[dev-dependencies]
anyhow = "1.0.70"
serial_test = "*"
2 changes: 0 additions & 2 deletions okex-client/src/client/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ pub enum OkexClientError {
WithdrawalIdDoesNotExist,
#[error("OkexClientError - NoLastPriceAvailable")]
NoLastPriceAvailable,
#[error("OkexClientError - NoPositionAvailable")]
NoPositionAvailable,
#[error("OkexClientError - NonParsablePositionData")]
NonParsablePositionData,
#[error("OkexClientError - DecimalConversion: {0}")]
Expand Down
24 changes: 16 additions & 8 deletions okex-client/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,9 +596,11 @@ impl OkexClient {
span.record("last_price", &tracing::field::display(&last));

// Position responses with data:
// No position on account: pos = 0 and everything else is empty
// Some position on account: pos, notional and last are properly populated
// Else: raise an error
// No position on account: pos = 0 and everything else is empty
// Some position on account: pos, notional and last are properly populated
// Else: raise an error
// Position responses without data:
// No position on account: successful api call, but no data
let d_result = pos.parse::<Decimal>();
let n_result = notional_usd.parse::<Decimal>();
let l_result = last.parse::<Decimal>();
Expand Down Expand Up @@ -629,7 +631,11 @@ impl OkexClient {
_ => Err(OkexClientError::NonParsablePositionData),
}
} else {
Err(OkexClientError::NoPositionAvailable)
Ok(PositionSize {
instrument_id: OkexInstrumentId::BtcUsdSwap,
usd_cents: Decimal::ZERO,
last_price_in_usd_cents: Decimal::ZERO,
})
}
}

Expand Down Expand Up @@ -660,8 +666,10 @@ impl OkexClient {
.await?;

match Self::extract_optional_response_data::<ClosePositionData>(response).await {
Err(OkexClientError::UnexpectedResponse { msg, .. })
if msg.starts_with("Position does not exist") =>
Err(OkexClientError::UnexpectedResponse { msg, code })
if code == "51023"
|| msg.starts_with("Position does not exist")
|| msg.starts_with("Position doesn't exist") =>
{
Ok(())
}
Expand Down Expand Up @@ -690,8 +698,8 @@ impl OkexClient {
let response_text = response.text().await?;
let OkexResponse { code, msg, data } =
serde_json::from_str::<OkexResponse<T>>(&response_text)?;
if let Some(data) = data {
if let Some(first) = data.into_iter().next() {
if code == "0" && data.is_some() {
if let Some(first) = data.unwrap().into_iter().next() {
return Ok(Some(first));
} else {
return Ok(None);
Expand Down
2 changes: 1 addition & 1 deletion price-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ chrono = { version = "0.4", features = [
"serde",
], default-features = false }
prost = "0.11"
tonic = "0.8"
tonic = "0.9"
axum-core = "0.3.3"
tokio = "1.26.0"
futures = "0.3.27"
Expand Down
2 changes: 1 addition & 1 deletion user-trades/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ serde_with = "2.3.1"

[dev-dependencies]
anyhow = "1.0.70"
serial_test = "1.0.0"
serial_test = "2.0.0"