|
| 1 | +use { |
| 2 | + bincode::deserialize, |
| 3 | + borsh::BorshDeserialize, |
| 4 | + solana_account_decoder::UiAccountEncoding, |
| 5 | + solana_client::{ |
| 6 | + client_error::ClientError, |
| 7 | + rpc_client::RpcClient, |
| 8 | + rpc_config::{RpcAccountInfoConfig, RpcProgramAccountsConfig}, |
| 9 | + rpc_filter::{Memcmp, MemcmpEncodedBytes, RpcFilterType}, |
| 10 | + }, |
| 11 | + solana_program::{program_pack::Pack, pubkey::Pubkey}, |
| 12 | + spl_stake_pool::{ |
| 13 | + borsh::try_from_slice_unchecked, |
| 14 | + stake_program, |
| 15 | + state::{StakePool, ValidatorList}, |
| 16 | + }, |
| 17 | +}; |
| 18 | + |
| 19 | +type Error = Box<dyn std::error::Error>; |
| 20 | + |
| 21 | +pub(crate) fn get_stake_pool( |
| 22 | + rpc_client: &RpcClient, |
| 23 | + pool_address: &Pubkey, |
| 24 | +) -> Result<StakePool, Error> { |
| 25 | + let account_data = rpc_client.get_account_data(pool_address)?; |
| 26 | + let stake_pool = StakePool::try_from_slice(account_data.as_slice()) |
| 27 | + .map_err(|err| format!("Invalid stake pool {}: {}", pool_address, err))?; |
| 28 | + Ok(stake_pool) |
| 29 | +} |
| 30 | + |
| 31 | +pub(crate) fn get_validator_list( |
| 32 | + rpc_client: &RpcClient, |
| 33 | + validator_list_address: &Pubkey, |
| 34 | +) -> Result<ValidatorList, Error> { |
| 35 | + let account_data = rpc_client.get_account_data(validator_list_address)?; |
| 36 | + let validator_list = try_from_slice_unchecked::<ValidatorList>(&account_data.as_slice()) |
| 37 | + .map_err(|err| format!("Invalid validator list {}: {}", validator_list_address, err))?; |
| 38 | + Ok(validator_list) |
| 39 | +} |
| 40 | + |
| 41 | +pub(crate) fn get_token_account( |
| 42 | + rpc_client: &RpcClient, |
| 43 | + token_account_address: &Pubkey, |
| 44 | + expected_token_mint: &Pubkey, |
| 45 | +) -> Result<spl_token::state::Account, Error> { |
| 46 | + let account_data = rpc_client.get_account_data(token_account_address)?; |
| 47 | + let token_account = spl_token::state::Account::unpack_from_slice(account_data.as_slice()) |
| 48 | + .map_err(|err| format!("Invalid token account {}: {}", token_account_address, err))?; |
| 49 | + |
| 50 | + if token_account.mint != *expected_token_mint { |
| 51 | + Err(format!( |
| 52 | + "Invalid token mint for {}, expected mint is {}", |
| 53 | + token_account_address, expected_token_mint |
| 54 | + ) |
| 55 | + .into()) |
| 56 | + } else { |
| 57 | + Ok(token_account) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +pub(crate) fn get_token_mint( |
| 62 | + rpc_client: &RpcClient, |
| 63 | + token_mint_address: &Pubkey, |
| 64 | +) -> Result<spl_token::state::Mint, Error> { |
| 65 | + let account_data = rpc_client.get_account_data(token_mint_address)?; |
| 66 | + let token_mint = spl_token::state::Mint::unpack_from_slice(account_data.as_slice()) |
| 67 | + .map_err(|err| format!("Invalid token mint {}: {}", token_mint_address, err))?; |
| 68 | + |
| 69 | + Ok(token_mint) |
| 70 | +} |
| 71 | + |
| 72 | +pub(crate) fn get_stake_state( |
| 73 | + rpc_client: &RpcClient, |
| 74 | + stake_address: &Pubkey, |
| 75 | +) -> Result<stake_program::StakeState, Error> { |
| 76 | + let account_data = rpc_client.get_account_data(stake_address)?; |
| 77 | + let stake_state = deserialize(account_data.as_slice()) |
| 78 | + .map_err(|err| format!("Invalid stake account {}: {}", stake_address, err))?; |
| 79 | + Ok(stake_state) |
| 80 | +} |
| 81 | + |
| 82 | +pub(crate) fn get_stake_accounts_by_withdrawer( |
| 83 | + rpc_client: &RpcClient, |
| 84 | + withdrawer: &Pubkey, |
| 85 | +) -> Result<Vec<(Pubkey, u64, stake_program::StakeState)>, ClientError> { |
| 86 | + rpc_client |
| 87 | + .get_program_accounts_with_config( |
| 88 | + &stake_program::id(), |
| 89 | + RpcProgramAccountsConfig { |
| 90 | + filters: Some(vec![RpcFilterType::Memcmp(Memcmp { |
| 91 | + offset: 44, // 44 is Withdrawer authority offset in stake account stake |
| 92 | + bytes: MemcmpEncodedBytes::Binary(format!("{}", withdrawer)), |
| 93 | + encoding: None, |
| 94 | + })]), |
| 95 | + account_config: RpcAccountInfoConfig { |
| 96 | + encoding: Some(UiAccountEncoding::Base64), |
| 97 | + ..RpcAccountInfoConfig::default() |
| 98 | + }, |
| 99 | + }, |
| 100 | + ) |
| 101 | + .map(|accounts| { |
| 102 | + accounts |
| 103 | + .into_iter() |
| 104 | + .filter_map( |
| 105 | + |(address, account)| match deserialize(account.data.as_slice()) { |
| 106 | + Ok(stake_state) => Some((address, account.lamports, stake_state)), |
| 107 | + Err(err) => { |
| 108 | + eprintln!("Invalid stake account data for {}: {}", address, err); |
| 109 | + None |
| 110 | + } |
| 111 | + }, |
| 112 | + ) |
| 113 | + .collect() |
| 114 | + }) |
| 115 | +} |
0 commit comments