Skip to content

Commit

Permalink
account-decoder: don't use strings to convert between Pubkey types (#…
Browse files Browse the repository at this point in the history
…17391)

* account-decoder: don't use strings to convert between Pubkey types

* transaction-status: don't use strings to convert between Pubkey types
  • Loading branch information
polachok authored May 21, 2021
1 parent 855ae79 commit 51178cc
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
8 changes: 4 additions & 4 deletions account-decoder/src/parse_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ use std::str::FromStr;
// A helper function to convert spl_token_v2_0::id() as spl_sdk::pubkey::Pubkey to
// solana_sdk::pubkey::Pubkey
pub fn spl_token_id_v2_0() -> Pubkey {
Pubkey::from_str(&spl_token_v2_0::id().to_string()).unwrap()
Pubkey::new_from_array(spl_token_v2_0::id().to_bytes())
}

// A helper function to convert spl_token_v2_0::native_mint::id() as spl_sdk::pubkey::Pubkey to
// solana_sdk::pubkey::Pubkey
pub fn spl_token_v2_0_native_mint() -> Pubkey {
Pubkey::from_str(&spl_token_v2_0::native_mint::id().to_string()).unwrap()
Pubkey::new_from_array(spl_token_v2_0::native_mint::id().to_bytes())
}

// A helper function to convert a solana_sdk::pubkey::Pubkey to spl_sdk::pubkey::Pubkey
pub fn spl_token_v2_0_pubkey(pubkey: &Pubkey) -> SplTokenPubkey {
SplTokenPubkey::from_str(&pubkey.to_string()).unwrap()
SplTokenPubkey::new_from_array(pubkey.to_bytes())
}

// A helper function to convert a spl_sdk::pubkey::Pubkey to solana_sdk::pubkey::Pubkey
pub fn pubkey_from_spl_token_v2_0(pubkey: &SplTokenPubkey) -> Pubkey {
Pubkey::from_str(&pubkey.to_string()).unwrap()
Pubkey::new_from_array(pubkey.to_bytes())
}

pub fn parse_token(
Expand Down
5 changes: 2 additions & 3 deletions transaction-status/src/parse_associated_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ use crate::parse_instruction::{
};
use serde_json::json;
use solana_sdk::{instruction::CompiledInstruction, pubkey::Pubkey};
use std::str::FromStr;

// A helper function to convert spl_associated_token_account_v1_0::id() as spl_sdk::pubkey::Pubkey
// to solana_sdk::pubkey::Pubkey
pub fn spl_associated_token_id_v1_0() -> Pubkey {
Pubkey::from_str(&spl_associated_token_account_v1_0::id().to_string()).unwrap()
Pubkey::new_from_array(spl_associated_token_account_v1_0::id().to_bytes())
}

pub fn parse_associated_token(
Expand Down Expand Up @@ -58,7 +57,7 @@ mod test {
};

fn convert_pubkey(pubkey: Pubkey) -> SplAssociatedTokenPubkey {
SplAssociatedTokenPubkey::from_str(&pubkey.to_string()).unwrap()
SplAssociatedTokenPubkey::new_from_array(pubkey.to_bytes())
}

fn convert_compiled_instruction(
Expand Down
10 changes: 3 additions & 7 deletions transaction-status/src/parse_instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,15 @@ use inflector::Inflector;
use serde_json::Value;
use solana_account_decoder::parse_token::spl_token_id_v2_0;
use solana_sdk::{instruction::CompiledInstruction, pubkey::Pubkey, system_program};
use std::{
collections::HashMap,
str::{from_utf8, FromStr},
};
use std::{collections::HashMap, str::from_utf8};
use thiserror::Error;

lazy_static! {
static ref ASSOCIATED_TOKEN_PROGRAM_ID: Pubkey = spl_associated_token_id_v1_0();
static ref BPF_LOADER_PROGRAM_ID: Pubkey = solana_sdk::bpf_loader::id();
static ref BPF_UPGRADEABLE_LOADER_PROGRAM_ID: Pubkey = solana_sdk::bpf_loader_upgradeable::id();
static ref MEMO_V1_PROGRAM_ID: Pubkey =
Pubkey::from_str(&spl_memo::v1::id().to_string()).unwrap();
static ref MEMO_V3_PROGRAM_ID: Pubkey = Pubkey::from_str(&spl_memo::id().to_string()).unwrap();
static ref MEMO_V1_PROGRAM_ID: Pubkey = Pubkey::new_from_array(spl_memo::v1::id().to_bytes());
static ref MEMO_V3_PROGRAM_ID: Pubkey = Pubkey::new_from_array(spl_memo::id().to_bytes());
static ref STAKE_PROGRAM_ID: Pubkey = solana_stake_program::id();
static ref SYSTEM_PROGRAM_ID: Pubkey = system_program::id();
static ref TOKEN_PROGRAM_ID: Pubkey = spl_token_id_v2_0();
Expand Down

0 comments on commit 51178cc

Please sign in to comment.