From 4468efb4377adfc3915562dc4a5c5a7521ed9a12 Mon Sep 17 00:00:00 2001 From: Drew Nutter Date: Tue, 14 Dec 2021 16:33:49 -0300 Subject: [PATCH 1/5] Let anchor-client use any Signer instead of only Keypair --- CHANGELOG.md | 1 + client/example/src/main.rs | 3 ++- client/src/lib.rs | 27 ++++++++++++++++----------- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d0d49a396e..4703a61985 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ incremented for features. ### Breaking * lang, ts: Error codes have been mapped to new numbers to allow for more errors per namespace ([#1096](https://github.com/project-serum/anchor/pull/1096)). +* client: Client::new and Client::new_with_options now accept `Rc` instead of `Keypair` ([#975](https://github.com/project-serum/anchor/pull/975)). ## [0.18.2] - 2021-11-14 diff --git a/client/example/src/main.rs b/client/example/src/main.rs index 6ce0b536df..baa58a8d90 100644 --- a/client/example/src/main.rs +++ b/client/example/src/main.rs @@ -22,6 +22,7 @@ use composite::accounts::{Bar, CompositeUpdate, Foo, Initialize}; use composite::instruction as composite_instruction; use composite::{DummyA, DummyB}; use rand::rngs::OsRng; +use std::rc::Rc; use std::time::Duration; #[derive(Parser, Debug)] @@ -51,7 +52,7 @@ fn main() -> Result<()> { ); // Client. - let client = Client::new_with_options(url, payer, CommitmentConfig::processed()); + let client = Client::new_with_options(url, Rc::new(payer), CommitmentConfig::processed()); // Run tests. composite(&client, opts.composite_pid)?; diff --git a/client/src/lib.rs b/client/src/lib.rs index d42dcc04f8..c28508f0d4 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -13,9 +13,10 @@ use solana_client::rpc_client::RpcClient; use solana_client::rpc_config::{RpcTransactionLogsConfig, RpcTransactionLogsFilter}; use solana_client::rpc_response::{Response as RpcResponse, RpcLogsResponse}; use solana_sdk::commitment_config::CommitmentConfig; -use solana_sdk::signature::{Keypair, Signature, Signer}; +use solana_sdk::signature::{Signature, Signer}; use solana_sdk::transaction::Transaction; use std::convert::Into; +use std::rc::Rc; use thiserror::Error; pub use anchor_lang; @@ -36,7 +37,7 @@ pub struct Client { } impl Client { - pub fn new(cluster: Cluster, payer: Keypair) -> Self { + pub fn new(cluster: Cluster, payer: Rc) -> Self { Self { cfg: Config { cluster, @@ -46,7 +47,11 @@ impl Client { } } - pub fn new_with_options(cluster: Cluster, payer: Keypair, options: CommitmentConfig) -> Self { + pub fn new_with_options( + cluster: Cluster, + payer: Rc, + options: CommitmentConfig, + ) -> Self { Self { cfg: Config { cluster, @@ -62,7 +67,7 @@ impl Client { cfg: Config { cluster: self.cfg.cluster.clone(), options: self.cfg.options, - payer: Keypair::from_bytes(&self.cfg.payer.to_bytes()).unwrap(), + payer: self.cfg.payer.clone(), }, } } @@ -71,7 +76,7 @@ impl Client { // Internal configuration for a client. struct Config { cluster: Cluster, - payer: Keypair, + payer: Rc, options: Option, } @@ -91,7 +96,7 @@ impl Program { RequestBuilder::from( self.program_id, self.cfg.cluster.url(), - Keypair::from_bytes(&self.cfg.payer.to_bytes()).unwrap(), + self.cfg.payer.clone(), self.cfg.options, RequestNamespace::Global, ) @@ -102,7 +107,7 @@ impl Program { RequestBuilder::from( self.program_id, self.cfg.cluster.url(), - Keypair::from_bytes(&self.cfg.payer.to_bytes()).unwrap(), + self.cfg.payer.clone(), self.cfg.options, RequestNamespace::State { new: false }, ) @@ -321,7 +326,7 @@ pub struct RequestBuilder<'a> { accounts: Vec, options: CommitmentConfig, instructions: Vec, - payer: Keypair, + payer: Rc, // Serialized instruction data for the target RPC. instruction_data: Option>, signers: Vec<&'a dyn Signer>, @@ -343,7 +348,7 @@ impl<'a> RequestBuilder<'a> { pub fn from( program_id: Pubkey, cluster: &str, - payer: Keypair, + payer: Rc, options: Option, namespace: RequestNamespace, ) -> Self { @@ -360,7 +365,7 @@ impl<'a> RequestBuilder<'a> { } } - pub fn payer(mut self, payer: Keypair) -> Self { + pub fn payer(mut self, payer: Rc) -> Self { self.payer = payer; self } @@ -451,7 +456,7 @@ impl<'a> RequestBuilder<'a> { let instructions = self.instructions()?; let mut signers = self.signers; - signers.push(&self.payer); + signers.push(&*self.payer); let rpc_client = RpcClient::new_with_commitment(self.cluster, self.options); From 457e07083ccb3d6c9b8be8bd1d6a4f667e0cb0bd Mon Sep 17 00:00:00 2001 From: Drew Nutter Date: Tue, 14 Dec 2021 19:38:55 -0300 Subject: [PATCH 2/5] move breaking changelog to unreleased section --- CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4703a61985..846e48d01e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ incremented for features. ## [Unreleased] +### Breaking + +* client: Client::new and Client::new_with_options now accept `Rc` instead of `Keypair` ([#975](https://github.com/project-serum/anchor/pull/975)). + ## [0.19.0] - 2021-12-08 ### Fixes @@ -32,7 +36,6 @@ incremented for features. ### Breaking * lang, ts: Error codes have been mapped to new numbers to allow for more errors per namespace ([#1096](https://github.com/project-serum/anchor/pull/1096)). -* client: Client::new and Client::new_with_options now accept `Rc` instead of `Keypair` ([#975](https://github.com/project-serum/anchor/pull/975)). ## [0.18.2] - 2021-11-14 From 92f52e5d8538fab53b991cb9431b672b6f4e9848 Mon Sep 17 00:00:00 2001 From: Drew Nutter Date: Fri, 17 Dec 2021 14:57:32 -0300 Subject: [PATCH 3/5] fix test --- tests/zero-copy/programs/zero-copy/tests/compute_unit_test.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/zero-copy/programs/zero-copy/tests/compute_unit_test.rs b/tests/zero-copy/programs/zero-copy/tests/compute_unit_test.rs index d1faeb39d7..83699dc4fa 100644 --- a/tests/zero-copy/programs/zero-copy/tests/compute_unit_test.rs +++ b/tests/zero-copy/programs/zero-copy/tests/compute_unit_test.rs @@ -1,6 +1,7 @@ #![cfg(feature = "test-bpf")] use { + std::rc::Rc; anchor_client::{ anchor_lang::Discriminator, solana_sdk::{ @@ -42,7 +43,7 @@ async fn update_foo() { let client = Client::new_with_options( Cluster::Debug, - Keypair::new(), + Rc::new(Keypair::new()), CommitmentConfig::processed(), ); let program = client.program(zero_copy::id()); From b9731dadf663e39e4b47bd59a9b520b07fc932d0 Mon Sep 17 00:00:00 2001 From: Drew Nutter Date: Sat, 18 Dec 2021 19:38:28 -0300 Subject: [PATCH 4/5] fix import --- tests/zero-copy/programs/zero-copy/tests/compute_unit_test.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/zero-copy/programs/zero-copy/tests/compute_unit_test.rs b/tests/zero-copy/programs/zero-copy/tests/compute_unit_test.rs index 83699dc4fa..634ed707e3 100644 --- a/tests/zero-copy/programs/zero-copy/tests/compute_unit_test.rs +++ b/tests/zero-copy/programs/zero-copy/tests/compute_unit_test.rs @@ -1,7 +1,7 @@ #![cfg(feature = "test-bpf")] use { - std::rc::Rc; + std::rc::Rc, anchor_client::{ anchor_lang::Discriminator, solana_sdk::{ From 744631ffa98974c3d2cbf187d27c9780b6613c53 Mon Sep 17 00:00:00 2001 From: Drew Nutter Date: Mon, 20 Dec 2021 13:34:16 -0300 Subject: [PATCH 5/5] fix tests --- client/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/client/src/lib.rs b/client/src/lib.rs index 65bd7b388d..33e110492d 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -367,6 +367,7 @@ impl<'a> RequestBuilder<'a> { } } + #[must_use] pub fn payer(mut self, payer: Rc) -> Self { self.payer = payer; self