-
Notifications
You must be signed in to change notification settings - Fork 336
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
Add helpers to stringify coins #899
Comments
The first one looks like a very good candidate for a For the second one I'd rather wait for https://doc.rust-lang.org/std/slice/trait.Join.html, which can cover the boilerplate. The separator is mostly caller specific I think. For human readable outputs you'd usually prefer |
Given that this is |
Right. I'm referring to the element separator in |
But even for a |
If that's a "standard", I guess sticking to it makes sense. |
Creating a coin denom that starts with numbers (i.e |
Looking at https://github.com/cosmos/cosmos-sdk/blob/master/types/coin.go we seem to get "guarantee" that the denom does not start with a digit. Also note Expected format: "{amount}{denomination}" and Expected format: "{amount0}{denomination},...,{amountN}{denominationN}" for the parsers. I guess it helps to align with that. |
I agree with implementing |
I'm thinking about the second part again. We could add and maintain it here. This gives us the opportunity to optimize the implementation in a way that you don't want every contract developer to be aware of. Collecting into |
Good point with optimization. Happy if you want to add it, but it seems not so important as |
An optimized version is here: /// Creates a comma separated string representation of the Display representation of the coins.
///
/// E.g. "" for the empty list, "123ustake" for a single element list and "123ucosm,8910ustake" for two elements.
pub fn coins_to_string(coins: &[Coin]) -> String {
let separators = coins.len().saturating_sub(1);
let mut out = String::with_capacity(coins.len() * 20 + separators);
let mut first = true;
for coin in coins {
if !first {
out.push(',');
} else {
first = false;
}
write!(out, "{}", coin).unwrap(); // See https://stackoverflow.com/a/28333723/2013738 for why unwrap is okay
}
out
} However, the smaller the API the better. We have to maintain this stuff basically forever. If we don't need it, let's not add it. |
I have this in some contracts to produce events. Maybe this would be a useful helper in
cosmwasm_std
?The text was updated successfully, but these errors were encountered: