-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
coins exposed to clients should be map instead of array #4076
Comments
I recall there was a reason we stuck with a slice instead of a map. For starters, maps are non-deterministic. So storing them as such may yield different application hashes. Not to mention, I doubt amino can even encode maps? cc @ValarDragon |
yeah that's what @alessio told me |
Great! Want to close this? |
oh, no, I meant that I think we should still export the map to clients. Even tho if we keep the slice internally |
You mean you want to implement |
to get the amount for a specific denom (eg const uatoms = coins.filter(coin => coin.denom === `uatom`) |
I understand now. What you're suggesting is creating a custom JSON encoder/decoder. However, I don't see anything inherently wrong with needing to filter imho. Now coins won't be a true representation of their internal structure. |
Surely theres a standard serializable golang hashmap? |
I googled this and one of the top results was #553 lol |
Also ref: #1273 |
If you want simple JSON map just for the clients, I think the following will suffice: types/coin.go
// ----------------------------------------------------------------------------
// Encoding
type CoinJSON map[string]Int
func (coins Coins) MarshalJSON() ([]byte, error) {
cj := make(CoinJSON)
for _, c := range coins {
cj[c.Denom] = c.Amount
}
return json.Marshal(cj)
}
func (coins *Coins) UnmarshalJSON(b []byte) error {
cj := make(CoinJSON)
if err := json.Unmarshal(b, &cj); err != nil {
return err
}
for denom, amount := range cj {
(*coins) = append((*coins), NewCoin(denom, amount))
}
coins.Sort()
return nil
} |
I think this will make life slightly easier for clients. |
Just REST would suffice for the scope of this issue I think |
With the code I showed above, it's as simple as that. You don't have to change anything else :) |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Summary
Expose
sdk.Coins
as amap[string]sdk.Int
instead of a slice of coins ([]sdk.Coin
)Problem Definition
currently
sdk.Coins
is defined as an array ofsdk.Coin
instead as a map (object), which makes it harder for clients to lookup a specific denomination since they have to filter the arrayProposal
sdk.Coin
should remain as a it is rn,sdk.Coins
should be refactored to amap[string]sdk.Int
:Proposed JSON
Current JSON
I've experienced this pain point myself on Lunie as well as @sabau, @faboweb and @jbibla
For Admin Use
The text was updated successfully, but these errors were encountered: