Skip to content

Commit 35cfa2a

Browse files
committed
more features
1 parent 7d757f0 commit 35cfa2a

File tree

3 files changed

+53
-22
lines changed

3 files changed

+53
-22
lines changed

src/cli.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,21 @@ pub enum SubCommand {
3434
#[clap(default_value = "1")]
3535
amount: String,
3636
},
37-
/// Requests an airdrop of SOL from the Solana network.
37+
/// Transfers SOL from a wallet.
3838
Transfer {
3939
/// Cluster to transfer tokens on.
4040
#[clap(short, long)]
4141
#[clap(default_value = "devnet")]
4242
cluster: Cluster,
4343

44+
/// Who to transfer from. Defaults to the upgrade authority ("upgrader").
45+
#[clap(short, long)]
46+
#[clap(default_value = "upgrader")]
47+
#[clap(possible_value("upgrader"), possible_value("deployer"))]
48+
from: String,
49+
4450
/// Who to transfer to. Defaults to the deployer of the network.
51+
#[clap(short, long)]
4552
#[clap(default_value = "deployer")]
4653
to: String,
4754

@@ -150,10 +157,11 @@ impl Opts {
150157
}
151158
SubCommand::Transfer {
152159
cluster,
160+
from,
153161
to,
154162
amount,
155163
} => {
156-
subcommands::transfer::process(&workspace, cluster, &to, &amount)?;
164+
subcommands::transfer::process(&workspace, &cluster, &from, &to, &amount)?;
157165
}
158166
SubCommand::UploadProgramBuffer {
159167
cluster,

src/subcommands/transfer.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@ use crate::workspace::Workspace;
22
use anchor_client::Cluster;
33
use anyhow::Result;
44

5-
pub fn process(workspace: &Workspace, cluster: Cluster, to_raw: &str, amount: &str) -> Result<()> {
6-
let ctx = workspace.new_upgrader_context(&cluster);
7-
let to = match to_raw {
8-
"deployer" => ctx.get_deployer_kp_path().display().to_string(),
9-
_ => to_raw.to_string(),
10-
};
11-
ctx.exec_args(&["transfer", &to, amount])?;
5+
pub fn process(
6+
workspace: &Workspace,
7+
cluster: &Cluster,
8+
from_raw: &str,
9+
to_raw: &str,
10+
amount: &str,
11+
) -> Result<()> {
12+
let ctx = workspace.new_cluster_context(cluster)?;
13+
let to = ctx.parse_wallet_alias(to_raw)?;
14+
ctx.exec_args(
15+
&["transfer", &to, amount],
16+
&ctx.parse_wallet_alias(from_raw)?,
17+
)?;
1218
Ok(())
1319
}

src/workspace.rs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,53 +85,61 @@ impl Workspace {
8585
})
8686
}
8787

88-
/// New [CommandContext] using the configured upgrade authority keypair.
89-
pub fn new_upgrader_context<'a, 'b>(&'a self, cluster: &'b Cluster) -> CommandContext<'a, 'b> {
90-
let keypair_path = self.cfg.upgrade_authority_keypair.clone().unwrap();
91-
CommandContext {
88+
/// The upgrader.
89+
pub fn get_upgrader_wallet(&self) -> Result<String> {
90+
self.cfg
91+
.upgrade_authority_keypair
92+
.clone()
93+
.ok_or_else(|| format_err!("upgrade_authority_keypair not found in Goki.toml"))
94+
}
95+
96+
/// New [CommandContext] using the specified cluster.
97+
pub fn new_cluster_context<'a, 'b>(
98+
&'a self,
99+
cluster: &'b Cluster,
100+
) -> Result<CommandContext<'a, 'b>> {
101+
Ok(CommandContext {
92102
workspace: self,
93103
cluster,
94-
keypair_path,
95-
}
104+
})
96105
}
97106
}
98107

99108
pub struct CommandContext<'a, 'b> {
100109
pub workspace: &'a Workspace,
101110
pub cluster: &'b Cluster,
102-
pub keypair_path: String,
103111
}
104112

105113
impl<'a, 'b> CommandContext<'a, 'b> {
106-
fn add_cluster_args(&self, command: &mut Command) -> Result<()> {
114+
fn add_cluster_args(&self, command: &mut Command, wallet: &str) -> Result<()> {
107115
command
108116
.args([
109117
"--url",
110118
self.workspace.get_cluster_url(self.cluster)?,
111119
"--keypair",
112120
])
113-
.arg(&self.keypair_path);
121+
.arg(wallet);
114122
Ok(())
115123
}
116124

117125
/// Executes a command.
118-
pub fn exec_command<F>(&self, mut builder: F) -> Result<Output>
126+
pub fn exec_command<F>(&self, mut builder: F, wallet: &str) -> Result<Output>
119127
where
120128
F: FnMut(&mut Command) -> Result<()>,
121129
{
122130
let cmd = &mut new_solana_cmd();
123-
self.add_cluster_args(cmd)?;
131+
self.add_cluster_args(cmd, wallet)?;
124132
builder(cmd)?;
125133
exec_command(cmd)
126134
}
127135

128136
/// Executes a command.
129-
pub fn exec_args<S>(&self, args: &[S]) -> Result<Output>
137+
pub fn exec_args<S>(&self, args: &[S], wallet: &str) -> Result<Output>
130138
where
131139
S: AsRef<OsStr>,
132140
{
133141
let cmd = &mut new_solana_cmd();
134-
self.add_cluster_args(cmd)?;
142+
self.add_cluster_args(cmd, wallet)?;
135143
args.iter().for_each(|arg| {
136144
cmd.arg(arg.as_ref());
137145
});
@@ -141,4 +149,13 @@ impl<'a, 'b> CommandContext<'a, 'b> {
141149
pub fn get_deployer_kp_path(&self) -> PathBuf {
142150
self.workspace.get_deployer_kp_path(self.cluster)
143151
}
152+
153+
pub fn parse_wallet_alias(&self, alias: &str) -> Result<String> {
154+
let result = match alias {
155+
"deployer" => Ok(self.get_deployer_kp_path().display().to_string()),
156+
"upgrader" => self.workspace.get_upgrader_wallet(),
157+
_ => Ok(alias.to_string()),
158+
};
159+
result.map_err(|err| format_err!("could not parse alias {}: {}", alias, err))
160+
}
144161
}

0 commit comments

Comments
 (0)