Skip to content

Commit

Permalink
Add HALF keyword support to exchange deposit
Browse files Browse the repository at this point in the history
  • Loading branch information
mvines committed Jan 5, 2024
1 parent 73f1ca4 commit 4763f6f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
26 changes: 26 additions & 0 deletions src/amount.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
pub enum Amount {
Half,
All,
Exact(u64),
}

impl Amount {
pub fn unwrap_or(self, all_amount: u64) -> u64 {
match self {
Self::All => all_amount,
Self::Half => all_amount / 2,
Self::Exact(exact) => exact,
}
}

pub fn unwrap_or_else<F>(self, f: F) -> u64
where
F: std::ops::FnOnce() -> u64,
{
if let Self::Exact(exact) = self {
exact
} else {
f()
}
}
}
12 changes: 7 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod amount;
mod db;
mod field_as_string;
mod get_transaction_balance_change;
Expand All @@ -6,7 +7,7 @@ mod rpc_client_utils;
mod stake_spreader;

use {
crate::get_transaction_balance_change::*,
crate::{amount::Amount, get_transaction_balance_change::*},
chrono::prelude::*,
chrono_humanize::HumanTime,
clap::{
Expand Down Expand Up @@ -390,7 +391,7 @@ async fn process_exchange_deposit<T: Signers>(
exchange_client: &dyn ExchangeClient,
token: MaybeToken,
deposit_address: Pubkey,
amount: Option<u64>,
amount: Amount,
from_address: Pubkey,
if_source_balance_exceeds: Option<u64>,
if_exchange_balance_less_than: Option<u64>,
Expand Down Expand Up @@ -4968,7 +4969,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.takes_value(true)
.validator(is_amount_or_all)
.required(true)
.help("Amount of liquidity tokens to deposit; accepts keyword ALL"),
.help("Amount of liquidity tokens to deposit; accepts keywords ALL and HALF"),
)
.arg(
Arg::with_name("liquidity_token")
Expand Down Expand Up @@ -6345,8 +6346,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
("deposit", Some(arg_matches)) => {
let token = MaybeToken::from(value_t!(arg_matches, "token", Token).ok());
let amount = match arg_matches.value_of("amount").unwrap() {
"ALL" => None,
amount => Some(token.amount(amount.parse().unwrap())),
"ALL" => Amount::All,
"HALF" => Amount::Half,
amount => Amount::Exact(token.amount(amount.parse().unwrap())),
};
let if_source_balance_exceeds =
value_t!(arg_matches, "if_source_balance_exceeds", f64)
Expand Down

0 comments on commit 4763f6f

Please sign in to comment.