Skip to content

Commit

Permalink
refactor: Exposed sponsor_by_faucet_service module to re-use in "carg…
Browse files Browse the repository at this point in the history
…o-near" (#246)
  • Loading branch information
FroVolod authored Oct 9, 2023
1 parent c2c6f1a commit 76a7588
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 67 deletions.
2 changes: 1 addition & 1 deletion src/commands/account/create_account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use strum::{EnumDiscriminants, EnumIter, EnumMessage};

mod create_implicit_account;
mod fund_myself_create_account;
mod sponsor_by_faucet_service;
pub mod sponsor_by_faucet_service;

#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
#[interactive_clap(context = crate::GlobalContext)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ impl GenerateKeypairContext {
/// Save an access key for this account:
pub enum SaveMode {
#[strum_discriminants(strum(
message = "save-to-keychain - Save automatically generated key pair to keychain"
message = "save-to-keychain - Save automatically generated key pair to keychain"
))]
/// Save automatically generated key pair to keychain
SaveToKeychain(SaveKeyPair),
#[strum_discriminants(strum(
message = "save-to-legacy-keychain - Save automatically generated key pair to the legacy keychain (compatible with JS CLI)"
message = "save-to-legacy-keychain - Save automatically generated key pair to the legacy keychain (compatible with JS CLI)"
))]
/// Save automatically generated key pair to the legacy keychain (compatible with JS CLI)
SaveToLegacyKeychain(SaveKeyPair),
Expand Down
137 changes: 73 additions & 64 deletions src/commands/account/create_account/sponsor_by_faucet_service/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod add_key;
mod network;
pub mod add_key;
pub mod network;

#[derive(Clone)]
pub struct SponsorServiceContext {
Expand All @@ -23,9 +23,9 @@ pub struct NewAccount {

#[derive(Clone)]
pub struct NewAccountContext {
config: crate::config::Config,
new_account_id: crate::types::account_id::AccountId,
on_before_creating_account_callback: self::network::OnBeforeCreatingAccountCallback,
pub config: crate::config::Config,
pub new_account_id: crate::types::account_id::AccountId,
pub on_before_creating_account_callback: self::network::OnBeforeCreatingAccountCallback,
}

impl NewAccountContext {
Expand All @@ -37,65 +37,12 @@ impl NewAccountContext {
let on_before_creating_account_callback: self::network::OnBeforeCreatingAccountCallback =
std::sync::Arc::new({
move |network_config, new_account_id, public_key| {
let faucet_service_url = match &network_config.faucet_url {
Some(url) => url,
None => return Err(color_eyre::Report::msg(format!(
"The <{}> network does not have a faucet (helper service) that can sponsor the creation of an account.",
&network_config.network_name
)))
};
let mut data = std::collections::HashMap::new();
data.insert("newAccountId", new_account_id.to_string());
data.insert("newAccountPublicKey", public_key.to_string());

let client = reqwest::blocking::Client::new();
match client.post(faucet_service_url.clone()).json(&data).send() {
Ok(response) => {
if response.status() >= reqwest::StatusCode::BAD_REQUEST {
return Err(color_eyre::Report::msg(format!(
"The faucet (helper service) server failed with status code <{}>",
response.status()
)));
}

let account_creation_transaction = response
.json::<near_jsonrpc_client::methods::tx::RpcTransactionStatusResponse>(
)?;
match account_creation_transaction.status {
near_primitives::views::FinalExecutionStatus::SuccessValue(
ref value,
) => {
if value == b"false" {
eprintln!(
"The new account <{}> could not be created successfully.",
&new_account_id
);
} else {
crate::common::update_used_account_list_as_signer(
&credentials_home_dir,
new_account_id.as_ref(),
);
eprintln!(
"New account <{}> created successfully.",
&new_account_id
);
}
eprintln!("Transaction ID: {id}\nTo see the transaction in the transaction explorer, please open this url in your browser:\n{path}{id}\n",
id=account_creation_transaction.transaction_outcome.id,
path=network_config.explorer_transaction_url
);
}
_ => {
crate::common::print_transaction_status(
&account_creation_transaction,
network_config,
)?;
}
}
Ok(())
}
Err(err) => Err(color_eyre::Report::msg(err.to_string())),
}
before_creating_account(
network_config,
new_account_id,
public_key,
&credentials_home_dir,
)
}
});

Expand All @@ -114,3 +61,65 @@ impl NewAccount {
super::fund_myself_create_account::NewAccount::input_new_account_id(context)
}
}

pub fn before_creating_account(
network_config: &crate::config::NetworkConfig,
new_account_id: &crate::types::account_id::AccountId,
public_key: &near_crypto::PublicKey,
credentials_home_dir: &std::path::Path,
) -> crate::CliResult {
let faucet_service_url = match &network_config.faucet_url {
Some(url) => url,
None => return Err(color_eyre::Report::msg(format!(
"The <{}> network does not have a faucet (helper service) that can sponsor the creation of an account.",
&network_config.network_name
)))
};
let mut data = std::collections::HashMap::new();
data.insert("newAccountId", new_account_id.to_string());
data.insert("newAccountPublicKey", public_key.to_string());

let client = reqwest::blocking::Client::new();
match client.post(faucet_service_url.clone()).json(&data).send() {
Ok(response) => {
if response.status() >= reqwest::StatusCode::BAD_REQUEST {
return Err(color_eyre::Report::msg(format!(
"The faucet (helper service) server failed with status code <{}>",
response.status()
)));
}

let account_creation_transaction =
response
.json::<near_jsonrpc_client::methods::tx::RpcTransactionStatusResponse>()?;
match account_creation_transaction.status {
near_primitives::views::FinalExecutionStatus::SuccessValue(ref value) => {
if value == b"false" {
eprintln!(
"The new account <{}> could not be created successfully.",
&new_account_id
);
} else {
crate::common::update_used_account_list_as_signer(
credentials_home_dir,
new_account_id.as_ref(),
);
eprintln!("New account <{}> created successfully.", &new_account_id);
}
eprintln!("Transaction ID: {id}\nTo see the transaction in the transaction explorer, please open this url in your browser:\n{path}{id}\n",
id=account_creation_transaction.transaction_outcome.id,
path=network_config.explorer_transaction_url
);
}
_ => {
crate::common::print_transaction_status(
&account_creation_transaction,
network_config,
)?;
}
}
Ok(())
}
Err(err) => Err(color_eyre::Report::msg(err.to_string())),
}
}

0 comments on commit 76a7588

Please sign in to comment.