2

Is it possible to get data with float point numbers from the exchange API in Phala's Fat Contract?

I have been trying to parse a json object from this API:

{"USD":0.106}

However, the price field is a floating point. Whenever I use f64 in the ink code, the deployment will fail with an error.

1 Answer 1

4

It's a well-known problem.

For your specific use case, we can walk-around like this:

  • You can use fixed crate to process real numbers, including parsing, to string, and arithmetic ops
  • To parse the json, you cannot use serde_json because it always introduce float points. Theoretically serde_json_core can be used, as long as you don't touch any type with float points. However, the API value is actually a float point, I don't know if you can give it a fixed point type or write your own serde parser.
  • In the worst case, you can try to manually parse the string (thinking about string processing in C), and combine with fixed to further work with real numbers

An additional information. We have SideVM without all these annoying limitations. It supports float points, std, sockets, and even async runtime (AsyncStd, partial tokio, Hyper, etc). The only caveat is that it's too powerful that we still haven't implement any resource rate-limiter to ensure the fair usage. So it's kinda like the parachain in 2019... We do have some ideas, and hopefully we can release it as a discussion to the public soon.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.