Skip to content

Create cosmwasm-math #1186

@webmaster128

Description

@webmaster128

This is a new package with the following goals:

  • Versioning independent of cosmwasm-std, such that CosmWasm 0.16 and 1.0 developers can use the same library
  • Add features required by defi users and implement them in a correct and heavility tested way
  • Ensure a small footprint in terms of code size and performance
  • Ensure we don't pull in float operations

TODOs:

  • Implement Sum for all types consistently (Use the same Sum implementation for all number types #1187)
  • Create checked_multiply_ratio returning an Option allowing to recover from overflow
  • Implement floor/ceil for all decimals
  • Copy over Uint64, Uint128, Uint256, Uint512, Decimal and Decimal256
  • Let all the checked_* functions returns Option instead of StdResult for better stdard library complience and avoiding performance hit (see Mark more Uint128, Uint64 and Decimal operations as const #1157).
  • Consider using macros to ensure API and implementation consistency across types
  • Signed integers
  • Signed decimals?
  • constify all the things
  • Find a consistent way to convert Uint64/Uin128 to the primitives u64 and u128. Are we happy with the current API?
  • Deprecate cosmwasm_std:{Decimal, Decimal256, Uint128, Uint256, Uint512, Uint64} and point the users to the math library
Feature Uint64 Uint128 Uint256 Uint512 Decimal Decimal256
const zero
const one ✅ (#1333) ✅ (#1333) ✅ (#1333) ✅ (#1333)
Sum
Add
AddAssign ✅ (#1253) ✅ (#1253)
Sub ✅ (#1242)
SubAssign ✅ (#1242) ✅ (#1253) ✅ (#1253)
Mul ✅ (#1245)
MulAssign ✅ (#1245) ✅ (#1255) ✅ (#1255)
Div ✅ (#1279) ✅ (#1279)
DivAssign ✅ (#1279) ✅ (#1279)
Rem ✅ (#1340 ) ✅ (#1340)
RemAssign ✅ (#1250) ✅ (#1250) ✅ (#1250) ✅ (#1250) ✅ (#1340 ) ✅ (#1340)
floor/ceil N/A N/A N/A N/A ✅ (#1347 ) ✅ (#1347)
pow ✅ (#1251) ✅ (#1251) ✅ (#1251) ✅ (#1342 ) ✅ (#1342)
checked_add ✅ (#1341 ) ✅ (#1341)
checked_sub ✅ (#1341 ) ✅ (#1341)
checked_rem ✅ (#1341 ) ✅ (#1341)
checked_mul
checked_div ✅ (#1341 ) ✅ (#1341)
checked_div_euclid (#1376) (#1376) N/A N/A
checked_pow
saturating_add ✅ (#1343 ) ✅ (#1343)
saturating_sub ✅ (#1343 ) ✅ (#1343)
saturating_mul ✅ (#1343 ) ✅ (#1343)
saturating_pow ✅ (#1343 ) ✅ (#1343) ✅ (#1342 ) ✅ (#1342)
wrapping_add ✅ (#1459) ✅ (#1459)
wrapping_sub ✅ (#1459) ✅ (#1459)
wrapping_mul ✅ (#1459) ✅ (#1459)
wrapping_pow ✅ (#1459) ✅ (#1459)
checked_from_ratio N/A N/A N/A N/A ✅ (#1281) ✅ (#1281)
checked_multiply_ratio ✅ (#1280) ✅ (#1280) ✅ (#1280) N/A N/A N/A
A const constructor ✅ (#1256) ✅ (#1264) ✅ (#1264)
const is_zero ✅ (#1256) ✅ (#1256) ✅ (#1256) ✅ (#1256) ✅ (#1256) ✅ (#1256)
const fn atomics N/A N/A N/A N/A ✅ (#1256) ✅ (#1256)
const fn decimal_places N/A N/A N/A N/A ✅ (#1256) ✅ (#1256)
pub const MAX: Self
pub const MIN: Self ✅ (#1366) ✅ (#1366) ✅ (#1366) ✅ (#1366) ✅ (#1366) ✅ (#1366)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions