Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 42 additions & 12 deletions crates/stellar-scaffold-cli/src/commands/build/clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ pub enum Error {
Json(#[from] serde_json::Error),
#[error("⛔ ️Failed to run npm command in {0:?}: {1:?}")]
NpmCommandFailure(std::path::PathBuf, String),
#[error(transparent)]
AccountFund(#[from] cli::keys::fund::Error),
}

impl Args {
Expand All @@ -109,7 +111,9 @@ impl Args {
// Create the '.stellar' directory if it doesn't exist
std::fs::create_dir_all(workspace_root.join(".stellar"))
.map_err(stellar_cli::config::locator::Error::Io)?;
Self::handle_accounts(current_env.accounts.as_deref()).await?;
self.clone()
.handle_accounts(current_env.accounts.as_deref(), &current_env.network)
.await?;
self.clone()
.handle_contracts(
current_env.contracts.as_ref(),
Expand Down Expand Up @@ -338,7 +342,11 @@ export default new Client.Client({{
Ok(())
}

async fn handle_accounts(accounts: Option<&[env_toml::Account]>) -> Result<(), Error> {
async fn handle_accounts(
self,
Comment thread
BlaineHeffron marked this conversation as resolved.
Outdated
accounts: Option<&[env_toml::Account]>,
network: &Network,
) -> Result<(), Error> {
let Some(accounts) = accounts else {
return Err(Error::NeedAtLeastOneAccount);
};
Expand All @@ -358,18 +366,40 @@ export default new Client.Client({{

for account in accounts {
eprintln!("🔐 creating keys for {:?}", account.name);
cli::keys::generate::Cmd::parse_arg_vec(&[&account.name, "--fund"])?
.run(&stellar_cli::commands::global::Args::default())
let args = stellar_cli::commands::global::Args {
locator: self.clone().get_config_locator(),
..Default::default()
};

match cli::keys::generate::Cmd::parse_arg_vec(&[&account.name, "--fund"])?
.run(&args)
.await
.or_else(|e| {
if e.to_string().contains("already exists") {
// ignore "already exists" errors
eprintln!("{e}");
Ok(())
} else {
Err(e)
{
Ok(()) => {}
Comment thread
BlaineHeffron marked this conversation as resolved.
Outdated
Err(e) if e.to_string().contains("already exists") => {
Comment thread
BlaineHeffron marked this conversation as resolved.
eprintln!("{e}");

// Only re-fund in testing/development & non-mainnet
let is_dev_env = matches!(
self.clone()
.stellar_scaffold_env(ScaffoldEnv::Production)
.as_str(),
"testing" | "development"
);
let is_not_mainnet = network
.clone()
.network_passphrase
.expect("network must contain network passphrase")
!= "Public Global Stellar Network ; September 2015";
if is_dev_env && is_not_mainnet {
eprintln!("💸 re-funding {:?}", account.name);
cli::keys::fund::Cmd::parse_arg_vec(&[&account.name])?
.run(&args)
.await?;
}
})?;
}
Err(e) => return Err(e.into()),
Comment thread
BlaineHeffron marked this conversation as resolved.
Outdated
}
Comment thread
BlaineHeffron marked this conversation as resolved.
}

std::env::set_var("STELLAR_ACCOUNT", &default_account);
Expand Down
36 changes: 36 additions & 0 deletions crates/stellar-scaffold-cli/tests/it/build_clients/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,39 @@ soroban_token_contract.client = false
assert!(stderr.contains("Account AliasOrSecret(\"alice\") funded"));
});
}

#[test]
fn funding_existing_account_toml() {
use std::fs;

TestEnv::from("soroban-init-boilerplate", |env| {
env.set_environments_toml(r#"
[development]
network = { rpc-url = "http://localhost:8000/rpc", network-passphrase = "Standalone Network ; February 2017"}

accounts = [
"alice",
]
[development.contracts]
soroban_hello_world_contract.client = true
soroban_increment_contract.client = false
soroban_custom_types_contract.client = false
soroban_auth_contract.client = false
soroban_token_contract.client = false
"#);

// Create alice.toml manually, simulating a pre-existing identity
let alice_toml_path = env.cwd.join(".stellar/identity/alice.toml");
let parent = alice_toml_path.parent().unwrap();
fs::create_dir_all(parent).unwrap();
fs::write(&alice_toml_path, r#"
seed_phrase = "own social that glimpse hurry lion arrange spot vault clip leisure innocent borrow peanut invest scrub network enter enemy digital uncover ivory expire peace"
"#).unwrap();

// Run scaffold_build and assert success
env.scaffold_build("development", true)
.assert()
.success()
.stderr_as_str();
});
}