From 632e9c8387c7ae8c2151d0fe647b305a0b7a35d5 Mon Sep 17 00:00:00 2001 From: lukacan Date: Thu, 28 Sep 2023 14:30:39 +0200 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=A7=91=E2=80=8D=F0=9F=92=BB=20trait?= =?UTF-8?q?=20Default=20for=20Client?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.toml | 1 + crates/client/Cargo.toml | 1 + crates/client/src/client.rs | 18 ++++++++++++++++++ .../src/templates/trdelnik-tests/test.rs | 2 +- examples/escrow/trdelnik-tests/tests/test.rs | 3 +++ .../turnstile/trdelnik-tests/tests/test.rs | 3 +++ 6 files changed, 27 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 8e5ef5cb..566ba3ec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -60,3 +60,4 @@ num-traits = "0.2.14" proc-macro2 = { version = "1.0.66", default-features = false } darling = "0.13.1" clap = { version = "4.3.19", features = ["derive"] } +shellexpand = "3.1.0" diff --git a/crates/client/Cargo.toml b/crates/client/Cargo.toml index de3d6521..d4d34527 100644 --- a/crates/client/Cargo.toml +++ b/crates/client/Cargo.toml @@ -49,3 +49,4 @@ honggfuzz = { version = "0.5.55", optional = true } arbitrary = { version = "1.3.0", optional = true } solana-program-test = { version = "1.16.9", optional = true} quinn-proto = { version = "0.9.4", optional = true} +shellexpand = { workspace = true } diff --git a/crates/client/src/client.rs b/crates/client/src/client.rs index 3d81ada3..dd426254 100644 --- a/crates/client/src/client.rs +++ b/crates/client/src/client.rs @@ -12,6 +12,7 @@ use anchor_client::{ instruction::Instruction, loader_instruction, pubkey::Pubkey, + signature::read_keypair_file, signer::{keypair::Keypair, Signer}, system_instruction, transaction::Transaction, @@ -36,6 +37,7 @@ use std::{thread::sleep, time::Duration}; // https://github.com/project-serum/anchor/pull/1307#issuecomment-1022592683 const RETRY_LOCALNET_EVERY_MILLIS: u64 = 500; +const DEFAULT_KEYPAIR_PATH: &str = "~/.config/solana/id.json"; type Payer = Rc; @@ -45,6 +47,22 @@ pub struct Client { anchor_client: AnchorClient, } +/// Implement Default trait for Client, which reads keypair from default path for `solana-keygen new` +impl Default for Client { + fn default() -> Self { + let payer = read_keypair_file(&*shellexpand::tilde(DEFAULT_KEYPAIR_PATH)) + .unwrap_or_else(|_| panic!("Default keypair {DEFAULT_KEYPAIR_PATH} not found.")); + Self { + payer: payer.clone(), + anchor_client: AnchorClient::new_with_options( + Cluster::Localnet, + Rc::new(payer.clone()), + CommitmentConfig::confirmed(), + ), + } + } +} + impl Client { /// Creates a new `Client` instance. pub fn new(payer: Keypair) -> Self { diff --git a/crates/client/src/templates/trdelnik-tests/test.rs b/crates/client/src/templates/trdelnik-tests/test.rs index b64b3188..a0807c7c 100644 --- a/crates/client/src/templates/trdelnik-tests/test.rs +++ b/crates/client/src/templates/trdelnik-tests/test.rs @@ -28,7 +28,7 @@ struct Fixture { impl Fixture { fn new() -> Self { Fixture { - client: Client::new(system_keypair(0)), + client: Client::default(), program: anchor_keypair("###PROGRAM_NAME###").unwrap(), state: keypair(42), } diff --git a/examples/escrow/trdelnik-tests/tests/test.rs b/examples/escrow/trdelnik-tests/tests/test.rs index 92ad9dfd..8d466af7 100644 --- a/examples/escrow/trdelnik-tests/tests/test.rs +++ b/examples/escrow/trdelnik-tests/tests/test.rs @@ -199,6 +199,9 @@ struct Fixture { impl Fixture { fn new() -> Self { Fixture { + // We use the hardcoded system_keypair(0). + // However the default option in the test template is now to use implementation of trait Default + // for Client which will read keypair from "~/.config/solana/id.json" - (default path for `solana-keygen new`) client: Client::new(system_keypair(0)), // We use the hardcoded program_keypair(1) to ensure users can run these tests without the diff --git a/examples/turnstile/trdelnik-tests/tests/test.rs b/examples/turnstile/trdelnik-tests/tests/test.rs index 9c14ea7d..4e698718 100644 --- a/examples/turnstile/trdelnik-tests/tests/test.rs +++ b/examples/turnstile/trdelnik-tests/tests/test.rs @@ -7,6 +7,9 @@ use trdelnik_client::{anyhow::Result, *}; async fn init_fixture() -> Fixture { // create a test fixture let fixture = Fixture { + // We use the hardcoded system_keypair(0). + // However the default option in the test template is now to use implementation of trait Default + // for Client which will read keypair from "~/.config/solana/id.json" - (default path for `solana-keygen new`) client: Client::new(system_keypair(0)), // We use the hardcoded program_keypair(1) to ensure users can run these tests without the From db4ebcc3ace0d31b73b8f639d17d462259980aa6 Mon Sep 17 00:00:00 2001 From: lukacan Date: Fri, 6 Oct 2023 13:09:14 +0200 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=94=A5=20removed=20unnecessary=20clon?= =?UTF-8?q?e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/client/src/client.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/client/src/client.rs b/crates/client/src/client.rs index dd426254..e5f06c01 100644 --- a/crates/client/src/client.rs +++ b/crates/client/src/client.rs @@ -56,7 +56,7 @@ impl Default for Client { payer: payer.clone(), anchor_client: AnchorClient::new_with_options( Cluster::Localnet, - Rc::new(payer.clone()), + Rc::new(payer), CommitmentConfig::confirmed(), ), }