Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[move-2024] Suins Move 2024 Migration #59

Merged
merged 10 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions packages/coupons/Move.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# @generated by Move, please check-in and do not edit manually.

[move]
version = 0
version = 1
manifest_digest = "F6F1ED65982D6E0324AD5094A06FB092A7A055409AB912038DC0D2DA42E13854"
deps_digest = "3C4103934B1E040BB6B23F1D610B4EF9F2F1166A50A104EADCF77467C004C600"

dependencies = [
{ name = "Sui" },
Expand All @@ -10,11 +12,11 @@ dependencies = [

[[move.package]]
name = "MoveStdlib"
source = { git = "https://github.com/MystenLabs/sui.git", rev = "testnet", subdir = "crates/sui-framework/packages/move-stdlib" }
source = { git = "https://github.com/MystenLabs/sui.git", rev = "mainnet", subdir = "crates/sui-framework/packages/move-stdlib" }

[[move.package]]
name = "Sui"
source = { git = "https://github.com/MystenLabs/sui.git", rev = "testnet", subdir = "crates/sui-framework/packages/sui-framework" }
source = { git = "https://github.com/MystenLabs/sui.git", rev = "mainnet", subdir = "crates/sui-framework/packages/sui-framework" }

dependencies = [
{ name = "MoveStdlib" },
Expand All @@ -27,3 +29,8 @@ source = { local = "../suins" }
dependencies = [
{ name = "Sui" },
]

[move.toolchain-version]
compiler-version = "1.22.0"
edition = "legacy"
flavor = "sui"
1 change: 1 addition & 0 deletions packages/coupons/Move.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[package]
name = "coupons"
version = "0.0.1"
edition = "2024.beta"

[dependencies]
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "mainnet", override=true }
Expand Down
30 changes: 15 additions & 15 deletions packages/coupons/sources/coupons.move
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,20 @@ module coupons::coupons {
use suins::registry::{Self, Registry};

// Authorization for the Coupons on SuiNS, to be able to register names on the app.
struct CouponsApp has drop {}
public struct CouponsApp has drop {}
/// Authorization Key for secondary apps (e.g. Discord) connected to this module.
struct AppKey<phantom App: drop> has copy, store, drop {}
public struct AppKey<phantom App: drop> has copy, store, drop {}

/// Create a `Data` struct that only authorized apps can get mutable access to.
/// We don't save the coupon's table directly on the shared object, because we want authorized apps to only perform
/// certain actions with the table (and not give full `mut` access to it).
struct Data has store {
public struct Data has store {
// hold a list of all coupons in the system.
coupons: Table<String, Coupon>
}

/// The CouponHouse Shared Object which holds a table of coupon codes available for claim.
struct CouponHouse has key, store {
public struct CouponHouse has key, store {
id: UID,
data: Data,
version: u8
Expand All @@ -72,8 +72,8 @@ module coupons::coupons {
/// - `Rules` are defined on the module `rules`, and covers a variety of everything we needed for the service.
/// - `type` 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.
struct Coupon has copy, store, drop {
type: u8, // 0 -> Percentage Discount | 1 -> Fixed Discount
public struct Coupon has copy, store, drop {
`type`: u8, // 0 -> Percentage Discount | 1 -> Fixed Discount
leecchh marked this conversation as resolved.
Show resolved Hide resolved
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.
}
Expand Down Expand Up @@ -209,13 +209,13 @@ module coupons::coupons {
_: &AdminCap,
self: &mut CouponHouse,
code: String,
type: u8,
`type`: u8,
amount: u64,
rules: CouponRules,
ctx: &mut TxContext
) {
assert_version_is_valid(self);
internal_save_coupon(&mut self.data, code, internal_create_coupon(type, amount, rules, ctx));
internal_save_coupon(&mut self.data, code, internal_create_coupon(`type`, amount, rules, ctx));
}

// Remove a coupon as a system's admin.
Expand All @@ -227,12 +227,12 @@ module coupons::coupons {
public fun app_add_coupon(
self: &mut Data,
code: String,
type: u8,
`type`: u8,
amount: u64,
rules: CouponRules,
ctx: &mut TxContext
){
internal_save_coupon(self, code, internal_create_coupon(type, amount, rules, ctx));
internal_save_coupon(self, code, internal_create_coupon(`type`, amount, rules, ctx));
}

// Remove a coupon as a registered app.
Expand All @@ -244,7 +244,7 @@ module coupons::coupons {
/// A helper to calculate the final price after the discount.
fun internal_calculate_sale_price(price: u64, coupon: &Coupon): u64{
// If it's fixed price, we just deduce the amount.
if(coupon.type == constants::fixed_price_discount_type()){
if(coupon.`type` == constants::fixed_price_discount_type()){
if(coupon.amount > price) return 0; // protect underflow case.
return price - coupon.amount
};
Expand All @@ -268,15 +268,15 @@ module coupons::coupons {

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

Expand Down
2 changes: 1 addition & 1 deletion packages/coupons/sources/range.move
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module coupons::range {
const EInvalidRange: u64 = 0;

/// A Range for u8 helper
struct Range has copy, store, drop {
public struct Range has copy, store, drop {
vec: vector<u8>
}

Expand Down
10 changes: 5 additions & 5 deletions packages/coupons/sources/rules.move
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module coupons::rules {
/// The Struct that holds the coupon's rules.
/// All rules are combined in `AND` fashion.
/// All of the checks have to pass for a coupon to be used.
struct CouponRules has copy, store, drop {
public struct CouponRules has copy, store, drop {
length: Option<Range>,
available_claims: Option<u64>,
user: Option<address>,
Expand Down Expand Up @@ -118,15 +118,15 @@ module coupons::rules {
range::is_in_range(option::borrow(&rules.years), target)
}

public fun assert_is_valid_discount_type(type: u8) {
assert!(vector::contains(&constants::discount_rule_types(), &type), EInvalidType);
public fun assert_is_valid_discount_type(`type`: u8) {
assert!(vector::contains(&constants::discount_rule_types(), &`type`), EInvalidType);
}

// verify that we are creating the coupons correctly (based on amount & type).
// for amounts, if we have a percentage discount, our max num is 100.
public fun assert_is_valid_amount(type: u8, amount: u64) {
public fun assert_is_valid_amount(`type`: u8, amount: u64) {
assert!(amount > 0, EInvalidAmount); // protect from division by 0. 0 doesn't make sense in any scenario.
if(type == constants::percentage_discount_type()){
if(`type` == constants::percentage_discount_type()){
assert!(amount<=100, EInvalidAmount)
}
}
Expand Down
14 changes: 7 additions & 7 deletions packages/coupons/tests/authorization_tests.move
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ module coupons::app_authorization_tests {

#[test]
fun admin_get_app_success() {
let scenario_val = setup::test_init();
let mut scenario_val = setup::test_init();
let scenario = &mut scenario_val;
// auth style as authorized app
{
test_scenario::next_tx(scenario, user());
let coupon_house = test_scenario::take_shared<CouponHouse>(scenario);
let mut coupon_house = test_scenario::take_shared<CouponHouse>(scenario);
coupons::app_data_mut<TestApp>(setup::test_app(), &mut coupon_house);
test_scenario::return_shared(coupon_house);
};
Expand All @@ -29,12 +29,12 @@ module coupons::app_authorization_tests {

#[test]
fun authorized_app_get_app_success(){
let scenario_val = setup::test_init();
let mut scenario_val = setup::test_init();
let scenario = &mut scenario_val;
{
test_scenario::next_tx(scenario, admin());

let coupon_house = test_scenario::take_shared<CouponHouse>(scenario);
let mut coupon_house = test_scenario::take_shared<CouponHouse>(scenario);
let admin_cap = test_scenario::take_from_sender<AdminCap>(scenario);

// test app deauthorization.
Expand All @@ -49,13 +49,13 @@ module coupons::app_authorization_tests {
test_scenario::end(scenario_val);
}

#[test, expected_failure(abort_code=coupons::coupons::EAppNotAuthorized)]
#[test, expected_failure(abort_code=::coupons::coupons::EAppNotAuthorized)]
fun unauthorized_app_failure() {
let scenario_val = setup::test_init();
let mut scenario_val = setup::test_init();
let scenario = &mut scenario_val;
{
test_scenario::next_tx(scenario, user());
let coupon_house = test_scenario::take_shared<CouponHouse>(scenario);
let mut coupon_house = test_scenario::take_shared<CouponHouse>(scenario);
coupons::app_data_mut<UnauthorizedTestApp>(setup::unauthorized_test_app(), &mut coupon_house);
test_scenario::return_shared(coupon_house);
};
Expand Down
Loading
Loading