1- use std:: fmt;
21use std:: mem:: size_of;
2+ use std:: { fmt, time:: Duration } ;
33
44use crate :: handler:: CliHandler ;
55use anyhow:: Result ;
@@ -30,6 +30,7 @@ use jito_vault_core::{
3030 vault_operator_delegation:: VaultOperatorDelegation ,
3131 vault_update_state_tracker:: VaultUpdateStateTracker ,
3232} ;
33+ use log:: info;
3334use solana_account_decoder:: { UiAccountEncoding , UiDataSliceConfig } ;
3435use solana_client:: {
3536 rpc_config:: { RpcAccountInfoConfig , RpcProgramAccountsConfig } ,
@@ -38,6 +39,7 @@ use solana_client::{
3839use solana_sdk:: { account:: Account , pubkey:: Pubkey } ;
3940use spl_associated_token_account:: get_associated_token_address;
4041use spl_stake_pool:: { find_withdraw_authority_program_address, state:: StakePool } ;
42+ use tokio:: time:: sleep;
4143
4244// ---------------------- HELPERS ----------------------
4345// So we can switch between the two implementations
@@ -68,10 +70,17 @@ pub async fn get_current_epoch_and_slot(handler: &CliHandler) -> Result<(u64, u6
6870 Ok ( ( epoch, slot) )
6971}
7072
71- pub async fn get_current_epoch_and_slot_unsafe ( handler : & CliHandler ) -> ( u64 , u64 ) {
72- get_current_epoch_and_slot ( handler)
73- . await
74- . expect ( "Failed to get epoch and slot" )
73+ pub async fn get_guaranteed_epoch_and_slot ( handler : & CliHandler ) -> ( u64 , u64 ) {
74+ loop {
75+ let current_epoch_and_slot_result = get_current_epoch_and_slot ( handler) . await ;
76+
77+ if let Ok ( result) = current_epoch_and_slot_result {
78+ return result;
79+ }
80+
81+ info ! ( "Could not fetch current epoch and slot. Retrying..." ) ;
82+ sleep ( Duration :: from_secs ( 1 ) ) . await ;
83+ }
7584}
7685
7786// ---------------------- TIP ROUTER ----------------------
@@ -406,37 +415,37 @@ pub async fn get_is_epoch_completed(handler: &CliHandler, epoch: u64) -> Result<
406415
407416// ---------------------- RESTAKING ----------------------
408417
409- pub async fn get_restaking_config ( handler : & CliHandler ) -> Result < RestakingConfig > {
410- let ( address, _, _) = RestakingConfig :: find_program_address ( & handler. restaking_program_id ) ;
418+ pub async fn get_vault_config ( handler : & CliHandler ) -> Result < VaultConfig > {
419+ let ( address, _, _) = VaultConfig :: find_program_address ( & handler. vault_program_id ) ;
411420 let account = get_account ( handler, & address) . await ?;
412421
413422 if account. is_none ( ) {
414- return Err ( anyhow:: anyhow!( "Account not found" ) ) ;
423+ return Err ( anyhow:: anyhow!( "Vault Config account not found" ) ) ;
415424 }
416425 let account = account. unwrap ( ) ;
417426
418- let account = RestakingConfig :: try_from_slice_unchecked ( account. data . as_slice ( ) ) ?;
427+ let account = VaultConfig :: try_from_slice_unchecked ( account. data . as_slice ( ) ) ?;
419428 Ok ( * account)
420429}
421430
422- pub async fn get_vault_config ( handler : & CliHandler ) -> Result < VaultConfig > {
423- let ( address, _, _) = VaultConfig :: find_program_address ( & handler. vault_program_id ) ;
431+ pub async fn get_restaking_config ( handler : & CliHandler ) -> Result < RestakingConfig > {
432+ let ( address, _, _) = RestakingConfig :: find_program_address ( & handler. restaking_program_id ) ;
424433 let account = get_account ( handler, & address) . await ?;
425434
426435 if account. is_none ( ) {
427- return Err ( anyhow:: anyhow!( "Account not found" ) ) ;
436+ return Err ( anyhow:: anyhow!( "Restaking Config Account not found" ) ) ;
428437 }
429438 let account = account. unwrap ( ) ;
430439
431- let account = VaultConfig :: try_from_slice_unchecked ( account. data . as_slice ( ) ) ?;
440+ let account = RestakingConfig :: try_from_slice_unchecked ( account. data . as_slice ( ) ) ?;
432441 Ok ( * account)
433442}
434443
435444pub async fn get_ncn ( handler : & CliHandler ) -> Result < Ncn > {
436445 let account = get_account ( handler, handler. ncn ( ) ?) . await ?;
437446
438447 if account. is_none ( ) {
439- return Err ( anyhow:: anyhow!( "Account not found" ) ) ;
448+ return Err ( anyhow:: anyhow!( "NCN account not found" ) ) ;
440449 }
441450 let account = account. unwrap ( ) ;
442451
@@ -445,9 +454,13 @@ pub async fn get_ncn(handler: &CliHandler) -> Result<Ncn> {
445454}
446455
447456pub async fn get_vault ( handler : & CliHandler , vault : & Pubkey ) -> Result < Vault > {
448- let account = get_account ( handler, vault)
449- . await ?
450- . expect ( "Account not found" ) ;
457+ let account = get_account ( handler, vault) . await ?;
458+
459+ if account. is_none ( ) {
460+ return Err ( anyhow:: anyhow!( "Vault account not found" ) ) ;
461+ }
462+ let account = account. unwrap ( ) ;
463+
451464 let account = Vault :: try_from_slice_unchecked ( account. data . as_slice ( ) ) ?;
452465 Ok ( * account)
453466}
@@ -460,9 +473,15 @@ pub async fn get_vault_update_state_tracker(
460473 let ( vault_update_state_tracker, _, _) =
461474 VaultUpdateStateTracker :: find_program_address ( & handler. vault_program_id , vault, ncn_epoch) ;
462475
463- let account = get_account ( handler, & vault_update_state_tracker)
464- . await ?
465- . expect ( "Account not found" ) ;
476+ let account = get_account ( handler, & vault_update_state_tracker) . await ?;
477+
478+ if account. is_none ( ) {
479+ return Err ( anyhow:: anyhow!(
480+ "Vault Update State Tracker account not found"
481+ ) ) ;
482+ }
483+ let account = account. unwrap ( ) ;
484+
466485 let account = VaultUpdateStateTracker :: try_from_slice_unchecked ( account. data . as_slice ( ) ) ?;
467486 Ok ( * account)
468487}
@@ -471,7 +490,7 @@ pub async fn get_operator(handler: &CliHandler, operator: &Pubkey) -> Result<Ope
471490 let account = get_account ( handler, operator) . await ?;
472491
473492 if account. is_none ( ) {
474- return Err ( anyhow:: anyhow!( "Account not found" ) ) ;
493+ return Err ( anyhow:: anyhow!( "Operator account not found" ) ) ;
475494 }
476495 let account = account. unwrap ( ) ;
477496
@@ -492,7 +511,7 @@ pub async fn get_ncn_operator_state(
492511 let account = get_account ( handler, & address) . await ?;
493512
494513 if account. is_none ( ) {
495- return Err ( anyhow:: anyhow!( "Account not found" ) ) ;
514+ return Err ( anyhow:: anyhow!( "NCN Operator State account not found" ) ) ;
496515 }
497516 let account = account. unwrap ( ) ;
498517
@@ -507,7 +526,7 @@ pub async fn get_vault_ncn_ticket(handler: &CliHandler, vault: &Pubkey) -> Resul
507526 let account = get_account ( handler, & address) . await ?;
508527
509528 if account. is_none ( ) {
510- return Err ( anyhow:: anyhow!( "Account not found" ) ) ;
529+ return Err ( anyhow:: anyhow!( "Vault NCN Ticket account not found" ) ) ;
511530 }
512531 let account = account. unwrap ( ) ;
513532
@@ -522,7 +541,7 @@ pub async fn get_ncn_vault_ticket(handler: &CliHandler, vault: &Pubkey) -> Resul
522541 let account = get_account ( handler, & address) . await ?;
523542
524543 if account. is_none ( ) {
525- return Err ( anyhow:: anyhow!( "Account not found" ) ) ;
544+ return Err ( anyhow:: anyhow!( "NCN Vault Ticket account not found" ) ) ;
526545 }
527546 let account = account. unwrap ( ) ;
528547
@@ -541,7 +560,9 @@ pub async fn get_vault_operator_delegation(
541560 let account = get_account ( handler, & address) . await ?;
542561
543562 if account. is_none ( ) {
544- return Err ( anyhow:: anyhow!( "Account not found" ) ) ;
563+ return Err ( anyhow:: anyhow!(
564+ "Vault Operator Delegation account not found"
565+ ) ) ;
545566 }
546567 let account = account. unwrap ( ) ;
547568
@@ -560,7 +581,7 @@ pub async fn get_operator_vault_ticket(
560581 let account = get_account ( handler, & address) . await ?;
561582
562583 if account. is_none ( ) {
563- return Err ( anyhow:: anyhow!( "Account not found" ) ) ;
584+ return Err ( anyhow:: anyhow!( "Operator Vault Ticket account not found" ) ) ;
564585 }
565586 let account = account. unwrap ( ) ;
566587
@@ -570,7 +591,13 @@ pub async fn get_operator_vault_ticket(
570591
571592pub async fn get_stake_pool ( handler : & CliHandler ) -> Result < StakePool > {
572593 let stake_pool = JITOSOL_POOL_ADDRESS ;
573- let account = get_account ( handler, & stake_pool) . await ?. unwrap ( ) ;
594+ let account = get_account ( handler, & stake_pool) . await ?;
595+
596+ if account. is_none ( ) {
597+ return Err ( anyhow:: anyhow!( "Stake Pool account not found" ) ) ;
598+ }
599+ let account = account. unwrap ( ) ;
600+
574601 let mut data_slice = account. data . as_slice ( ) ;
575602 let account = StakePool :: deserialize ( & mut data_slice)
576603 . map_err ( |_| anyhow:: anyhow!( "Invalid stake pool account" ) ) ?;
@@ -880,39 +907,18 @@ impl NcnTickets {
880907 slot : u64 ,
881908 epoch_length : u64 ,
882909 ) -> Result < Self > {
883- let ncn = handler. ncn ( ) . expect ( "NCN not found" ) ;
910+ let ncn = handler. ncn ( ) ?;
911+ let vault_account = get_vault ( handler, vault) . await ?;
884912
885913 let ( ncn_vault_ticket_address, _, _) =
886914 NcnVaultTicket :: find_program_address ( & handler. restaking_program_id , ncn, vault) ;
887915 let ncn_vault_ticket = get_ncn_vault_ticket ( handler, vault) . await ;
888- let ncn_vault_ticket = {
889- match ncn_vault_ticket {
890- Ok ( account) => Some ( account) ,
891- Err ( e) => {
892- if e. to_string ( ) . contains ( "Account not found" ) {
893- None
894- } else {
895- return Err ( e) ;
896- }
897- }
898- }
899- } ;
916+ let ncn_vault_ticket = ncn_vault_ticket. ok ( ) ;
900917
901918 let ( vault_ncn_ticket_address, _, _) =
902919 VaultNcnTicket :: find_program_address ( & handler. vault_program_id , vault, ncn) ;
903920 let vault_ncn_ticket = get_vault_ncn_ticket ( handler, vault) . await ;
904- let vault_ncn_ticket = {
905- match vault_ncn_ticket {
906- Ok ( account) => Some ( account) ,
907- Err ( e) => {
908- if e. to_string ( ) . contains ( "Account not found" ) {
909- None
910- } else {
911- return Err ( e) ;
912- }
913- }
914- }
915- } ;
921+ let vault_ncn_ticket = vault_ncn_ticket. ok ( ) ;
916922
917923 let ( vault_operator_delegation_address, _, _) =
918924 VaultOperatorDelegation :: find_program_address (
@@ -922,55 +928,20 @@ impl NcnTickets {
922928 ) ;
923929 let vault_operator_delegation =
924930 get_vault_operator_delegation ( handler, vault, operator) . await ;
925- let vault_operator_delegation = {
926- match vault_operator_delegation {
927- Ok ( account) => Some ( account) ,
928- Err ( e) => {
929- if e. to_string ( ) . contains ( "Account not found" ) {
930- None
931- } else {
932- return Err ( e) ;
933- }
934- }
935- }
936- } ;
931+ let vault_operator_delegation = vault_operator_delegation. ok ( ) ;
937932
938933 let ( operator_vault_ticket_address, _, _) = OperatorVaultTicket :: find_program_address (
939934 & handler. restaking_program_id ,
940935 operator,
941936 vault,
942937 ) ;
943938 let operator_vault_ticket = get_operator_vault_ticket ( handler, vault, operator) . await ;
944- let operator_vault_ticket = {
945- match operator_vault_ticket {
946- Ok ( account) => Some ( account) ,
947- Err ( e) => {
948- if e. to_string ( ) . contains ( "Account not found" ) {
949- None
950- } else {
951- return Err ( e) ;
952- }
953- }
954- }
955- } ;
939+ let operator_vault_ticket = operator_vault_ticket. ok ( ) ;
956940
957941 let ( ncn_operator_state_address, _, _) =
958942 NcnOperatorState :: find_program_address ( & handler. restaking_program_id , ncn, operator) ;
959943 let ncn_operator_state = get_ncn_operator_state ( handler, operator) . await ;
960- let ncn_operator_state = {
961- match ncn_operator_state {
962- Ok ( account) => Some ( account) ,
963- Err ( e) => {
964- if e. to_string ( ) . contains ( "Account not found" ) {
965- None
966- } else {
967- return Err ( e) ;
968- }
969- }
970- }
971- } ;
972-
973- let vault_account = get_vault ( handler, vault) . await . expect ( "Vault not found" ) ;
944+ let ncn_operator_state = ncn_operator_state. ok ( ) ;
974945
975946 Ok ( Self {
976947 slot,
@@ -992,6 +963,28 @@ impl NcnTickets {
992963 } )
993964 }
994965
966+ pub const fn st_mint ( & self ) -> Pubkey {
967+ self . vault_account . supported_mint
968+ }
969+
970+ pub fn delegation ( & self ) -> ( u64 , u64 , u64 ) {
971+ if self . vault_operator_delegation . is_none ( ) {
972+ return ( 0 , 0 , 0 ) ;
973+ }
974+
975+ let delegation_state = self
976+ . vault_operator_delegation
977+ . as_ref ( )
978+ . unwrap ( )
979+ . delegation_state ;
980+
981+ (
982+ delegation_state. staked_amount ( ) ,
983+ delegation_state. cooling_down_amount ( ) ,
984+ delegation_state. total_security ( ) . unwrap ( ) ,
985+ )
986+ }
987+
995988 pub fn ncn_operator ( & self ) -> u8 {
996989 if self . ncn_operator_state . is_none ( ) {
997990 return Self :: DNE ;
@@ -1157,26 +1150,13 @@ impl fmt::Display for NcnTickets {
11571150 self . operator_vault_ticket_address
11581151 ) ?;
11591152
1160- let st_mint = self . vault_account . supported_mint ;
1161- let delegation = {
1162- if self . vault_operator_delegation . is_some ( ) {
1163- self . vault_operator_delegation
1164- . unwrap ( )
1165- . delegation_state
1166- . total_security ( )
1167- . unwrap ( )
1168- } else {
1169- 0
1170- }
1171- } ;
1172-
11731153 writeln ! (
11741154 f,
11751155 "Vault -> Operator: {} {} {}: {}" ,
11761156 check( self . vault_operator( ) ) ,
11771157 self . vault_operator_delegation_address,
1178- st_mint,
1179- delegation
1158+ self . st_mint( ) ,
1159+ self . delegation( ) . 2
11801160 ) ?;
11811161 writeln ! ( f, "\n " ) ?;
11821162
0 commit comments