refactor(loyalty): compute via std::u128::mul_div; drop unused openzeppelin_math#33
refactor(loyalty): compute via std::u128::mul_div; drop unused openzeppelin_math#33bidzyyys wants to merge 3 commits into
Conversation
…ppelin_math compute_loyalty used a hand-rolled `(amount * coefficient) / denom` in u128 (the comments flagged the intermediate as near-overflow), and the `@openzeppelin-move/integer-math` dependency was declared but never used. Replace the arithmetic with `std::u128::mul_div`, which upcasts to u256 internally (no intermediate overflow) and rounds down - same result, and no external math dependency. u64::mul_div is unusable here: the divisor LOYALTY_FLOAT_SCALING * 10^payment_decimals reaches 1e27 at the 18-decimal cap, well over u64::MAX. The now-unused openzeppelin_math dep is removed. No public API or frontend change (coefficient stays a scaled u64). Tests: 97/97 (sui move test --build-env testnet).
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (3)
💤 Files with no reviewable changes (1)
WalkthroughThis PR bumps the SUI CLI version used in CI from 1.73.2 to 1.74.1, removes the openzeppelin_math dependency from the payments contract's Move.toml, and refactors compute_loyalty in config.move to compute a denominator and use u128::mul_div instead of manual multiplication/division. ChangesCI and Loyalty Calculation Updates
Estimated code review effort: 2 (Simple) | ~10 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
The resolved Sui framework requires a newer verifier than 1.73.2 (sui::rangeproofs UNEXPECTED_VERIFIER_ERROR under the old CLI).
Use `value.min(max)` instead of an explicit if/else; the cast to u64 is safe because the value is <= max <= u64::MAX after clamping.
| let value = (payment_amount as u128).mul_div(self.loyalty_coefficient as u128, denom); | ||
|
|
There was a problem hiding this comment.
In the previous approach I was avoiding u256 multiplication (it’s usually 4x time more expensive than u128 multiplication). But tbh I don't now how that difference works on sui and what performance benefit will be in our smart contract.. Probably minuscule. May be ask auditing team?
| manifest_digest = "5745706258F61D6CE210904B3E6AE87A73CE9D31A6F93BE4718C442529332A87" | ||
| deps = { std = "MoveStdlib", sui = "Sui" } | ||
|
|
||
| [pinned.testnet.openzeppelin_math] |
What & why
compute_loyaltyhand-rolled(amount * coefficient) / denominu128(its own comments flagged the intermediate as near-overflow), and the@openzeppelin-move/integer-mathdependency was declared but never used.std::u128::mul_div, which upcasts the product tou256internally (no intermediate overflow) and rounds down — same result, no external math dependency. The result is clamped tomax_loyalty_per_paymentand only cast tou64once<= max.openzeppelin_mathis removed fromMove.toml.std::u64::mul_divcan't be used here: the divisorLOYALTY_FLOAT_SCALING * 10^payment_decimalsreaches1e27at the 18-decimal cap — well overu64::MAX.Scope
u64), so no frontend change.Tests
sui move test --build-env testnet: 97/97. Formatter + lint clean.Summary by CodeRabbit