From 7667bfa63acdd2e147bebf3b5be9577b28827174 Mon Sep 17 00:00:00 2001 From: kevinheavey Date: Thu, 31 Oct 2024 12:38:01 +0400 Subject: [PATCH 1/8] extract math-utils from spl-math --- Cargo.lock | 11 +++++++++++ Cargo.toml | 1 + libraries/math-utils/Cargo.toml | 19 +++++++++++++++++++ libraries/math-utils/README.md | 8 ++++++++ .../src/approximations.rs | 0 .../src/checked_ceil_div.rs | 0 libraries/math-utils/src/lib.rs | 6 ++++++ .../src/precise_number.rs | 0 libraries/{math => math-utils}/src/uint.rs | 0 libraries/math/Cargo.toml | 1 + libraries/math/README.md | 2 +- libraries/math/src/lib.rs | 6 ++---- 12 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 libraries/math-utils/Cargo.toml create mode 100644 libraries/math-utils/README.md rename libraries/{math => math-utils}/src/approximations.rs (100%) rename libraries/{math => math-utils}/src/checked_ceil_div.rs (100%) create mode 100644 libraries/math-utils/src/lib.rs rename libraries/{math => math-utils}/src/precise_number.rs (100%) rename libraries/{math => math-utils}/src/uint.rs (100%) diff --git a/Cargo.lock b/Cargo.lock index 9dc064bbd7c..0cd9001039f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8135,10 +8135,21 @@ dependencies = [ "solana-program", "solana-program-test", "solana-sdk", + "spl-math-utils", "thiserror", "uint", ] +[[package]] +name = "spl-math-utils" +version = "0.1.0" +dependencies = [ + "libm", + "num-traits", + "proptest", + "uint", +] + [[package]] name = "spl-memo" version = "5.0.0" diff --git a/Cargo.toml b/Cargo.toml index e5db1081db4..a39b33944b7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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", diff --git a/libraries/math-utils/Cargo.toml b/libraries/math-utils/Cargo.toml new file mode 100644 index 00000000000..c5f119c7589 --- /dev/null +++ b/libraries/math-utils/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "spl-math-utils" +version = "0.1.0" +description = "Solana Program Library Math Utils" +authors = ["Solana Labs Maintainers "] +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"] diff --git a/libraries/math-utils/README.md b/libraries/math-utils/README.md new file mode 100644 index 00000000000..17e2fa99371 --- /dev/null +++ b/libraries/math-utils/README.md @@ -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. diff --git a/libraries/math/src/approximations.rs b/libraries/math-utils/src/approximations.rs similarity index 100% rename from libraries/math/src/approximations.rs rename to libraries/math-utils/src/approximations.rs diff --git a/libraries/math/src/checked_ceil_div.rs b/libraries/math-utils/src/checked_ceil_div.rs similarity index 100% rename from libraries/math/src/checked_ceil_div.rs rename to libraries/math-utils/src/checked_ceil_div.rs diff --git a/libraries/math-utils/src/lib.rs b/libraries/math-utils/src/lib.rs new file mode 100644 index 00000000000..646e19863bc --- /dev/null +++ b/libraries/math-utils/src/lib.rs @@ -0,0 +1,6 @@ +//! Math utilities. + +pub mod approximations; +pub mod checked_ceil_div; +pub mod precise_number; +pub mod uint; \ No newline at end of file diff --git a/libraries/math/src/precise_number.rs b/libraries/math-utils/src/precise_number.rs similarity index 100% rename from libraries/math/src/precise_number.rs rename to libraries/math-utils/src/precise_number.rs diff --git a/libraries/math/src/uint.rs b/libraries/math-utils/src/uint.rs similarity index 100% rename from libraries/math/src/uint.rs rename to libraries/math-utils/src/uint.rs diff --git a/libraries/math/Cargo.toml b/libraries/math/Cargo.toml index eefe4bb8a22..129f1b07f44 100644 --- a/libraries/math/Cargo.toml +++ b/libraries/math/Cargo.toml @@ -16,6 +16,7 @@ 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" diff --git a/libraries/math/README.md b/libraries/math/README.md index 17e2fa99371..2035ce0c8f0 100644 --- a/libraries/math/README.md +++ b/libraries/math/README.md @@ -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 diff --git a/libraries/math/src/lib.rs b/libraries/math/src/lib.rs index 3e9c5ba83b4..5c6c6b1b9c7 100644 --- a/libraries/math/src/lib.rs +++ b/libraries/math/src/lib.rs @@ -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"); From b9e65ab6d6a3a76a8b22229622209bcff1122fc1 Mon Sep 17 00:00:00 2001 From: kevinheavey Date: Thu, 31 Oct 2024 12:39:17 +0400 Subject: [PATCH 2/8] newline --- libraries/math-utils/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/math-utils/src/lib.rs b/libraries/math-utils/src/lib.rs index 646e19863bc..54c5a430917 100644 --- a/libraries/math-utils/src/lib.rs +++ b/libraries/math-utils/src/lib.rs @@ -3,4 +3,4 @@ pub mod approximations; pub mod checked_ceil_div; pub mod precise_number; -pub mod uint; \ No newline at end of file +pub mod uint; From 671922f56c3c104cfafe6c59e92f537a0f469a06 Mon Sep 17 00:00:00 2001 From: kevinheavey Date: Thu, 31 Oct 2024 12:44:36 +0400 Subject: [PATCH 3/8] replace spl-math with spl-math-utils in spl-token-swap --- Cargo.lock | 2 +- token-swap/program/Cargo.toml | 2 +- token-swap/program/fuzz/src/instructions.rs | 2 +- token-swap/program/src/curve/calculator.rs | 4 ++-- token-swap/program/src/curve/constant_price.rs | 2 +- token-swap/program/src/curve/constant_product.rs | 2 +- token-swap/program/src/curve/offset.rs | 2 +- 7 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0cd9001039f..c3618f6649e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8816,7 +8816,7 @@ dependencies = [ "roots", "solana-program", "solana-sdk", - "spl-math", + "spl-math-utils", "spl-token 6.0.0", "spl-token-2022 5.0.2", "test-case", diff --git a/token-swap/program/Cargo.toml b/token-swap/program/Cargo.toml index fc880f3a9f5..588283406e9 100644 --- a/token-swap/program/Cargo.toml +++ b/token-swap/program/Cargo.toml @@ -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" diff --git a/token-swap/program/fuzz/src/instructions.rs b/token-swap/program/fuzz/src/instructions.rs index 62f765fde46..ddb378e9eee 100644 --- a/token-swap/program/fuzz/src/instructions.rs +++ b/token-swap/program/fuzz/src/instructions.rs @@ -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::{ diff --git a/token-swap/program/src/curve/calculator.rs b/token-swap/program/src/curve/calculator.rs index 36d9de468ea..e329b315a3d 100644 --- a/token-swap/program/src/curve/calculator.rs +++ b/token-swap/program/src/curve/calculator.rs @@ -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. @@ -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. diff --git a/token-swap/program/src/curve/constant_price.rs b/token-swap/program/src/curve/constant_price.rs index b9e5272b50a..041a05834c5 100644 --- a/token-swap/program/src/curve/constant_price.rs +++ b/token-swap/program/src/curve/constant_price.rs @@ -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. diff --git a/token-swap/program/src/curve/constant_product.rs b/token-swap/program/src/curve/constant_product.rs index c13a66b564d..59dc5321bc3 100644 --- a/token-swap/program/src/curve/constant_product.rs +++ b/token-swap/program/src/curve/constant_product.rs @@ -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 diff --git a/token-swap/program/src/curve/offset.rs b/token-swap/program/src/curve/offset.rs index 8d5937536d0..a40cb8b1dba 100644 --- a/token-swap/program/src/curve/offset.rs +++ b/token-swap/program/src/curve/offset.rs @@ -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 From 1b5738bd388865ba0862ff32340455d92fb49fae Mon Sep 17 00:00:00 2001 From: kevinheavey Date: Thu, 31 Oct 2024 12:47:26 +0400 Subject: [PATCH 4/8] remove now-unused deps from spl-math --- Cargo.lock | 3 --- libraries/math/Cargo.toml | 3 --- 2 files changed, 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c3618f6649e..b8823b16bcf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8128,16 +8128,13 @@ name = "spl-math" version = "0.3.0" dependencies = [ "borsh 1.5.1", - "libm", "num-derive", "num-traits", - "proptest", "solana-program", "solana-program-test", "solana-sdk", "spl-math-utils", "thiserror", - "uint", ] [[package]] diff --git a/libraries/math/Cargo.toml b/libraries/math/Cargo.toml index 129f1b07f44..018102d3dea 100644 --- a/libraries/math/Cargo.toml +++ b/libraries/math/Cargo.toml @@ -18,13 +18,10 @@ 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"] From f4b43b5d4fb860d015948d4fd7439a213fa7ae93 Mon Sep 17 00:00:00 2001 From: kevinheavey Date: Thu, 31 Oct 2024 12:52:01 +0400 Subject: [PATCH 5/8] remove spl-math from stake-pool deps (it was already unused) --- Cargo.lock | 1 - stake-pool/program/Cargo.toml | 3 --- 2 files changed, 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b8823b16bcf..e6bdcb42554 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8364,7 +8364,6 @@ dependencies = [ "solana-sdk", "solana-security-txt", "solana-vote-program", - "spl-math", "spl-pod 0.4.0", "spl-token 6.0.0", "spl-token-2022 5.0.2", diff --git a/stake-pool/program/Cargo.toml b/stake-pool/program/Cargo.toml index ad4c5c35550..622f6f4ad36 100644 --- a/stake-pool/program/Cargo.toml +++ b/stake-pool/program/Cargo.toml @@ -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", ] } From af11bcb42841732192173d4066a3e4d4b329f800 Mon Sep 17 00:00:00 2001 From: kevinheavey Date: Thu, 31 Oct 2024 12:53:12 +0400 Subject: [PATCH 6/8] fix math-utils dep in spl-token-swap-fuzz --- Cargo.lock | 2 +- token-swap/program/fuzz/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e6bdcb42554..ea3b821fb22 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8826,7 +8826,7 @@ dependencies = [ "arbitrary", "honggfuzz", "solana-program", - "spl-math", + "spl-math-utils", "spl-token 6.0.0", "spl-token-swap", ] diff --git a/token-swap/program/fuzz/Cargo.toml b/token-swap/program/fuzz/Cargo.toml index 4d010363edf..4dca388cc99 100644 --- a/token-swap/program/fuzz/Cargo.toml +++ b/token-swap/program/fuzz/Cargo.toml @@ -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"] } From bb7c40a64ac384a345c9ef813be59ced390357d1 Mon Sep 17 00:00:00 2001 From: kevinheavey Date: Thu, 31 Oct 2024 22:30:40 +0400 Subject: [PATCH 7/8] moce crate dirs --- Cargo.toml | 2 +- libraries/math-example/Cargo.toml | 31 +++++++++++++++++++ .../{math-utils => math-example}/README.md | 2 +- libraries/{math => math-example}/Xargo.toml | 0 .../{math => math-example}/src/entrypoint.rs | 0 libraries/{math => math-example}/src/error.rs | 0 .../{math => math-example}/src/instruction.rs | 0 libraries/math-example/src/lib.rs | 13 ++++++++ .../{math => math-example}/src/processor.rs | 0 .../tests/instruction_count.rs | 0 libraries/math-utils/Cargo.toml | 19 ------------ libraries/math-utils/src/lib.rs | 6 ---- libraries/math/Cargo.toml | 24 ++++---------- libraries/math/README.md | 2 +- .../src/approximations.rs | 0 .../src/checked_ceil_div.rs | 0 libraries/math/src/lib.rs | 17 +++------- .../src/precise_number.rs | 0 libraries/{math-utils => math}/src/uint.rs | 0 token-swap/program/Cargo.toml | 2 +- token-swap/program/fuzz/Cargo.toml | 2 +- 21 files changed, 60 insertions(+), 60 deletions(-) create mode 100644 libraries/math-example/Cargo.toml rename libraries/{math-utils => math-example}/README.md (62%) rename libraries/{math => math-example}/Xargo.toml (100%) rename libraries/{math => math-example}/src/entrypoint.rs (100%) rename libraries/{math => math-example}/src/error.rs (100%) rename libraries/{math => math-example}/src/instruction.rs (100%) create mode 100644 libraries/math-example/src/lib.rs rename libraries/{math => math-example}/src/processor.rs (100%) rename libraries/{math => math-example}/tests/instruction_count.rs (100%) delete mode 100644 libraries/math-utils/Cargo.toml delete mode 100644 libraries/math-utils/src/lib.rs rename libraries/{math-utils => math}/src/approximations.rs (100%) rename libraries/{math-utils => math}/src/checked_ceil_div.rs (100%) rename libraries/{math-utils => math}/src/precise_number.rs (100%) rename libraries/{math-utils => math}/src/uint.rs (100%) diff --git a/Cargo.toml b/Cargo.toml index a39b33944b7..a2ca9427310 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,7 +36,7 @@ members = [ "libraries/discriminator", "libraries/concurrent-merkle-tree", "libraries/math", - "libraries/math-utils", + "libraries/math-example", "libraries/merkle-tree-reference", "libraries/pod", "libraries/program-error", diff --git a/libraries/math-example/Cargo.toml b/libraries/math-example/Cargo.toml new file mode 100644 index 00000000000..df8d4b5dcf9 --- /dev/null +++ b/libraries/math-example/Cargo.toml @@ -0,0 +1,31 @@ +[package] +name = "spl-math" +version = "0.3.0" +description = "Solana Program Library Math" +authors = ["Solana Labs Maintainers "] +repository = "https://github.com/solana-labs/solana-program-library" +license = "Apache-2.0" +edition = "2021" + +[features] +no-entrypoint = [] +test-sbf = [] + +[dependencies] +borsh = "1.5.1" +num-derive = "0.4" +num-traits = "0.2" +solana-program = "2.1.0" +spl-math-utils = { path = "../math", version = "0.1.0" } +thiserror = "1.0" + +[dev-dependencies] +solana-program-test = "2.1.0" +solana-sdk = "2.1.0" + +[lib] +crate-type = ["cdylib", "lib"] + + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] diff --git a/libraries/math-utils/README.md b/libraries/math-example/README.md similarity index 62% rename from libraries/math-utils/README.md rename to libraries/math-example/README.md index 17e2fa99371..2035ce0c8f0 100644 --- a/libraries/math-utils/README.md +++ b/libraries/math-example/README.md @@ -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 diff --git a/libraries/math/Xargo.toml b/libraries/math-example/Xargo.toml similarity index 100% rename from libraries/math/Xargo.toml rename to libraries/math-example/Xargo.toml diff --git a/libraries/math/src/entrypoint.rs b/libraries/math-example/src/entrypoint.rs similarity index 100% rename from libraries/math/src/entrypoint.rs rename to libraries/math-example/src/entrypoint.rs diff --git a/libraries/math/src/error.rs b/libraries/math-example/src/error.rs similarity index 100% rename from libraries/math/src/error.rs rename to libraries/math-example/src/error.rs diff --git a/libraries/math/src/instruction.rs b/libraries/math-example/src/instruction.rs similarity index 100% rename from libraries/math/src/instruction.rs rename to libraries/math-example/src/instruction.rs diff --git a/libraries/math-example/src/lib.rs b/libraries/math-example/src/lib.rs new file mode 100644 index 00000000000..5c6c6b1b9c7 --- /dev/null +++ b/libraries/math-example/src/lib.rs @@ -0,0 +1,13 @@ +//! Math operations using unsigned integers + +#![deny(missing_docs)] +#![forbid(unsafe_code)] + +mod entrypoint; +pub mod error; +pub mod instruction; +pub mod processor; + +pub use spl_math_utils::{approximations, checked_ceil_div, precise_number, uint}; + +solana_program::declare_id!("Math111111111111111111111111111111111111111"); diff --git a/libraries/math/src/processor.rs b/libraries/math-example/src/processor.rs similarity index 100% rename from libraries/math/src/processor.rs rename to libraries/math-example/src/processor.rs diff --git a/libraries/math/tests/instruction_count.rs b/libraries/math-example/tests/instruction_count.rs similarity index 100% rename from libraries/math/tests/instruction_count.rs rename to libraries/math-example/tests/instruction_count.rs diff --git a/libraries/math-utils/Cargo.toml b/libraries/math-utils/Cargo.toml deleted file mode 100644 index c5f119c7589..00000000000 --- a/libraries/math-utils/Cargo.toml +++ /dev/null @@ -1,19 +0,0 @@ -[package] -name = "spl-math-utils" -version = "0.1.0" -description = "Solana Program Library Math Utils" -authors = ["Solana Labs Maintainers "] -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"] diff --git a/libraries/math-utils/src/lib.rs b/libraries/math-utils/src/lib.rs deleted file mode 100644 index 54c5a430917..00000000000 --- a/libraries/math-utils/src/lib.rs +++ /dev/null @@ -1,6 +0,0 @@ -//! Math utilities. - -pub mod approximations; -pub mod checked_ceil_div; -pub mod precise_number; -pub mod uint; diff --git a/libraries/math/Cargo.toml b/libraries/math/Cargo.toml index 018102d3dea..c5f119c7589 100644 --- a/libraries/math/Cargo.toml +++ b/libraries/math/Cargo.toml @@ -1,31 +1,19 @@ [package] -name = "spl-math" -version = "0.3.0" -description = "Solana Program Library Math" +name = "spl-math-utils" +version = "0.1.0" +description = "Solana Program Library Math Utils" authors = ["Solana Labs Maintainers "] repository = "https://github.com/solana-labs/solana-program-library" license = "Apache-2.0" edition = "2021" -[features] -no-entrypoint = [] -test-sbf = [] - [dependencies] -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] -solana-program-test = "2.1.0" -solana-sdk = "2.1.0" - -[lib] -crate-type = ["cdylib", "lib"] - +proptest = "1.5.0" +libm = "0.2.11" [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] diff --git a/libraries/math/README.md b/libraries/math/README.md index 2035ce0c8f0..17e2fa99371 100644 --- a/libraries/math/README.md +++ b/libraries/math/README.md @@ -1,6 +1,6 @@ # Math -Program wrapper around the spl-math-utils crate. The program exists for testing purposes. +Library with utilities for on-chain math. ## Audit diff --git a/libraries/math-utils/src/approximations.rs b/libraries/math/src/approximations.rs similarity index 100% rename from libraries/math-utils/src/approximations.rs rename to libraries/math/src/approximations.rs diff --git a/libraries/math-utils/src/checked_ceil_div.rs b/libraries/math/src/checked_ceil_div.rs similarity index 100% rename from libraries/math-utils/src/checked_ceil_div.rs rename to libraries/math/src/checked_ceil_div.rs diff --git a/libraries/math/src/lib.rs b/libraries/math/src/lib.rs index 5c6c6b1b9c7..54c5a430917 100644 --- a/libraries/math/src/lib.rs +++ b/libraries/math/src/lib.rs @@ -1,13 +1,6 @@ -//! Math operations using unsigned integers +//! Math utilities. -#![deny(missing_docs)] -#![forbid(unsafe_code)] - -mod entrypoint; -pub mod error; -pub mod instruction; -pub mod processor; - -pub use spl_math_utils::{approximations, checked_ceil_div, precise_number, uint}; - -solana_program::declare_id!("Math111111111111111111111111111111111111111"); +pub mod approximations; +pub mod checked_ceil_div; +pub mod precise_number; +pub mod uint; diff --git a/libraries/math-utils/src/precise_number.rs b/libraries/math/src/precise_number.rs similarity index 100% rename from libraries/math-utils/src/precise_number.rs rename to libraries/math/src/precise_number.rs diff --git a/libraries/math-utils/src/uint.rs b/libraries/math/src/uint.rs similarity index 100% rename from libraries/math-utils/src/uint.rs rename to libraries/math/src/uint.rs diff --git a/token-swap/program/Cargo.toml b/token-swap/program/Cargo.toml index 588283406e9..d136d6bcacc 100644 --- a/token-swap/program/Cargo.toml +++ b/token-swap/program/Cargo.toml @@ -18,7 +18,7 @@ enum_dispatch = "0.3.13" num-derive = "0.4" num-traits = "0.2" solana-program = "2.1.0" -spl-math-utils = { version = "0.1", path = "../../libraries/math-utils" } +spl-math-utils = { version = "0.1", path = "../../libraries/math" } 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" diff --git a/token-swap/program/fuzz/Cargo.toml b/token-swap/program/fuzz/Cargo.toml index 4dca388cc99..5d80af48897 100644 --- a/token-swap/program/fuzz/Cargo.toml +++ b/token-swap/program/fuzz/Cargo.toml @@ -12,7 +12,7 @@ publish = false honggfuzz = { version = "0.5.56" } arbitrary = { version = "1.3", features = ["derive"] } solana-program = "2.1.0" -spl-math-utils = { version = "0.1", path = "../../../libraries/math-utils" } +spl-math-utils = { version = "0.1", path = "../../../libraries/math" } spl-token = { version = "6.0", path = "../../../token/program", features = [ "no-entrypoint" ] } spl-token-swap = { path = "..", features = ["fuzz", "no-entrypoint"] } From 88ffaa30af914dd317d87c1d4863b5bcb538d202 Mon Sep 17 00:00:00 2001 From: kevinheavey Date: Thu, 31 Oct 2024 22:34:42 +0400 Subject: [PATCH 8/8] make spl-math the library crate and spl-math-example the program crate --- Cargo.lock | 26 +++++++++---------- libraries/math-example/Cargo.toml | 8 +++--- libraries/math-example/src/lib.rs | 2 +- .../math-example/tests/instruction_count.rs | 2 +- libraries/math/Cargo.toml | 6 ++--- token-swap/program/Cargo.toml | 2 +- token-swap/program/fuzz/Cargo.toml | 2 +- token-swap/program/fuzz/src/instructions.rs | 2 +- token-swap/program/src/curve/calculator.rs | 4 +-- .../program/src/curve/constant_price.rs | 2 +- .../program/src/curve/constant_product.rs | 2 +- token-swap/program/src/curve/offset.rs | 2 +- 12 files changed, 30 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ea3b821fb22..18438277e95 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8127,24 +8127,24 @@ dependencies = [ name = "spl-math" version = "0.3.0" dependencies = [ - "borsh 1.5.1", - "num-derive", + "libm", "num-traits", - "solana-program", - "solana-program-test", - "solana-sdk", - "spl-math-utils", - "thiserror", + "proptest", + "uint", ] [[package]] -name = "spl-math-utils" +name = "spl-math-example" version = "0.1.0" dependencies = [ - "libm", + "borsh 1.5.1", + "num-derive", "num-traits", - "proptest", - "uint", + "solana-program", + "solana-program-test", + "solana-sdk", + "spl-math", + "thiserror", ] [[package]] @@ -8812,7 +8812,7 @@ dependencies = [ "roots", "solana-program", "solana-sdk", - "spl-math-utils", + "spl-math", "spl-token 6.0.0", "spl-token-2022 5.0.2", "test-case", @@ -8826,7 +8826,7 @@ dependencies = [ "arbitrary", "honggfuzz", "solana-program", - "spl-math-utils", + "spl-math", "spl-token 6.0.0", "spl-token-swap", ] diff --git a/libraries/math-example/Cargo.toml b/libraries/math-example/Cargo.toml index df8d4b5dcf9..2bf3618c64d 100644 --- a/libraries/math-example/Cargo.toml +++ b/libraries/math-example/Cargo.toml @@ -1,7 +1,7 @@ [package] -name = "spl-math" -version = "0.3.0" -description = "Solana Program Library Math" +name = "spl-math-example" +version = "0.1.0" +description = "Solana Program Library Math Example" authors = ["Solana Labs Maintainers "] repository = "https://github.com/solana-labs/solana-program-library" license = "Apache-2.0" @@ -16,7 +16,7 @@ borsh = "1.5.1" num-derive = "0.4" num-traits = "0.2" solana-program = "2.1.0" -spl-math-utils = { path = "../math", version = "0.1.0" } +spl-math = { path = "../math", version = "0.3.0" } thiserror = "1.0" [dev-dependencies] diff --git a/libraries/math-example/src/lib.rs b/libraries/math-example/src/lib.rs index 5c6c6b1b9c7..e0162970628 100644 --- a/libraries/math-example/src/lib.rs +++ b/libraries/math-example/src/lib.rs @@ -8,6 +8,6 @@ pub mod error; pub mod instruction; pub mod processor; -pub use spl_math_utils::{approximations, checked_ceil_div, precise_number, uint}; +pub use spl_math::{approximations, checked_ceil_div, precise_number, uint}; solana_program::declare_id!("Math111111111111111111111111111111111111111"); diff --git a/libraries/math-example/tests/instruction_count.rs b/libraries/math-example/tests/instruction_count.rs index 921c0319f63..93025ad86cd 100644 --- a/libraries/math-example/tests/instruction_count.rs +++ b/libraries/math-example/tests/instruction_count.rs @@ -5,7 +5,7 @@ use { solana_program_test::*, solana_sdk::{signature::Signer, transaction::Transaction}, - spl_math::{id, instruction, processor::process_instruction}, + spl_math_example::{id, instruction, processor::process_instruction}, }; #[tokio::test] diff --git a/libraries/math/Cargo.toml b/libraries/math/Cargo.toml index c5f119c7589..72bf60c978e 100644 --- a/libraries/math/Cargo.toml +++ b/libraries/math/Cargo.toml @@ -1,7 +1,7 @@ [package] -name = "spl-math-utils" -version = "0.1.0" -description = "Solana Program Library Math Utils" +name = "spl-math" +version = "0.3.0" +description = "Solana Program Library Math" authors = ["Solana Labs Maintainers "] repository = "https://github.com/solana-labs/solana-program-library" license = "Apache-2.0" diff --git a/token-swap/program/Cargo.toml b/token-swap/program/Cargo.toml index d136d6bcacc..e86e75c8f38 100644 --- a/token-swap/program/Cargo.toml +++ b/token-swap/program/Cargo.toml @@ -18,7 +18,7 @@ enum_dispatch = "0.3.13" num-derive = "0.4" num-traits = "0.2" solana-program = "2.1.0" -spl-math-utils = { version = "0.1", path = "../../libraries/math" } +spl-math = { version = "0.3", path = "../../libraries/math" } 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" diff --git a/token-swap/program/fuzz/Cargo.toml b/token-swap/program/fuzz/Cargo.toml index 5d80af48897..e565e75d0f0 100644 --- a/token-swap/program/fuzz/Cargo.toml +++ b/token-swap/program/fuzz/Cargo.toml @@ -12,7 +12,7 @@ publish = false honggfuzz = { version = "0.5.56" } arbitrary = { version = "1.3", features = ["derive"] } solana-program = "2.1.0" -spl-math-utils = { version = "0.1", path = "../../../libraries/math" } +spl-math = { version = "0.3", path = "../../../libraries/math" } spl-token = { version = "6.0", path = "../../../token/program", features = [ "no-entrypoint" ] } spl-token-swap = { path = "..", features = ["fuzz", "no-entrypoint"] } diff --git a/token-swap/program/fuzz/src/instructions.rs b/token-swap/program/fuzz/src/instructions.rs index ddb378e9eee..62f765fde46 100644 --- a/token-swap/program/fuzz/src/instructions.rs +++ b/token-swap/program/fuzz/src/instructions.rs @@ -2,7 +2,7 @@ use { arbitrary::Arbitrary, honggfuzz::fuzz, - spl_math_utils::precise_number::PreciseNumber, + spl_math::precise_number::PreciseNumber, spl_token::error::TokenError, spl_token_swap::{ curve::{ diff --git a/token-swap/program/src/curve/calculator.rs b/token-swap/program/src/curve/calculator.rs index e329b315a3d..36d9de468ea 100644 --- a/token-swap/program/src/curve/calculator.rs +++ b/token-swap/program/src/curve/calculator.rs @@ -2,7 +2,7 @@ #[cfg(feature = "fuzz")] use arbitrary::Arbitrary; -use {crate::error::SwapError, spl_math_utils::precise_number::PreciseNumber, std::fmt::Debug}; +use {crate::error::SwapError, spl_math::precise_number::PreciseNumber, std::fmt::Debug}; /// Initial amount of pool tokens for swap contract, hard-coded to something /// "sensible" given a maximum of u128. @@ -194,7 +194,7 @@ pub trait CurveCalculator: Debug + DynPack { /// Test helpers for curves #[cfg(test)] pub mod test { - use {super::*, proptest::prelude::*, spl_math_utils::uint::U256}; + use {super::*, proptest::prelude::*, spl_math::uint::U256}; /// The epsilon for most curves when performing the conversion test, /// comparing a one-sided deposit to a swap + deposit. diff --git a/token-swap/program/src/curve/constant_price.rs b/token-swap/program/src/curve/constant_price.rs index 041a05834c5..b9e5272b50a 100644 --- a/token-swap/program/src/curve/constant_price.rs +++ b/token-swap/program/src/curve/constant_price.rs @@ -12,7 +12,7 @@ use { program_error::ProgramError, program_pack::{IsInitialized, Pack, Sealed}, }, - spl_math_utils::{checked_ceil_div::CheckedCeilDiv, precise_number::PreciseNumber, uint::U256}, + spl_math::{checked_ceil_div::CheckedCeilDiv, precise_number::PreciseNumber, uint::U256}, }; /// Get the amount of pool tokens for the given amount of token A or B. diff --git a/token-swap/program/src/curve/constant_product.rs b/token-swap/program/src/curve/constant_product.rs index 59dc5321bc3..c13a66b564d 100644 --- a/token-swap/program/src/curve/constant_product.rs +++ b/token-swap/program/src/curve/constant_product.rs @@ -12,7 +12,7 @@ use { program_error::ProgramError, program_pack::{IsInitialized, Pack, Sealed}, }, - spl_math_utils::{checked_ceil_div::CheckedCeilDiv, precise_number::PreciseNumber}, + spl_math::{checked_ceil_div::CheckedCeilDiv, precise_number::PreciseNumber}, }; /// ConstantProductCurve struct implementing CurveCalculator diff --git a/token-swap/program/src/curve/offset.rs b/token-swap/program/src/curve/offset.rs index a40cb8b1dba..8d5937536d0 100644 --- a/token-swap/program/src/curve/offset.rs +++ b/token-swap/program/src/curve/offset.rs @@ -19,7 +19,7 @@ use { program_error::ProgramError, program_pack::{IsInitialized, Pack, Sealed}, }, - spl_math_utils::precise_number::PreciseNumber, + spl_math::precise_number::PreciseNumber, }; /// Offset curve, uses ConstantProduct under the hood, but adds an offset to