Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧑‍💻 trait Default for Client #105

Merged
merged 2 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 1 addition & 0 deletions crates/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
18 changes: 18 additions & 0 deletions crates/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<Keypair>;

Expand All @@ -45,6 +47,22 @@ pub struct Client {
anchor_client: AnchorClient<Payer>,
}

/// 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),
CommitmentConfig::confirmed(),
),
}
}
}

impl Client {
/// Creates a new `Client` instance.
pub fn new(payer: Keypair) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion crates/client/src/templates/trdelnik-tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down
3 changes: 3 additions & 0 deletions examples/escrow/trdelnik-tests/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions examples/turnstile/trdelnik-tests/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading