Cosmos-sdk: sdk.Dec JSON Marshal to String()

Created on 11 Oct 2018  ·  9Comments  ·  Source: cosmos/cosmos-sdk

Summary of Bug

sdk.Dec of the value 1000 marshalls to a string representation 1000000000000. 1000000000000 is an internal precision representation. The marshalled value should be 1000.000000000.

Steps to Reproduce


For Admin Use

  • [ ] Not duplicate issue
  • [ ] Appropriate labels applied
  • [ ] Appropriate contributors tagged
  • [ ] Contributor assigned/self-assigned
proposal-accepted

Most helpful comment

Yeah, I'm pretty sure we can marshal differently for json/amino - we should totally marshal json decimal to a string 👍

All 9 comments

One thing to note is that if we go with marshalling using String() we'll probably need to use NewDecFromStr() which has a bigger performance hit than simply using big int's MarshalText/UnmarshalText.

I don't really agree, why would we add a decimal?

In JSON that might lead to reduced precision due to floating-point arithmetic.

Perhaps the LCD should expose the constant precision factor via API to enable frontends to convert easily.

There's two encodings here, JSON and amino. This proposal is basically asking about switching from encoding the number directly to instead encode the number as a string / bytes. I have no problem with marshalling as a string with the decimal in JSON. That seems like a much better approach. This then improves the clarity, and ease of integrating. There is no fear of floating point arithmetic, as it would encoded as a string essentially.

For amino I don't think we should do this. Part of the efficiency of protobuf is that you know the type at the field, so you don't have to do stuff like this.

Yeah, I'm pretty sure we can marshal differently for json/amino - we should totally marshal json decimal to a string 👍

Marshaling to a string with a decimal is fine but is that what @faboweb is asking for?

I guess there were two ways to interpet marshal as string, marshalling to the string type, and marshalling to the string representation (with the decimal point). I actually meant both in my comment, but I should've clarified. I believe fabo is asking for both as well.

I am only asking for marshalling to string not amino.

FYI were switching to ints here once fee distribution is merged, so this shouldn't be a problem. I still agree with changing this anyway though, as it will be needed when handling stuff for fee distribution.

Nice!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jackzampolin picture jackzampolin  ·  3Comments

fedekunze picture fedekunze  ·  3Comments

kevlubkcm picture kevlubkcm  ·  3Comments

ValarDragon picture ValarDragon  ·  3Comments

ValarDragon picture ValarDragon  ·  3Comments