Skip to content

Commit

Permalink
fix(zk_toolbox): Show balance (#2254)
Browse files Browse the repository at this point in the history
## What ❔

Show balance of account if it's not enough for contract deployment

## Why ❔

<!-- Why are these changes done? What goal do they contribute to? What
are the principles behind them? -->
<!-- Example: PR templates ensure PR reviewers, observers, and future
iterators are in context about the evolution of repos. -->

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [ ] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [ ] Tests for the changes have been added / updated.
- [ ] Documentation comments have been added / updated.
- [ ] Code has been formatted via `zk fmt` and `zk lint`.
- [ ] Spellcheck has been run via `zk spellcheck`.

Signed-off-by: Danil <deniallugo@gmail.com>
  • Loading branch information
Deniallugo committed Jun 17, 2024
1 parent 63be1f3 commit f1d9f03
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
8 changes: 4 additions & 4 deletions zk_toolbox/crates/common/src/forge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,16 @@ impl ForgeScript {
})
}

pub async fn check_the_balance(&self, minimum_value: U256) -> anyhow::Result<bool> {
pub async fn get_the_balance(&self) -> anyhow::Result<Option<U256>> {
let Some(rpc_url) = self.rpc_url() else {
return Ok(true);
return Ok(None);
};
let Some(private_key) = self.private_key() else {
return Ok(true);
return Ok(None);
};
let client = create_ethers_client(private_key, rpc_url, None)?;
let balance = client.get_balance(client.address(), None).await?;
Ok(balance > minimum_value)
Ok(Some(balance))
}
}

Expand Down
16 changes: 11 additions & 5 deletions zk_toolbox/crates/zk_inception/src/forge_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@ pub async fn check_the_balance(forge: &ForgeScript) -> anyhow::Result<()> {
return Ok(());
};

while !forge
.check_the_balance(U256::from(MINIMUM_BALANCE_FOR_WALLET))
.await?
{
if !common::PromptConfirm::new(msg_address_doesnt_have_enough_money_prompt(&address)).ask()
let expected_balance = U256::from(MINIMUM_BALANCE_FOR_WALLET);
while let Some(balance) = forge.get_the_balance().await? {
if balance >= expected_balance {
return Ok(());
}
if !common::PromptConfirm::new(msg_address_doesnt_have_enough_money_prompt(
&address,
balance,
expected_balance,
))
.ask()
{
break;
}
Expand Down
19 changes: 16 additions & 3 deletions zk_toolbox/crates/zk_inception/src/messages.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use ethers::types::H160;
use ethers::{
types::{H160, U256},
utils::format_ether,
};

/// Common messages
pub(super) const MSG_SELECTED_CONFIG: &str = "Selected config";
Expand Down Expand Up @@ -129,12 +132,15 @@ pub(super) const MSG_FAILED_TO_DROP_PROVER_DATABASE_ERR: &str = "Failed to drop
pub(super) fn msg_server_db_url_prompt(chain_name: &str) -> String {
format!("Please provide server database url for chain {chain_name}")
}

pub(super) fn msg_prover_db_url_prompt(chain_name: &str) -> String {
format!("Please provide prover database url for chain {chain_name}")
}

pub(super) fn msg_prover_db_name_prompt(chain_name: &str) -> String {
format!("Please provide prover database name for chain {chain_name}")
}

pub(super) fn msg_server_db_name_prompt(chain_name: &str) -> String {
format!("Please provide server database name for chain {chain_name}")
}
Expand Down Expand Up @@ -170,8 +176,15 @@ pub(super) const MSG_BUILDING_L1_CONTRACTS: &str = "Building L1 contracts...";

/// Forge utils related messages
pub(super) const MSG_DEPLOYER_PK_NOT_SET_ERR: &str = "Deployer private key is not set";
pub(super) fn msg_address_doesnt_have_enough_money_prompt(address: &H160) -> String {

pub(super) fn msg_address_doesnt_have_enough_money_prompt(
address: &H160,
actual: U256,
expected: U256,
) -> String {
let actual = format_ether(actual);
let expected = format_ether(expected);
format!(
"Address {address:?} doesn't have enough money to deploy contracts do you want to try again?"
"Address {address:?} doesn't have enough money to deploy contracts only {actual} ETH but expected: {expected} ETH do you want to try again?"
)
}

0 comments on commit f1d9f03

Please sign in to comment.