From 7e5e017915ada48a3f6f34d478361dad3c669657 Mon Sep 17 00:00:00 2001 From: ohamamarachi474-del Date: Sun, 18 Oct 2026 14:39:51 +0100 Subject: [PATCH] refactor: replace magic numbers in nft and dao with named constants Co-Authored-By: Claude Sonnet 5.5 --- contracts/dao/src/lib.rs | 7 +++++-- contracts/nft/src/lib.rs | 10 +++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/contracts/dao/src/lib.rs b/contracts/dao/src/lib.rs index 2f13943..4a0ce0b 100644 --- a/contracts/dao/src/lib.rs +++ b/contracts/dao/src/lib.rs @@ -20,7 +20,10 @@ use soroban_sdk::{ const DEFAULT_TIMELOCK_LEDGERS: u32 = 17_280; /// Minimum quorum required for a proposal to pass (numerator out of 10 000). -const QUORUM_BPS: u32 = 1_000; // 10 % +const QUORUM_BPS: u32 = 1_000; // 10 % (1_000 / 10_000) + +/// Basis-point denominator: 10_000 bps = 100 %. +const BPS_DENOMINATOR: u32 = 10_000; // ── Data Types ──────────────────────────────────────────────────────────────── @@ -353,7 +356,7 @@ impl DaoGovernance { // Check quorum: total votes cast must be ≥ QUORUM_BPS of total supply. let total_votes = proposal.votes_for + proposal.votes_against; - let quorum = (total_supply * QUORUM_BPS as i128) / 10_000; + let quorum = (total_supply * QUORUM_BPS as i128) / BPS_DENOMINATOR as i128; let passed = total_votes >= quorum && proposal.votes_for > proposal.votes_against; diff --git a/contracts/nft/src/lib.rs b/contracts/nft/src/lib.rs index ee3f393..26758e7 100644 --- a/contracts/nft/src/lib.rs +++ b/contracts/nft/src/lib.rs @@ -18,6 +18,10 @@ pub mod errors; use errors::NftError; +/// Maximum royalty a creator may set on secondary sales, in basis points. +/// 3000 bps = 30%; the cap protects buyers from prohibitive resale fees. +const MAX_ROYALTY_BPS: u32 = 3000; + // ── Data Types ──────────────────────────────────────────────────────────────── /// Current state of an NFT certificate. @@ -119,7 +123,7 @@ impl NftOwnership { /// * `owner` — initial owner (may differ from creator on primary sale). /// * `title` — human-readable certificate name. /// * `metadata_uri` — URI pointing to off-chain metadata (IPFS / HTTPS). - /// * `royalty_bps` — basis points paid to creator on secondary sales (≤ 3000). + /// * `royalty_bps` — basis points paid to creator on secondary sales (≤ MAX_ROYALTY_BPS). /// * `commission_id` — linked commission id; 0 if standalone. /// * `transferable` — whether the NFT may be transferred after minting. /// * `burnable` — whether the NFT may be burned by the owner. @@ -137,7 +141,7 @@ impl NftOwnership { burnable: bool, ) -> Result { creator.require_auth(); - if royalty_bps > 3000 { + if royalty_bps > MAX_ROYALTY_BPS { return Err(NftError::RoyaltyTooHigh); } @@ -302,7 +306,7 @@ impl NftOwnership { /// Only the original creator may update royalties. pub fn update_royalty(env: Env, creator: Address, nft_id: u64, new_royalty_bps: u32) -> Result<(), NftError> { creator.require_auth(); - if new_royalty_bps > 3000 { + if new_royalty_bps > MAX_ROYALTY_BPS { return Err(NftError::RoyaltyTooHigh); }