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: okx deposit idempotency #449

Merged
merged 2 commits into from
Oct 23, 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
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions bria-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ tonic-build = { version = "0.8", features = ["prost"] }

[dev-dependencies]
anyhow = "1.0.70"
rand = { workspace = true }
3 changes: 2 additions & 1 deletion bria-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ impl BriaClient {
&mut self,
destination: String,
satoshis: rust_decimal::Decimal,
external_id: String,
) -> Result<String, BriaClientError> {
use rust_decimal::prelude::ToPrimitive;

Expand All @@ -109,7 +110,7 @@ impl BriaClient {
.abs()
.to_u64()
.ok_or(BriaClientError::CouldNotConvertSatoshisToU64)?,
external_id: None,
external_id: Some(external_id),
metadata: metadata.map(serde_json::from_value).transpose()?,
});

Expand Down
6 changes: 5 additions & 1 deletion bria-client/tests/bria_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ async fn send_onchain_payment() -> anyhow::Result<()> {
let destination = "bcrt1q5cwegu66cf344du3ffrvnwjz9u246xlydqezsa".to_string();
let satoshis = rust_decimal::Decimal::from(5000);

let id = client.send_onchain_payment(destination, satoshis).await?;
use rand::distributions::{Alphanumeric, DistString};
let external_id = Alphanumeric.sample_string(&mut rand::thread_rng(), 32);
let id = client
.send_onchain_payment(destination, satoshis, external_id)
.await?;
assert!(!id.is_empty());

Ok(())
Expand Down
15 changes: 8 additions & 7 deletions hedging/src/okex/job/adjust_funding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,16 @@ pub(super) async fn execute(
if let Some(client_id) =
okex_transfers.reserve_transfer_slot(reservation).await?
{
span.record(
"client_transfer_id",
&tracing::field::display(String::from(client_id)),
);
let client_transfer_id = String::from(client_id.clone());
span.record("client_transfer_id", &client_transfer_id);

let amount_in_sats = amount * SATS_PER_BTC;
let _ = bria
.send_onchain_payment(deposit_address, amount_in_sats)
.await?;
bria.send_onchain_payment(
deposit_address,
amount_in_sats,
client_transfer_id,
)
.await?;
}
}
OkexFundingAdjustment::OnchainWithdraw(amount) => {
Expand Down
13 changes: 11 additions & 2 deletions okex-client/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ impl OkexClient {
})
}

/// https://www.okx.com/docs-v5/en/#funding-account-rest-api-get-balance
#[instrument(name = "okex_client.funding_account_balance", skip(self), err)]
pub async fn funding_account_balance(&self) -> Result<AvailableBalance, OkexClientError> {
let request_path = "/api/v5/asset/balances?ccy=BTC";
Expand Down Expand Up @@ -403,7 +404,13 @@ impl OkexClient {
})
}

#[instrument(name = "okex_client.fetch_deposit", skip(self), err)]
/// https://www.okx.com/docs-v5/en/#funding-account-rest-api-get-deposit-history
#[instrument(
name = "okex_client.fetch_deposit",
fields(deposit_found, okex_deposit_state),
skip(self),
err
)]
pub async fn fetch_deposit(
&self,
depo_addr: String,
Expand All @@ -428,10 +435,12 @@ impl OkexClient {
});

if let Some(deposit_data) = deposit {
tracing::Span::current().record("deposit_found", true);
tracing::Span::current().record("okex_deposit_state", &deposit_data.state);
Ok(DepositStatus {
state: match &deposit_data.state[..] {
"0" => "pending".to_string(), // waiting for confirmation
"1" => "success".to_string(), // deposit credited, cannot withdraw
"1" => "pending".to_string(), // deposit credited, cannot withdraw
Copy link
Member Author

Choose a reason for hiding this comment

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

Here is the root cause

"2" => "success".to_string(), // deposit successful, can withdraw
"8" => "pending".to_string(), // pending due to temporary deposit suspension on this crypto currency
"12" => "pending".to_string(), // account or deposit is frozen
Expand Down