Skip to content

Commit

Permalink
Merge pull request #105 from Ackee-Blockchain/feat/client-default-payer
Browse files Browse the repository at this point in the history
πŸ§‘β€πŸ’» trait Default for Client
  • Loading branch information
Ikrk committed Oct 6, 2023
2 parents 0134183 + db4ebcc commit e1556bd
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 1 deletion.
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

0 comments on commit e1556bd

Please sign in to comment.