Skip to content

Commit

Permalink
feat!: upgrade to solana@v2
Browse files Browse the repository at this point in the history
  • Loading branch information
ifiokjr committed Oct 19, 2024
1 parent 2f1be60 commit d08acc1
Show file tree
Hide file tree
Showing 13 changed files with 1,129 additions and 1,112 deletions.
1,808 changes: 899 additions & 909 deletions Cargo.lock

Large diffs are not rendered by default.

47 changes: 25 additions & 22 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ repository = "https://github.com/ifiokjr/wasm_solana"
rust-version = "1.76.0"

[workspace.dependencies]
anchor-lang = "0.30.1"
anchor-spl = "0.30.1"
anchor-lang = { git = "https://github.com/coral-xyz/anchor", rev = "d3a51c5" }
anchor-spl = { git = "https://github.com/coral-xyz/anchor", rev = "d3a51c5" }
anyhow = "1"
assert2 = "0.3"
async-trait = "0.1"
Expand Down Expand Up @@ -52,25 +52,28 @@ serde_bytes = "0.11"
serde_json = "1"
serde_tuple = "1"
serde_with = "3"
solana-banks-client = "=1.18"
solana-banks-interface = "=1.18"
solana-config-program = "=1.18"
solana-faucet = "=1.18"
solana-program = "=1.18"
solana-program-runtime = "=1.18"
solana-program-test = "=1.18"
solana-rpc = "=1.18"
solana-sdk = "=1.18"
solana-test-validator = "=1.18"
solana-version = "=1.18"
spl-associated-token-account = "3"
spl-memo = "4"
spl-pod = "0.2"
spl-tlv-account-resolution = "0.6"
spl-token = "4"
spl-token-2022 = "3"
spl-token-group-interface = "0.2"
spl-token-metadata-interface = "0.3"
solana-banks-client = "2"
solana-banks-interface = "2"
solana-client = "2"
solana-config-program = "2"
solana-faucet = "2"
solana-program = "2"
solana-program-runtime = "2"
solana-program-test = "2"
solana-rpc = "2"
solana-sdk = "2"
solana-test-validator = "2"
solana-version = "2"
solana-zk-token-sdk = "2"
spl-associated-token-account = "5"
spl-memo = "5"
spl-pod = "0.4"
spl-tlv-account-resolution = "0.8"
spl-token = "6"
spl-token-2022 = "5"
spl-token-group-interface = "0.4"
spl-token-metadata-interface = "0.5"
strum = "0.26"
tarpc = "0.29"
test-log = "0.2"
thiserror = "1"
Expand Down Expand Up @@ -100,7 +103,7 @@ git = "https://github.com/anza-xyz/curve25519-dalek.git"
rev = "b500cdc2a920cd5bff9e2dd974d7b97349d61464"

[workspace.metadata.bin]
anchor-cli = { version = "0.30.1", bins = ["anchor"] }
anchor-cli = { git = "https://github.com/coral-xyz/anchor", rev = "d3a51c5", version = "0.30.1", bins = ["anchor"] }
cargo-insta = { version = "1.40.0" }
cargo-llvm-cov = { version = "0.6.10" }
cargo-nextest = { version = "0.9.72" }
Expand Down
19 changes: 8 additions & 11 deletions crates/test_utils_solana/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use chrono_humanize::Tense;
use solana_banks_client::BanksClient;
use solana_banks_client::BanksClientError;
use solana_banks_interface::BanksTransactionResultWithSimulation;
use solana_program_runtime::compute_budget_processor::DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT;
use solana_program_runtime::invoke_context::BuiltinFunctionWithContext;
use solana_program_test::BanksTransactionResultWithMetadata;
use solana_program_test::ProgramTest;
Expand Down Expand Up @@ -43,8 +42,6 @@ use wallet_standard::prelude::*;
use wasm_client_anchor::AnchorClientResult;
use wasm_client_anchor::prelude::*;

pub const MAX_COMPUTE_UNITS: u64 = DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT as u64;

#[async_trait(?Send)]
pub trait BanksClientAsyncExtension {
/// Sign the transaction with the provided wallet.
Expand Down Expand Up @@ -237,7 +234,7 @@ pub trait ProgramTestExtension {
/// The program is upgradeable if `Some` `program_authority` is provided.
fn add_bpf_program(
&mut self,
program_name: &str,
program_name: &'static str,
program_id: Pubkey,
program_authority: Option<Pubkey>,
process_instruction: Option<BuiltinFunctionWithContext>,
Expand All @@ -249,7 +246,7 @@ pub trait ProgramTestExtension {
/// [`ProgramTestExtension::add_bpf_program`]
fn add_bpf_program_with_program_data(
&mut self,
program_name: &str,
program_name: &'static str,
program_id: Pubkey,
program_authority: Option<Pubkey>,
program_data: Pubkey,
Expand Down Expand Up @@ -293,7 +290,7 @@ impl ProgramTestExtension for ProgramTest {
anchor_data: T,
executable: bool,
) {
let discriminator = &T::discriminator();
let discriminator = &T::DISCRIMINATOR;
let data = anchor_data
.try_to_vec()
.expect("Cannot serialize provided anchor account");
Expand All @@ -309,7 +306,7 @@ impl ProgramTestExtension for ProgramTest {
owner: Pubkey,
size: usize,
) {
let discriminator = &T::discriminator();
let discriminator = T::DISCRIMINATOR;
let data = vec![0_u8; size];
let mut v = Vec::new();
v.extend_from_slice(discriminator);
Expand Down Expand Up @@ -419,7 +416,7 @@ impl ProgramTestExtension for ProgramTest {

fn add_bpf_program(
&mut self,
program_name: &str,
program_name: &'static str,
program_id: Pubkey,
program_authority: Option<Pubkey>,
process_instruction: Option<BuiltinFunctionWithContext>,
Expand Down Expand Up @@ -486,7 +483,7 @@ impl ProgramTestExtension for ProgramTest {

fn add_bpf_program_with_program_data(
&mut self,
program_name: &str,
program_name: &'static str,
program_id: Pubkey,
program_authority: Option<Pubkey>,
program_data_pubkey: Pubkey,
Expand Down Expand Up @@ -706,10 +703,10 @@ pub trait FromAnchorData {
impl FromAnchorData for AccountSharedData {
fn from_anchor_data<T: AnchorSerialize + Discriminator>(data: T, owner: Pubkey) -> Self {
let mut bytes = Vec::new();
let discriminator = T::discriminator();
let discriminator = T::DISCRIMINATOR;
let anchor_data = data.try_to_vec().expect("cannot serialize anchor data");

bytes.extend_from_slice(&discriminator);
bytes.extend_from_slice(discriminator);
bytes.extend_from_slice(&anchor_data);

let rent = Rent::default().minimum_balance(bytes.len());
Expand Down
3 changes: 2 additions & 1 deletion crates/wasm_client_solana/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ serde_with = { workspace = true }
solana-program = { workspace = true }
solana-sdk = { workspace = true }
solana-version = { workspace = true }
solana-zk-token-sdk = { workspace = true }
spl-associated-token-account = { workspace = true, features = ["no-entrypoint"] }
spl-memo = { workspace = true, features = ["no-entrypoint"] }
spl-token = { workspace = true, features = ["no-entrypoint"] }
spl-token-2022 = { workspace = true, features = ["no-entrypoint"] }
spl-token-2022 = { workspace = true, features = ["no-entrypoint", "serde-traits"] }
spl-token-group-interface = { workspace = true }
spl-token-metadata-interface = { workspace = true }
thiserror = { workspace = true }
Expand Down
23 changes: 21 additions & 2 deletions crates/wasm_client_solana/src/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ pub async fn initialize_address_lookup_table<
})
.collect::<Vec<_>>();

let Some(instruction) = instructions.first().cloned() else {
let Some(instruction) = instructions.first().map(wasm_safe_instruction_clone) else {
return Ok(lookup_table_address);
};

Expand Down Expand Up @@ -402,7 +402,11 @@ pub async fn initialize_address_lookup_table<
instruction_chunk.len().mul(compute_units as usize) as u32,
);
let mut instructions = vec![compute_limit_instruction];
instructions.append(&mut instruction_chunk.to_vec());
let mut cloned_chunk = instruction_chunk
.iter()
.map(wasm_safe_instruction_clone)
.collect::<Vec<_>>();
instructions.append(&mut cloned_chunk);
let mut versioned_transaction = VersionedTransaction::new_unsigned_v0(
&payer,
&instructions,
Expand All @@ -423,3 +427,18 @@ pub async fn initialize_address_lookup_table<

Ok(lookup_table_address)
}

fn wasm_safe_instruction_clone(instruction: &Instruction) -> Instruction {
#[cfg(target_arch = "wasm32")]
{
Instruction {
program_id: instruction.program_id.clone(),
accounts: instruction.accounts.clone(),
data: instruction.data.clone(),
}
}
#[cfg(not(target_arch = "wasm32"))]
{
instruction.clone()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use spl_token_2022::extension::ExtensionType;
use spl_token_2022::extension::StateWithExtensions;
use spl_token_2022::extension::{self};
use spl_token_2022::solana_program::pubkey::Pubkey;
use spl_token_2022::solana_zk_token_sdk::zk_token_elgamal::pod::ElGamalPubkey;
use spl_token_2022::solana_zk_sdk::encryption::pod::elgamal::PodElGamalPubkey;
use spl_token_group_interface::state::TokenGroup;
use spl_token_group_interface::state::TokenGroupMember;
use spl_token_metadata_interface::state::TokenMetadata;
Expand Down Expand Up @@ -373,7 +373,7 @@ impl From<extension::confidential_transfer::ConfidentialTransferMint>
confidential_transfer_mint: extension::confidential_transfer::ConfidentialTransferMint,
) -> Self {
let authority: Option<Pubkey> = confidential_transfer_mint.authority.into();
let auditor_elgamal_pubkey: Option<ElGamalPubkey> =
let auditor_elgamal_pubkey: Option<PodElGamalPubkey> =
confidential_transfer_mint.auditor_elgamal_pubkey.into();
Self {
authority,
Expand All @@ -390,7 +390,7 @@ impl From<extension::confidential_transfer::ConfidentialTransferMint>
pub struct UiConfidentialTransferFeeConfig {
#[serde_as(as = "Option<DisplayFromStr>")]
pub authority: Option<Pubkey>,
pub withdraw_withheld_authority_elgamal_pubkey: Option<String>,
pub withdraw_withheld_authority_elgamal_pubkey: String,
pub harvest_to_mint_enabled: bool,
pub withheld_amount: String,
}
Expand All @@ -402,14 +402,11 @@ impl From<extension::confidential_transfer_fee::ConfidentialTransferFeeConfig>
confidential_transfer_fee_config: extension::confidential_transfer_fee::ConfidentialTransferFeeConfig,
) -> Self {
let authority: Option<Pubkey> = confidential_transfer_fee_config.authority.into();
let withdraw_withheld_authority_elgamal_pubkey: Option<ElGamalPubkey> =
confidential_transfer_fee_config
.withdraw_withheld_authority_elgamal_pubkey
.into();
Self {
authority,
withdraw_withheld_authority_elgamal_pubkey: withdraw_withheld_authority_elgamal_pubkey
.map(|pubkey| pubkey.to_string()),
withdraw_withheld_authority_elgamal_pubkey: confidential_transfer_fee_config
.withdraw_withheld_authority_elgamal_pubkey
.to_string(),
harvest_to_mint_enabled: confidential_transfer_fee_config
.harvest_to_mint_enabled
.into(),
Expand Down Expand Up @@ -631,10 +628,12 @@ impl From<extension::group_member_pointer::GroupMemberPointer> for UiGroupMember
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct UiTokenGroup {
#[serde_as(as = "Option<DisplayFromStr>")]
pub update_authority: Option<Pubkey>,
#[serde_as(as = "DisplayFromStr")]
pub mint: Pubkey,
pub size: u32,
pub max_size: u32,
pub size: u64,
pub max_size: u64,
}

impl From<TokenGroup> for UiTokenGroup {
Expand All @@ -658,7 +657,7 @@ pub struct UiTokenGroupMember {
pub mint: Pubkey,
#[serde_as(as = "DisplayFromStr")]
pub group: Pubkey,
pub member_number: u32,
pub member_number: u64,
}

impl From<TokenGroupMember> for UiTokenGroupMember {
Expand Down
16 changes: 13 additions & 3 deletions crates/wasm_client_solana/src/solana_transaction_status/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use solana_sdk::message::v0::LoadedMessage;
use solana_sdk::message::v0::MessageAddressTableLookup;
use solana_sdk::message::v0::{self};
use solana_sdk::pubkey::Pubkey;
use solana_sdk::reserved_account_keys::ReservedAccountKeys;
pub use solana_sdk::reward_type::RewardType;
use solana_sdk::signature::Signature;
use solana_sdk::transaction::Result as TransactionResult;
Expand Down Expand Up @@ -951,12 +952,16 @@ impl VersionedTransactionWithStatusMeta {
show_rewards: bool,
) -> Result<EncodedTransactionWithStatusMeta, EncodeError> {
let version = self.validate_version(max_supported_transaction_version)?;
let reserved_account_keys = ReservedAccountKeys::new_all_activated();

let account_keys = match &self.transaction.message {
VersionedMessage::Legacy(message) => parse_legacy_message_accounts(message),
VersionedMessage::V0(message) => {
let loaded_message =
LoadedMessage::new_borrowed(message, &self.meta.loaded_addresses);
let loaded_message = LoadedMessage::new_borrowed(
message,
&self.meta.loaded_addresses,
&reserved_account_keys.active,
);
parse_v0_message_accounts(&loaded_message)
}
};
Expand Down Expand Up @@ -1222,8 +1227,13 @@ impl EncodableWithMeta for v0::Message {
meta: &TransactionStatusMeta,
) -> Self::Encoded {
if encoding == UiTransactionEncoding::JsonParsed {
let reserved_account_keys = ReservedAccountKeys::new_all_activated();
let account_keys = AccountKeys::new(&self.account_keys, Some(&meta.loaded_addresses));
let loaded_message = LoadedMessage::new_borrowed(self, &meta.loaded_addresses);
let loaded_message = LoadedMessage::new_borrowed(
self,
&meta.loaded_addresses,
&reserved_account_keys.active,
);
UiMessage::Parsed(UiParsedMessage {
account_keys: parse_v0_message_accounts(&loaded_message),
recent_blockhash: self.recent_blockhash,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashSet;

use serde::Deserialize;
use serde::Serialize;
use serde_with::DisplayFromStr;
Expand Down Expand Up @@ -32,7 +34,7 @@ pub fn parse_legacy_message_accounts(message: &Message) -> Vec<ParsedAccount> {
for (i, pubkey) in message.account_keys.iter().enumerate() {
accounts.push(ParsedAccount {
pubkey: *pubkey,
writable: message.is_writable(i),
writable: message.is_maybe_writable(i, Some(&HashSet::new())),
signer: message.is_signer(i),
source: Some(ParsedAccountSource::Transaction),
});
Expand Down Expand Up @@ -68,6 +70,7 @@ mod test {
use solana_sdk::message::v0;
use solana_sdk::message::v0::LoadedAddresses;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::reserved_account_keys::ReservedAccountKeys;

use super::*;

Expand Down Expand Up @@ -137,6 +140,7 @@ mod test {
writable: vec![pubkey4],
readonly: vec![pubkey5],
},
&ReservedAccountKeys::empty_key_set(),
);

assert_eq!(parse_v0_message_accounts(&message), vec![
Expand Down
Loading

0 comments on commit d08acc1

Please sign in to comment.