diff --git a/rpc/src/rpc.rs b/rpc/src/rpc.rs index 6a9b3d6359bd99..c19c28c7a20052 100644 --- a/rpc/src/rpc.rs +++ b/rpc/src/rpc.rs @@ -384,19 +384,14 @@ impl JsonRpcRequestProcessor { if program_id == &spl_token_id_v2_0() && encoding == UiAccountEncoding::JsonParsed { get_parsed_token_accounts(bank.clone(), keyed_accounts.into_iter()).collect() } else { - keyed_accounts - .into_iter() - .map(|(pubkey, account)| RpcKeyedAccount { + let mut encoded_accounts = vec![]; + for (pubkey, account) in keyed_accounts { + encoded_accounts.push(RpcKeyedAccount { pubkey: pubkey.to_string(), - account: UiAccount::encode( - &pubkey, - &account, - encoding, - None, - data_slice_config, - ), - }) - .collect() + account: encode_account(&account, &pubkey, encoding, data_slice_config)?, + }); + } + encoded_accounts }; Ok(result).map(|result| match with_context { true => OptionalContext::Context(new_response(&bank, result)), @@ -1995,26 +1990,41 @@ fn get_encoded_account( encoding: UiAccountEncoding, data_slice: Option, ) -> Result> { - let mut response = None; - if let Some(account) = bank.get_account(pubkey) { - if account.owner() == &spl_token_id_v2_0() && encoding == UiAccountEncoding::JsonParsed { - response = Some(get_parsed_token_account(bank.clone(), pubkey, account)); - } else if (encoding == UiAccountEncoding::Binary || encoding == UiAccountEncoding::Base58) - && account.data().len() > 128 - { - let message = "Encoded binary (base 58) data should be less than 128 bytes, please use Base64 encoding.".to_string(); - return Err(error::Error { - code: error::ErrorCode::InvalidRequest, - message, - data: None, - }); - } else { - response = Some(UiAccount::encode( - pubkey, &account, encoding, None, data_slice, - )); + match bank.get_account(pubkey) { + Some(account) => { + let response = if account.owner() == &spl_token_id_v2_0() + && encoding == UiAccountEncoding::JsonParsed + { + get_parsed_token_account(bank.clone(), pubkey, account) + } else { + encode_account(&account, pubkey, encoding, data_slice)? + }; + Ok(Some(response)) } + None => Ok(None), + } +} + +fn encode_account( + account: &T, + pubkey: &Pubkey, + encoding: UiAccountEncoding, + data_slice: Option, +) -> Result { + if (encoding == UiAccountEncoding::Binary || encoding == UiAccountEncoding::Base58) + && account.data().len() > 128 + { + let message = "Encoded binary (base 58) data should be less than 128 bytes, please use Base64 encoding.".to_string(); + Err(error::Error { + code: error::ErrorCode::InvalidRequest, + message, + data: None, + }) + } else { + Ok(UiAccount::encode( + pubkey, account, encoding, None, data_slice, + )) } - Ok(response) } fn get_spl_token_owner_filter(program_id: &Pubkey, filters: &[RpcFilterType]) -> Option {