Browse Source

client: Allow extraction of instructions from RequestBuilder (#952)

Drew Nutter 3 years ago
parent
commit
2758e8d6c4
1 changed files with 31 additions and 27 deletions
  1. 31 27
      client/src/lib.rs

+ 31 - 27
client/src/lib.rs

@@ -407,42 +407,46 @@ impl<'a> RequestBuilder<'a> {
         self
     }
 
-    pub fn send(self) -> Result<Signature, ClientError> {
-        let accounts = match self.namespace {
-            RequestNamespace::State { new } => {
-                let mut accounts = match new {
-                    false => vec![AccountMeta::new(
+    pub fn instructions(&self) -> Result<Vec<Instruction>, ClientError> {
+        let mut accounts = match self.namespace {
+            RequestNamespace::State { new } => match new {
+                false => vec![AccountMeta::new(
+                    anchor_lang::__private::state::address(&self.program_id),
+                    false,
+                )],
+                true => vec![
+                    AccountMeta::new_readonly(self.payer.pubkey(), true),
+                    AccountMeta::new(
                         anchor_lang::__private::state::address(&self.program_id),
                         false,
-                    )],
-                    true => vec![
-                        AccountMeta::new_readonly(self.payer.pubkey(), true),
-                        AccountMeta::new(
-                            anchor_lang::__private::state::address(&self.program_id),
-                            false,
-                        ),
-                        AccountMeta::new_readonly(
-                            Pubkey::find_program_address(&[], &self.program_id).0,
-                            false,
-                        ),
-                        AccountMeta::new_readonly(system_program::ID, false),
-                        AccountMeta::new_readonly(self.program_id, false),
-                    ],
-                };
-                accounts.extend_from_slice(&self.accounts);
-                accounts
-            }
-            _ => self.accounts,
+                    ),
+                    AccountMeta::new_readonly(
+                        Pubkey::find_program_address(&[], &self.program_id).0,
+                        false,
+                    ),
+                    AccountMeta::new_readonly(system_program::ID, false),
+                    AccountMeta::new_readonly(self.program_id, false),
+                ],
+            },
+            _ => Vec::new(),
         };
-        let mut instructions = self.instructions;
-        if let Some(ix_data) = self.instruction_data {
+        accounts.extend_from_slice(&self.accounts);
+
+        let mut instructions = self.instructions.clone();
+        if let Some(ix_data) = &self.instruction_data {
             instructions.push(Instruction {
                 program_id: self.program_id,
-                data: ix_data,
+                data: ix_data.clone(),
                 accounts,
             });
         }
 
+        Ok(instructions)
+    }
+
+    pub fn send(self) -> Result<Signature, ClientError> {
+        let instructions = self.instructions()?;
+
         let mut signers = self.signers;
         signers.push(&self.payer);