Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
stake-pool-cli: Sign messages before checking fee (#2759)
Browse files Browse the repository at this point in the history
  • Loading branch information
joncinque authored Jan 20, 2022
1 parent ba46fed commit 2e03106
Showing 1 changed file with 43 additions and 36 deletions.
79 changes: 43 additions & 36 deletions stake-pool/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use {
solana_remote_wallet::remote_wallet::RemoteWalletManager,
solana_sdk::{
commitment_config::CommitmentConfig,
hash::Hash,
native_token::{self, Sol},
signature::{Keypair, Signer},
signers::Signers,
Expand Down Expand Up @@ -135,6 +136,12 @@ fn get_signer(
})
}

fn get_latest_blockhash(client: &RpcClient) -> Result<Hash, Error> {
Ok(client
.get_latest_blockhash_with_commitment(CommitmentConfig::confirmed())?
.0)
}

fn send_transaction_no_wait(
config: &Config,
transaction: Transaction,
Expand Down Expand Up @@ -170,7 +177,7 @@ fn checked_transaction_with_signers<T: Signers>(
instructions: &[Instruction],
signers: &T,
) -> Result<Transaction, Error> {
let recent_blockhash = config.rpc_client.get_latest_blockhash()?;
let recent_blockhash = get_latest_blockhash(&config.rpc_client)?;
let transaction = Transaction::new_signed_with_payer(
instructions,
Some(&config.fee_payer.pubkey()),
Expand Down Expand Up @@ -365,27 +372,10 @@ fn command_create_pool(
Some(&config.fee_payer.pubkey()),
);

let recent_blockhash = config.rpc_client.get_latest_blockhash()?;
check_fee_payer_balance(
config,
total_rent_free_balances
+ config
.rpc_client
.get_fee_for_message(setup_transaction.message())?
+ config
.rpc_client
.get_fee_for_message(initialize_transaction.message())?,
)?;
let recent_blockhash = get_latest_blockhash(&config.rpc_client)?;
let mut setup_signers = vec![config.fee_payer.as_ref(), &mint_keypair, &reserve_keypair];
unique_signers!(setup_signers);
setup_transaction.sign(&setup_signers, recent_blockhash);
send_transaction(config, setup_transaction)?;

println!(
"Creating stake pool {} with validator list {}",
stake_pool_keypair.pubkey(),
validator_list_keypair.pubkey()
);
let mut initialize_signers = vec![
config.fee_payer.as_ref(),
&stake_pool_keypair,
Expand All @@ -405,6 +395,23 @@ fn command_create_pool(
unique_signers!(initialize_signers);
initialize_transaction.sign(&initialize_signers, recent_blockhash);
}
check_fee_payer_balance(
config,
total_rent_free_balances
+ config
.rpc_client
.get_fee_for_message(setup_transaction.message())?
+ config
.rpc_client
.get_fee_for_message(initialize_transaction.message())?,
)?;
send_transaction(config, setup_transaction)?;

println!(
"Creating stake pool {} with validator list {}",
stake_pool_keypair.pubkey(),
validator_list_keypair.pubkey()
);
send_transaction(config, initialize_transaction)?;
Ok(())
}
Expand Down Expand Up @@ -761,16 +768,16 @@ fn command_deposit_stake(
let mut transaction =
Transaction::new_with_payer(&instructions, Some(&config.fee_payer.pubkey()));

let recent_blockhash = config.rpc_client.get_latest_blockhash()?;
let recent_blockhash = get_latest_blockhash(&config.rpc_client)?;
unique_signers!(signers);
transaction.sign(&signers, recent_blockhash);
check_fee_payer_balance(
config,
total_rent_free_balances
+ config
.rpc_client
.get_fee_for_message(transaction.message())?,
)?;
unique_signers!(signers);
transaction.sign(&signers, recent_blockhash);
send_transaction(config, transaction)?;
Ok(())
}
Expand Down Expand Up @@ -802,7 +809,7 @@ fn command_deposit_all_stake(
&mut total_rent_free_balances,
));
if !create_token_account_instructions.is_empty() {
let recent_blockhash = config.rpc_client.get_latest_blockhash()?;
let recent_blockhash = get_latest_blockhash(&config.rpc_client)?;
let transaction = Transaction::new_signed_with_payer(
&create_token_account_instructions,
Some(&config.fee_payer.pubkey()),
Expand Down Expand Up @@ -903,7 +910,7 @@ fn command_deposit_all_stake(
)
};

let recent_blockhash = config.rpc_client.get_latest_blockhash()?;
let recent_blockhash = get_latest_blockhash(&config.rpc_client)?;
let transaction = Transaction::new_signed_with_payer(
&instructions,
Some(&config.fee_payer.pubkey()),
Expand Down Expand Up @@ -1034,16 +1041,16 @@ fn command_deposit_sol(
let mut transaction =
Transaction::new_with_payer(&instructions, Some(&config.fee_payer.pubkey()));

let recent_blockhash = config.rpc_client.get_latest_blockhash()?;
let recent_blockhash = get_latest_blockhash(&config.rpc_client)?;
unique_signers!(signers);
transaction.sign(&signers, recent_blockhash);
check_fee_payer_balance(
config,
total_rent_free_balances
+ config
.rpc_client
.get_fee_for_message(transaction.message())?,
)?;
unique_signers!(signers);
transaction.sign(&signers, recent_blockhash);
send_transaction(config, transaction)?;
Ok(())
}
Expand Down Expand Up @@ -1495,19 +1502,19 @@ fn command_withdraw_stake(
let mut transaction =
Transaction::new_with_payer(&instructions, Some(&config.fee_payer.pubkey()));

let recent_blockhash = config.rpc_client.get_latest_blockhash()?;
let recent_blockhash = get_latest_blockhash(&config.rpc_client)?;
for new_stake_keypair in &new_stake_keypairs {
signers.push(new_stake_keypair);
}
unique_signers!(signers);
transaction.sign(&signers, recent_blockhash);
check_fee_payer_balance(
config,
total_rent_free_balances
+ config
.rpc_client
.get_fee_for_message(transaction.message())?,
)?;
for new_stake_keypair in &new_stake_keypairs {
signers.push(new_stake_keypair);
}
unique_signers!(signers);
transaction.sign(&signers, recent_blockhash);
send_transaction(config, transaction)?;
Ok(())
}
Expand Down Expand Up @@ -1620,15 +1627,15 @@ fn command_withdraw_sol(
let mut transaction =
Transaction::new_with_payer(&instructions, Some(&config.fee_payer.pubkey()));

let recent_blockhash = config.rpc_client.get_latest_blockhash()?;
let recent_blockhash = get_latest_blockhash(&config.rpc_client)?;
unique_signers!(signers);
transaction.sign(&signers, recent_blockhash);
check_fee_payer_balance(
config,
config
.rpc_client
.get_fee_for_message(transaction.message())?,
)?;
unique_signers!(signers);
transaction.sign(&signers, recent_blockhash);
send_transaction(config, transaction)?;
Ok(())
}
Expand Down

0 comments on commit 2e03106

Please sign in to comment.