-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[cli] Add balance formatting for balance and gas commands #16477
[cli] Add balance formatting for balance and gas commands #16477
Conversation
The latest updates on your projects. Learn more about Vercel for Git โ๏ธ
|
15cf15d
to
bd02d77
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All good with some nits!
@@ -1829,11 +1830,12 @@ impl Display for SuiClientCommandResult { | |||
} | |||
|
|||
let mut builder = TableBuilder::default(); | |||
builder.set_header(vec!["gasCoinId", "gasBalance"]); | |||
builder.set_header(vec!["gasCoinId", "mistBalance (MIST)", "suiBalance (SUI)"]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not really related to this PR, but I don't understand why we use camelCase
for table header names? as opposed to "Gas Coin ID", "MIST Balance", "SUI Balance".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first time I started using json_to_table
, I somehow decided that the table headings should match the json keys when using the --json
flag. When passing a json object to json_to_table
, the headings will be camelCase
, so I kept it like that for other commands. We can of course change, not an issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I figured it was something like this, but then the addition of the " (MIST)" and " (SUI)" here kind of go counter to that. I think it would be fine to be more aesthetic in our choice of headings for the pretty tables FWIW!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, I'll go once through all of them and fix it.
@@ -2392,43 +2396,130 @@ fn pretty_print_balance( | |||
builder: &mut TableBuilder, | |||
with_coins: bool, | |||
) { | |||
let format_decmials = 3; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3 is a bit of a maverick choice for a currency isn't it (as opposed to 2)? Is there some precedence for it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just the way I implemented it, 3 meant actually 2 :)) Don't ask :D
(integer_part, fractional_part) | ||
} | ||
|
||
fn format_balance(value: u128, coin_decimals: u8, format_decimals: usize) -> String { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this implementation can be simplified slightly:
fn format_balance(value: u128, coin_decimals: u8, format_decimals: usize, symbol: Option<&str>) -> String {
let mut suffix = if let Some(symbol) {
format!(" {symbol}")
} else {
"".to_string()
};
let mut coin_decimals = coin_decimals as u32;
let billions = 10u128.pow(coin_decimals + 9);
let millions = 10u128.pow(coin_decimals + 6);
let thousands = 10u128.pow(coin_decimals + 3);
let (whole, fractional) = if value > billions {
coin_decimals += 9;
suffix = format!("B{suffix}");
divide(value, billions)
} else if value > millions {
coin_decimals += 6;
suffix = format!("M{suffix}");
divide(value, millions)
} else if value > thousands {
coin_decimals += 3;
suffix = format!("K{suffix}");
divide(value, thousands)
};
let fractional = format!("{fractional:0width$}", width = coin_decimals as usize)
.truncate(format_decimals);
format!("{whole}.{fractional}{suffix}")
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. I don't fully follow the code, for example the coin_decimals += 9 / 6 / 3
, they don't seem to do anything?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I figured it out! It was missing the first call to divide, before the whole if else branch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cannot figure it out how to make it work, in some cases it does not work properly (e.g., thousands). I'll use the other version and add it on my list to see if we can the code nicer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The high level idea of this implementation is that it treats billions, millions, and thousands as their own "unit", with suffix "B SUI", "M SUI", "K SUI", etc. I think what I missed though was the base case (if the amount was smaller than a thousand).
The addition to coin_decimals
is to deal with padding the fractional part with the right number of zeroes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here it is with what I think are the necessary fixes:
fn format_balance(value: u128, coin_decimals: u8, format_decimals: usize, symbol: Option<&str>) -> String {
let mut suffix = if let Some(symbol) {
format!(" {symbol}")
} else {
"".to_string()
};
let mut coin_decimals = coin_decimals as u32;
let billions = 10u128.pow(coin_decimals + 9);
let millions = 10u128.pow(coin_decimals + 6);
let thousands = 10u128.pow(coin_decimals + 3);
let units = 10u128.pow(coin_decimals);
let (whole, fractional) = if value > billions {
coin_decimals += 9;
suffix = format!("B{suffix}");
divide(value, billions)
} else if value > millions {
coin_decimals += 6;
suffix = format!("M{suffix}");
divide(value, millions)
} else if value > thousands {
coin_decimals += 3;
suffix = format!("K{suffix}");
divide(value, thousands)
} else {
divide(value, units)
};
let fractional = format!("{fractional:0width$}", width = coin_decimals as usize)
.truncate(format_decimals);
format!("{whole}.{fractional}{suffix}")
}
โฆ#16477) ## Description This PR improves the formatting of the coin objects for `client gas` and `client balance`. ## Test Plan Visual tests + existing tests. ``` ๐ sui % sui client gas 0x28409ff3622bbe6f08ac6199ac676864097378ca61cb084c8ccd60e2b187c2bd [warn] Client/Server api version mismatch, client api version : 1.20.0, server api version : 1.19.1 โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ โ Showing 11 gas coins and their balances. โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโค โ gasCoinId โ mistBalance (MIST) โ suiBalance (SUI) โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโค โ 0x086c2acfcfe3486f7bdc2776c437e86c514bc57f0bcf45b56c02df5adf262661 โ 467370580 โ 0.46 โ โ 0x0932dd16f91af65277c9a70a984b0ca38650145851b803bd74afcd10a9c8faf4 โ 65053495712 โ 65.05 โ โ 0x1ca885902867d76eea3a658ca0f8ad8dd78677a3d49fe39a639704b6ebcf6402 โ 360226368 โ 0.36 โ โ 0x2d62f203fd1dc550d3aa441b777be9558616689f0d89dc5a77a8504e92411d7c โ 292949372 โ 0.29 โ โ 0x64ab87ecf605263352171f9cd6ef19bf63c771a8919f07cb585c23d4833995aa โ 420680104 โ 0.42 โ โ 0x85ffbe1439d1cc8a2f927410424ac050c61c518130670e9cf12211298938915d โ 443874748 โ 0.44 โ โ 0x956feeb8146c8be86b21adc6a32989a34e3aa33e2f2fe817e73739b8a7578102 โ 292698372 โ 0.29 โ โ 0x9a42ab27f66e887caeaee1a096baf1433ab59a690599f12c1eb83beb1c085407 โ 672390364 โ 0.67 โ โ 0x9af10229657774af1973a50773fbf5a6192dc2d6cd8c4af6778246fa45c9d0fc โ 474064540 โ 0.47 โ โ 0xb29207f2bcd6d737cfb13385e6545f3b676ced7fa8559b774d01d8c9335146fe โ 242133524 โ 0.24 โ โ 0xbfd24877ade8a38c1665ec0be579a6a2ce50cb06a987ed0cf994b5f65e3f8541 โ 290452196 โ 0.29 โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโค โ Showing 11 gas coins and their balances. โ โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ ๐ sui % sui client balance 0x28409ff3622bbe6f08ac6199ac676864097378ca61cb084c8ccd60e2b187c2bd --with-coins [warn] Client/Server api version mismatch, client api version : 1.20.0, server api version : 1.19.1 โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ โ Balance of coins owned by this address โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค โ โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ โ โ โ Sui: 11 coins, Balance: 69005151084 (69.00 SUI) โ โ โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค โ โ โ coinId balance (raw) balance โ โ โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค โ โ โ 0x086c2acfcfe3486f7bdc2776c437e86c514bc57f0bcf45b56c02df5adf262661 484525540 0.48 SUI โ โ โ โ 0x0932dd16f91af65277c9a70a984b0ca38650145851b803bd74afcd10a9c8faf4 65036962456 65.03 SUI โ โ โ โ 0x1ca885902867d76eea3a658ca0f8ad8dd78677a3d49fe39a639704b6ebcf6402 368036368 0.36 SUI โ โ โ โ 0x2d62f203fd1dc550d3aa441b777be9558616689f0d89dc5a77a8504e92411d7c 292949372 0.29 SUI โ โ โ โ 0x64ab87ecf605263352171f9cd6ef19bf63c771a8919f07cb585c23d4833995aa 418646128 0.41 SUI โ โ โ โ 0x85ffbe1439d1cc8a2f927410424ac050c61c518130670e9cf12211298938915d 450418100 0.45 SUI โ โ โ โ 0x956feeb8146c8be86b21adc6a32989a34e3aa33e2f2fe817e73739b8a7578102 299864424 0.29 SUI โ โ โ โ 0x9a42ab27f66e887caeaee1a096baf1433ab59a690599f12c1eb83beb1c085407 670363684 0.67 SUI โ โ โ โ 0x9af10229657774af1973a50773fbf5a6192dc2d6cd8c4af6778246fa45c9d0fc 472029956 0.47 SUI โ โ โ โ 0xb29207f2bcd6d737cfb13385e6545f3b676ced7fa8559b774d01d8c9335146fe 220902860 0.22 SUI โ โ โ โ 0xbfd24877ade8a38c1665ec0be579a6a2ce50cb06a987ed0cf994b5f65e3f8541 290452196 0.29 SUI โ โ โ โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ โ โ โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ โ โ โ USD Coin: 1 coin, Balance: 11001500 (11.00 USDC) โ โ โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค โ โ โ coinId balance (raw) balance โ โ โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค โ โ โ 0x035f43b7a4a3859c7ebc0d59fa8bfbd83631a8941f2b80a17d50b095d48fc2e5 11001500 11.00 USDC โ โ โ โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ โ โ โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ โ โ โ Tether USD: 1 coin, Balance: 0 (0.00 USDT) โ โ โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค โ โ โ coinId balance (raw) balance โ โ โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค โ โ โ 0x46239adea94e1d530c1a735eb81c8a37f2312eb90a5faa590f4b3294bc1f6451 0 0.00 USDT โ โ โ โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ โ โ โ โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ ๐ sui % sui client balance 0x28409ff3622bbe6f08ac6199ac676864097378ca61cb084c8ccd60e2b187c2bd [warn] Client/Server api version mismatch, client api version : 1.20.0, server api version : 1.19.1 โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ โ Balance of coins owned by this address โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค โ โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ โ โ โ coin balance (raw) balance โ โ โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค โ โ โ Sui 68981878692 68.98 SUI โ โ โ โ USD Coin 11001500 11.00 USDC โ โ โ โ Tether USD 0 0.00 USDT โ โ โ โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ โ โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ ``` --- If your changes are not user-facing and do not break anything, you can skip the following section. Otherwise, please briefly describe what has changed under the Release Notes section. ### Type of Change (Check all that apply) - [ ] protocol change - [x] user-visible impact - [ ] breaking change for a client SDKs - [ ] breaking change for FNs (FN binary must upgrade) - [ ] breaking change for validators or node operators (must upgrade binaries) - [ ] breaking change for on-chain data layout - [ ] necessitate either a data wipe or data migration ### Release notes Improved the way balance is displayed for `sui client gas` and `sui client balance` commands.
Description
This PR improves the formatting of the coin objects for
client gas
andclient balance
.Test Plan
Visual tests + existing tests.
If your changes are not user-facing and do not break anything, you can skip the following section. Otherwise, please briefly describe what has changed under the Release Notes section.
Type of Change (Check all that apply)
Release notes
Improved the way balance is displayed for
sui client gas
andsui client balance
commands.