Skip to content

Commit

Permalink
chore: use jitter to dedup deposits
Browse files Browse the repository at this point in the history
  • Loading branch information
bodymindarts committed Oct 24, 2023
1 parent 7c05b42 commit 5776be2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
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 hedging/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ bitfinex-client = { path = "../bitfinex-client" }
bria-client = { path = "../bria-client" }
galoy-client = { path = "../galoy-client" }

rand = { workspace = true }
rust_decimal_macros = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
Expand Down
15 changes: 13 additions & 2 deletions hedging/src/okex/job/adjust_funding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const SATS_PER_BTC: Decimal = dec!(100_000_000);
#[instrument(name = "hedging.okex.job.adjust_funding", skip_all, fields(correlation_id = %correlation_id,
target_liability, current_position, last_price_in_usd_cents, funding_available_balance,
trading_available_balance, onchain_fees, action, client_transfer_id,
amount_with_jitter,
transferred_funding, lag_ok), err)]
pub(super) async fn execute(
correlation_id: CorrelationId,
Expand Down Expand Up @@ -134,10 +135,16 @@ pub(super) async fn execute(
return Ok(());
}

let amount_with_jitter = {
use rand::Rng;
let mut rng = rand::thread_rng();
let jitter: f64 = rng.gen_range(0.00000001..=0.00001);
amount + Decimal::try_from(jitter).expect("jitter")
};
let deposit_address = okex.get_funding_deposit_address().await?.value;
let reservation = TransferReservation {
shared: &shared,
action_size: Some(amount),
action_size: Some(amount_with_jitter),
fee: Decimal::ZERO,
transfer_from: "galoy".to_string(),
transfer_to: deposit_address.clone(),
Expand All @@ -147,8 +154,12 @@ pub(super) async fn execute(
{
let client_transfer_id = String::from(client_id.clone());
span.record("client_transfer_id", &client_transfer_id);
span.record(
"amount_with_jitter",
&tracing::field::display(amount_with_jitter),
);

let amount_in_sats = amount * SATS_PER_BTC;
let amount_in_sats = amount_with_jitter * SATS_PER_BTC;
bria.send_onchain_payment(
deposit_address,
amount_in_sats,
Expand Down

0 comments on commit 5776be2

Please sign in to comment.