Skip to content

Commit f29c29f

Browse files
committed
Add airdrop looper
1 parent 390a98e commit f29c29f

File tree

3 files changed

+57
-15
lines changed

3 files changed

+57
-15
lines changed

src/cli.rs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const LOCATION_HELP: &str =
1616
- Solana Program Registry artifact, for example `spr:QuarryProtocol/quarry_mine`
1717
";
1818

19-
#[derive(Clone, Debug, clap::Subcommand)]
19+
#[derive(Clone, Debug, clap::Subcommand, PartialEq, Eq)]
2020
pub enum SubCommand {
2121
/// Initializes a new Goki workspace.
2222
Init,
@@ -33,6 +33,16 @@ pub enum SubCommand {
3333
/// Airdrop request amount in SOL.
3434
#[clap(default_value = "1")]
3535
amount: String,
36+
37+
/// Number of times to request an airdrop.
38+
#[clap(short, long)]
39+
#[clap(default_value = "1")]
40+
iterations: u32,
41+
42+
/// Interval between airdrop requests, in milliseconds.
43+
#[clap(short, long)]
44+
#[clap(default_value = "5000")]
45+
interval: u64,
3646
},
3747
/// Transfers SOL from a wallet.
3848
Transfer {
@@ -143,17 +153,30 @@ pub struct Opts {
143153

144154
impl Opts {
145155
pub async fn run(&self) -> Result<()> {
156+
if self.command.clone() == SubCommand::Init {
157+
return subcommands::init::process(&self.workspace_path);
158+
}
159+
146160
let workspace = Workspace::load(&self.workspace_path)?;
147161
println!("Using workspace at {}", workspace.path.display());
148162
match self.command.clone() {
149-
SubCommand::Init => {
150-
subcommands::init::process(&workspace)?;
151-
}
163+
SubCommand::Init => {}
152164
SubCommand::Show => {
153165
subcommands::show::process(&workspace)?;
154166
}
155-
SubCommand::Airdrop { cluster, amount } => {
156-
subcommands::airdrop::process(&workspace, cluster, amount.as_str())?;
167+
SubCommand::Airdrop {
168+
cluster,
169+
amount,
170+
iterations,
171+
interval,
172+
} => {
173+
subcommands::airdrop::process(
174+
&workspace,
175+
cluster,
176+
amount.as_str(),
177+
iterations,
178+
interval,
179+
)?;
157180
}
158181
SubCommand::Transfer {
159182
cluster,

src/subcommands/airdrop.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
11
use anchor_client::Cluster;
22
use anyhow::{format_err, Result};
3+
use std::{thread, time::Duration};
34

45
use crate::workspace::Workspace;
56

6-
pub fn process(workspace: &Workspace, cluster: Cluster, amount: &str) -> Result<()> {
7+
pub fn process(
8+
workspace: &Workspace,
9+
cluster: Cluster,
10+
amount: &str,
11+
iterations: u32,
12+
interval: u64,
13+
) -> Result<()> {
714
if cluster == Cluster::Mainnet {
815
return Err(format_err!("cannot request an airdrop from mainnet"));
916
}
1017

11-
workspace.exec_deployer_command(&cluster, |cmd| {
12-
cmd.arg("airdrop").arg(amount);
13-
Ok(())
14-
})?;
18+
for _ in 0..iterations {
19+
workspace.exec_deployer_command(&cluster, |cmd| {
20+
cmd.arg("airdrop").arg(amount);
21+
Ok(())
22+
})?;
23+
24+
thread::sleep(Duration::from_millis(interval));
25+
}
1526

1627
Ok(())
1728
}

src/subcommands/init.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,22 @@ use std::{fs, path::Path};
99
use crate::utils::{exec_command, gen_keypair_file};
1010
use crate::{config::Config, workspace::Workspace};
1111

12-
pub fn process(workspace: &Workspace) -> Result<()> {
13-
if Config::discover()?.is_some() {
14-
println!("Goki.toml already exists in workspace")
12+
pub fn process(path: &Path) -> Result<()> {
13+
let maybe_cfg = Config::discover()?;
14+
let cfg = if let Some(cfg) = maybe_cfg {
15+
println!("Goki.toml already exists in workspace");
16+
cfg.into_inner()
1517
} else {
1618
let cfg = Config::default();
1719
let mut file = File::create("Goki.toml")?;
1820
file.write_all(cfg.to_string().as_bytes())?;
19-
}
21+
cfg
22+
};
23+
24+
let workspace = &Workspace {
25+
path: path.to_path_buf(),
26+
cfg,
27+
};
2028

2129
fs::create_dir_all(workspace.deployer_dir())?;
2230

0 commit comments

Comments
 (0)