Skip to content

Commit cc3dd4f

Browse files
committed
sys-lend: ensure destination token account exists
1 parent e06fdf2 commit cc3dd4f

File tree

1 file changed

+37
-13
lines changed

1 file changed

+37
-13
lines changed

src/bin/sys-lend.rs

+37-13
Original file line numberDiff line numberDiff line change
@@ -818,19 +818,35 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
818818
unreachable!();
819819
};
820820

821-
if maybe_token.is_sol() && op == Operation::Deposit {
822-
// Wrap SOL into wSOL
823-
instructions.extend(vec![
824-
spl_associated_token_account::instruction::create_associated_token_account_idempotent(
825-
&address,
826-
&address,
827-
&token.mint(),
828-
&spl_token::id(),
829-
),
830-
system_instruction::transfer(&address, &token.ata(&address), amount),
831-
spl_token::instruction::sync_native(&spl_token::id(), &token.ata(&address)).unwrap(),
832-
]);
833-
required_compute_units += 20_000;
821+
match op {
822+
Operation::Deposit => {
823+
if maybe_token.is_sol() {
824+
// Wrap SOL into wSOL
825+
instructions.extend(vec![
826+
spl_associated_token_account::instruction::create_associated_token_account_idempotent(
827+
&address,
828+
&address,
829+
&token.mint(),
830+
&spl_token::id(),
831+
),
832+
system_instruction::transfer(&address, &token.ata(&address), amount),
833+
spl_token::instruction::sync_native(&spl_token::id(), &token.ata(&address)).unwrap(),
834+
]);
835+
required_compute_units += 20_000;
836+
}
837+
}
838+
Operation::Withdraw => {
839+
// Ensure the destination token account exists
840+
instructions.push(
841+
spl_associated_token_account::instruction::create_associated_token_account_idempotent(
842+
&address,
843+
&address,
844+
&token.mint(),
845+
&spl_token::id(),
846+
),
847+
);
848+
required_compute_units += 25_000;
849+
}
834850
}
835851

836852
instructions.extend(result.instructions);
@@ -960,6 +976,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
960976
return Err(format!("Simulation failure: {simulation_result:?}").into());
961977
}
962978

979+
/*
980+
println!(
981+
"Transaction paid for {} extra additional compute units",
982+
required_compute_units as u64
983+
- simulation_result.units_consumed.unwrap_or_default()
984+
);
985+
*/
986+
963987
let signature = transaction.signatures[0];
964988

965989
let msg = match cmd {

0 commit comments

Comments
 (0)