CosmWasm 1.5

Simon Warta
CosmWasm
Published in
3 min readNov 6, 2023

The latest release of the CosmWasm 1.x series brings support for floating point operations, amongst other things.

Foto von Seth Doyle auf Unsplash

CosmWasm 1.5 has been released, bringing support for floating points to the virtual machine and various additional math additions to the standard library.

Floats

In CosmWasm < 1.5, all float operations in the Wasm blob were denied. The reason for that is that floating point operations have certain ways of being non-deterministic. In Wasm, this particularly affects the binary representation of NaN, which can be different depending on the CPU, operating system, and other system conditions. Luckily Wasmer has a NaN normalization feature, which allows us to avoid this problem.

When the Wasm file was uploaded to the chain, it was scanned for float operations. Once a single operation was found, the upload was rejected. While this is the safe approach, it caused a lot of trouble for developers who used dependencies that make use of floats. In many cases, those instructions were never executed but prevented the upload. Finding the source of such operations can easily cost days and weeks of development time, leading to a frustrating experience. The other scenario is code that actively relies on floats. While finance math and cryptography typically don’t use floats, there are interesting use cases that can be explored with floats available, such as:

  • bonding curves
  • non-linear voting power
  • games
  • generative art

In ComsWasm 1.5, we not only enabled NaN normalization. We created a massive test suite that executes all known float operations over many different inputs and ensured to get the same hash across a variety of systems. This gives us the confidence that using floats in CosmWasm is safe.

This does not mean that floats are encouraged to be used for everything. Especially finance math should stick to decimals as you want to avoid 0.1 + 0.2 being 0.30000000000000004 in your app. Also, json-serde-wasm does not have float support right now, making floats in JSON messages unusable. This might change in the future, but you also might want to avoid doing that in order to not lose precision when communicating between different systems like Wasm contract <-> JavaScript client.

Standard library additions

The changes here affect cosmwasm-std and are available to all contract developers, no matter which version of CosmWasm runs on the chain.

More signed integers math

In 1.3, we introduced signed integers and now added a bunch of additional APIs for those, such as:

  • abs/unsigned_abs
  • checked_multiply_ratio and full_mul
  • is_negative
  • Various From and TryFrom implementations between signed and unsigned integer types

JSON encoders and decoders

Historically the struct to binary encoding in CosmWasm has always been JSON. But we are preparing for a future in which other encodings are usable as well, e.g. for more compact storage encoding. For this reason, encoding becomes more explicit:

  • to_json_{vec,binary} replaces the old to_{vec,binary}
  • from_json replaces the old from_{slice,binary}
  • to_json_string was added for cases in which you need a String and don’t want to handle Binary -> String conversion errors that can never happen in valid JSON.

Signed decimals

SignedDecimal and SignedDecimal256 now complement Decimal and Decimal256 and allow storing negative values. Pretty straightforward.

For more details, check out the CHANGELOG. CosmWasm 1.5 will be embedded in the upcoming wasmd 0.44. All cosmwasm-std improvements can safely be used on chains running older versions of CosmWasm.

--

--

Simon Warta
CosmWasm

Building CosmWasm and CosmJS at Confio | Ex IOV | Co-Founder of Kullo | Board member prolina Foundation