Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Extract spl-math-utils from spl-math #7421

Merged
merged 8 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ members = [
"libraries/discriminator",
"libraries/concurrent-merkle-tree",
"libraries/math",
"libraries/math-utils",
"libraries/merkle-tree-reference",
"libraries/pod",
"libraries/program-error",
Expand Down
19 changes: 19 additions & 0 deletions libraries/math-utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "spl-math-utils"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have this crate be spl-math and change the one with the program to spl-math-example?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Set the spl-math version back to 0.3 and set spl-math-example version to 0.1

version = "0.1.0"
description = "Solana Program Library Math Utils"
authors = ["Solana Labs Maintainers <[email protected]>"]
repository = "https://github.com/solana-labs/solana-program-library"
license = "Apache-2.0"
edition = "2021"

[dependencies]
num-traits = "0.2"
uint = "0.10"

[dev-dependencies]
proptest = "1.5.0"
libm = "0.2.11"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
8 changes: 8 additions & 0 deletions libraries/math-utils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Math

Library with utilities for on-chain math.

## Audit

The repository [README](https://github.com/solana-labs/solana-program-library#audits)
contains information about program audits.
6 changes: 6 additions & 0 deletions libraries/math-utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//! Math utilities.

pub mod approximations;
pub mod checked_ceil_div;
pub mod precise_number;
pub mod uint;
4 changes: 1 addition & 3 deletions libraries/math/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@ borsh = "1.5.1"
num-derive = "0.4"
num-traits = "0.2"
solana-program = "2.1.0"
spl-math-utils = { path = "../math-utils", version = "0.1.0" }
thiserror = "1.0"
uint = "0.10"

[dev-dependencies]
proptest = "1.5.0"
solana-program-test = "2.1.0"
solana-sdk = "2.1.0"
libm = "0.2.11"

[lib]
crate-type = ["cdylib", "lib"]
Expand Down
2 changes: 1 addition & 1 deletion libraries/math/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Math

Library with utilities for on-chain math.
Program wrapper around the spl-math-utils crate. The program exists for testing purposes.

## Audit

Expand Down
6 changes: 2 additions & 4 deletions libraries/math/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
#![deny(missing_docs)]
#![forbid(unsafe_code)]

pub mod approximations;
pub mod checked_ceil_div;
mod entrypoint;
pub mod error;
pub mod instruction;
pub mod precise_number;
pub mod processor;
pub mod uint;

pub use spl_math_utils::{approximations, checked_ceil_div, precise_number, uint};

solana_program::declare_id!("Math111111111111111111111111111111111111111");
3 changes: 0 additions & 3 deletions stake-pool/program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ serde = "1.0.214"
serde_derive = "1.0.103"
solana-program = "2.1.0"
solana-security-txt = "1.1.1"
spl-math = { version = "0.3", path = "../../libraries/math", features = [
"no-entrypoint",
] }
spl-pod = { version = "0.4.0", path = "../../libraries/pod", features = [
"borsh",
] }
Expand Down
2 changes: 1 addition & 1 deletion token-swap/program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ enum_dispatch = "0.3.13"
num-derive = "0.4"
num-traits = "0.2"
solana-program = "2.1.0"
spl-math = { version = "0.3", path = "../../libraries/math", features = [ "no-entrypoint" ] }
spl-math-utils = { version = "0.1", path = "../../libraries/math-utils" }
spl-token = { version = "6.0", path = "../../token/program", features = [ "no-entrypoint" ] }
spl-token-2022 = { version = "5.0.2", path = "../../token/program-2022", features = [ "no-entrypoint" ] }
thiserror = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion token-swap/program/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ publish = false
honggfuzz = { version = "0.5.56" }
arbitrary = { version = "1.3", features = ["derive"] }
solana-program = "2.1.0"
spl-math = { version = "0.3", path = "../../../libraries/math", features = [ "no-entrypoint" ] }
spl-math-utils = { version = "0.1", path = "../../../libraries/math-utils" }
spl-token = { version = "6.0", path = "../../../token/program", features = [ "no-entrypoint" ] }
spl-token-swap = { path = "..", features = ["fuzz", "no-entrypoint"] }

Expand Down
2 changes: 1 addition & 1 deletion token-swap/program/fuzz/src/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use {
arbitrary::Arbitrary,
honggfuzz::fuzz,
spl_math::precise_number::PreciseNumber,
spl_math_utils::precise_number::PreciseNumber,
spl_token::error::TokenError,
spl_token_swap::{
curve::{
Expand Down
4 changes: 2 additions & 2 deletions token-swap/program/src/curve/calculator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#[cfg(feature = "fuzz")]
use arbitrary::Arbitrary;
use {crate::error::SwapError, spl_math::precise_number::PreciseNumber, std::fmt::Debug};
use {crate::error::SwapError, spl_math_utils::precise_number::PreciseNumber, std::fmt::Debug};

/// Initial amount of pool tokens for swap contract, hard-coded to something
/// "sensible" given a maximum of u128.
Expand Down Expand Up @@ -194,7 +194,7 @@ pub trait CurveCalculator: Debug + DynPack {
/// Test helpers for curves
#[cfg(test)]
pub mod test {
use {super::*, proptest::prelude::*, spl_math::uint::U256};
use {super::*, proptest::prelude::*, spl_math_utils::uint::U256};

/// The epsilon for most curves when performing the conversion test,
/// comparing a one-sided deposit to a swap + deposit.
Expand Down
2 changes: 1 addition & 1 deletion token-swap/program/src/curve/constant_price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use {
program_error::ProgramError,
program_pack::{IsInitialized, Pack, Sealed},
},
spl_math::{checked_ceil_div::CheckedCeilDiv, precise_number::PreciseNumber, uint::U256},
spl_math_utils::{checked_ceil_div::CheckedCeilDiv, precise_number::PreciseNumber, uint::U256},
};

/// Get the amount of pool tokens for the given amount of token A or B.
Expand Down
2 changes: 1 addition & 1 deletion token-swap/program/src/curve/constant_product.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use {
program_error::ProgramError,
program_pack::{IsInitialized, Pack, Sealed},
},
spl_math::{checked_ceil_div::CheckedCeilDiv, precise_number::PreciseNumber},
spl_math_utils::{checked_ceil_div::CheckedCeilDiv, precise_number::PreciseNumber},
};

/// ConstantProductCurve struct implementing CurveCalculator
Expand Down
2 changes: 1 addition & 1 deletion token-swap/program/src/curve/offset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use {
program_error::ProgramError,
program_pack::{IsInitialized, Pack, Sealed},
},
spl_math::precise_number::PreciseNumber,
spl_math_utils::precise_number::PreciseNumber,
};

/// Offset curve, uses ConstantProduct under the hood, but adds an offset to
Expand Down
Loading