Skip to content

Commit

Permalink
[Wallet CLI] Add command to configure gateway URL (MystenLabs#1706)
Browse files Browse the repository at this point in the history
  • Loading branch information
666lcz authored May 2, 2022
1 parent 63c4674 commit c644561
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 28 deletions.
52 changes: 35 additions & 17 deletions sui/src/unit_tests/cli_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,11 @@ async fn test_create_example_nft_command() -> Result<(), anyhow::Error> {
let (network, mut context, address) = setup_network_and_wallet().await?;

let result = WalletCommands::CreateExampleNFT {
name: Option::None,
description: Option::None,
url: Option::None,
gas: Option::None,
gas_budget: Option::None,
name: None,
description: None,
url: None,
gas: None,
gas_budget: None,
}
.execute(&mut context)
.await?;
Expand Down Expand Up @@ -1059,16 +1059,22 @@ async fn test_switch_command() -> Result<(), anyhow::Error> {

// Switch the address
let addr2 = context.config.accounts.get(1).cloned().unwrap();
let resp = WalletCommands::Switch { address: addr2 }
.execute(&mut context)
.await?;
let resp = WalletCommands::Switch {
address: Some(addr2),
gateway: None,
}
.execute(&mut context)
.await?;
assert_eq!(addr2, context.active_address()?);
assert_ne!(addr1, context.active_address()?);
assert_eq!(
format!("{resp}"),
format!(
"{}",
WalletCommandResult::Switch(SwitchResponse { address: addr2 })
WalletCommandResult::Switch(SwitchResponse {
address: Some(addr2),
gateway: None
})
)
);

Expand All @@ -1086,15 +1092,21 @@ async fn test_switch_command() -> Result<(), anyhow::Error> {

// Check that we can switch to this address
// Switch the address
let resp = WalletCommands::Switch { address: new_addr }
.execute(&mut context)
.await?;
let resp = WalletCommands::Switch {
address: Some(new_addr),
gateway: None,
}
.execute(&mut context)
.await?;
assert_eq!(new_addr, context.active_address()?);
assert_eq!(
format!("{resp}"),
format!(
"{}",
WalletCommandResult::Switch(SwitchResponse { address: new_addr })
WalletCommandResult::Switch(SwitchResponse {
address: Some(new_addr),
gateway: None
})
)
);
network.kill().await?;
Expand Down Expand Up @@ -1136,14 +1148,20 @@ async fn test_active_address_command() -> Result<(), anyhow::Error> {
assert_eq!(a, addr1);

let addr2 = context.config.accounts.get(1).cloned().unwrap();
let resp = WalletCommands::Switch { address: addr2 }
.execute(&mut context)
.await?;
let resp = WalletCommands::Switch {
address: Some(addr2),
gateway: None,
}
.execute(&mut context)
.await?;
assert_eq!(
format!("{resp}"),
format!(
"{}",
WalletCommandResult::Switch(SwitchResponse { address: addr2 })
WalletCommandResult::Switch(SwitchResponse {
address: Some(addr2),
gateway: None
})
)
);
network.kill().await?;
Expand Down
42 changes: 33 additions & 9 deletions sui/src/wallet_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use sui_types::object::{Object, ObjectRead};
use sui_types::SUI_FRAMEWORK_ADDRESS;

use crate::config::{Config, PersistedConfig, WalletConfig};
use crate::gateway_config::GatewayType;
use crate::keystore::Keystore;
use crate::sui_json::{resolve_move_function_args, SuiJsonCallArg, SuiJsonValue};

Expand All @@ -53,12 +54,17 @@ pub struct WalletOpts {
#[derive(StructOpt, Debug)]
#[clap(rename_all = "kebab-case", no_binary_name = true)]
pub enum WalletCommands {
/// Switch active address
/// Switch active address and network(e.g., devnet, local rpc server)
#[clap(name = "switch")]
Switch {
/// Address to switch wallet commands to
/// An Sui address to be used as the active address for subsequent
/// commands.
#[clap(long, parse(try_from_str = decode_bytes_hex))]
address: SuiAddress,
address: Option<SuiAddress>,
/// The gateway URL (e.g., local rpc server, devnet rpc server, etc) to be
/// used for subsequent commands.
#[clap(long, value_hint = ValueHint::Url)]
gateway: Option<String>,
},

/// Default address used for commands when none specified
Expand Down Expand Up @@ -462,13 +468,31 @@ impl WalletCommands {

WalletCommandResult::MergeCoin(response)
}
WalletCommands::Switch { address } => {
if !context.config.accounts.contains(address) {
return Err(anyhow!("Address {} not managed by wallet", address));
WalletCommands::Switch { address, gateway } => {
if let Some(addr) = address {
if !context.config.accounts.contains(addr) {
return Err(anyhow!("Address {} not managed by wallet", addr));
}
context.config.active_address = Some(*addr);
context.config.save()?;
}
context.config.active_address = Some(*address);
context.config.save()?;
WalletCommandResult::Switch(SwitchResponse { address: *address })

if let Some(gateway) = gateway {
// TODO: handle embedded gateway
context.config.gateway = GatewayType::RPC(gateway.clone());
context.config.save()?;
}

if Option::is_none(address) && Option::is_none(gateway) {
return Err(anyhow!(
"No address or gateway specified. Please Specify one."
));
}

WalletCommandResult::Switch(SwitchResponse {
address: *address,
gateway: gateway.clone(),
})
}
WalletCommands::ActiveAddress {} => {
WalletCommandResult::ActiveAddress(context.active_address().ok())
Expand Down
10 changes: 8 additions & 2 deletions sui_core/src/gateway_state/gateway_responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,19 @@ impl Display for PublishResponse {
#[derive(Serialize, Clone, Debug)]
pub struct SwitchResponse {
/// Active address
pub address: SuiAddress,
pub address: Option<SuiAddress>,
pub gateway: Option<String>,
}

impl Display for SwitchResponse {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let mut writer = String::new();
writeln!(writer, "Active address switched to {}", self.address)?;
if let Some(addr) = self.address {
writeln!(writer, "Active address switched to {}", addr)?;
}
if let Some(gateway) = &self.gateway {
writeln!(writer, "Active gateway switched to {}", gateway)?;
}
write!(f, "{}", writer)
}
}

0 comments on commit c644561

Please sign in to comment.