Skip to content

Commit

Permalink
Fix balance comparison to zero test (#4240)
Browse files Browse the repository at this point in the history
  • Loading branch information
elmattic authored Apr 22, 2024
1 parent 0ffc0fa commit 3c7111d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@
- [#4183](https://github.com/ChainSafe/forest/issues/4183) Add support for the
`Filecoin.EthGetBlockByNumber` RPC method.

- [#4240](https://github.com/ChainSafe/forest/pull/4240) Added `--fixed-unit`
and `--exact-balance` flags to `forest-wallet balance` similarly to
`forest-wallet list` subcommand.

### Changed

- [#4170](https://github.com/ChainSafe/forest/pull/4170) Change the default
Expand Down
15 changes: 9 additions & 6 deletions scripts/tests/calibnet_wallet_check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ forest_init
# Amount to send to 2nd address (note: `send` command defaults to FIL if no units are specified)
FIL_AMT="500 atto FIL"

# Amount for an empty wallet
FIL_ZERO="0 FIL"

$FOREST_WALLET_PATH import preloaded_wallet.key
$FOREST_WALLET_PATH --remote-wallet import preloaded_wallet.key

Expand Down Expand Up @@ -77,24 +80,24 @@ MSG=$($FOREST_WALLET_PATH send "$ADDR_TWO" "$FIL_AMT")
MSG_REMOTE=$($FOREST_WALLET_PATH --remote-wallet send "$ADDR_THREE" "$FIL_AMT")
: "$MSG_REMOTE"

ADDR_TWO_BALANCE=0
ADDR_TWO_BALANCE=$FIL_ZERO
i=0
while [[ $i != 20 && $ADDR_TWO_BALANCE == 0 ]]; do
while [[ $i != 20 && $ADDR_TWO_BALANCE == "$FIL_ZERO" ]]; do
i=$((i+1))

: "Checking balance $i/20"
sleep 30s
ADDR_TWO_BALANCE=$($FOREST_WALLET_PATH balance "$ADDR_TWO")
ADDR_TWO_BALANCE=$($FOREST_WALLET_PATH balance "$ADDR_TWO" -e)
done

ADDR_THREE_BALANCE=0
ADDR_THREE_BALANCE=$FIL_ZERO
i=0
while [[ $i != 20 && $ADDR_THREE_BALANCE == 0 ]]; do
while [[ $i != 20 && $ADDR_THREE_BALANCE == "$FIL_ZERO" ]]; do
i=$((i+1))

: "Checking balance $i/20"
sleep 30s
ADDR_THREE_BALANCE=$($FOREST_WALLET_PATH --remote-wallet balance "$ADDR_TWO")
ADDR_THREE_BALANCE=$($FOREST_WALLET_PATH --remote-wallet balance "$ADDR_THREE" -e)
done

# wallet list should contain address two with transfered FIL amount
Expand Down
41 changes: 29 additions & 12 deletions src/wallet/subcommands/wallet_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,15 @@ pub enum WalletCommands {
Balance {
/// The address of the account to check
address: String,
/// Output is rounded to 4 significant figures by default.
/// Do not round
// ENHANCE(aatifsyed): add a --round/--no-round argument pair
#[arg(long, alias = "exact-balance", short_alias = 'e')]
no_round: bool,
/// Output may be given an SI prefix like `atto` by default.
/// Do not do this, showing whole FIL at all times.
#[arg(long, alias = "fixed-unit", short_alias = 'f')]
no_abbrev: bool,
},
/// Get the default address of the wallet
Default,
Expand Down Expand Up @@ -313,11 +322,15 @@ impl WalletCommands {
println!("{addr}");
Ok(())
}
Self::Balance { address } => {
Self::Balance {
address,
no_round,
no_abbrev,
} => {
let StrictAddress(address) = StrictAddress::from_str(&address)
.with_context(|| format!("Invalid address: {address}"))?;
let balance = WalletBalance::call(&backend.remote, (address,)).await?;
println!("{balance}");
println!("{}", format_balance(&balance, no_round, no_abbrev));
Ok(())
}
Self::Default => {
Expand Down Expand Up @@ -405,16 +418,7 @@ impl WalletCommands {
let balance_token_amount =
WalletBalance::call(&backend.remote, (address,)).await?;

let balance_string = match (no_round, no_abbrev) {
// no_round, absolute
(true, true) => format!("{:#}", balance_token_amount.pretty()),
// no_round, relative
(true, false) => format!("{}", balance_token_amount.pretty()),
// round, absolute
(false, true) => format!("{:#.4}", balance_token_amount.pretty()),
// round, relative
(false, false) => format!("{:.4}", balance_token_amount.pretty()),
};
let balance_string = format_balance(&balance_token_amount, no_round, no_abbrev);

println!("{address:41} {default_address_mark:7} {balance_string}");
}
Expand Down Expand Up @@ -561,3 +565,16 @@ fn input_password_to_load_encrypted_keystore(data_dir: PathBuf) -> dialoguer::Re
.into_inner()
.expect("validation succeeded, so keystore must be emplaced"))
}

fn format_balance(balance: &TokenAmount, no_round: bool, no_abbrev: bool) -> String {
match (no_round, no_abbrev) {
// no_round, absolute
(true, true) => format!("{:#}", balance.pretty()),
// no_round, relative
(true, false) => format!("{}", balance.pretty()),
// round, absolute
(false, true) => format!("{:#.4}", balance.pretty()),
// round, relative
(false, false) => format!("{:.4}", balance.pretty()),
}
}

0 comments on commit 3c7111d

Please sign in to comment.