Skip to content

Commit

Permalink
[CLI] Add support for setting faucet auth token
Browse files Browse the repository at this point in the history
  • Loading branch information
banool committed Aug 22, 2023
1 parent c84d1e2 commit 01fa5a0
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 24 deletions.
2 changes: 1 addition & 1 deletion crates/aptos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async-trait = { workspace = true }
base64 = { workspace = true }
bcs = { workspace = true }
chrono = { workspace = true }
clap = { workspace = true, features = ["unstable-styles"] }
clap = { workspace = true, features = ["env", "unstable-styles"] }
clap_complete = { workspace = true }
codespan-reporting = { workspace = true }
dirs = { workspace = true }
Expand Down
12 changes: 5 additions & 7 deletions crates/aptos/src/account/fund.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
account::create::DEFAULT_FUNDED_COINS,
common::{
types::{CliCommand, CliTypedResult, FaucetOptions, ProfileOptions, RestOptions},
utils::{fund_account, wait_for_transactions},
utils::wait_for_transactions,
},
};
use aptos_types::account_address::AccountAddress;
Expand Down Expand Up @@ -51,12 +51,10 @@ impl CliCommand<String> for FundWithFaucet {
} else {
self.profile_options.account_address()?
};
let hashes = fund_account(
self.faucet_options.faucet_url(&self.profile_options)?,
self.amount,
address,
)
.await?;
let hashes = self
.faucet_options
.fund_account(&self.profile_options, self.amount, &address)
.await?;
let client = self.rest_options.client(&self.profile_options)?;
wait_for_transactions(&client, hashes).await?;
return Ok(format!(
Expand Down
10 changes: 8 additions & 2 deletions crates/aptos/src/common/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ pub struct InitTool {
#[clap(long)]
pub faucet_url: Option<Url>,

/// Auth token, if we're using the faucet. This is only used this time, we don't
/// store it.
#[clap(long, env)]
pub faucet_auth_token: Option<String>,

/// Whether to skip the faucet for a non-faucet endpoint
#[clap(long)]
pub skip_faucet: bool,
Expand Down Expand Up @@ -226,10 +231,11 @@ impl CliCommand<()> for InitTool {
address, NUM_DEFAULT_OCTAS
);
let hashes = fund_account(
Url::parse(faucet_url)
&Url::parse(faucet_url)
.map_err(|err| CliError::UnableToParse("rest_url", err.to_string()))?,
self.faucet_auth_token.as_deref(),
NUM_DEFAULT_OCTAS,
address,
&address,
)
.await?;
wait_for_transactions(&client, hashes).await?;
Expand Down
31 changes: 28 additions & 3 deletions crates/aptos/src/common/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0

use super::utils::fund_account;
use crate::{
common::{
init::Network,
Expand Down Expand Up @@ -1268,14 +1269,22 @@ pub struct FaucetOptions {
/// URL for the faucet endpoint e.g. `https://faucet.devnet.aptoslabs.com`
#[clap(long)]
faucet_url: Option<reqwest::Url>,

/// Auth token to bypass faucet ratelimits. You can also set this as an environment
/// variable with FAUCET_AUTH_TOKEN.
#[clap(long, env)]
faucet_auth_token: Option<String>,
}

impl FaucetOptions {
pub fn new(faucet_url: Option<reqwest::Url>) -> Self {
FaucetOptions { faucet_url }
pub fn new(faucet_url: Option<reqwest::Url>, faucet_auth_token: Option<String>) -> Self {
FaucetOptions {
faucet_url,
faucet_auth_token,
}
}

pub fn faucet_url(&self, profile: &ProfileOptions) -> CliTypedResult<reqwest::Url> {
fn faucet_url(&self, profile: &ProfileOptions) -> CliTypedResult<reqwest::Url> {
if let Some(ref faucet_url) = self.faucet_url {
Ok(faucet_url.clone())
} else if let Some(Some(url)) = CliConfig::load_profile(
Expand All @@ -1290,6 +1299,22 @@ impl FaucetOptions {
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()))
}
}

/// Fund an account with the faucet.
pub async fn fund_account(
&self,
profile: &ProfileOptions,
num_octas: u64,
address: &AccountAddress,
) -> CliTypedResult<Vec<HashValue>> {
fund_account(
&self.faucet_url(profile)?,
self.faucet_auth_token.as_deref(),
num_octas,
address,
)
.await
}
}

/// Gas price options for manipulating how to prioritize transactions
Expand Down
23 changes: 13 additions & 10 deletions crates/aptos/src/common/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,23 +416,26 @@ pub fn read_line(input_name: &'static str) -> CliTypedResult<String> {
Ok(input_buf)
}

/// Fund account (and possibly create it) from a faucet
/// Fund account (and possibly create it) from a faucet. This returns txn hashes that
/// you must wait for before the account is funded.
pub async fn fund_account(
faucet_url: Url,
faucet_url: &Url,
faucet_auth_token: Option<&str>,
num_octas: u64,
address: AccountAddress,
address: &AccountAddress,
) -> CliTypedResult<Vec<HashValue>> {
let response = reqwest::Client::new()
let mut builder = reqwest::Client::new()
.post(format!(
"{}mint?amount={}&auth_key={}",
faucet_url, num_octas, address
))
.body("{}")
.send()
.await
.map_err(|err| {
CliError::ApiError(format!("Failed to fund account with faucet: {:#}", err))
})?;
.body("{}");
if let Some(ref faucet_auth_token) = faucet_auth_token {
builder = builder.header("Authorization", format!("Bearer {}", faucet_auth_token))
}
let response = builder.send().await.map_err(|err| {
CliError::ApiError(format!("Failed to fund account with faucet: {:#}", err))
})?;
if response.status() == 200 {
let hashes: Vec<HashValue> = response
.json()
Expand Down
3 changes: 2 additions & 1 deletion crates/aptos/src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ impl CliTestFramework {
network: Some(Network::Custom),
rest_url: Some(self.endpoint.clone()),
faucet_url: Some(self.faucet_endpoint.clone()),
faucet_auth_token: None,
rng_args: RngArgs::from_seed([0; 32]),
private_key_options: PrivateKeyInputOptions::from_private_key(private_key)?,
profile_options: Default::default(),
Expand Down Expand Up @@ -1079,7 +1080,7 @@ impl CliTestFramework {
}

pub fn faucet_options(&self) -> FaucetOptions {
FaucetOptions::new(Some(self.faucet_endpoint.clone()))
FaucetOptions::new(Some(self.faucet_endpoint.clone()), None)
}

fn transaction_options(
Expand Down

0 comments on commit 01fa5a0

Please sign in to comment.