From 68362aca5811165e7ead35016a1faaa3c50e215c Mon Sep 17 00:00:00 2001 From: Sammy Harris <41593264+stegaBOB@users.noreply.github.com> Date: Sat, 30 Jul 2022 14:53:10 -0400 Subject: [PATCH] feat: Adds transaction function to RequestBuilder (#1985) --- CHANGELOG.md | 6 ++++- client/src/lib.rs | 65 ++++++++++++++++++++++++++--------------------- 2 files changed, 41 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e3219254..be8c5f97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,10 @@ The minor version will be incremented upon a breaking change and the patch versi ## [Unreleased] +### Features + +* client: Add `transaction` functions to RequestBuilder ([#1958](https://github.com/coral-xyz/anchor/pull/1958)). + ## [0.25.0] - 2022-07-05 ### Features @@ -26,7 +30,7 @@ The minor version will be incremented upon a breaking change and the patch versi * cli: Allow passing arguments to an underlying script with `anchor run` ([#1914](https://github.com/coral-xyz/anchor/pull/1914)). * ts: Implement a coder for system program ([#1920](https://github.com/coral-xyz/anchor/pull/1920)). * ts: Add `program.coder.types` for encoding/decoding user-defined types ([#1931](https://github.com/coral-xyz/anchor/pull/1931)). -* client: Add send_with_spinner_and_config function to RequestBuilder ([#1926](https://github.com/coral-xyz/anchor/pull/1926)). +* client: Add `send_with_spinner_and_config` function to RequestBuilder ([#1926](https://github.com/coral-xyz/anchor/pull/1926)). * ts: Implement a coder for SPL associated token program ([#1939](https://github.com/coral-xyz/anchor/pull/1939)). * ts: verbose error for missing `ANCHOR_WALLET` variable when using `NodeWallet.local()` ([#1958](https://github.com/coral-xyz/anchor/pull/1958)). * ts: Add `MethodsBuilder#accountsStrict` for strict typing on ix account input ([#2019](https://github.com/coral-xyz/anchor/pull/2019)). diff --git a/client/src/lib.rs b/client/src/lib.rs index 5bbf862a..90fbf534 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -1,6 +1,7 @@ //! `anchor_client` provides an RPC client to send transactions and fetch //! deserialized accounts from Solana programs written in `anchor_lang`. +use anchor_lang::solana_program::hash::Hash; use anchor_lang::solana_program::instruction::{AccountMeta, Instruction}; use anchor_lang::solana_program::program_error::ProgramError; use anchor_lang::solana_program::pubkey::Pubkey; @@ -534,23 +535,42 @@ impl<'a> RequestBuilder<'a> { Ok(instructions) } - pub fn send(self) -> Result { + fn signed_transaction_with_blockhash( + &self, + latest_hash: Hash, + ) -> Result { let instructions = self.instructions()?; - - let mut signers = self.signers; + let mut signers = self.signers.clone(); signers.push(&*self.payer); - let rpc_client = RpcClient::new_with_commitment(self.cluster, self.options); + let tx = Transaction::new_signed_with_payer( + &instructions, + Some(&self.payer.pubkey()), + &signers, + latest_hash, + ); - let tx = { - let latest_hash = rpc_client.get_latest_blockhash()?; - Transaction::new_signed_with_payer( - &instructions, - Some(&self.payer.pubkey()), - &signers, - latest_hash, - ) - }; + Ok(tx) + } + + pub fn signed_transaction(&self) -> Result { + let latest_hash = + RpcClient::new_with_commitment(&self.cluster, self.options).get_latest_blockhash()?; + let tx = self.signed_transaction_with_blockhash(latest_hash)?; + + Ok(tx) + } + + pub fn transaction(&self) -> Result { + let instructions = &self.instructions; + let tx = Transaction::new_with_payer(instructions, Some(&self.payer.pubkey())); + Ok(tx) + } + + pub fn send(self) -> Result { + let rpc_client = RpcClient::new_with_commitment(&self.cluster, self.options); + let latest_hash = rpc_client.get_latest_blockhash()?; + let tx = self.signed_transaction_with_blockhash(latest_hash)?; rpc_client .send_and_confirm_transaction(&tx) @@ -561,22 +581,9 @@ impl<'a> RequestBuilder<'a> { self, config: RpcSendTransactionConfig, ) -> Result { - let instructions = self.instructions()?; - - let mut signers = self.signers; - signers.push(&*self.payer); - - let rpc_client = RpcClient::new_with_commitment(self.cluster, self.options); - - let tx = { - let latest_hash = rpc_client.get_latest_blockhash()?; - Transaction::new_signed_with_payer( - &instructions, - Some(&self.payer.pubkey()), - &signers, - latest_hash, - ) - }; + let rpc_client = RpcClient::new_with_commitment(&self.cluster, self.options); + let latest_hash = rpc_client.get_latest_blockhash()?; + let tx = self.signed_transaction_with_blockhash(latest_hash)?; rpc_client .send_and_confirm_transaction_with_spinner_and_config(