Skip to content

Commit

Permalink
Make find_program_address client example runnable (#23492)
Browse files Browse the repository at this point in the history
(cherry picked from commit 6428602)
  • Loading branch information
brson authored and mergify-bot committed Mar 24, 2022
1 parent 2ed9655 commit a164dc5
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 56 deletions.
10 changes: 10 additions & 0 deletions sdk/program/src/example_mocks.rs
Expand Up @@ -130,6 +130,16 @@ pub mod solana_sdk {
}
}

pub fn new_signed_with_payer<T: Signers>(
instructions: &[Instruction],
payer: Option<&Pubkey>,
signing_keypairs: &T,
recent_blockhash: Hash,
) -> Self {
let message = Message::new(instructions, payer);
Self::new(signing_keypairs, message, recent_blockhash)
}

pub fn sign<T: Signers>(&mut self, _keypairs: &T, _recent_blockhash: Hash) {}
}
}
Expand Down
125 changes: 69 additions & 56 deletions sdk/program/src/pubkey.rs
Expand Up @@ -346,22 +346,24 @@ impl Pubkey {
///
/// The client program:
///
/// ```ignore
/// # // NB: This example depends on solana_sdk and solana_client, and adding
/// # // those as dev-dependencies would create an unpublishable circular
/// # // dependency, hence it is ignored.
/// #
/// ```
/// # use borsh::{BorshSerialize, BorshDeserialize};
/// # use solana_program::pubkey::Pubkey;
/// # use solana_program::instruction::Instruction;
/// # use solana_program::hash::Hash;
/// # use solana_program::instruction::AccountMeta;
/// # use solana_program::system_program;
/// # use solana_sdk::signature::Keypair;
/// # use solana_sdk::signature::{Signer, Signature};
/// # use solana_sdk::transaction::Transaction;
/// # use solana_program::example_mocks::{solana_sdk, solana_client};
/// # use solana_program::{
/// # pubkey::Pubkey,
/// # instruction::Instruction,
/// # hash::Hash,
/// # instruction::AccountMeta,
/// # system_program,
/// # };
/// # use solana_sdk::{
/// # signature::Keypair,
/// # signature::{Signer, Signature},
/// # transaction::Transaction,
/// # };
/// # use solana_client::rpc_client::RpcClient;
/// # use std::convert::TryFrom;
/// # use anyhow::Result;
/// #
/// # #[derive(BorshSerialize, BorshDeserialize, Debug)]
/// # struct InstructionData {
Expand All @@ -370,52 +372,63 @@ impl Pubkey {
/// # }
/// #
/// # pub static VAULT_ACCOUNT_SIZE: u64 = 1024;
/// #
/// fn create_vault_account(
/// client: &RpcClient,
/// program_id: Pubkey,
/// payer: &Keypair,
/// ) -> Result<()> {
/// // Derive the PDA from the payer account, a string representing the unique
/// // purpose of the account ("vault"), and the address of our on-chain program.
/// let (vault_pubkey, vault_bump_seed) = Pubkey::find_program_address(
/// &[b"vault", payer.pubkey().as_ref()],
/// &program_id
/// );
///
/// // Get the amount of lamports needed to pay for the vault's rent
/// let vault_account_size = usize::try_from(VAULT_ACCOUNT_SIZE)?;
/// let lamports = client.get_minimum_balance_for_rent_exemption(vault_account_size)?;
///
/// // The on-chain program's instruction data, imported from that program's crate.
/// let instr_data = InstructionData {
/// vault_bump_seed,
/// lamports,
/// };
///
/// // The accounts required by both our on-chain program and the system program's
/// // `create_account` instruction, including the vault's address.
/// let accounts = vec![
/// AccountMeta::new(payer.pubkey(), true),
/// AccountMeta::new(vault_pubkey, false),
/// AccountMeta::new(system_program::ID, false),
/// ];
///
/// // Create the instruction by serializing our instruction data via borsh
/// let instruction = Instruction::new_with_borsh(
/// program_id,
/// &instr_data,
/// accounts,
/// );
///
/// let blockhash = client.get_latest_blockhash()?;
///
/// let transaction = Transaction::new_signed_with_payer(
/// &[instruction],
/// Some(&payer.pubkey()),
/// &[payer],
/// blockhash,
/// );
///
/// client.send_and_confirm_transaction(&transaction)?;
///
/// Ok(())
/// }
/// # let program_id = Pubkey::new_unique();
/// # let payer = Keypair::new();
/// # let rpc_client = RpcClient::new("no-run".to_string());
/// # let client = RpcClient::new(String::new());
/// #
/// # create_vault_account(&client, program_id, &payer)?;
/// #
/// // Derive the PDA from the payer account, a string representing the unique
/// // purpose of the account ("vault"), and the address of our on-chain program.
/// let (vault_pubkey, vault_bump_seed) = Pubkey::find_program_address(
/// &[b"vault", payer.pubkey().as_ref()],
/// &program_id
/// );
///
/// // Get the amount of lamports needed to pay for the vault's rent
/// let vault_account_size = usize::try_from(VAULT_ACCOUNT_SIZE)?;
/// let lamports = rpc_client.get_minimum_balance_for_rent_exemption(vault_account_size)?;
///
/// // The on-chain program's instruction data, imported from that program's crate.
/// let instr_data = InstructionData {
/// vault_bump_seed,
/// lamports,
/// };
///
/// // The accounts required by both our on-chain program and the system program's
/// // `create_account` instruction, including the vault's address.
/// let accounts = vec![
/// AccountMeta::new(payer.pubkey(), true),
/// AccountMeta::new(vault_pubkey, false),
/// AccountMeta::new(system_program::ID, false),
/// ];
///
/// // Create the instruction by serializing our instruction data via borsh
/// let instruction = Instruction::new_with_borsh(
/// program_id,
/// &instr_data,
/// accounts,
/// );
///
/// let blockhash = rpc_client.get_latest_blockhash()?;
///
/// let transaction = Transaction::new_signed_with_payer(
/// &[instruction],
/// Some(&payer.pubkey()),
/// &[&payer],
/// blockhash,
/// );
///
/// rpc_client.send_and_confirm_transaction(&transaction)?;
/// # Ok::<(), anyhow::Error>(())
/// ```
pub fn find_program_address(seeds: &[&[u8]], program_id: &Pubkey) -> (Pubkey, u8) {
Expand Down

0 comments on commit a164dc5

Please sign in to comment.