Make find_program_address client example runnable (#23492) (#23901)

(cherry picked from commit 6428602cd9)

Co-authored-by: Brian Anderson <andersrb@gmail.com>
This commit is contained in:
mergify[bot]
2022-03-24 15:00:32 +00:00
committed by GitHub
parent 2ed9655958
commit 1b930a1485
2 changed files with 79 additions and 56 deletions

View File

@ -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) {} pub fn sign<T: Signers>(&mut self, _keypairs: &T, _recent_blockhash: Hash) {}
} }
} }

View File

@ -346,22 +346,24 @@ impl Pubkey {
/// ///
/// The client program: /// 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 borsh::{BorshSerialize, BorshDeserialize};
/// # use solana_program::pubkey::Pubkey; /// # use solana_program::example_mocks::{solana_sdk, solana_client};
/// # use solana_program::instruction::Instruction; /// # use solana_program::{
/// # use solana_program::hash::Hash; /// # pubkey::Pubkey,
/// # use solana_program::instruction::AccountMeta; /// # instruction::Instruction,
/// # use solana_program::system_program; /// # hash::Hash,
/// # use solana_sdk::signature::Keypair; /// # instruction::AccountMeta,
/// # use solana_sdk::signature::{Signer, Signature}; /// # system_program,
/// # use solana_sdk::transaction::Transaction; /// # };
/// # use solana_sdk::{
/// # signature::Keypair,
/// # signature::{Signer, Signature},
/// # transaction::Transaction,
/// # };
/// # use solana_client::rpc_client::RpcClient; /// # use solana_client::rpc_client::RpcClient;
/// # use std::convert::TryFrom; /// # use std::convert::TryFrom;
/// # use anyhow::Result;
/// # /// #
/// # #[derive(BorshSerialize, BorshDeserialize, Debug)] /// # #[derive(BorshSerialize, BorshDeserialize, Debug)]
/// # struct InstructionData { /// # struct InstructionData {
@ -370,10 +372,12 @@ impl Pubkey {
/// # } /// # }
/// # /// #
/// # pub static VAULT_ACCOUNT_SIZE: u64 = 1024; /// # pub static VAULT_ACCOUNT_SIZE: u64 = 1024;
/// # let program_id = Pubkey::new_unique();
/// # let payer = Keypair::new();
/// # let rpc_client = RpcClient::new("no-run".to_string());
/// # /// #
/// fn create_vault_account(
/// client: &RpcClient,
/// program_id: Pubkey,
/// payer: &Keypair,
/// ) -> Result<()> {
/// // Derive the PDA from the payer account, a string representing the unique /// // 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. /// // purpose of the account ("vault"), and the address of our on-chain program.
/// let (vault_pubkey, vault_bump_seed) = Pubkey::find_program_address( /// let (vault_pubkey, vault_bump_seed) = Pubkey::find_program_address(
@ -383,7 +387,7 @@ impl Pubkey {
/// ///
/// // Get the amount of lamports needed to pay for the vault's rent /// // Get the amount of lamports needed to pay for the vault's rent
/// let vault_account_size = usize::try_from(VAULT_ACCOUNT_SIZE)?; /// let vault_account_size = usize::try_from(VAULT_ACCOUNT_SIZE)?;
/// let lamports = rpc_client.get_minimum_balance_for_rent_exemption(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. /// // The on-chain program's instruction data, imported from that program's crate.
/// let instr_data = InstructionData { /// let instr_data = InstructionData {
@ -406,16 +410,25 @@ impl Pubkey {
/// accounts, /// accounts,
/// ); /// );
/// ///
/// let blockhash = rpc_client.get_latest_blockhash()?; /// let blockhash = client.get_latest_blockhash()?;
/// ///
/// let transaction = Transaction::new_signed_with_payer( /// let transaction = Transaction::new_signed_with_payer(
/// &[instruction], /// &[instruction],
/// Some(&payer.pubkey()), /// Some(&payer.pubkey()),
/// &[&payer], /// &[payer],
/// blockhash, /// blockhash,
/// ); /// );
/// ///
/// rpc_client.send_and_confirm_transaction(&transaction)?; /// client.send_and_confirm_transaction(&transaction)?;
///
/// Ok(())
/// }
/// # let program_id = Pubkey::new_unique();
/// # let payer = Keypair::new();
/// # let client = RpcClient::new(String::new());
/// #
/// # create_vault_account(&client, program_id, &payer)?;
/// #
/// # Ok::<(), anyhow::Error>(()) /// # Ok::<(), anyhow::Error>(())
/// ``` /// ```
pub fn find_program_address(seeds: &[&[u8]], program_id: &Pubkey) -> (Pubkey, u8) { pub fn find_program_address(seeds: &[&[u8]], program_id: &Pubkey) -> (Pubkey, u8) {