Skip to content

Commit

Permalink
✨ feat: add i64, i128, i256
Browse files Browse the repository at this point in the history
  • Loading branch information
josemvcerqueira authored Dec 23, 2024
2 parents e80ea7d + 5dbb391 commit 9b078b5
Show file tree
Hide file tree
Showing 27 changed files with 4,865 additions and 3,233 deletions.
30 changes: 2 additions & 28 deletions Move.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,10 @@ name = "InterestMath"
edition = "2024.beta" # edition = "legacy" to use legacy (pre-2024) Move
license = "MIT" # e.g., "MIT", "GPL", "Apache 2.0"
authors = ["Jose Cerqueira (jose@interestprotocol.com)"]
published-at = "0x0a885c86b868d83e5094ef8a34985d510a99f4dd1491d115297eb23cea427595"
published-at = "0xb4939698f1873771901021b5435569dadfd4e4d82d8d1005f30fa9771687d006"

[dependencies]
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "framework/mainnet" }

# For remote import, use the `{ git = "...", subdir = "...", rev = "..." }`.
# Revision can be a branch, a tag, and a commit hash.
# MyRemotePackage = { git = "https://some.remote/host.git", subdir = "remote/path", rev = "main" }

# For local dependencies use `local = path`. Path is relative to the package root
# Local = { local = "../path/to" }

# To resolve a version conflict and force a specific version for dependency
# override use `override = true`
# Override = { local = "../conflicting/version", override = true }

[addresses]
interest_math = "0x0a885c86b868d83e5094ef8a34985d510a99f4dd1491d115297eb23cea427595"

# Named addresses will be accessible in Move as `@name`. They're also exported:
# for example, `std = "0x1"` is exported by the Standard Library.
# alice = "0xA11CE"

[dev-dependencies]
# The dev-dependencies section allows overriding dependencies for `--test` and
# `--dev` modes. You can introduce test-only dependencies here.
# Local = { local = "../path/to/dev-build" }

[dev-addresses]
# The dev-addresses section allows overwriting named addresses for the `--test`
# and `--dev` modes.
# alice = "0xB0B"

interest_math = "0xb4939698f1873771901021b5435569dadfd4e4d82d8d1005f30fa9771687d006"
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@

### It contains the following modules:

- `fixed_point_wad`: Fixed point arithmetic with 1e18 precision.
- `fixed18`: Fixed point arithmetic with 1e18 precision.
- `u64`: Utility functions for u64.
- `u128`: Utility functions for u128.
- `u256`: Utility functions for u256.
- `i32`: Utility functions for i32.
- `i64`: Utility functions for i64.
- `i128`: Utility functions for i128.
- `i256`: Utility functions for i256.

## Immutable

[The package is immutable](https://suivision.xyz/txblock/7zTJr3Xe5Sj1MZqMFHHvmzLsxgfo5QRTR6r85xzGwCvo)
[The package is immutable](https://testnet.suivision.xyz/txblock/5eZ4W4JySQkVpCoZLsjoGxmtHbzqMcVp17akGzKKDTDz)

## Mainnet Code
## Testnet Code

[Explorer](https://suivision.xyz/package/0x0a885c86b868d83e5094ef8a34985d510a99f4dd1491d115297eb23cea427595?tab=Code)
[Explorer](https://testnet.suivision.xyz/package/0xb4939698f1873771901021b5435569dadfd4e4d82d8d1005f30fa9771687d006?tab=Code)
151 changes: 151 additions & 0 deletions sources/fixed18.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
module interest_math::fixed18;

use interest_math::uint_macro as macro;

// === Constants ===

const FIXED_18_BASE: u256 = 1_000_000_000_000_000_000;

// === Structs ===

public struct Fixed18 has copy, drop, store { value: u256 }

// === Conversion Functions ===

public fun raw_value(self: Fixed18): u256 {
self.value
}

public fun from_u256(value: u256): Fixed18 {
Fixed18 { value: (value * FIXED_18_BASE) }
}

public fun from_u128(value: u128): Fixed18 {
Fixed18 { value: ((value as u256) * FIXED_18_BASE) }
}

public fun from_u64(value: u64): Fixed18 {
Fixed18 { value: ((value as u256) * FIXED_18_BASE) }
}

public fun from_raw_u256(value: u256): Fixed18 {
Fixed18 { value }
}

public fun from_raw_u128(value: u128): Fixed18 {
Fixed18 { value: (value as u256) }
}

public fun from_raw_u64(value: u64): Fixed18 {
Fixed18 { value: (value as u256) }
}

public fun to_u256(x: Fixed18, decimals: u8): u256 {
let value = macro::mul_div_down!<u256>(x.value, macro::pow!<u256>(10, decimals), FIXED_18_BASE);
value
}

public fun to_u128(x: Fixed18, decimals: u8): u128 {
let value = macro::mul_div_down!<u256>(x.value, macro::pow!<u256>(10, decimals), FIXED_18_BASE);
value as u128
}

public fun to_u64(x: Fixed18, decimals: u8): u64 {
let value = macro::mul_div_down!<u256>(x.value, macro::pow!<u256>(10, decimals), FIXED_18_BASE);
value as u64
}

public fun to_u256_up(x: Fixed18, decimals: u8): u256 {
let value = macro::mul_div_up!<u256>(x.value, macro::pow!<u256>(10, decimals), FIXED_18_BASE);
value
}

public fun to_u128_up(x: Fixed18, decimals: u8): u128 {
let value = macro::mul_div_up!<u256>(x.value, macro::pow!<u256>(10, decimals), FIXED_18_BASE);
value as u128
}

public fun to_u64_up(x: Fixed18, decimals: u8): u64 {
let value = macro::mul_div_up!<u256>(x.value, macro::pow!<u256>(10, decimals), FIXED_18_BASE);
value as u64
}

public fun u64_to_d18(x: u64, decimals: u8): Fixed18 {
let value = macro::mul_div_up!(x, FIXED_18_BASE, macro::pow!<u256>(10, decimals));
Fixed18 { value }
}

public fun u128_to_d18(x: u128, decimals: u8): Fixed18 {
let value = macro::mul_div_up!((x as u256), FIXED_18_BASE, macro::pow!<u256>(10, decimals));
Fixed18 { value }
}

public fun u256_to_d18(x: u256, decimals: u8): Fixed18 {
let value = macro::mul_div_up!(x, FIXED_18_BASE, macro::pow!<u256>(10, decimals));
Fixed18 { value }
}

public fun u64_to_d18_up(x: u64, decimals: u8): Fixed18 {
let value = macro::mul_div_up!((x as u256), FIXED_18_BASE, macro::pow!<u256>(10, decimals));
Fixed18 { value }
}

public fun u128_to_d18_up(x: u128, decimals: u8): Fixed18 {
let value = macro::mul_div_up!((x as u256), FIXED_18_BASE, macro::pow!<u256>(10, decimals));
Fixed18 { value }
}

public fun u256_to_d18_up(x: u256, decimals: u8): Fixed18 {
let value = macro::mul_div_up!(x, FIXED_18_BASE, macro::pow!<u256>(10, decimals));
Fixed18 { value }
}

// === Try Functions ===

public fun try_mul_down(x: Fixed18, y: Fixed18): (bool, Fixed18) {
let (pred, value) = macro::try_mul_div_down!(x.value, y.value, FIXED_18_BASE);
(pred, Fixed18 { value })
}

public fun try_mul_up(x: Fixed18, y: Fixed18): (bool, Fixed18) {
let (pred, value) = macro::try_mul_div_up!(x.value, y.value, FIXED_18_BASE);
(pred, Fixed18 { value })
}

public fun try_div_down(x: Fixed18, y: Fixed18): (bool, Fixed18) {
let (pred, value) = macro::try_mul_div_down!(x.value, FIXED_18_BASE, y.value);
(pred, Fixed18 { value })
}

public fun try_div_up(x: Fixed18, y: Fixed18): (bool, Fixed18) {
let (pred, value) = macro::try_mul_div_up!(x.value, FIXED_18_BASE, y.value);
(pred, Fixed18 { value })
}

// === Arithmetic Functions ===

public fun mul_down(x: Fixed18, y: Fixed18): Fixed18 {
let value = macro::mul_div_down!(x.value, y.value, FIXED_18_BASE);
Fixed18 { value }
}

public fun mul_up(x: Fixed18, y: Fixed18): Fixed18 {
let value = macro::mul_div_up!(x.value, y.value, FIXED_18_BASE);
Fixed18 { value }
}

public fun div_down(x: Fixed18, y: Fixed18): Fixed18 {
let value = macro::mul_div_down!(x.value, FIXED_18_BASE, y.value);
Fixed18 { value }
}

public fun div_up(x: Fixed18, y: Fixed18): Fixed18 {
let value = macro::mul_div_up!(x.value, FIXED_18_BASE, y.value);
Fixed18 { value }
}

// === Utility Functions ===

public fun base(): u256 {
FIXED_18_BASE
}
Loading

0 comments on commit 9b078b5

Please sign in to comment.