From 1b930a14855d0c3fd31072a3df7c7e80e66ab3ed Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Thu, 24 Mar 2022 15:00:32 +0000 Subject: [PATCH] Make find_program_address client example runnable (#23492) (#23901) (cherry picked from commit 6428602cd98d4d73397b92c044449df5b4d34624) Co-authored-by: Brian Anderson --- sdk/program/src/example_mocks.rs | 10 +++ sdk/program/src/pubkey.rs | 125 +++++++++++++++++-------------- 2 files changed, 79 insertions(+), 56 deletions(-) diff --git a/sdk/program/src/example_mocks.rs b/sdk/program/src/example_mocks.rs index 5b53ccd8d5..8a93fdda4d 100644 --- a/sdk/program/src/example_mocks.rs +++ b/sdk/program/src/example_mocks.rs @@ -130,6 +130,16 @@ pub mod solana_sdk { } } + pub fn new_signed_with_payer( + 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(&mut self, _keypairs: &T, _recent_blockhash: Hash) {} } } diff --git a/sdk/program/src/pubkey.rs b/sdk/program/src/pubkey.rs index a9220a7ad4..6368b38397 100644 --- a/sdk/program/src/pubkey.rs +++ b/sdk/program/src/pubkey.rs @@ -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 { @@ -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) {