-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Ociswap v2 Adapter v1]: Calculate the active tick from the price.
- Loading branch information
Showing
13 changed files
with
1,189 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Oops, something went wrong.