Browse Source

feat: Adds transaction function to RequestBuilder (#1985)

Sammy Harris 3 years ago
parent
commit
68362aca58
2 changed files with 41 additions and 30 deletions
  1. 5 1
      CHANGELOG.md
  2. 36 29
      client/src/lib.rs

+ 5 - 1
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)).

+ 36 - 29
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<Signature, ClientError> {
+    fn signed_transaction_with_blockhash(
+        &self,
+        latest_hash: Hash,
+    ) -> Result<Transaction, ClientError> {
         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<Transaction, ClientError> {
+        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<Transaction, ClientError> {
+        let instructions = &self.instructions;
+        let tx = Transaction::new_with_payer(instructions, Some(&self.payer.pubkey()));
+        Ok(tx)
+    }
+
+    pub fn send(self) -> Result<Signature, ClientError> {
+        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<Signature, ClientError> {
-        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(