Skip to content

Commit c74571e

Browse files
authored
Merge pull request #100 from Vvictor-commits/feat/public-headers-helper
feat: add shared input validation helper for quote read methods
2 parents 54663ae + 4940e6b commit c74571e

1 file changed

Lines changed: 20 additions & 21 deletions

File tree

creator-keys/src/lib.rs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,24 @@ fn read_required_protocol_fee_config(env: &Env) -> Result<fee::FeeConfig, Contra
264264
read_protocol_fee_config(env).ok_or(ContractError::FeeConfigNotSet)
265265
}
266266

267+
/// Resolves and validates the shared inputs required by read-only quote methods.
268+
///
269+
/// Reads the key price from storage and confirms the creator is registered.
270+
/// Returns `(price)` on success, or the appropriate [`ContractError`] on failure.
271+
fn resolve_quote_inputs(env: &Env, creator: &Address) -> Result<i128, ContractError> {
272+
let price: i128 = env
273+
.storage()
274+
.persistent()
275+
.get(&constants::storage::KEY_PRICE)
276+
.ok_or(ContractError::KeyPriceNotSet)?;
277+
278+
if read_creator_profile(env, creator).is_none() {
279+
return Err(ContractError::NotRegistered);
280+
}
281+
282+
Ok(price)
283+
}
284+
267285
/// Formats a quote response with overflow-safe total amount calculation.
268286
///
269287
/// Returns `Err(ContractError::Overflow)` if any addition or subtraction would overflow.
@@ -715,18 +733,8 @@ impl CreatorKeysContract {
715733
/// Returns a [`QuoteResponse`] containing the current price and fee breakdown.
716734
/// Fees are calculated based on the fixed key price.
717735
pub fn get_buy_quote(env: Env, creator: Address) -> Result<QuoteResponse, ContractError> {
718-
let price: i128 = env
719-
.storage()
720-
.persistent()
721-
.get(&constants::storage::KEY_PRICE)
722-
.ok_or(ContractError::KeyPriceNotSet)?;
723-
724-
if read_creator_profile(&env, &creator).is_none() {
725-
return Err(ContractError::NotRegistered);
726-
}
727-
736+
let price = resolve_quote_inputs(&env, &creator)?;
728737
let (creator_fee, protocol_fee) = Self::compute_fees_for_payment(env.clone(), price)?;
729-
730738
checked_format_quote_response(price, creator_fee, protocol_fee, true)
731739
}
732740

@@ -740,23 +748,14 @@ impl CreatorKeysContract {
740748
creator: Address,
741749
holder: Address,
742750
) -> Result<QuoteResponse, ContractError> {
743-
let price: i128 = env
744-
.storage()
745-
.persistent()
746-
.get(&constants::storage::KEY_PRICE)
747-
.ok_or(ContractError::KeyPriceNotSet)?;
748-
749-
if read_creator_profile(&env, &creator).is_none() {
750-
return Err(ContractError::NotRegistered);
751-
}
751+
let price = resolve_quote_inputs(&env, &creator)?;
752752

753753
let balance = Self::get_key_balance(env.clone(), creator, holder);
754754
if balance == 0 {
755755
return Err(ContractError::InsufficientBalance);
756756
}
757757

758758
let (creator_fee, protocol_fee) = Self::compute_fees_for_payment(env.clone(), price)?;
759-
760759
checked_format_quote_response(price, creator_fee, protocol_fee, false)
761760
}
762761
}

0 commit comments

Comments
 (0)