Skip to content

Commit

Permalink
[Ociswap v2 Adapter v1]: Calculate the active tick from the price.
Browse files Browse the repository at this point in the history
  • Loading branch information
0xOmarA committed Feb 14, 2024
1 parent 8198664 commit 93ce873
Show file tree
Hide file tree
Showing 13 changed files with 1,189 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ members = [
"libraries/gateway-client",
"libraries/scrypto-interface",
"libraries/ports-interface",
"libraries/scrypto-math",
# Tools
"tools/bootstrap",
# Tests
Expand All @@ -25,6 +26,7 @@ description = "The implementation of project Ignition in Scrypto for the Radix L

[workspace.dependencies]
sbor = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "ef169b1e1348b8dbad977ba81d086ee1e80d6ff8" }
utils = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "ef169b1e1348b8dbad977ba81d086ee1e80d6ff8" }
scrypto = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "ef169b1e1348b8dbad977ba81d086ee1e80d6ff8" }
transaction = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "ef169b1e1348b8dbad977ba81d086ee1e80d6ff8" }
radix-engine = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "ef169b1e1348b8dbad977ba81d086ee1e80d6ff8" }
Expand Down
15 changes: 15 additions & 0 deletions libraries/scrypto-math/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "scrypto-math"
version = "0.4.0"
edition = "2021"
license = "MIT"
description = "Math library extending Radix Scrypto with more advanced mathematical operations"
repository = "https://github.com/ociswap/scrypto-math"

[dependencies]
utils = { workspace = true }
radix-engine-common = { workspace = true }
radix-engine-interface = { workspace = true }

num-traits = { version = "0.2.15" }
pretty_assertions = { version = "1.4.0" }
21 changes: 21 additions & 0 deletions libraries/scrypto-math/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Ociswap

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
77 changes: 77 additions & 0 deletions libraries/scrypto-math/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# scrypto_math

## Why
Radix Scrypto currently is lacking more advanced mathematical operations like `exp`, `log` or `pow`.

`scrypto_math` aims to provide an alternative until these functionalities are provided upstream. The ultimate goal of `scrypto_math` however is to make itself obsolete.

## Usage
Add `scrypto_math` to your depdencies in the `Cargo.toml` of your Scrypto blueprint.
```rust
[dependencies]
scrypto_math = { git = "https://github.com/ociswap/scrypto-math", tag = "v0.4.0" }
```
Import the module:
```rust
use scrypto_math::*;
```

## Featues

### Exponential Function
The exponential function is provided for `Decimal` and `PreciseDecimal` with a polynomial approximation error lower than ~ 18 significant digits.
Background: the final result is calculated via `exp(x) = 2**k * R(r)` and the approximation `R(r)` is bound by an maximum error of `2**-59` (~ 18 decimal places).

For `Decimal`:
```rust
let exp: Option<Decimal> = dec!(4).exp();
```

For `PreciseDecimal`:
```rust
let exp: Option<PreciseDecimal> = pdec!(4).exp();
```

You can see a full blueprint example including tests here [AdvancedMathDemo](examples/advanced_math/src/lib.rs).

### Logarithm Function
Logarithm is available for `Decimal` and `PreciseDecimal` with a maximum polynomial approximation error bound by `2**-58.45` (~ 18 decimal places).

For `Decimal`:
```rust
let ln: Option<Decimal> = dec!(2).ln();
let log2: Option<Decimal> = dec!(3).log2();
let log10: Option<Decimal> = dec!(4).log10();
let log8: Option<Decimal> = dec!(5).log_base(base: dec!(8));
```

For `PreciseDecimal`:
```rust
let ln: Option<PreciseDecimal> = pdec!(2).ln();
let log2: Option<PreciseDecimal> = pdec!(3).log2();
let log10: Option<PreciseDecimal> = pdec!(4).log10();
let log8: Option<PreciseDecimal> = pdec!(5).log_base(base: pdec!(8));
```

You can see a full blueprint example including tests here [AdvancedMathDemo](examples/advanced_math/src/lib.rs).

### Power Function
The power function internally uses both `exp` and `ln` and also covers various special cases like `0**0` or `-2**3`.

For `Decimal`:
```rust
let pow: Option<Decimal> = dec!("3.14").pow("-14.12");
```

For `PreciseDecimal`:
```rust
let pow: Option<PreciseDecimal> = pdec!("3.14").pow("-45.97");
```

You can see a full blueprint example including tests here [AdvancedMathDemo](examples/advanced_math/src/lib.rs).

## Contributions
We are happy to collaborate and review and merge pull requests :)

## Disclaimer
Though covered by an extensive test suite, use at your own risk.
Loading

0 comments on commit 93ce873

Please sign in to comment.