Skip to content

Commit dc3c442

Browse files
committed
sys-lend: more cleanup
1 parent 1ad18f6 commit dc3c442

File tree

1 file changed

+42
-39
lines changed

1 file changed

+42
-39
lines changed

src/bin/sys-lend.rs

+42-39
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
357357
.help("Wallet"),
358358
)
359359
.arg(
360-
Arg::with_name("amount")
360+
Arg::with_name("ui_amount")
361361
.value_name("AMOUNT")
362362
.takes_value(true)
363363
.validator(is_amount_or_all)
@@ -410,7 +410,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
410410
.help("Wallet"),
411411
)
412412
.arg(
413-
Arg::with_name("amount")
413+
Arg::with_name("ui_amount")
414414
.value_name("AMOUNT")
415415
.takes_value(true)
416416
.validator(is_amount_or_all)
@@ -456,7 +456,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
456456
.help("Wallet"),
457457
)
458458
.arg(
459-
Arg::with_name("amount")
459+
Arg::with_name("ui_amount")
460460
.value_name("AMOUNT")
461461
.takes_value(true)
462462
.validator(is_amount_or_all)
@@ -792,7 +792,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
792792
let minimum_apy_bps = value_t!(matches, "minimum_apy", u16).unwrap_or(0);
793793

794794
let token_balance = maybe_token.balance(rpc_client, &address)?;
795-
let amount = match matches.value_of("amount").unwrap() {
795+
let requested_amount = match matches.value_of("ui_amount").unwrap() {
796796
"ALL" => {
797797
if cmd == Command::Deposit {
798798
token_balance.saturating_sub(if maybe_token.is_sol() {
@@ -804,24 +804,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
804804
u64::MAX
805805
}
806806
}
807-
amount => token.amount(amount.parse::<f64>().unwrap()),
807+
ui_amount => token.amount(ui_amount.parse::<f64>().unwrap()),
808808
};
809809

810-
if cmd == Command::Deposit {
811-
if amount > token_balance {
812-
return Err(format!(
813-
"Deposit amount of {} is greater than current balance of {}",
814-
maybe_token.format_amount(amount),
815-
maybe_token.format_amount(token_balance),
816-
)
817-
.into());
818-
}
819-
if amount == 0 {
820-
println!("Nothing to deposit");
821-
return Ok(());
822-
}
823-
}
824-
825810
is_token_supported(&token, &pools)?;
826811

827812
let supply_balance = pools
@@ -872,48 +857,66 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
872857
// Deposit pool has the highest APR
873858
let deposit_pool = pools.last().ok_or("No available pool")?;
874859

875-
// Withdraw pool has the lowest APR and a balance >= the requested `amount`
860+
// Withdraw pool has the lowest APR and a balance >= `requested_amount`
876861
let withdraw_pool = pools
877862
.iter()
878863
.find(|pool| {
879864
let balance = *supply_balance.get(*pool).unwrap();
880865

881-
if amount == u64::MAX {
866+
if requested_amount == u64::MAX {
882867
balance > 1 // Solend/Kamino leave 1 in sometimes :-/
883868
} else {
884-
balance >= amount
869+
balance >= requested_amount
885870
}
886871
})
887872
.unwrap_or(deposit_pool);
888873

889-
let ops = match cmd {
874+
let (ops, mut amount) = match cmd {
890875
Command::Deposit => {
891-
vec![(Operation::Deposit, deposit_pool)]
876+
if requested_amount > token_balance {
877+
return Err(format!(
878+
"Deposit amount of {} is greater than current balance of {}",
879+
maybe_token.format_amount(requested_amount),
880+
maybe_token.format_amount(token_balance),
881+
)
882+
.into());
883+
}
884+
if requested_amount == 0 {
885+
println!("Nothing to deposit");
886+
return Ok(());
887+
}
888+
889+
(vec![(Operation::Deposit, deposit_pool)], requested_amount)
892890
}
893-
Command::Withdraw => vec![(Operation::Withdraw, withdraw_pool)],
894-
Command::Rebalance => {
895-
if withdraw_pool == deposit_pool {
896-
println!("Nothing to rebalance");
891+
Command::Withdraw | Command::Rebalance => {
892+
let mut requested_amount = requested_amount;
893+
if requested_amount == u64::MAX {
894+
requested_amount = *supply_balance.get(withdraw_pool).unwrap();
895+
}
896+
897+
if requested_amount == 0 {
898+
println!("Nothing to withdraw");
897899
return Ok(());
898900
}
899901

900-
vec![
901-
(Operation::Withdraw, withdraw_pool),
902-
(Operation::Deposit, deposit_pool),
903-
]
902+
(
903+
if cmd == Command::Withdraw {
904+
vec![(Operation::Withdraw, withdraw_pool)]
905+
} else {
906+
vec![
907+
(Operation::Withdraw, withdraw_pool),
908+
(Operation::Deposit, deposit_pool),
909+
]
910+
},
911+
requested_amount,
912+
)
904913
}
905914
};
906915

907916
let mut instructions = vec![];
908917
let mut address_lookup_tables = vec![];
909918
let mut required_compute_units = 0;
910-
let mut amount = amount;
911919
for (op, pool) in ops {
912-
if amount == u64::MAX {
913-
assert_eq!(op, Operation::Withdraw);
914-
amount = *supply_balance.get(withdraw_pool).unwrap();
915-
}
916-
917920
let result = if pool.starts_with("kamino-") {
918921
kamino_deposit_or_withdraw(
919922
op,

0 commit comments

Comments
 (0)