Skip to content

Commit

Permalink
[cli] Change default to devnet
Browse files Browse the repository at this point in the history
  • Loading branch information
gregnazario committed Oct 17, 2022
1 parent 32ffb08 commit 3d9ae01
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 57 deletions.
99 changes: 56 additions & 43 deletions crates/aptos/src/common/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::str::FromStr;

pub const DEFAULT_REST_URL: &str = "https://fullnode.devnet.aptoslabs.com";
pub const DEFAULT_FAUCET_URL: &str = "https://faucet.devnet.aptoslabs.com";
const NUM_DEFAULT_COINS: u64 = 10000;
/// 1 APT (might not actually get that much, depending on the faucet)
const NUM_DEFAULT_OCTAS: u64 = 100000000;

/// Tool to initialize current directory for the aptos tool
///
Expand Down Expand Up @@ -88,25 +87,28 @@ impl CliCommand<()> for InitTool {
eprintln!("Configuring for profile {}", profile_name);

// Choose a network
// TODO: Change custom to a specific network int he future
eprintln!("Choose network from [testnet, devnet, local, custom | defaults to custom]");
eprintln!(
"Choose network from [devnet, testnet, mainnet, local, custom | defaults to devnet]"
);
let input = read_line("network")?;
let input = input.trim();
let network = if input.is_empty() {
eprintln!("No network given, using devnet...");
Network::Custom
Network::Devnet
} else {
Network::from_str(input)?
};

let mut is_community_faucet = false;

match network {
Network::Mainnet => {
profile_config.rest_url =
Some("https://fullnode.mainnet.aptoslabs.com".to_string());
profile_config.faucet_url = None;
}
Network::Testnet => {
profile_config.rest_url =
Some("https://fullnode.testnet.aptoslabs.com".to_string());
profile_config.faucet_url = None;
is_community_faucet = true;
}
Network::Devnet => {
profile_config.rest_url = Some("https://fullnode.devnet.aptoslabs.com".to_string());
Expand Down Expand Up @@ -193,12 +195,12 @@ impl CliCommand<()> for InitTool {
} else {
eprintln!(
"Account {} doesn't exist, creating it and funding it with {} Octas",
address, NUM_DEFAULT_COINS
address, NUM_DEFAULT_OCTAS
);
match fund_account(
Url::parse(faucet_url)
.map_err(|err| CliError::UnableToParse("rest_url", err.to_string()))?,
NUM_DEFAULT_COINS,
NUM_DEFAULT_OCTAS,
address,
)
.await
Expand All @@ -209,10 +211,12 @@ impl CliCommand<()> for InitTool {
}
} else if account_exists {
eprintln!("Account {} has been already found onchain", address);
} else if is_community_faucet {
eprintln!("Account {} does not exist, you may need to fund the account through a community faucet e.g. https://aptoslabs.com/testnet-faucet", address);
} else if network == Network::Testnet {
eprintln!("Account {} does not exist, you will need to create and fun the account through a community faucet e.g. https://aptoslabs.com/testnet-faucet, or by transferring funds from another account", address);
} else if network == Network::Mainnet {
eprintln!("Account {} does not exist, you will need to create and fun the account through a faucet or by transferring funds from another account", address);
} else {
eprintln!("Account {} has been initialized locally, but must have coins transferred to it to create the account onchain", address);
eprintln!("Account {} has been initialized locally, but you must have coins transferred to it to create the account onchain", address);
}

// Ensure the loaded config has profiles setup for a possible empty file
Expand All @@ -235,70 +239,79 @@ impl InitTool {
// Rest Endpoint
let rest_url = if let Some(ref rest_url) = self.rest_url {
eprintln!("Using command line argument for rest URL {}", rest_url);
rest_url.clone()
Some(rest_url.to_string())
} else {
let current = profile_config.rest_url.as_deref();
eprintln!(
"Enter your rest endpoint [Current: {} | No input: {}]",
profile_config.rest_url.as_deref().unwrap_or("None"),
DEFAULT_REST_URL
"Enter your rest endpoint [Current: {} | No input: Exit (or keep the existing if present)]",
current.unwrap_or("None"),
);
let input = read_line("Rest endpoint")?;
let input = input.trim();
if input.is_empty() {
eprintln!("No rest url given, using {}...", DEFAULT_REST_URL);
reqwest::Url::parse(DEFAULT_REST_URL).map_err(|err| {
CliError::UnexpectedError(format!("Failed to parse default rest URL {}", err))
})?
if let Some(current) = current {
eprintln!("No rest url given, keeping the existing url...");
Some(current.to_string())
} else {
eprintln!("No rest url given, exiting...");
return Err(CliError::AbortedError);
}
} else {
reqwest::Url::parse(input)
.map_err(|err| CliError::UnableToParse("Rest Endpoint", err.to_string()))?
Some(
reqwest::Url::parse(input)
.map_err(|err| CliError::UnableToParse("Rest Endpoint", err.to_string()))?
.to_string(),
)
}
};
profile_config.rest_url = Some(rest_url.to_string());
profile_config.rest_url = rest_url;

// Faucet Endpoint
let faucet_url = if self.skip_faucet {
eprintln!("Not configuring a faucet because --skip-faucet was provided");
None
} else if let Some(ref faucet_url) = self.faucet_url {
eprintln!("Using command line argument for faucet URL {}", faucet_url);
Some(faucet_url.clone())
Some(faucet_url.to_string())
} else {
let current = profile_config.faucet_url.as_deref();
eprintln!(
"Enter your faucet endpoint [Current: {} | No input: {} | 'skip' to not use a faucet]",
profile_config
.faucet_url.as_deref()
"Enter your faucet endpoint [Current: {} | No input: Skip (or keep the existing one if present) | 'skip' to not use a faucet]",
current
.unwrap_or("None"),
DEFAULT_FAUCET_URL
);
let input = read_line("Faucet endpoint")?;
let input = input.trim();
if input.is_empty() {
eprintln!("No faucet url given, using {}...", DEFAULT_FAUCET_URL);
Some(reqwest::Url::parse(DEFAULT_FAUCET_URL).map_err(|err| {
CliError::UnexpectedError(format!("Failed to parse default faucet URL {}", err))
})?)
if let Some(current) = current {
eprintln!("No faucet url given, keeping the existing url...");
Some(current.to_string())
} else {
eprintln!("No faucet url given, skipping faucet...");
None
}
} else if input.to_lowercase() == "skip" {
eprintln!("Skipping faucet");
eprintln!("Skipping faucet...");
None
} else {
Some(
reqwest::Url::parse(input).map_err(|err| {
CliError::UnableToParse("Faucet Endpoint", err.to_string())
})?,
reqwest::Url::parse(input)
.map_err(|err| CliError::UnableToParse("Faucet Endpoint", err.to_string()))?
.to_string(),
)
}
};
profile_config.faucet_url = faucet_url.as_ref().map(|inner| inner.to_string());
profile_config.faucet_url = faucet_url;
Ok(())
}
}

/// A simplified list of all networks supported by the CLI
///
/// Any command using this, will be simpler to setup as profiles
#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
pub enum Network {
Mainnet,
Testnet,
Devnet,
Local,
Expand All @@ -310,13 +323,14 @@ impl FromStr for Network {

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s.to_lowercase().trim() {
"mainnet" => Self::Mainnet,
"testnet" => Self::Testnet,
"devnet" => Self::Devnet,
"local" => Self::Local,
"custom" => Self::Custom,
str => {
return Err(CliError::CommandArgumentError(format!(
"Invalid network {}. Must be one of [testnet, devnet, local, custom]",
"Invalid network {}. Must be one of [devnet, testnet, mainnet, local, custom]",
str
)))
}
Expand All @@ -326,7 +340,6 @@ impl FromStr for Network {

impl Default for Network {
fn default() -> Self {
// This unfortunately has to be custom if people play with their configs
Self::Custom
Self::Devnet
}
}
20 changes: 6 additions & 14 deletions crates/aptos/src/common/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@
use crate::common::init::Network;
use crate::common::utils::prompt_yes_with_override;
use crate::{
common::{
init::{DEFAULT_FAUCET_URL, DEFAULT_REST_URL},
utils::{
chain_id, check_if_file_exists, create_dir_if_not_exist, dir_default_to_current,
get_auth_key, get_sequence_number, read_from_file, start_logger, to_common_result,
to_common_success_result, write_to_file, write_to_file_with_opts,
write_to_user_only_file,
},
common::utils::{
chain_id, check_if_file_exists, create_dir_if_not_exist, dir_default_to_current,
get_auth_key, get_sequence_number, read_from_file, start_logger, to_common_result,
to_common_success_result, write_to_file, write_to_file_with_opts, write_to_user_only_file,
},
config::GlobalConfig,
genesis::git::from_yaml,
Expand Down Expand Up @@ -886,9 +882,7 @@ impl RestOptions {
reqwest::Url::parse(&url)
.map_err(|err| CliError::UnableToParse("Rest URL", err.to_string()))
} else {
reqwest::Url::parse(DEFAULT_REST_URL).map_err(|err| {
CliError::UnexpectedError(format!("Failed to parse default rest URL {}", err))
})
Err(CliError::CommandArgumentError("No rest url given. Please add --url or add a rest_url to the .aptos/config.yaml for the current profile".to_string()))
}
}

Expand Down Expand Up @@ -1194,9 +1188,7 @@ impl FaucetOptions {
reqwest::Url::parse(&url)
.map_err(|err| CliError::UnableToParse("config faucet_url", err.to_string()))
} else {
reqwest::Url::parse(DEFAULT_FAUCET_URL).map_err(|err| {
CliError::UnexpectedError(format!("Failed to parse default faucet URL {}", err))
})
Err(CliError::CommandArgumentError("No faucet given. Please add --faucet-url or add a faucet URL to the .aptos/config.yaml for the current profile".to_string()))
}
}
}
Expand Down

0 comments on commit 3d9ae01

Please sign in to comment.