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

Commit 0247661

Browse files
committed
Automatically update the stake pool if needed, use the --no-update flag to disable
1 parent 8ad223a commit 0247661

File tree

1 file changed

+62
-26
lines changed

1 file changed

+62
-26
lines changed

stake-pool/cli/src/main.rs

Lines changed: 62 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,11 @@ struct Config {
6060
owner: Box<dyn Signer>,
6161
fee_payer: Box<dyn Signer>,
6262
dry_run: bool,
63+
no_update: bool,
6364
}
6465

6566
type Error = Box<dyn std::error::Error>;
66-
type CommandResult = Result<Option<Transaction>, Error>;
67+
type CommandResult = Result<(), Error>;
6768

6869
const STAKE_STATE_LEN: usize = 200;
6970
const MAX_ACCOUNTS_TO_UPDATE: usize = 10;
@@ -115,6 +116,22 @@ fn get_authority_accounts(config: &Config, authority: &Pubkey) -> Vec<(Pubkey, A
115116
.unwrap()
116117
}
117118

119+
fn send_transaction(
120+
config: &Config,
121+
transaction: Transaction,
122+
) -> solana_client::client_error::Result<()> {
123+
if config.dry_run {
124+
let result = config.rpc_client.simulate_transaction(&transaction)?;
125+
println!("Simulate result: {:?}", result);
126+
} else {
127+
let signature = config
128+
.rpc_client
129+
.send_and_confirm_transaction_with_spinner(&transaction)?;
130+
println!("Signature: {}", signature);
131+
}
132+
Ok(())
133+
}
134+
118135
fn command_create_pool(config: &Config, fee: PoolFee, max_validators: u32) -> CommandResult {
119136
let mint_account = Keypair::new();
120137
println!("Creating mint {}", mint_account.pubkey());
@@ -242,7 +259,8 @@ fn command_create_pool(config: &Config, fee: PoolFee, max_validators: u32) -> Co
242259
];
243260
unique_signers!(signers);
244261
transaction.sign(&signers, recent_blockhash);
245-
Ok(Some(transaction))
262+
send_transaction(&config, transaction)?;
263+
Ok(())
246264
}
247265

248266
fn command_vsa_create(config: &Config, pool: &Pubkey, vote_account: &Pubkey) -> CommandResult {
@@ -275,7 +293,8 @@ fn command_vsa_create(config: &Config, pool: &Pubkey, vote_account: &Pubkey) ->
275293
let (recent_blockhash, fee_calculator) = config.rpc_client.get_recent_blockhash()?;
276294
check_fee_payer_balance(config, fee_calculator.calculate_fee(&transaction.message()))?;
277295
transaction.sign(&[config.fee_payer.as_ref()], recent_blockhash);
278-
Ok(Some(transaction))
296+
send_transaction(&config, transaction)?;
297+
Ok(())
279298
}
280299

281300
fn command_vsa_add(
@@ -288,6 +307,10 @@ fn command_vsa_add(
288307
return Err("Stake account is not active.".into());
289308
}
290309

310+
if !config.no_update {
311+
command_update(config, pool)?;
312+
}
313+
291314
// Get stake pool state
292315
let pool_data = config.rpc_client.get_account_data(&pool)?;
293316
let pool_data = StakePool::try_from_slice(pool_data.as_slice()).unwrap();
@@ -369,7 +392,8 @@ fn command_vsa_add(
369392
)?;
370393
unique_signers!(signers);
371394
transaction.sign(&signers, recent_blockhash);
372-
Ok(Some(transaction))
395+
send_transaction(&config, transaction)?;
396+
Ok(())
373397
}
374398

375399
fn command_vsa_remove(
@@ -379,6 +403,10 @@ fn command_vsa_remove(
379403
withdraw_from: &Pubkey,
380404
new_authority: &Option<Pubkey>,
381405
) -> CommandResult {
406+
if !config.no_update {
407+
command_update(config, pool)?;
408+
}
409+
382410
// Get stake pool state
383411
let pool_data = config.rpc_client.get_account_data(&pool)?;
384412
let pool_data: StakePool = StakePool::try_from_slice(pool_data.as_slice()).unwrap();
@@ -453,7 +481,8 @@ fn command_vsa_remove(
453481
&[config.fee_payer.as_ref(), config.owner.as_ref()],
454482
recent_blockhash,
455483
);
456-
Ok(Some(transaction))
484+
send_transaction(&config, transaction)?;
485+
Ok(())
457486
}
458487

459488
fn unwrap_create_token_account<F>(
@@ -509,6 +538,10 @@ fn command_deposit(
509538
stake: &Pubkey,
510539
token_receiver: &Option<Pubkey>,
511540
) -> CommandResult {
541+
if !config.no_update {
542+
command_update(config, pool)?;
543+
}
544+
512545
// Get stake pool state
513546
let pool_data = config.rpc_client.get_account_data(&pool)?;
514547
let pool_data: StakePool = StakePool::try_from_slice(pool_data.as_slice()).unwrap();
@@ -627,7 +660,8 @@ fn command_deposit(
627660
)?;
628661
unique_signers!(signers);
629662
transaction.sign(&signers, recent_blockhash);
630-
Ok(Some(transaction))
663+
send_transaction(&config, transaction)?;
664+
Ok(())
631665
}
632666

633667
fn command_list(config: &Config, pool: &Pubkey) -> CommandResult {
@@ -679,7 +713,7 @@ fn command_list(config: &Config, pool: &Pubkey) -> CommandResult {
679713
}
680714
println!("Total: {}", Sol(total_balance));
681715

682-
Ok(None)
716+
Ok(())
683717
}
684718

685719
fn command_update(config: &Config, pool: &Pubkey) -> CommandResult {
@@ -723,8 +757,9 @@ fn command_update(config: &Config, pool: &Pubkey) -> CommandResult {
723757

724758
if instructions.is_empty() && pool_data.last_update_epoch == epoch_info.epoch {
725759
println!("Stake pool balances are up to date, no update required.");
726-
Ok(None)
760+
Ok(())
727761
} else {
762+
println!("Updating stake pool...");
728763
instructions.push(update_pool_balance(
729764
&spl_stake_pool::id(),
730765
pool,
@@ -737,7 +772,8 @@ fn command_update(config: &Config, pool: &Pubkey) -> CommandResult {
737772
let (recent_blockhash, fee_calculator) = config.rpc_client.get_recent_blockhash()?;
738773
check_fee_payer_balance(config, fee_calculator.calculate_fee(&transaction.message()))?;
739774
transaction.sign(&[config.fee_payer.as_ref()], recent_blockhash);
740-
Ok(Some(transaction))
775+
send_transaction(&config, transaction)?;
776+
Ok(())
741777
}
742778
}
743779

@@ -829,6 +865,10 @@ fn command_withdraw(
829865
withdraw_from: &Pubkey,
830866
stake_receiver_param: &Option<Pubkey>,
831867
) -> CommandResult {
868+
if !config.no_update {
869+
command_update(config, pool)?;
870+
}
871+
832872
// Get stake pool state
833873
let pool_data = config.rpc_client.get_account_data(&pool)?;
834874
let pool_data = StakePool::try_from_slice(pool_data.as_slice()).unwrap();
@@ -959,8 +999,8 @@ fn command_withdraw(
959999
)?;
9601000
unique_signers!(signers);
9611001
transaction.sign(&signers, recent_blockhash);
962-
963-
Ok(Some(transaction))
1002+
send_transaction(&config, transaction)?;
1003+
Ok(())
9641004
}
9651005

9661006
fn command_set_owner(
@@ -1014,7 +1054,8 @@ fn command_set_owner(
10141054
let mut signers = vec![config.fee_payer.as_ref(), config.owner.as_ref()];
10151055
unique_signers!(signers);
10161056
transaction.sign(&signers, recent_blockhash);
1017-
Ok(Some(transaction))
1057+
send_transaction(&config, transaction)?;
1058+
Ok(())
10181059
}
10191060

10201061
fn main() {
@@ -1051,6 +1092,13 @@ fn main() {
10511092
.global(true)
10521093
.help("Simluate transaction instead of executing"),
10531094
)
1095+
.arg(
1096+
Arg::with_name("no_update")
1097+
.long("no-update")
1098+
.takes_value(false)
1099+
.global(true)
1100+
.help("Do not automatically update the stake pool if needed"),
1101+
)
10541102
.arg(
10551103
Arg::with_name("json_rpc_url")
10561104
.long("url")
@@ -1354,13 +1402,15 @@ fn main() {
13541402
});
13551403
let verbose = matches.is_present("verbose");
13561404
let dry_run = matches.is_present("dry_run");
1405+
let no_update = matches.is_present("no_update");
13571406

13581407
Config {
13591408
rpc_client: RpcClient::new_with_commitment(json_rpc_url, CommitmentConfig::confirmed()),
13601409
verbose,
13611410
owner,
13621411
fee_payer,
13631412
dry_run,
1413+
no_update,
13641414
}
13651415
};
13661416

@@ -1439,20 +1489,6 @@ fn main() {
14391489
}
14401490
_ => unreachable!(),
14411491
}
1442-
.and_then(|transaction| {
1443-
if let Some(transaction) = transaction {
1444-
if config.dry_run {
1445-
let result = config.rpc_client.simulate_transaction(&transaction)?;
1446-
println!("Simulate result: {:?}", result);
1447-
} else {
1448-
let signature = config
1449-
.rpc_client
1450-
.send_and_confirm_transaction_with_spinner(&transaction)?;
1451-
println!("Signature: {}", signature);
1452-
}
1453-
}
1454-
Ok(())
1455-
})
14561492
.map_err(|err| {
14571493
eprintln!("{}", err);
14581494
exit(1);

0 commit comments

Comments
 (0)