Skip to content

Commit ccbe0f1

Browse files
committed
only fund the account if the account not found error comes up
1 parent 566a185 commit ccbe0f1

1 file changed

Lines changed: 64 additions & 40 deletions

File tree

  • crates/stellar-scaffold-cli/src/commands/build

crates/stellar-scaffold-cli/src/commands/build/clients.rs

Lines changed: 64 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ pub enum Error {
6464
ScriptParseFailure(String),
6565
#[error("⛔ ️Failed to execute subcommand: {0:?}\n{1:?}")]
6666
SubCommandExecutionFailure(String, String),
67+
#[error("⛔ ️STELLAR_ACCOUNT environment variable not set")]
68+
StellarAccountNotSet,
6769
#[error(transparent)]
6870
ContractInstall(#[from] cli::contract::upload::Error),
6971
#[error(transparent)]
@@ -112,7 +114,7 @@ impl Args {
112114
std::fs::create_dir_all(workspace_root.join(".stellar"))
113115
.map_err(stellar_cli::config::locator::Error::Io)?;
114116
self.clone()
115-
.handle_accounts(current_env.accounts.as_deref(), &current_env.network)
117+
.handle_accounts(current_env.accounts.as_deref())
116118
.await?;
117119
self.clone()
118120
.handle_contracts(
@@ -345,7 +347,6 @@ export default new Client.Client({{
345347
async fn handle_accounts(
346348
self,
347349
accounts: Option<&[env_toml::Account]>,
348-
network: &Network,
349350
) -> Result<(), Error> {
350351
let Some(accounts) = accounts else {
351352
return Err(Error::NeedAtLeastOneAccount);
@@ -371,32 +372,14 @@ export default new Client.Client({{
371372
..Default::default()
372373
};
373374

374-
match cli::keys::generate::Cmd::parse_arg_vec(&[&account.name, "--fund"])?
375+
match cli::keys::generate::Cmd::parse_arg_vec(&[&account.name])?
375376
.run(&args)
376377
.await
377378
{
378379
Ok(()) => {}
379380
Err(e) if e.to_string().contains("already exists") => {
380381
eprintln!("{e}");
381-
382-
// Only re-fund in testing/development & non-mainnet
383-
let is_dev_env = matches!(
384-
self.clone()
385-
.stellar_scaffold_env(ScaffoldEnv::Production)
386-
.as_str(),
387-
"testing" | "development"
388-
);
389-
let is_not_mainnet = network
390-
.clone()
391-
.network_passphrase
392-
.expect("network must contain network passphrase")
393-
!= "Public Global Stellar Network ; September 2015";
394-
if is_dev_env && is_not_mainnet {
395-
eprintln!("💸 re-funding {:?}", account.name);
396-
cli::keys::fund::Cmd::parse_arg_vec(&[&account.name])?
397-
.run(&args)
398-
.await?;
399-
}
382+
// Just log that the identity already exists, funding will happen on-demand
400383
}
401384
Err(e) => return Err(e.into()),
402385
}
@@ -555,21 +538,20 @@ export default new Client.Client({{
555538

556539
// Deploy new contract if we got here
557540
let contract_id = self.deploy_contract(name, &hash, &settings).await?;
541+
// Run after_deploy script if in development or test environment
542+
if (env == "development" || env == "testing") && settings.after_deploy.is_some() {
543+
eprintln!("🚀 Running after_deploy script for {name:?}");
544+
self.run_after_deploy_script(
545+
name,
546+
&contract_id,
547+
settings.after_deploy.as_ref().unwrap(),
548+
)
549+
.await?;
550+
}
558551
self.save_contract_alias(name, &contract_id, network)?;
559552
contract_id
560553
};
561554

562-
// Run after_deploy script if in development or test environment
563-
if (env == "development" || env == "testing") && settings.after_deploy.is_some() {
564-
eprintln!("🚀 Running after_deploy script for {name:?}");
565-
self.run_after_deploy_script(
566-
name,
567-
&contract_id,
568-
settings.after_deploy.as_ref().unwrap(),
569-
)
570-
.await?;
571-
}
572-
573555
self.clone()
574556
.generate_contract_bindings(name, &contract_id.to_string())
575557
.await?;
@@ -583,19 +565,61 @@ export default new Client.Client({{
583565
wasm_path: &std::path::Path,
584566
) -> Result<String, Error> {
585567
eprintln!("📲 installing {name:?} wasm bytecode on-chain...");
586-
let hash = cli::contract::upload::Cmd::parse_arg_vec(&[
568+
569+
let upload_result = cli::contract::upload::Cmd::parse_arg_vec(&[
587570
"--wasm",
588571
wasm_path
589572
.to_str()
590573
.expect("we do not support non-utf8 paths"),
591574
])?
592575
.run_against_rpc_server(None, None)
593-
.await?
594-
.into_result()
595-
.expect("no hash returned by 'contract upload'")
596-
.to_string();
597-
eprintln!(" ↳ hash: {hash}");
598-
Ok(hash)
576+
.await;
577+
578+
match upload_result {
579+
Ok(hash) => {
580+
let hash_str = hash
581+
.into_result()
582+
.expect("no hash returned by 'contract upload'")
583+
.to_string();
584+
eprintln!(" ↳ hash: {hash_str}");
585+
Ok(hash_str)
586+
}
587+
Err(e) if e.to_string().contains("Account not found") => {
588+
eprintln!("Account not found, attempting to fund...");
589+
590+
// Get the default account name from environment
591+
let account_name = std::env::var("STELLAR_ACCOUNT")
592+
.map_err(|_| Error::StellarAccountNotSet)?;
593+
594+
// Fund the account
595+
let args = stellar_cli::commands::global::Args {
596+
locator: self.clone().get_config_locator(),
597+
..Default::default()
598+
};
599+
600+
eprintln!("💸 funding {account_name:?}");
601+
cli::keys::fund::Cmd::parse_arg_vec(&[&account_name])?
602+
.run(&args)
603+
.await?;
604+
605+
// Retry the upload
606+
eprintln!("🔄 retrying wasm upload...");
607+
let hash = cli::contract::upload::Cmd::parse_arg_vec(&[
608+
"--wasm",
609+
wasm_path
610+
.to_str()
611+
.expect("we do not support non-utf8 paths"),
612+
])?
613+
.run_against_rpc_server(None, None)
614+
.await?
615+
.into_result()
616+
.expect("no hash returned by 'contract upload'")
617+
.to_string();
618+
eprintln!(" ↳ hash: {hash}");
619+
Ok(hash)
620+
}
621+
Err(e) => Err(e.into()),
622+
}
599623
}
600624

601625
fn parse_script_line(line: &str) -> Result<(Option<String>, Vec<String>), Error> {

0 commit comments

Comments
 (0)