Skip to content

Commit

Permalink
format coupons and discounts (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
leecchh authored Nov 27, 2024
1 parent f5bb391 commit 421ca77
Show file tree
Hide file tree
Showing 14 changed files with 2,266 additions and 1,802 deletions.
26 changes: 14 additions & 12 deletions packages/coupons/sources/constants.move
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@
// SPDX-License-Identifier: Apache-2.0

// Some constants used in coupons.
module coupons::constants {
/// discount types
/// Percentage discount (0,100]
const PERCENTAGE_DISCOUNT: u8 = 0;
/// Fixed MIST discount (e.g. -5 SUI)
const FIXED_PRICE_DISCOUNT: u8 = 1;
module coupons::constants;

/// A getter for the percentage discount type.
public fun percentage_discount_type(): u8 { PERCENTAGE_DISCOUNT }
/// discount types
/// Percentage discount (0,100]
const PERCENTAGE_DISCOUNT: u8 = 0;
/// Fixed MIST discount (e.g. -5 SUI)
const FIXED_PRICE_DISCOUNT: u8 = 1;

/// A getter for the fixed price discount type.
public fun fixed_price_discount_type(): u8 { FIXED_PRICE_DISCOUNT }
/// A getter for the percentage discount type.
public fun percentage_discount_type(): u8 { PERCENTAGE_DISCOUNT }

/// A vector with all the discount rule types.
public fun discount_rule_types(): vector<u8> { vector[PERCENTAGE_DISCOUNT, FIXED_PRICE_DISCOUNT] }
/// A getter for the fixed price discount type.
public fun fixed_price_discount_type(): u8 { FIXED_PRICE_DISCOUNT }

/// A vector with all the discount rule types.
public fun discount_rule_types(): vector<u8> {
vector[PERCENTAGE_DISCOUNT, FIXED_PRICE_DISCOUNT]
}
91 changes: 47 additions & 44 deletions packages/coupons/sources/coupon.move
Original file line number Diff line number Diff line change
@@ -1,52 +1,55 @@
module coupons::coupon {
use coupons::{
rules::{Self, CouponRules},
constants
};
module coupons::coupon;

/// A Coupon has a type, a value and a ruleset.
/// - `Rules` are defined on the module `rules`, and covers a variety of everything we needed for the service.
/// - `kind` is a u8 constant, defined on `constants` which makes a coupon fixed price or discount percentage
/// - `value` is a u64 constant, which can be in the range of (0,100] for discount percentage, or any value > 0 for fixed price.
public struct Coupon has copy, store, drop {
kind: u8, // 0 -> Percentage Discount | 1 -> Fixed Discount
amount: u64, // if type == 0, we need it to be between 0, 100. We only allow int stlye (not 0.5% discount).
rules: CouponRules, // A list of base Rules for the coupon.
}
use coupons::constants;
use coupons::rules::{Self, CouponRules};

/// An internal function to create a coupon object.
public(package) fun new(
kind: u8,
amount: u64,
rules: CouponRules,
_ctx: &mut TxContext
): Coupon {
rules::assert_is_valid_amount(kind, amount);
rules::assert_is_valid_discount_type(kind);
Coupon {
kind, amount, rules
}
}
/// A Coupon has a type, a value and a ruleset.
/// - `Rules` are defined on the module `rules`, and covers a variety of
/// everything we needed for the service.
/// - `kind` is a u8 constant, defined on `constants` which makes a coupon fixed
/// price or discount percentage
/// - `value` is a u64 constant, which can be in the range of (0,100] for
/// discount percentage, or any value > 0 for fixed price.
public struct Coupon has copy, store, drop {
kind: u8, // 0 -> Percentage Discount | 1 -> Fixed Discount
amount: u64, // if type == 0, we need it to be between 0, 100. We only allow int stlye (not 0.5% discount).
rules: CouponRules, // A list of base Rules for the coupon.
}

public(package) fun rules(coupon: &Coupon): &CouponRules {
&coupon.rules
/// An internal function to create a coupon object.
public(package) fun new(
kind: u8,
amount: u64,
rules: CouponRules,
_ctx: &mut TxContext,
): Coupon {
rules::assert_is_valid_amount(kind, amount);
rules::assert_is_valid_discount_type(kind);
Coupon {
kind,
amount,
rules,
}
}

public(package) fun rules_mut(coupon: &mut Coupon): &mut CouponRules {
&mut coupon.rules
}
public(package) fun rules(coupon: &Coupon): &CouponRules {
&coupon.rules
}

/// A helper to calculate the final price after the discount.
public(package) fun calculate_sale_price(coupon: &Coupon, price: u64): u64 {
// If it's fixed price, we just deduce the amount.
if(coupon.kind == constants::fixed_price_discount_type()){
if(coupon.amount > price) return 0; // protect underflow case.
return price - coupon.amount
};
public(package) fun rules_mut(coupon: &mut Coupon): &mut CouponRules {
&mut coupon.rules
}

// If it's discount price, we calculate the discount
let discount = (((price as u128) * (coupon.amount as u128) / 100) as u64);
// then remove it from the sale price.
price - discount
}
/// A helper to calculate the final price after the discount.
public(package) fun calculate_sale_price(coupon: &Coupon, price: u64): u64 {
// If it's fixed price, we just deduce the amount.
if (coupon.kind == constants::fixed_price_discount_type()) {
if (coupon.amount > price) return 0; // protect underflow case.
return price - coupon.amount
};

// If it's discount price, we calculate the discount
let discount = (((price as u128) * (coupon.amount as u128) / 100) as u64);
// then remove it from the sale price.
price - discount
}
Loading

0 comments on commit 421ca77

Please sign in to comment.