Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit b124c5a

Browse files
committed
Refactor to always use checked_transaction_*
1 parent 238a67f commit b124c5a

File tree

1 file changed

+97
-115
lines changed

1 file changed

+97
-115
lines changed

stake-pool/cli/src/main.rs

Lines changed: 97 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -186,22 +186,34 @@ fn send_transaction(
186186
Ok(())
187187
}
188188

189-
fn checked_transaction_with_signers<T: Signers>(
189+
fn checked_transaction_with_signers_and_additional_fee<T: Signers>(
190190
config: &Config,
191191
instructions: &[Instruction],
192192
signers: &T,
193+
additional_fee: u64,
193194
) -> Result<Transaction, Error> {
194195
let recent_blockhash = get_latest_blockhash(&config.rpc_client)?;
195196
let message = Message::new_with_blockhash(
196197
instructions,
197198
Some(&config.fee_payer.pubkey()),
198199
&recent_blockhash,
199200
);
200-
check_fee_payer_balance(config, config.rpc_client.get_fee_for_message(&message)?)?;
201+
check_fee_payer_balance(
202+
config,
203+
additional_fee.saturating_add(config.rpc_client.get_fee_for_message(&message)?),
204+
)?;
201205
let transaction = Transaction::new(signers, message, recent_blockhash);
202206
Ok(transaction)
203207
}
204208

209+
fn checked_transaction_with_signers<T: Signers>(
210+
config: &Config,
211+
instructions: &[Instruction],
212+
signers: &T,
213+
) -> Result<Transaction, Error> {
214+
checked_transaction_with_signers_and_additional_fee(config, instructions, signers, 0)
215+
}
216+
205217
fn new_stake_account(
206218
fee_payer: &Pubkey,
207219
instructions: &mut Vec<Instruction>,
@@ -293,7 +305,7 @@ fn command_create_pool(
293305
println!("Stake pool withdraw authority {}", withdraw_authority);
294306
}
295307

296-
let mut instructions = vec![
308+
let mut setup_instructions = vec![
297309
// Account for the stake pool reserve
298310
system_instruction::create_account(
299311
&config.fee_payer.pubkey(),
@@ -332,67 +344,52 @@ fn command_create_pool(
332344
config,
333345
&mint_keypair.pubkey(),
334346
&config.manager.pubkey(),
335-
&mut instructions,
347+
&mut setup_instructions,
336348
&mut total_rent_free_balances,
337349
);
338350
println!("Creating pool fee collection account {}", pool_fee_account);
339351

340-
let recent_blockhash = get_latest_blockhash(&config.rpc_client)?;
341-
let setup_message = Message::new_with_blockhash(
342-
&instructions,
343-
Some(&config.fee_payer.pubkey()),
344-
&recent_blockhash,
345-
);
346-
let initialize_message = Message::new_with_blockhash(
347-
&[
348-
// Validator stake account list storage
349-
system_instruction::create_account(
350-
&config.fee_payer.pubkey(),
351-
&validator_list_keypair.pubkey(),
352-
validator_list_balance,
353-
validator_list_size as u64,
354-
&spl_stake_pool::id(),
355-
),
356-
// Account for the stake pool
357-
system_instruction::create_account(
358-
&config.fee_payer.pubkey(),
359-
&stake_pool_keypair.pubkey(),
360-
stake_pool_account_lamports,
361-
get_packed_len::<StakePool>() as u64,
362-
&spl_stake_pool::id(),
363-
),
364-
// Initialize stake pool
365-
spl_stake_pool::instruction::initialize(
366-
&spl_stake_pool::id(),
367-
&stake_pool_keypair.pubkey(),
368-
&config.manager.pubkey(),
369-
&config.staker.pubkey(),
370-
&withdraw_authority,
371-
&validator_list_keypair.pubkey(),
372-
&reserve_keypair.pubkey(),
373-
&mint_keypair.pubkey(),
374-
&pool_fee_account,
375-
&spl_token::id(),
376-
deposit_authority.as_ref().map(|x| x.pubkey()),
377-
epoch_fee,
378-
withdrawal_fee,
379-
deposit_fee,
380-
referral_fee,
381-
max_validators,
382-
),
383-
],
384-
Some(&config.fee_payer.pubkey()),
385-
&recent_blockhash,
386-
);
387-
check_fee_payer_balance(
388-
config,
389-
total_rent_free_balances
390-
+ config.rpc_client.get_fee_for_message(&setup_message)?
391-
+ config.rpc_client.get_fee_for_message(&initialize_message)?,
392-
)?;
352+
let initialize_instructions = &[
353+
// Validator stake account list storage
354+
system_instruction::create_account(
355+
&config.fee_payer.pubkey(),
356+
&validator_list_keypair.pubkey(),
357+
validator_list_balance,
358+
validator_list_size as u64,
359+
&spl_stake_pool::id(),
360+
),
361+
// Account for the stake pool
362+
system_instruction::create_account(
363+
&config.fee_payer.pubkey(),
364+
&stake_pool_keypair.pubkey(),
365+
stake_pool_account_lamports,
366+
get_packed_len::<StakePool>() as u64,
367+
&spl_stake_pool::id(),
368+
),
369+
// Initialize stake pool
370+
spl_stake_pool::instruction::initialize(
371+
&spl_stake_pool::id(),
372+
&stake_pool_keypair.pubkey(),
373+
&config.manager.pubkey(),
374+
&config.staker.pubkey(),
375+
&withdraw_authority,
376+
&validator_list_keypair.pubkey(),
377+
&reserve_keypair.pubkey(),
378+
&mint_keypair.pubkey(),
379+
&pool_fee_account,
380+
&spl_token::id(),
381+
deposit_authority.as_ref().map(|x| x.pubkey()),
382+
epoch_fee,
383+
withdrawal_fee,
384+
deposit_fee,
385+
referral_fee,
386+
max_validators,
387+
),
388+
];
393389
let mut setup_signers = vec![config.fee_payer.as_ref(), &mint_keypair, &reserve_keypair];
394390
unique_signers!(setup_signers);
395-
let setup_transaction = Transaction::new(&setup_signers, setup_message, recent_blockhash);
391+
let setup_transaction =
392+
checked_transaction_with_signers(config, &setup_instructions, &setup_signers)?;
396393
let mut initialize_signers = vec![
397394
config.fee_payer.as_ref(),
398395
&stake_pool_keypair,
@@ -407,11 +404,30 @@ fn command_create_pool(
407404
let mut initialize_signers = initialize_signers.clone();
408405
initialize_signers.push(&deposit_authority);
409406
unique_signers!(initialize_signers);
410-
Transaction::new(&initialize_signers, initialize_message, recent_blockhash)
407+
checked_transaction_with_signers(config, initialize_instructions, &initialize_signers)?
411408
} else {
412409
unique_signers!(initialize_signers);
413-
Transaction::new(&initialize_signers, initialize_message, recent_blockhash)
410+
checked_transaction_with_signers(config, initialize_instructions, &initialize_signers)?
414411
};
412+
413+
// Fee checks were done on the individual transactions, but this is to make
414+
// sure the wallet can cover both in sequence
415+
check_fee_payer_balance(
416+
config,
417+
total_rent_free_balances
418+
+ config
419+
.rpc_client
420+
.get_fee_for_message(&setup_transaction.message)?
421+
+ config
422+
.rpc_client
423+
.get_fee_for_message(&initialize_transaction.message)?,
424+
)?;
425+
426+
println!(
427+
"Setting up required accounts for stake pool: reserve stake {} and mint {}",
428+
reserve_keypair.pubkey(),
429+
mint_keypair.pubkey()
430+
);
415431
send_transaction(config, setup_transaction)?;
416432

417433
println!(
@@ -786,18 +802,13 @@ fn command_deposit_stake(
786802

787803
instructions.append(&mut deposit_instructions);
788804

789-
let recent_blockhash = get_latest_blockhash(&config.rpc_client)?;
790-
let message = Message::new_with_blockhash(
791-
&instructions,
792-
Some(&config.fee_payer.pubkey()),
793-
&recent_blockhash,
794-
);
795-
check_fee_payer_balance(
805+
unique_signers!(signers);
806+
let transaction = checked_transaction_with_signers_and_additional_fee(
796807
config,
797-
total_rent_free_balances + config.rpc_client.get_fee_for_message(&message)?,
808+
&instructions,
809+
&signers,
810+
total_rent_free_balances,
798811
)?;
799-
unique_signers!(signers);
800-
let transaction = Transaction::new(&signers, message, recent_blockhash);
801812
send_transaction(config, transaction)?;
802813
Ok(())
803814
}
@@ -829,17 +840,12 @@ fn command_deposit_all_stake(
829840
&mut total_rent_free_balances,
830841
));
831842
if !create_token_account_instructions.is_empty() {
832-
let recent_blockhash = get_latest_blockhash(&config.rpc_client)?;
833-
let message = Message::new_with_blockhash(
834-
&create_token_account_instructions,
835-
Some(&config.fee_payer.pubkey()),
836-
&recent_blockhash,
837-
);
838-
check_fee_payer_balance(
843+
let transaction = checked_transaction_with_signers_and_additional_fee(
839844
config,
840-
total_rent_free_balances + config.rpc_client.get_fee_for_message(&message)?,
845+
&create_token_account_instructions,
846+
&[config.fee_payer.as_ref()],
847+
total_rent_free_balances,
841848
)?;
842-
let transaction = Transaction::new(&[config.fee_payer.as_ref()], message, recent_blockhash);
843849
send_transaction(config, transaction)?;
844850
}
845851

@@ -932,14 +938,7 @@ fn command_deposit_all_stake(
932938
)
933939
};
934940

935-
let recent_blockhash = get_latest_blockhash(&config.rpc_client)?;
936-
let message = Message::new_with_blockhash(
937-
&instructions,
938-
Some(&config.fee_payer.pubkey()),
939-
&recent_blockhash,
940-
);
941-
check_fee_payer_balance(config, config.rpc_client.get_fee_for_message(&message)?)?;
942-
let transaction = Transaction::new(&signers, message, recent_blockhash);
941+
let transaction = checked_transaction_with_signers(config, &instructions, &signers)?;
943942
send_transaction(config, transaction)?;
944943
}
945944
Ok(())
@@ -1054,18 +1053,13 @@ fn command_deposit_sol(
10541053

10551054
instructions.push(deposit_instruction);
10561055

1057-
let recent_blockhash = get_latest_blockhash(&config.rpc_client)?;
1058-
let message = Message::new_with_blockhash(
1059-
&instructions,
1060-
Some(&config.fee_payer.pubkey()),
1061-
&recent_blockhash,
1062-
);
1063-
check_fee_payer_balance(
1056+
unique_signers!(signers);
1057+
let transaction = checked_transaction_with_signers_and_additional_fee(
10641058
config,
1065-
total_rent_free_balances + config.rpc_client.get_fee_for_message(&message)?,
1059+
&instructions,
1060+
&signers,
1061+
total_rent_free_balances,
10661062
)?;
1067-
unique_signers!(signers);
1068-
let transaction = Transaction::new(&signers, message, recent_blockhash);
10691063
send_transaction(config, transaction)?;
10701064
Ok(())
10711065
}
@@ -1624,21 +1618,16 @@ fn command_withdraw_stake(
16241618
}
16251619
}
16261620

1627-
let recent_blockhash = get_latest_blockhash(&config.rpc_client)?;
1628-
let message = Message::new_with_blockhash(
1629-
&instructions,
1630-
Some(&config.fee_payer.pubkey()),
1631-
&recent_blockhash,
1632-
);
16331621
for new_stake_keypair in &new_stake_keypairs {
16341622
signers.push(new_stake_keypair);
16351623
}
1636-
check_fee_payer_balance(
1624+
unique_signers!(signers);
1625+
let transaction = checked_transaction_with_signers_and_additional_fee(
16371626
config,
1638-
total_rent_free_balances + config.rpc_client.get_fee_for_message(&message)?,
1627+
&instructions,
1628+
&signers,
1629+
total_rent_free_balances,
16391630
)?;
1640-
unique_signers!(signers);
1641-
let transaction = Transaction::new(&signers, message, recent_blockhash);
16421631
send_transaction(config, transaction)?;
16431632
Ok(())
16441633
}
@@ -1748,15 +1737,8 @@ fn command_withdraw_sol(
17481737

17491738
instructions.push(withdraw_instruction);
17501739

1751-
let recent_blockhash = get_latest_blockhash(&config.rpc_client)?;
1752-
let message = Message::new_with_blockhash(
1753-
&instructions,
1754-
Some(&config.fee_payer.pubkey()),
1755-
&recent_blockhash,
1756-
);
1757-
check_fee_payer_balance(config, config.rpc_client.get_fee_for_message(&message)?)?;
17581740
unique_signers!(signers);
1759-
let transaction = Transaction::new(&signers, message, recent_blockhash);
1741+
let transaction = checked_transaction_with_signers(config, &instructions, &signers)?;
17601742
send_transaction(config, transaction)?;
17611743
Ok(())
17621744
}

0 commit comments

Comments
 (0)