Skip to content

Commit

Permalink
fix(CLI): don't overwrite accounts (#121)
Browse files Browse the repository at this point in the history
Need to make sure that `contract.[contract-name].init` scripts that might do things like set an admin continue to work, without the admin account being accidentally overridden.
  • Loading branch information
BlaineHeffron authored Jul 19, 2024
1 parent b92f855 commit d1bf183
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
23 changes: 19 additions & 4 deletions crates/loam-cli/src/commands/build/clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,14 @@ export default new Client.Client({{
Ok(())
}

async fn account_exists(account_name: &str) -> Result<bool, Error> {
// TODO: this is a workaround until generate is changed to not overwrite accounts
Ok(cli::keys::fund::Cmd::parse_arg_vec(&[account_name])?
.run()
.await
.is_ok())
}

async fn handle_accounts(accounts: Option<&[env_toml::Account]>) -> Result<(), Error> {
let Some(accounts) = accounts else {
return Err(Error::NeedAtLeastOneAccount);
Expand All @@ -244,10 +252,17 @@ export default new Client.Client({{
};

for account in accounts {
eprintln!("🔐 creating keys for {:?}", account.name);
cli::keys::generate::Cmd::parse_arg_vec(&[&account.name])?
.run()
.await?;
if Self::account_exists(&account.name).await? {
eprintln!(
"ℹ️ account {:?} already exists, skipping key creation",
account.name
);
} else {
eprintln!("🔐 creating keys for {:?}", account.name);
cli::keys::generate::Cmd::parse_arg_vec(&[&account.name])?
.run()
.await?;
}
}

std::env::set_var("STELLAR_ACCOUNT", &default_account);
Expand Down
5 changes: 5 additions & 0 deletions crates/loam-cli/tests/it/build_clients/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ soroban_token_contract.client = false
assert!(env.cwd.join(".soroban/identity/alice.toml").exists());
assert!(env.cwd.join(".soroban/identity/bob.toml").exists());

// check that they dont get overwritten if build is run again
let stderr = env.loam("build").assert().success().stderr_as_str();
assert!(stderr.contains("account \"alice\" already exists"));
assert!(stderr.contains("account \"bob\" already exists"));

// check that they're actually funded
let stderr = env
.soroban("keys")
Expand Down

0 comments on commit d1bf183

Please sign in to comment.