Skip to content

Commit

Permalink
Merge pull request #376 from Phoenix-Protocol-Group/decimal256-implem…
Browse files Browse the repository at this point in the history
…entation

Decimal256 implementation
  • Loading branch information
gangov authored Aug 5, 2024
2 parents 65ac3ee + 68adc32 commit d43b7fd
Show file tree
Hide file tree
Showing 5 changed files with 2,257 additions and 985 deletions.
5 changes: 4 additions & 1 deletion packages/decimal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ license = { workspace = true }
description = "A precise decimal arithmetic package for Soroban contracts"

[dependencies]
soroban-sdk = { workspace = true, features = ["alloc"]}
soroban-sdk = { workspace = true, features = ["alloc"] }

[dev-dependencies]
soroban-sdk = { workspace = true, features = ["testutils"] }
23 changes: 22 additions & 1 deletion packages/decimal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
This code is taken from the [cosmwasm-std crate](https://github.com/CosmWasm/cosmwasm.), which is licensed under the Apache License 2.0
The contract provides a `Decimal` struct for arithmetic operations, suitable for blockchain De-Fi operations, where precision is of highest importance. It ensures that calculations are accurate up to 18 decimal places.

## Messages
# Decimal(i128)
## Methods

- `new(value: i128) -> Self`: Creates a new Decimal.
- `raw(value: i128) -> Self`: Returns the raw value from `i128`.
Expand All @@ -16,3 +17,23 @@ The contract provides a `Decimal` struct for arithmetic operations, suitable for
- `from_ratio(numerator: impl Into<i128>, denominator: impl Into<i128>) -> Self`: Returns the ratio (numerator / denominator) as a Decimal.
- `abs(&self) -> Self`: Returns the absolute value of the Decimal.
- `to_string(&self, env: &Env) -> String`: Converts the Decimal to a string.


# Decimal256
## Methods

- `new(value: u128) -> Self`: Creates a new Decimal.
- `raw(value: u128) -> Self`: Returns the raw value from `u128`.
- `one() -> Self`: Create a `1.0` Decimal.
- `zero() -> Self`: Create a `0.0` Decimal.
- `percent(x: u64) -> Self`: Convert `x%` into Decimal.
- `permille(x: u64) -> Self`: Convert permille `(x/1000)` into Decimal.
- `bps(x: u64) -> Self`: Convert basis points `(x/10000)` into Decimal.
- `from_atomics(atomics: u128, decimal_places: i32) -> Self`: Creates a Decimal from atomic units and decimal places.
- `inv(&self) -> Option<Self>`: Returns the multiplicative inverse `1/d` for decimal `d`.
- `from_ratio(numerator: impl Into<U256>, denominator: impl Into<U256>) -> Self`: Returns the ratio (numerator / denominator) as a Decimal.
- `abs(&self) -> Self`: Returns the absolute value of the Decimal.
- `to_string(&self, env: &Env) -> String`: Converts the Decimal to a string.


N.B.: `from_atomics(atomics: u128, decimal_places: i32) -> Self` currently supports maximum `38` as input for `decimcal_places`
Loading

0 comments on commit d43b7fd

Please sign in to comment.